1 |
|
---|
2 |
|
---|
3 |
""" |
---|
4 |
Parse combined sponge data XML files. |
---|
5 |
|
---|
6 |
Usage: |
---|
7 |
|
---|
8 |
> python parse.py path/to/xml/file |
---|
9 |
> python parse.py -t |
---|
10 |
> python parse.py --test |
---|
11 |
|
---|
12 |
Test silent import. |
---|
13 |
|
---|
14 |
>>> import parse |
---|
15 |
""" |
---|
16 |
|
---|
17 |
__author__ = "Chris Calloway" |
---|
18 |
__email__ = "cbc@chriscalloway.org" |
---|
19 |
__copyright__ = "Copyright 2010 UNC-CH Department of Marine Science" |
---|
20 |
__license__ = "GPL2" |
---|
21 |
|
---|
22 |
import sys |
---|
23 |
import os |
---|
24 |
import glob |
---|
25 |
import doctest |
---|
26 |
import unittest |
---|
27 |
from StringIO import StringIO |
---|
28 |
|
---|
29 |
USAGE = "\n".join(__doc__.splitlines()[3:8]) |
---|
30 |
TEST_PATH = "tests/parse" |
---|
31 |
|
---|
32 |
|
---|
33 |
def _test(): |
---|
34 |
""" |
---|
35 |
Run doctests as unittest suite. |
---|
36 |
|
---|
37 |
Test silent import |
---|
38 |
|
---|
39 |
>>> from parse import _test |
---|
40 |
""" |
---|
41 |
|
---|
42 |
suite = [] |
---|
43 |
suite.append(doctest.DocTestSuite()) |
---|
44 |
suite = unittest.TestSuite(suite) |
---|
45 |
unittest.TextTestRunner().run(suite) |
---|
46 |
|
---|
47 |
return |
---|
48 |
|
---|
49 |
|
---|
50 |
def doc_path(): |
---|
51 |
""" |
---|
52 |
Return the XML document file path from the command line. |
---|
53 |
|
---|
54 |
Supply too few arguments on command line. |
---|
55 |
|
---|
56 |
>>> save_stdout = sys.stdout |
---|
57 |
>>> temp_stdout = StringIO() |
---|
58 |
>>> sys.stdout = temp_stdout |
---|
59 |
>>> sys.argv = [] |
---|
60 |
>>> _doc_path = doc_path() |
---|
61 |
>>> sys.stdout = save_stdout |
---|
62 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
63 |
True |
---|
64 |
|
---|
65 |
Supply too many arguments on the command line. |
---|
66 |
|
---|
67 |
>>> save_stdout = sys.stdout |
---|
68 |
>>> temp_stdout = StringIO() |
---|
69 |
>>> sys.stdout = temp_stdout |
---|
70 |
>>> sys.argv = ["", "", "",] |
---|
71 |
>>> _doc_path = doc_path() |
---|
72 |
>>> sys.stdout = save_stdout |
---|
73 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
74 |
True |
---|
75 |
|
---|
76 |
Supply non-file argument. |
---|
77 |
|
---|
78 |
>>> save_stdout = sys.stdout |
---|
79 |
>>> temp_stdout = StringIO() |
---|
80 |
>>> sys.stdout = temp_stdout |
---|
81 |
>>> _doc_path = os.path.join( |
---|
82 |
... os.path.dirname( |
---|
83 |
... os.path.abspath(__file__)), |
---|
84 |
... TEST_PATH) |
---|
85 |
>>> sys.argv = ["", _doc_path] |
---|
86 |
>>> _doc_path = doc_path() |
---|
87 |
>>> sys.stdout = save_stdout |
---|
88 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
89 |
True |
---|
90 |
|
---|
91 |
Supply nonexistent file argument. |
---|
92 |
|
---|
93 |
>>> save_stdout = sys.stdout |
---|
94 |
>>> temp_stdout = StringIO() |
---|
95 |
>>> sys.stdout = temp_stdout |
---|
96 |
>>> _doc_path = os.path.join( |
---|
97 |
... os.path.dirname( |
---|
98 |
... os.path.abspath(__file__)), |
---|
99 |
... TEST_PATH, "xxxxx") |
---|
100 |
>>> sys.argv = ["", _doc_path] |
---|
101 |
>>> _doc_path = doc_path() |
---|
102 |
>>> sys.stdout = save_stdout |
---|
103 |
>>> USAGE == temp_stdout.getvalue()[:-1] |
---|
104 |
True |
---|
105 |
|
---|
106 |
Supply valid XML document path argument. |
---|
107 |
|
---|
108 |
>>> _doc_path = os.path.join( |
---|
109 |
... os.path.dirname( |
---|
110 |
... os.path.abspath(__file__)), |
---|
111 |
... TEST_PATH, "xml","*","*.xml") |
---|
112 |
>>> _doc_path = glob.glob(_doc_path)[0] |
---|
113 |
>>> sys.argv = ["", _doc_path] |
---|
114 |
>>> _doc_path == doc_path() |
---|
115 |
True |
---|
116 |
""" |
---|
117 |
|
---|
118 |
path = None |
---|
119 |
try: |
---|
120 |
if len(sys.argv) == 2: |
---|
121 |
if sys.argv[1] == "-t" or sys.argv[1] == "--test": |
---|
122 |
_test() |
---|
123 |
else: |
---|
124 |
path = sys.argv[1] |
---|
125 |
if not os.path.exists(path): |
---|
126 |
raise IOError(path + \ |
---|
127 |
" does not exist.") |
---|
128 |
elif not os.path.isfile(path): |
---|
129 |
raise IOError(path + \ |
---|
130 |
" is not a file.") |
---|
131 |
else: |
---|
132 |
raise IOError("Incorrect number of arguments supplied.") |
---|
133 |
except IOError: |
---|
134 |
print USAGE |
---|
135 |
return path |
---|
136 |
|
---|
137 |
|
---|
138 |
def _main(): |
---|
139 |
""" |
---|
140 |
Run module as script. |
---|
141 |
|
---|
142 |
Test silent import. |
---|
143 |
|
---|
144 |
>>> from parse import _main |
---|
145 |
""" |
---|
146 |
|
---|
147 |
_doc_path = doc_path() |
---|
148 |
if _doc_path: |
---|
149 |
print _doc_path |
---|
150 |
|
---|
151 |
return |
---|
152 |
|
---|
153 |
if __name__ == "__main__": |
---|
154 |
_main() |
---|