def test_copy_data_files05(self):
        """###.maff => copy nothing if no page"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000.maff",
    "type": ""
  }
})""")

        with zipfile.ZipFile(
                os.path.join(self.test_input, '20200101000000000.maff'),
                'w') as zh:
            zh.writestr('index.html', 'dummy')

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(
            set(
                glob.iglob(os.path.join(self.test_output, '**'),
                           recursive=True)), {
                               os.path.join(self.test_output, ''),
                               os.path.join(self.test_output, 'scrapbook.rdf'),
                           })
Esempio n. 2
0
    def test_meta_icon06(self):
        """Data folder => data folder"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "",
    "icon": "../icons/favicon.bmp"
  }
})""")
        icon_file = os.path.join(self.test_input, 'icons', 'favicon.bmp')
        os.makedirs(os.path.dirname(icon_file), exist_ok=True)
        with open(icon_file, 'wb') as fh:
            fh.write(
                b64decode(
                    'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABACAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAAAP8AAAAA'
                ))

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}icon'],
            'resource://scrapbook/data/icons/favicon.bmp')
        self.assertTrue(
            os.path.isfile(
                os.path.join(self.test_output, 'data', 'icons',
                             'favicon.bmp')))
Esempio n. 3
0
    def test_meta_icon04(self):
        """Favicon cache => icon folder"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000.html",
    "type": "",
    "icon": ".wsb/tree/favicon/dbc82be549e49d6db9a5719086722a4f1c5079cd.bmp"
  }
})""")
        icon_file = os.path.join(
            self.test_input_tree, 'favicon',
            'dbc82be549e49d6db9a5719086722a4f1c5079cd.bmp')
        os.makedirs(os.path.dirname(icon_file), exist_ok=True)
        with open(icon_file, 'wb') as fh:
            fh.write(
                b64decode(
                    'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABACAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAAAP8AAAAA'
                ))

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}icon'],
            'resource://scrapbook/icon/dbc82be549e49d6db9a5719086722a4f1c5079cd.bmp'
        )
        self.assertTrue(
            os.path.isfile(
                os.path.join(self.test_output, 'icon',
                             'dbc82be549e49d6db9a5719086722a4f1c5079cd.bmp')))
    def test_meta_type02(self):
        """note => notex"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "note"
  }
})""")

        index_file = os.path.join(self.test_input, '20200101000000000',
                                  'index.html')
        os.makedirs(os.path.dirname(index_file), exist_ok=True)
        with open(index_file, 'w', encoding='UTF-8') as fh:
            fh.write('note page content')

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}type'], 'notex')
Esempio n. 5
0
    def test_copy_data_files02(self):
        """###.html => copy ###.html to <ID>/*"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000.html",
    "type": ""
  }
})""")

        with open(os.path.join(self.test_input, '20200101000000000.html'),
                  'w',
                  encoding='UTF-8') as fh:
            fh.write('page content')
        with open(os.path.join(self.test_input, 'page.html'),
                  'w',
                  encoding='UTF-8') as fh:
            fh.write('dummy')

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(
            set(os.listdir(os.path.join(self.test_output, 'data'))),
            {oid},
        )
        self.assertEqual(
            set(os.listdir(os.path.join(self.test_output, 'data', oid))),
            {'index.html'},
        )
Esempio n. 6
0
    def test_meta_marked03(self):
        """false marked property => normal type"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "",
    "marked": false
  }
})""")

        index_file = os.path.join(self.test_input, '20200101000000000',
                                  'index.html')
        os.makedirs(os.path.dirname(index_file), exist_ok=True)
        with open(index_file, 'w', encoding='UTF-8') as fh:
            fh.write('page content')

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}type'], '')
        self.assertIsNone(
            tree.find(f'{RDF}Description').attrib.get(f'{NS1}marked'))
