File: package_test_data.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (55 lines) | stat: -rwxr-xr-x 1,356 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3

import sys

sys.path.insert(0, '.')

from zim.fs import *
from zim.notebook import Path
import zim.stores.files
import zim.stores.xml


def walk(store, path):
	for page in store.get_pagelist(path):
		yield page
		for child in walk(store, page):
			yield child


def package(dir, file):
	if file.exists():
		file.remove()
	fh = file.open('w')
	fh.write('<?xml version="1.0" enconding="utf-8"?>\n')
	fh.write('<!-- this file is NOT in store.xml format -->\n')
	fh.write('<pagelist>\n')
	source = zim.stores.files.Store(None, Path(':'), dir=dir)
	for page in walk(source, Path(':')):
		if not page.hascontent:
			continue
		text = page.source.read()
		text = text.replace('&', '&amp;')
		text = text.replace('<', '&lt;')
		text = text.replace('>', '&gt;')
		fh.write('<page name="%s">\n' % page.name)
		fh.write(text)
		fh.write('</page>\n')
	fh.write('</pagelist>\n')
	fh.close()


def extract(file, dir):
	if dir.exists():
		raise Exception('dir exists already')
	assert False, 'TODO'


if __name__ == '__main__':
	if len(sys.argv) == 4 and sys.argv[1] == '--package':
		package(Dir(sys.argv[2]), File(sys.argv[3]))
	elif len(sys.argv) == 4 and sys.argv[1] == '--extract':
		extract(File(sys.argv[2]), Dir(sys.argv[3]))
	else:
		print('usage: %s --package DIR FILE\n'
		      '       %s --extract FILE DIR' % (sys.argv[0], sys.argv[0]))