File: create_large_test_notebook.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 (93 lines) | stat: -rwxr-xr-x 1,730 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python3

import sys
import os
import random

if len(sys.argv) != 2:
	sys.exit("Usage: {} DIRECTORY".format(sys.argv[0]))

root = sys.argv[1]
assert not os.path.exists(root), 'Need new directory'

width = 25
depth = 2
n_links = 3


name = 'some_page_%i_%i'
content = '''\
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.26

====== {title} ======
//Some test data//

Foooo Bar!

{links}
'''
content += ('la la laaa' * 20 + '\n') * 10


def random_links(my_depth):
	links = []

	for n in range(random.randint(0, n_links)):
		links.append("[[%s]]\n" % random_name(my_depth))

	if my_depth < depth:
		for n in range(random.randint(0, n_links)):
			links.append("[[+%s]]\n" % random_name(my_depth+1))
	else:
		for n in range(random.randint(0, n_links)):
			links.append("[[%s]]\n" % random_name(my_depth-1))

	for n in range(random.randint(0, n_links)):
		links.append("[[%s]]\n" % random_date_page())

	return ''.join(links)


def random_name(depth):
	i = random.randint(1, width)
	return name % (depth, i)


def random_date_page():
	year = 2010 + random.randint(0, 8)
	month = random.randint(1, 12)
	day = random.randint(1, 30)
	return ":Date:%i:%i:%i" % (year, month, day)


def populate_level(path, j):
	path += os.path.sep
	os.mkdir(path)
	d = 1

	for i in range(1, width+1):
		myname = name % (j, i)

		filename = os.path.join(path, "{}.txt".format(myname))
		print(">", filename)
		with open(filename, "w") as notebook:
			notebook.write(
				content.format(
					title=myname,
					links=random_links(j),
					# links=''
				)
			)

		if j < depth:
			d += populate_level(path + myname, j + 1)

	return d


d = populate_level(root, 0)
f = d * width

print("Total {} files {} directories".format(f, d))
print("Done")