Esempio n. 7
0
    def test_meta_icon02(self):
        """File with empty icon => moz-icon:// """
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "file",
    "icon": ""
  }
})""")
        index_file = os.path.join(self.test_input, '20200101000000000',
                                  'index.html')
        os.makedirs(os.path.dirname(index_file), exist_ok=True)
        with open(index_file, 'w', encoding='UTF-8') as fh:
            fh.write(
                '<meta charset="UTF-8"><meta http-equiv="refresh" content="0;URL=./myfile.txt">'
            )

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}icon'],
            'moz-icon://myfile.txt?size=16')
Esempio n. 8
0
    def test_id_mapping02(self):
        """If conflict, increament by 1 from timestamp"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "type": "folder"
  },
  "20200101000000001": {
    "type": "folder"
  },
  "20200101000000010": {
    "type": "folder"
  }
})""")
        with open(self.test_input_toc, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.toc({
  "root": [
    "20200101000000000",
    "20200101000000001",
    "20200101000000010"
  ]
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual([
            node.attrib[f'{NS1}id']
            for node in tree.findall(f'{RDF}Description')
        ],
                         [
                             util.datetime_to_id_legacy(
                                 util.id_to_datetime('20200101000000000')),
                             util.datetime_to_id_legacy(
                                 util.id_to_datetime('20200101000001000')),
                             util.datetime_to_id_legacy(
                                 util.id_to_datetime('20200101000002000')),
                         ])
        self.assertEqual([
            node.attrib[f'{RDF}resource']
            for node in tree.findall(f'{RDF}Seq/{RDF}li')
        ], [
            'urn:scrapbook:item' + util.datetime_to_id_legacy(
                util.id_to_datetime('20200101000000000')),
            'urn:scrapbook:item' + util.datetime_to_id_legacy(
                util.id_to_datetime('20200101000001000')),
            'urn:scrapbook:item' + util.datetime_to_id_legacy(
                util.id_to_datetime('20200101000002000')),
        ])
Esempio n. 9
0
    def test_toc_no_root(self):
        """root list not exist => empty root container"""
        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertIsNotNone(
            tree.find(f'{RDF}Seq[@{RDF}about="urn:scrapbook:root"]'))
        self.assertIsNone(
            tree.find(f'{RDF}Seq[@{RDF}about="urn:scrapbook:root"]/{RDF}li'))
Esempio n. 10
0
    def test_id_mapping04(self):
        """Increament by 1 from now if not timestamp"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "dummy1": {
    "type": "folder"
  },
  "dummy2": {
    "type": "folder"
  },
  "dummy3": {
    "type": "folder"
  }
})""")
        with open(self.test_input_toc, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.toc({
  "root": [
    "dummy1",
    "dummy2",
    "dummy3"
  ]
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        ts_now = datetime.now(timezone.utc).timestamp()
        id_list = [
            n.attrib[f'{NS1}id'] for n in tree.findall(f'{RDF}Description')
        ]
        ts_list = [
            util.id_to_datetime_legacy(id).timestamp() for id in id_list
        ]
        self.assertAlmostEqual(ts_list[0], ts_now, delta=3)
        self.assertEqual(ts_list[0] + 1, ts_list[1])
        self.assertEqual(ts_list[0] + 2, ts_list[2])

        self.assertEqual([
            node.attrib[f'{RDF}resource']
            for node in tree.findall(f'{RDF}Seq/{RDF}li')
        ], [
            'urn:scrapbook:item' + id_list[0],
            'urn:scrapbook:item' + id_list[1],
            'urn:scrapbook:item' + id_list[2],
        ])
Esempio n. 11
0
    def test_toc_duplicate(self):
        """Duplicated item => preserve only the first one (depth first)"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000001": {
    "type": "folder"
  },
  "20200101000002": {
    "type": "folder"
  },
  "20200101000003": {
    "type": "folder"
  }
})""")
        with open(self.test_input_toc, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.toc({
  "root": [
    "20200101000001",
    "20200101000002",
    "20200101000003"
  ],
  "20200101000001": [
    "20200101000002"
  ]
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual([
            node.attrib[f'{RDF}resource'] for node in tree.findall(
                f'{RDF}Seq[@{RDF}about="urn:scrapbook:root"]/{RDF}li')
        ], [
            'urn:scrapbook:item20200101000001',
            'urn:scrapbook:item20200101000003',
        ])

        self.assertEqual([
            node.attrib[f'{RDF}resource'] for node in tree.findall(
                f'{RDF}Seq[@{RDF}about="urn:scrapbook:item20200101000001"]/{RDF}li'
            )
        ], [
            'urn:scrapbook:item20200101000002',
        ])
    def test_copy_data_files06(self):
        """foo.bar => copy it and create meta refresh"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "中文#1.xhtml",
    "type": ""
  }
})""")

        with open(os.path.join(self.test_input, '中文#1.xhtml'),
                  'w',
                  encoding='UTF-8') as fh:
            fh.write("""\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Title of document</title>
</head>
<body>
some content
</body>
</html>
""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(
            set(
                glob.iglob(os.path.join(self.test_output, '**'),
                           recursive=True)),
            {
                os.path.join(self.test_output, ''),
                os.path.join(self.test_output, 'scrapbook.rdf'),
                os.path.join(self.test_output, 'data'),
                os.path.join(self.test_output, 'data', oid),
                os.path.join(self.test_output, 'data', oid, 'index.html'),
                os.path.join(self.test_output, 'data', oid, '中文#1.xhtml'),
            })
        self.assertEqual(
            util.parse_meta_refresh(
                os.path.join(self.test_output, 'data', oid,
                             'index.html')).target,
            './%E4%B8%AD%E6%96%87%231.xhtml')
