Repository navigation
Expand file tree
/
Copy pathpull-templates.py
More file actions
executable file
·73 lines (57 loc) · 2.21 KB
/
Copy pathpull-templates.py
File metadata and controls
executable file
·73 lines (57 loc) · 2.21 KB
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
#! /usr/bin/env python
"""Script to pull templates and macros from an openlibrary instance to repository.
"""
import _init_path
import os
import web
from optparse import OptionParser
from openlibrary.api import OpenLibrary, marshal
def parse_options(args=None):
parser = OptionParser(args)
parser.add_option("-s", "--server", dest="server", default="http://openlibrary.org/", help="URL of the openlibrary website (default: %default)")
parser.add_option("--template-root", dest="template_root", default="/upstream", help="Template root (default: %default)")
parser.add_option("--default-plugin", dest="default_plugin", default="upstream", help="Default plugin (default: %default)")
options, args = parser.parse_args()
options.template_root = options.template_root.rstrip("/")
return options, args
def write(path, text):
print "saving", path
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
f = open(path, "w")
f.write(text.encode("utf-8"))
f.close()
def delete(path):
print "deleting", path
if os.path.exists(path):
os.remove(path)
def make_path(doc):
key = doc['key'].rsplit(".")[0]
key = web.lstrips(key, options.template_root)
plugin = doc.get("plugin", options.default_plugin)
return "openlibrary/plugins/%s%s.html" % (plugin, key)
def get_value(doc, property):
value = doc.get(property, "")
if isinstance(value, dict) and "value" in value:
return value['value']
else:
return value
def main():
global options
options, args = parse_options()
ol = OpenLibrary(options.server)
for pattern in args:
docs = ol.query({"key~": pattern, "*": None}, limit=1000)
for doc in marshal(docs):
# Anand: special care to ignore bad documents in the database.
if "--duplicate" in doc['key']:
continue
if doc['type']['key'] == '/type/template':
write(make_path(doc), get_value(doc, 'body'))
elif doc['type']['key'] == '/type/macro':
write(make_path(doc), get_value(doc, 'macro'))
else:
delete(make_path(doc))
if __name__ == "__main__":
main()