-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdd2d2.py
More file actions
executable file
·73 lines (61 loc) · 2.66 KB
/
dd2d2.py
File metadata and controls
executable file
·73 lines (61 loc) · 2.66 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 python3
# code by Jon May [jonmay@isi.edu]
import argparse
import sys
import codecs
if sys.version_info[0] == 2:
from itertools import izip
else:
izip = zip
from collections import defaultdict as dd
import re
import os.path
import gzip
import tempfile
import shutil
import atexit
import lxml.etree as ET
scriptdir = os.path.dirname(os.path.abspath(__file__))
reader = codecs.getreader('utf8')
writer = codecs.getwriter('utf8')
def prepfile(fh, code):
ret = gzip.open(fh.name, code) if fh.name.endswith(".gz") else fh
if sys.version_info[0] == 2:
if code.startswith('r'):
ret = reader(fh)
elif code.startswith('w'):
ret = writer(fh)
else:
sys.stderr.write("I didn't understand code "+code+"\n")
sys.exit(1)
return ret
def addonoffarg(parser, arg, dest=None, default=True, help="TODO"):
''' add the switches --arg and --no-arg that set parser.arg to true/false, respectively'''
group = parser.add_mutually_exclusive_group()
dest = arg if dest is None else dest
group.add_argument('--%s' % arg, dest=dest, action='store_true', default=default, help=help)
group.add_argument('--no-%s' % arg, dest=dest, action='store_false', default=default, help="See --%s" % arg)
def main():
parser = argparse.ArgumentParser(description="filter an elisa xml file to given doc list. preset for devdomain to domain2",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--infile", "-i", nargs='?', type=argparse.FileType('r'), default=sys.stdin, help="input file")
parser.add_argument("--documents", "-d", nargs='+', default=["IL3_SN_JONMAY_00000000_123456789_IL3_NW_020018_20150706_G004049YC", "IL3_SN_JONMAY_00000000_123456789_UROOM_TWEETS_1", "IL3_SN_JONMAY_00000000_123456789_UROOM_DOCUMENT_2", "IL3_SN_JONMAY_00000000_123456789_UROOM_TWEETS_2"], help="docs to keep")
parser.add_argument("--outfile", "-o", nargs='?', type=argparse.FileType('w'), default=sys.stdout, help="output file")
workdir = tempfile.mkdtemp(prefix=os.path.basename(__file__), dir=os.getenv('TMPDIR', '/tmp'))
def cleanwork():
shutil.rmtree(workdir, ignore_errors=True)
atexit.register(cleanwork)
try:
args = parser.parse_args()
except IOError as msg:
parser.error(str(msg))
infile = prepfile(args.infile, 'r')
outfile = prepfile(args.outfile, 'w')
root = ET.parse(infile)
for doc in root.findall(".//DOCUMENT"):
if doc.get("id") not in args.documents:
doc.getparent().remove(doc)
# outfile.write(ET.tostring(root, pretty_print=True, encoding='utf-8').decode('utf-8'))
outfile.write(ET.tostring(root, pretty_print=True, encoding='utf-8'))
if __name__ == '__main__':
main()