Esempio n. 13
0
    def test_id_mapping03(self):
        """Legacy timestamp => use as-is"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000": {
    "type": "folder"
  },
  "20200101000010": {
    "type": "folder"
  },
  "20200101000100": {
    "type": "folder"
  }
})""")
        with open(self.test_input_toc, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.toc({
  "root": [
    "20200101000000",
    "20200101000010",
    "20200101000100"
  ]
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual([
            node.attrib[f'{NS1}id']
            for node in tree.findall(f'{RDF}Description')
        ], [
            '20200101000000',
            '20200101000010',
            '20200101000100',
        ])
        self.assertEqual([
            node.attrib[f'{RDF}resource']
            for node in tree.findall(f'{RDF}Seq/{RDF}li')
        ], [
            'urn:scrapbook:item20200101000000',
            'urn:scrapbook:item20200101000010',
            'urn:scrapbook:item20200101000100',
        ])
Esempio n. 14
0
    def test_meta_separator(self):
        """A sample of typical WebScrapBook separator item."""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "type": "separator",
    "title": "Hello 中文",
    "create": "20200102000000000",
    "modify": "20200103000000000"
  }
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(
            dict(tree.find(f'{NC}BookmarkSeparator').attrib), {
                f'{RDF}about':
                f'urn:scrapbook:item{oid}',
                f'{NS1}id':
                oid,
                f'{NS1}type':
                'separator',
                f'{NS1}title':
                'Hello 中文',
                f'{NS1}create':
                util.datetime_to_id_legacy(
                    util.id_to_datetime('20200102000000000')),
                f'{NS1}modify':
                util.datetime_to_id_legacy(
                    util.id_to_datetime('20200103000000000')),
                f'{NS1}source':
                '',
                f'{NS1}icon':
                '',
                f'{NS1}comment':
                '',
                f'{NS1}chars':
                '',
            })
    def test_meta_type01(self):
        """postit => note"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "postit"
  }
})""")

        index_file = os.path.join(self.test_input, '20200101000000000',
                                  'index.html')
        os.makedirs(os.path.dirname(index_file), exist_ok=True)
        with open(index_file, 'w', encoding='UTF-8') as fh:
            fh.write("""\
<!DOCTYPE html><html><head>\
<meta charset="UTF-8">\
<meta name="viewport" content="width=device-width">\
<style>pre { white-space: pre-wrap; overflow-wrap: break-word; }</style>\
</head><body><pre>
postit page content
</pre></body></html>""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}type'], 'note')

        # check output legacy note format
        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        with open(os.path.join(self.test_output, 'data', oid, 'index.html'),
                  encoding='UTF-8') as fh:
            self.assertEqual(
                fh.read(), """\
<html><head><meta http-equiv="Content-Type" content="text/html;Charset=UTF-8"></head><body><pre>
postit page content
</pre></body></html>""")
    def test_copy_data_files01(self):
        """###/index.html => copy ###/* to <ID>/*"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": ""
  }
})""")

        index_dir = os.path.join(self.test_input, '20200101000000000')
        os.makedirs(index_dir, exist_ok=True)
        with open(os.path.join(index_dir, 'index.html'), 'w',
                  encoding='UTF-8') as fh:
            fh.write('page content')
        with open(os.path.join(index_dir, 'page.html'), 'w',
                  encoding='UTF-8') as fh:
            fh.write('dummy')
        os.makedirs(os.path.join(self.test_input, '20200101000000001'),
                    exist_ok=True)
        with open(os.path.join(self.test_input, 'other.html'),
                  'w',
                  encoding='UTF-8') as fh:
            fh.write('dummy')

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(
            set(
                glob.iglob(os.path.join(self.test_output, '**'),
                           recursive=True)),
            {
                os.path.join(self.test_output, ''),
                os.path.join(self.test_output, 'scrapbook.rdf'),
                os.path.join(self.test_output, 'data'),
                os.path.join(self.test_output, 'data', oid),
                os.path.join(self.test_output, 'data', oid, 'index.html'),
                os.path.join(self.test_output, 'data', oid, 'page.html'),
            })
Esempio n. 17
0
    def test_meta_type02(self):
        """note => notex"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "note"
  }
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}type'], 'notex')
Esempio n. 18
0
    def test_meta_modify(self):
        """empty modify property => no modify property"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "",
    "modify": ""
  }
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertIsNone(
            tree.find(f'{RDF}Description').attrib.get(f'{NS1}modify'))
Esempio n. 19
0
    def test_meta_icon01(self):
        """Empty icon with icon-moz property => moz-icon:// """
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "image",
    "icon": "",
    "icon-moz": "moz-icon://myimage.png?size=16"
  }
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}icon'],
            'moz-icon://myimage.png?size=16')
