File: package_test_data.py

package info (click to toggle)
zim 0.62-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,660 kB
  • ctags: 6,996
  • sloc: python: 52,094; xml: 1,135; makefile: 45; sh: 36
file content (55 lines) | stat: -rwxr-xr-x 1,354 bytes parent folder | download | duplicates (6)
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/python

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" econding="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 alread'
	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])