forked from ivoa-std/ivoatex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submission.py
290 lines (242 loc) · 7.83 KB
/
submission.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
"""
A little script to operate the IVOA submission form with data scrubbed
from ivoatex sources.
The keys used here are taken from screen-scraping
https://www.ivoa.net/cgi-bin/up.cgi (which is also the target to POST
to).
Fields needed:
* doctitle
* conciseName
* email
* filename
* author
* editor
* abstract
* comment
* group (one of app, dal, dm, gws, reg, dcp, std, semantics, the, voe, vot,
voq)
* docver1, docver2
* year, month, day
* doctype (one of note, wd, pr, rec, other)
"""
import pprint
import os
import re
import subprocess
import sys
import tempfile
from xml.etree import ElementTree as etree
try:
import requests
except ImportError:
sys.exit("*** Automatic document submission needs python-requests.\n"
"*** Install a package named like this or get it from\n"
"*** https://pypi.python.org/pypi/requests")
DOCREPO_URL = 'https://www.ivoa.net/cgi-bin/up.cgi'
class ReportableError(Exception):
"""raise this with a human-readable error message to cause a non-traceback
program exit.
"""
def H(el_name):
"""returns an XHTML 1.0 Qname for el_name.
"""
return etree.QName("http://www.w3.org/1999/xhtml", el_name)
def to_text(el):
"""returns a concatenation of the text contents of el and its sub-elements.
"""
return "".join(el.itertext()).strip()
class DocumentMeta(object):
"""a blackboard to collect the various pieces of information on the
document.
For now, we just use attributes named like the fields in the
IVOA docrepo API.
"""
_attrs = ["doctitle", "conciseName", "email",
"author", "editor", "abstract",
"comment", "group", "docver1", "docver2",
"year", "month", "day", "doctype"]
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
self._authors = []
self._editors = []
self.group = None
self.comment = ""
def get_post_payload(self):
"""returns a dictionary ready to post with requests.
"""
payload = {}
for name in self._attrs:
if not hasattr(self, name):
raise ReportableError("Metadata item %s missing.\n This usually"
" is because the generated HTML is bad."%name)
payload[name] = getattr(self, name)
return payload
def get_date(self):
"""returns the document date in ISO format.
"""
return "%s-%s-%s"%(self.year, self.month, self.day)
def add_info_from_document(self):
"""tries to obtain missing metadata from the formatted (XHTML) source.
"""
with open(self.conciseName+".html", "rb") as f:
tree = etree.parse(f)
# The following would be a bit smoother if we had xpath; there's
# no xpath engine in the stdlib, though (and no BeautifulSoup),
# so let's do a bit of manual work rather than pull in a fat
# dependency.
# first h1 is the document title
for el in tree.iter(H("h1")):
self.doctitle = to_text(el)
break
# pull things with ids or unique classes
for el in tree.iter():
if el.get("id")=="abstract":
# first element currently is an h2 with text "Abstract".
# actual content is in the element tail. Ugh. This needs
# cleanup.
el[0].text = ""
self.abstract = to_text(el)
elif el.get("id")=="ivoagroup":
self.group = self._get_wg_code(to_text(el))
elif el.get("class")=="author":
self._authors.append(to_text(el))
elif el.get("class")=="editor":
self._editors.append(to_text(el))
@property
def author(self):
return ", ".join(self._authors)
@property
def editor(self):
return ", ".join(self._editors)
@classmethod
def from_makefile(cls):
"""creates a basic document meta with attributes obtainable
from the makefile filled in.
"""
meta_keys = {}
with open("Makefile", encoding="utf-8") as f:
for ln in f:
mat = re.match("(\w+)\s*=\s*(.*)", ln)
if mat:
meta_keys[mat.group(1)] = mat.group(2)
kwargs = {}
for input_key, parser_function in [
("DOCNAME", lambda v: [("conciseName", v.strip())]),
("DOCVERSION", cls._parse_DOCVERSION),
("DOCDATE", cls._parse_DOCDATE),
("AUTHOR_EMAIL", cls._parse_AUTHOR_EMAIL),
("DOCTYPE", lambda v: [("doctype", v.strip().lower())])]:
if input_key not in meta_keys:
raise ReportableError("%s not defined/garbled in Makefile"
" but required for upload."%input_key)
kwargs.update(
dict(parser_function(meta_keys[input_key])))
res = cls(**kwargs)
if "IVOA_GROUP" in meta_keys:
res.group = res._get_wg_code(meta_keys["IVOA_GROUP"])
return res
_wg_mapping = {
"Applications": "app",
"DAL": "dal",
"Data Access Layer": "dal",
"Data Models": "dm",
"Grid and Web Services": "gws",
"Registry": "reg",
"Data Curation and Preservation": "dcp",
"Documents and Standards": "std",
"Standards and Processes": "std",
"Semantics": "semantics",
"Theory": "the",
"VO Event": "voe",
"Time Domain": "voe",
"Education": "edu",
"No Group": "none",
}
def _get_wg_code(self, wg_string):
"""returns one of the docrepo codes for the ivoa WGs.
This will look at wg_string only if self.group isn't already
set (in which case self.group is simply returned); this allows
overriding the WG name in the Makefile if necessary.
"""
if self.group:
return self.group
if wg_string not in self._wg_mapping.keys():
raise ReportableError("ivoagroup must be one of %s. If this is"
" really inappropriate, set IVOA_GROUP = No Group in the Makefile"%
", ".join(self._wg_mapping.keys()))
return self._wg_mapping[wg_string]
@staticmethod
def _parse_DOCVERSION(version_string):
"""helps from_makefile by returning form keys from the document version.
"""
mat = re.match("(\d).(\d+)", version_string)
if not mat:
raise ReportableError("DOCVERSION in Makefile (%s) garbled."%
version_string)
yield "docver1", mat.group(1)
yield "docver2", mat.group(2)
@staticmethod
def _parse_DOCDATE(date_string):
"""helps from_makefile by returning form keys from the document date.
"""
mat = re.match("(\d\d\d\d)-(\d\d)-(\d\d)", date_string)
if not mat:
raise ReportableError("DOCDATE in Makefile (%s) garbled."%
date_string)
yield "year", mat.group(1)
yield "month", mat.group(2)
yield "day", mat.group(3)
@staticmethod
def _parse_AUTHOR_EMAIL(email_string):
"""helps from_makefile by returning a form key for the email.
"""
yield "email", email_string
def review_and_comment(document_meta):
"""prints document_meta and lets the user add a remark if they want.
"""
editor = os.environ.get("VISUAL",
os.environ.get("EDITOR", "nano"))
fd, path_name = comment_src = tempfile.mkstemp()
try:
os.write(fd, b'# optionally enter comment(s) below.\n')
os.close(fd)
subprocess.check_call([editor, path_name])
with open(path_name, encoding="utf-8") as f:
document_meta.comment = re.sub("(?m)^#.*$", "", f.read())
finally:
os.unlink(path_name)
pprint.pprint(document_meta.get_post_payload())
print("-----------------------\n")
print("Going to upload %s\n"%document_meta.doctitle)
print("*** Version: %s.%s, %s of %s ***\n"%(
document_meta.docver1,
document_meta.docver2,
document_meta.doctype,
document_meta.get_date()))
print("Hit ^C if this (or anthing in the dict above) is wrong,"
" enter to upload.")
input()
def main(archive_file_name):
document_meta = DocumentMeta.from_makefile()
document_meta.add_info_from_document()
review_and_comment(document_meta)
sys.stdout.write("Uploading... ")
sys.stdout.flush()
with open(sys.argv[1], "rb") as upload:
resp = requests.post(DOCREPO_URL,
data=document_meta.get_post_payload(),
files=[('filename', (sys.argv[1], upload))])
sys.stdout.write("done (result in docrepo-response.html)\n")
with open("docrepo-response.html", "w", encoding="utf-8") as f:
f.write(resp.text)
if __name__=="__main__":
try:
if len(sys.argv)!=2:
raise ReportableError(
"Usage: %s <upload package file name>"%sys.argv[0])
main(sys.argv[1])
except ReportableError as msg:
sys.stderr.write("*** Failure while preparing submission:\n")
sys.exit(msg)