-
Notifications
You must be signed in to change notification settings - Fork 4
/
export.py
executable file
·160 lines (131 loc) · 4.86 KB
/
export.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgi
from modules.gitdox_sql import *
from modules.ether import ether_to_sgml, get_socialcalc, build_meta_tag, ether_to_csv
from modules.logintools import login
import zipfile
from StringIO import StringIO
from shutil import copyfileobj
import sys, tempfile
from collections import defaultdict
def create_zip(content_name_pairs):
io_file = StringIO()
zf = zipfile.ZipFile(io_file, mode='w', compression=zipfile.ZIP_DEFLATED)
for content, name in content_name_pairs:
try:
zf.writestr(name, content)#.encode("utf8"))
except Exception as e:
try:
zf.writestr(name, content.encode("utf8"))
except:
print("Content-type:text/html\r\n\r\n")
raise e
return io_file
def export_all_docs(config=None, corpus_filter=None, status=None, extension="sgml", names=None, no_corpus_name=False):
docs = get_all_docs(corpus_filter,status, names)
files = []
all_corpus_meta = defaultdict(dict)
for doc in docs:
doc_id, docname, corpus, mode, content = doc
if corpus not in all_corpus_meta:
corpus_meta = get_doc_meta(doc_id, corpus=True)
for md in corpus_meta:
key, val = md[2], md[3]
all_corpus_meta[corpus][key] = val
if corpus_filter is None and not no_corpus_name: # All documents exported, use corpus prefix to avoid name clashes
filename = corpus + "_" + docname
else: # Only exporting one user specified corpus, name documents without prefix
filename = docname
if mode == "xml" and config!="[CSV]":
content = build_meta_tag(doc_id) + content.strip() + "\n</meta>\n"
files.append((content,filename + ".xml"))
elif mode in ["ether","entities"]:
ether_name = "_".join(["gd", corpus, docname])
if config=="[CSV]":
csv = ether_to_csv(ether_url,ether_name)
files.append((csv, filename + ".csv"))
else:
sgml = ether_to_sgml(get_socialcalc(ether_url, ether_name),doc_id,config=config)
files.append((sgml, filename + "." + extension))
for corp in all_corpus_meta:
serialized_meta = ""
for key in all_corpus_meta[corp]:
serialized_meta += key + "\t" + all_corpus_meta[corp][key] + "\n"
files.append((serialized_meta.encode("utf8"), "_meta_" + corp + ".tab"))
zip_io = create_zip(files)
temp = tempfile.NamedTemporaryFile(delete=False, mode='w+b')
temp.write(zip_io.getvalue())
temp.close()
if corpus_filter is not None:
zipname = corpus_filter + ".zip"
else:
zipname = "export.zip"
print("Content-type: application/download")
print("Content-Disposition: attachment; filename=" + zipname)
print("")
sys.stdout.flush()
with open(temp.name,'rb') as z:
copyfileobj(z, sys.stdout)
os.remove(temp.name)
def export_doc(doc_id, stylesheet=None):
docname, corpus, filename, status, assignee_username, mode, schema = get_doc_info(doc_id)
ether_name = "_".join(["gd", corpus, docname])
sgml = ether_to_sgml(get_socialcalc(ether_url, ether_name), doc_id, config=stylesheet)
cpout = ""
cpout += "Content-Type: application/download\n"
if "tt" in stylesheet:
cpout += "Content-Disposition: attachment; filename=" + docname + ".tt\n\n"
else:
cpout += "Content-Disposition: attachment; filename=" + corpus + "_" + docname + ".sgml\n\n"
if isinstance(cpout,unicode):
cpout = str(cpout.encode("utf8"))
cpout += sgml
return cpout
if __name__ == "__main__":
#print("Content-type:text/html\r\n\r\n")
import cgitb
#cgitb.enable()
from paths import ether_url
thisscript = os.environ.get('SCRIPT_NAME', '')
action = None
theform = cgi.FieldStorage()
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
action, userconfig = login(theform, userdir, thisscript, action)
user = userconfig["username"]
admin = userconfig["admin"]
export_stylesheet = "--default--"
if "extension" in theform:
extension = theform.getvalue("extension")
else:
extension = "sgml"
if "stylesheet" in theform:
export_stylesheet = theform.getvalue("stylesheet")
else:
export_stylesheet = None
if "status" in theform:
status = theform.getvalue("status")
else:
status = None
if "corpus" in theform:
corpus = theform.getvalue("corpus")
else:
corpus = None
if corpus == "--ALL--":
corpus = None
if status == "--ALL--":
status = None
if status is not None:
status = status.replace("%252C",",").replace("%2C",",") # unmangle commas
no_corpus_name = False
if "no_corpus_name" in theform:
no_corpus_name = bool(theform.getvalue("no_corpus_name"))
if "docs" in theform:
docs = theform.getvalue("docs").replace("%252C",",").replace("%2C",",")
if docs == "%all%" or docs=="--ALL--":
export_all_docs(export_stylesheet,corpus_filter=corpus,extension=extension,status=status,no_corpus_name=no_corpus_name)
elif ',' in docs or not docs.isdigit():
export_all_docs(export_stylesheet,corpus_filter=corpus,extension=extension,status=status,names=docs,no_corpus_name=no_corpus_name)
else:
print(export_doc(docs, export_stylesheet))