Esempio n. 20
0
    def test_meta_icon03(self):
        """Absolute URL => use as-is"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "",
    "icon": "data:image/bmp;base64,Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABACAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAAAP8AAAAA"
  }
})""")

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        self.assertEqual(
            tree.find(f'{RDF}Description').attrib[f'{NS1}icon'],
            'data:image/bmp;base64,Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABACAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAAAP8AAAAA'
        )
    def test_copy_data_files04(self):
        """###.maff => copy internal files of first topdir to <ID>/*"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000.maff",
    "type": ""
  }
})""")

        with zipfile.ZipFile(
                os.path.join(self.test_input, '20200101000000000.maff'),
                'w') as zh:
            zh.writestr('20200101000000000/index.html', 'page content')
            zh.writestr('20200101000000000/page.html', 'dummy')
            zh.writestr('20200101000000000/subdir/page2.html', 'dummy2')
            zh.writestr('20200101000000001/index.html', 'page content 2')

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(
            set(
                glob.iglob(os.path.join(self.test_output, '**'),
                           recursive=True)),
            {
                os.path.join(self.test_output, ''),
                os.path.join(self.test_output, 'scrapbook.rdf'),
                os.path.join(self.test_output, 'data'),
                os.path.join(self.test_output, 'data', oid),
                os.path.join(self.test_output, 'data', oid, 'index.html'),
                os.path.join(self.test_output, 'data', oid, 'page.html'),
                os.path.join(self.test_output, 'data', oid, 'subdir'),
                os.path.join(self.test_output, 'data', oid, 'subdir',
                             'page2.html'),
            })
Esempio n. 22
0
    def test_copy_data_files03(self):
        """###.htz => copy internal files to <ID>/*"""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000.htz",
    "type": ""
  }
})""")

        with zipfile.ZipFile(
                os.path.join(self.test_input, '20200101000000000.htz'),
                'w') as zh:
            zh.writestr('index.html', 'page content')
            zh.writestr('page.html', 'dummy')
            zh.writestr('subdir/page2.html', 'dummy2')

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(
            set(os.listdir(os.path.join(self.test_output, 'data'))),
            {oid},
        )
        self.assertEqual(
            set(os.listdir(os.path.join(self.test_output, 'data', oid))),
            {'index.html', 'page.html', 'subdir'},
        )
        self.assertEqual(
            set(
                os.listdir(
                    os.path.join(self.test_output, 'data', oid, 'subdir'))),
            {'page2.html'},
        )
Esempio n. 23
0
    def test_meta_basic(self):
        """A sample of typical WebScrapBook item."""
        with open(self.test_input_meta, 'w', encoding='UTF-8') as fh:
            fh.write("""\
scrapbook.meta({
  "20200101000000000": {
    "index": "20200101000000000/index.html",
    "type": "",
    "title": "Hello 中文",
    "create": "20200102000000000",
    "modify": "20200103000000000",
    "source": "http://example.com",
    "icon": "favicon.bmp",
    "comment": "some comment\\nsecond line\\nthird line",
    "charset": "UTF-8",
    "locked": true
  }
})""")

        index_file = os.path.join(self.test_input, '20200101000000000',
                                  'index.html')
        os.makedirs(os.path.dirname(index_file), exist_ok=True)
        with open(index_file, 'w', encoding='UTF-8') as fh:
            fh.write('page content')

        icon_file = os.path.join(self.test_input, '20200101000000000',
                                 'favicon.bmp')
        os.makedirs(os.path.dirname(icon_file), exist_ok=True)
        with open(icon_file, 'wb') as fh:
            fh.write(
                b64decode(
                    'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABACAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAAAP8AAAAA'
                ))

        for info in wsb2sb.run(self.test_input, self.test_output):
            pass

        with open(self.test_output_rdf, 'rb') as fh:
            tree = etree.parse(fh)

        oid = util.datetime_to_id_legacy(
            util.id_to_datetime('20200101000000000'))
        self.assertEqual(tree.getroot().tag, f'{RDF}RDF')
        self.assertEqual(
            dict(tree.find(f'{RDF}Description').attrib), {
                f'{RDF}about':
                f'urn:scrapbook:item{oid}',
                f'{NS1}id':
                oid,
                f'{NS1}type':
                '',
                f'{NS1}title':
                'Hello 中文',
                f'{NS1}create':
                util.datetime_to_id_legacy(
                    util.id_to_datetime('20200102000000000')),
                f'{NS1}modify':
                util.datetime_to_id_legacy(
                    util.id_to_datetime('20200103000000000')),
                f'{NS1}source':
                'http://example.com',
                f'{NS1}icon':
                f'resource://scrapbook/data/{oid}/favicon.bmp',
                f'{NS1}comment':
                'some comment __BR__ second line __BR__ third line',
                f'{NS1}chars':
                'UTF-8',
                f'{NS1}lock':
                'true'
            })