23
23
import os
24
24
import typing
25
25
26
+ import case_utils
26
27
import dateutil
27
28
import dateutil .parser
28
29
import dateutil .relativedelta
29
30
import rdflib .util
30
-
31
- import case_utils
32
31
from case_utils .namespace import NS_RDF , NS_UCO_CORE , NS_UCO_OBSERVABLE , NS_XSD
33
32
34
33
_logger = logging .getLogger (os .path .basename (__file__ ))
35
34
36
35
37
36
class ProcessUCOObject (object ):
38
- def __init__ (self , graph : rdflib .Graph , ns_base : rdflib .Namespace , * args : typing .Any , prefix_slug :str = "process-" , ** kwargs : typing .Any ) -> None :
37
+ def __init__ (
38
+ self ,
39
+ graph : rdflib .Graph ,
40
+ ns_base : rdflib .Namespace ,
41
+ * args : typing .Any ,
42
+ prefix_slug : str = "process-" ,
43
+ ** kwargs : typing .Any
44
+ ) -> None :
39
45
"""
40
46
Initializing a ProcessUCOObject will create one triple in the graph. To add data to the new node, call populate_from_gnu_time_log().
41
47
"""
@@ -64,7 +70,9 @@ def populate_from_gnu_time_log(self, gnu_time_log: str) -> None:
64
70
# Reminders on fromtimestamp vs. utcfromtimestamp:
65
71
# https://docs.python.org/3/library/datetime.html#datetime.datetime.utcfromtimestamp
66
72
# "To get an aware datetime object, call fromtimestamp()"
67
- exit_time_datetime = datetime .datetime .fromtimestamp (st_time_log .st_mtime , tz = datetime .timezone .utc )
73
+ exit_time_datetime = datetime .datetime .fromtimestamp (
74
+ st_time_log .st_mtime , tz = datetime .timezone .utc
75
+ )
68
76
exit_time_str = exit_time_datetime .isoformat ()
69
77
self .exit_time = exit_time_str
70
78
else :
@@ -84,22 +92,22 @@ def populate_from_gnu_time_log(self, gnu_time_log: str) -> None:
84
92
elapsed_str = kvdict ["Elapsed (wall clock) time (h:mm:ss or m:ss)" ]
85
93
elapsed_str_parts = elapsed_str .split (":" )
86
94
elapsed_seconds_str = elapsed_str_parts [- 1 ]
87
- elapsed_seconds = int (elapsed_str_parts [ - 1 ] .split ("." )[0 ])
88
- elapsed_microseconds = int (elapsed_str_parts [ - 1 ] .split ("." )[1 ]) * 10000
95
+ elapsed_seconds = int (elapsed_seconds_str .split ("." )[0 ])
96
+ elapsed_microseconds = int (elapsed_seconds_str .split ("." )[1 ]) * 10000
89
97
elapsed_minutes = int (elapsed_str_parts [- 2 ])
90
98
if len (elapsed_str_parts ) == 3 :
91
- elapsed_minutes += ( 60 * int (elapsed_str_parts [- 3 ]) )
99
+ elapsed_minutes += 60 * int (elapsed_str_parts [- 3 ])
92
100
93
101
delta = dateutil .relativedelta .relativedelta (
94
- minutes = elapsed_minutes ,
95
- seconds = elapsed_seconds ,
96
- microseconds = elapsed_microseconds
102
+ minutes = elapsed_minutes ,
103
+ seconds = elapsed_seconds ,
104
+ microseconds = elapsed_microseconds ,
97
105
)
98
106
99
- #logging.debug("delta = %r." % delta)
107
+ # logging.debug("delta = %r." % delta)
100
108
created_time_datetime = exit_time_datetime - delta
101
- #logging.debug("created_time_datetime = %r." % created_time_datetime)
102
- #logging.debug("exit_time_datetime = %r." % exit_time_datetime)
109
+ # logging.debug("created_time_datetime = %r." % created_time_datetime)
110
+ # logging.debug("exit_time_datetime = %r." % exit_time_datetime)
103
111
104
112
self .created_time = created_time_datetime .isoformat ()
105
113
@@ -112,12 +120,18 @@ def created_time(self, value: str) -> None:
112
120
"""
113
121
Only set once.
114
122
"""
115
- assert not value is None
123
+ assert value is not None
116
124
assert self ._created_time is None
117
- str_value = str (value ) # For e.g. datetime objects.
125
+ str_value = str (value ) # For e.g. datetime objects.
118
126
# Confirm text is ISO-8601.
119
- check_value = dateutil .parser .isoparse (str_value )
120
- self .graph .add ((self .n_process_facet , NS_UCO_OBSERVABLE .observableCreatedTime , rdflib .Literal (str_value , datatype = NS_XSD .dateTime )))
127
+ _ = dateutil .parser .isoparse (str_value )
128
+ self .graph .add (
129
+ (
130
+ self .n_process_facet ,
131
+ NS_UCO_OBSERVABLE .observableCreatedTime ,
132
+ rdflib .Literal (str_value , datatype = NS_XSD .dateTime ),
133
+ )
134
+ )
121
135
self ._created_time = value
122
136
123
137
@property
@@ -127,7 +141,9 @@ def exit_status(self) -> typing.Optional[int]:
127
141
@exit_status .setter
128
142
def exit_status (self , value : int ) -> None :
129
143
assert isinstance (value , int )
130
- self .graph .add ((self .n_process_facet , NS_UCO_OBSERVABLE .exitStatus , rdflib .Literal (value )))
144
+ self .graph .add (
145
+ (self .n_process_facet , NS_UCO_OBSERVABLE .exitStatus , rdflib .Literal (value ))
146
+ )
131
147
132
148
@property
133
149
def exit_time (self ) -> typing .Optional [str ]:
@@ -138,11 +154,11 @@ def exit_time(self, value: str) -> None:
138
154
"""
139
155
Only set once.
140
156
"""
141
- assert not value is None
157
+ assert value is not None
142
158
assert self ._exit_time is None
143
- str_value = str (value ) # For e.g. datetime objects.
159
+ str_value = str (value ) # For e.g. datetime objects.
144
160
# Confirm text is ISO-8601.
145
- check_value = dateutil .parser .isoparse (str_value )
161
+ _ = dateutil .parser .isoparse (str_value )
146
162
literal_time = rdflib .Literal (str_value , datatype = NS_XSD .dateTime )
147
163
self .graph .add ((self .n_process_facet , NS_UCO_OBSERVABLE .exitTime , literal_time ))
148
164
self ._exit_time = value
@@ -153,8 +169,12 @@ def n_process_facet(self) -> rdflib.URIRef:
153
169
Created on first access.
154
170
"""
155
171
if self ._n_process_facet is None :
156
- self ._n_process_facet = self .ns_base ["process-facet-" + case_utils .local_uuid .local_uuid ()]
157
- self .graph .add ((self ._n_process_facet , NS_RDF .type , NS_UCO_OBSERVABLE .ProcessFacet ))
172
+ self ._n_process_facet = self .ns_base [
173
+ "process-facet-" + case_utils .local_uuid .local_uuid ()
174
+ ]
175
+ self .graph .add (
176
+ (self ._n_process_facet , NS_RDF .type , NS_UCO_OBSERVABLE .ProcessFacet )
177
+ )
158
178
self .graph .add ((self .node , NS_UCO_CORE .hasFacet , self ._n_process_facet ))
159
179
return self ._n_process_facet
160
180
@@ -178,7 +198,7 @@ def build_process_object(
178
198
ns_base : rdflib .Namespace ,
179
199
gnu_time_log : str ,
180
200
mtime : typing .Optional [str ] = None ,
181
- prefix_slug : typing .Optional [str ] = None
201
+ prefix_slug : typing .Optional [str ] = None ,
182
202
) -> ProcessUCOObject :
183
203
"""
184
204
This function builds a Process UCO Object from a file that contains the output of GNU Time's --verbose flag.
@@ -194,24 +214,37 @@ def build_process_object(
194
214
case_utils .local_uuid .configure ()
195
215
196
216
process_object_kwargs = dict ()
197
- if not prefix_slug is None :
217
+ if prefix_slug is not None :
198
218
process_object_kwargs ["prefix_slug" ] = prefix_slug
199
219
process_object = ProcessUCOObject (graph , ns_base , ** process_object_kwargs )
200
220
201
- if not mtime is None :
221
+ if mtime is not None :
202
222
process_object .exit_time = mtime
203
223
204
224
process_object .populate_from_gnu_time_log (gnu_time_log )
205
225
206
226
return process_object
207
227
228
+
208
229
argument_parser = argparse .ArgumentParser (epilog = __doc__ )
209
230
argument_parser .add_argument ("--base-prefix" , default = "http://example.org/kb/" )
210
231
argument_parser .add_argument ("--debug" , action = "store_true" )
211
- argument_parser .add_argument ("--done-log" , help = "A file recording the completion time of the process that GNU Time was timing, as an ISO-8601 string. If this argument is not provided, the Stat mtime of gnu_time_log is used." )
212
- argument_parser .add_argument ("--output-format" , help = "Override extension-based format guesser." )
213
- argument_parser .add_argument ("gnu_time_log" , help = "A file recording the output of the process wrapper GNU time, with the --verbose flag (recorded with the --output flag). Used to retrieve exit status, conclusion time (if --done-log not provided), and run length." )
214
- argument_parser .add_argument ("out_graph" , help = "A self-contained RDF graph file, in the format either requested by --output-format or guessed based on extension." )
232
+ argument_parser .add_argument (
233
+ "--done-log" ,
234
+ help = "A file recording the completion time of the process that GNU Time was timing, as an ISO-8601 string. If this argument is not provided, the Stat mtime of gnu_time_log is used." ,
235
+ )
236
+ argument_parser .add_argument (
237
+ "--output-format" , help = "Override extension-based format guesser."
238
+ )
239
+ argument_parser .add_argument (
240
+ "gnu_time_log" ,
241
+ help = "A file recording the output of the process wrapper GNU time, with the --verbose flag (recorded with the --output flag). Used to retrieve exit status, conclusion time (if --done-log not provided), and run length." ,
242
+ )
243
+ argument_parser .add_argument (
244
+ "out_graph" ,
245
+ help = "A self-contained RDF graph file, in the format either requested by --output-format or guessed based on extension." ,
246
+ )
247
+
215
248
216
249
def main () -> None :
217
250
args = argument_parser .parse_args ()
@@ -228,12 +261,14 @@ def main() -> None:
228
261
if args .done_log :
229
262
with open (args .done_log , "r" ) as mtime_fh :
230
263
mtime_str = mtime_fh .read (64 ).strip ()
231
- process_object = build_process_object (graph , NS_BASE , args .gnu_time_log , mtime_str )
264
+ _ = build_process_object (graph , NS_BASE , args .gnu_time_log , mtime_str )
232
265
233
- #_logger.debug("args.output_format = %r." % args.output_format)
266
+ # _logger.debug("args.output_format = %r." % args.output_format)
234
267
output_format = args .output_format or rdflib .util .guess_format (args .out_graph )
268
+ assert isinstance (output_format , str )
269
+
270
+ graph .serialize (destination = args .out_graph , format = output_format )
235
271
236
- graph .serialize (destination = args .out_graph )
237
272
238
273
if __name__ == "__main__" :
239
274
main ()
0 commit comments