2323import os
2424import typing
2525
26+ import case_utils
2627import dateutil
2728import dateutil .parser
2829import dateutil .relativedelta
2930import rdflib .util
30-
31- import case_utils
3231from case_utils .namespace import NS_RDF , NS_UCO_CORE , NS_UCO_OBSERVABLE , NS_XSD
3332
3433_logger = logging .getLogger (os .path .basename (__file__ ))
3534
3635
3736class 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 :
3945 """
4046 Initializing a ProcessUCOObject will create one triple in the graph. To add data to the new node, call populate_from_gnu_time_log().
4147 """
@@ -64,7 +70,9 @@ def populate_from_gnu_time_log(self, gnu_time_log: str) -> None:
6470 # Reminders on fromtimestamp vs. utcfromtimestamp:
6571 # https://docs.python.org/3/library/datetime.html#datetime.datetime.utcfromtimestamp
6672 # "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+ )
6876 exit_time_str = exit_time_datetime .isoformat ()
6977 self .exit_time = exit_time_str
7078 else :
@@ -84,22 +92,22 @@ def populate_from_gnu_time_log(self, gnu_time_log: str) -> None:
8492 elapsed_str = kvdict ["Elapsed (wall clock) time (h:mm:ss or m:ss)" ]
8593 elapsed_str_parts = elapsed_str .split (":" )
8694 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
8997 elapsed_minutes = int (elapsed_str_parts [- 2 ])
9098 if len (elapsed_str_parts ) == 3 :
91- elapsed_minutes += ( 60 * int (elapsed_str_parts [- 3 ]) )
99+ elapsed_minutes += 60 * int (elapsed_str_parts [- 3 ])
92100
93101 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 ,
97105 )
98106
99- #logging.debug("delta = %r." % delta)
107+ # logging.debug("delta = %r." % delta)
100108 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)
103111
104112 self .created_time = created_time_datetime .isoformat ()
105113
@@ -112,12 +120,18 @@ def created_time(self, value: str) -> None:
112120 """
113121 Only set once.
114122 """
115- assert not value is None
123+ assert value is not None
116124 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.
118126 # 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+ )
121135 self ._created_time = value
122136
123137 @property
@@ -127,7 +141,9 @@ def exit_status(self) -> typing.Optional[int]:
127141 @exit_status .setter
128142 def exit_status (self , value : int ) -> None :
129143 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+ )
131147
132148 @property
133149 def exit_time (self ) -> typing .Optional [str ]:
@@ -138,11 +154,11 @@ def exit_time(self, value: str) -> None:
138154 """
139155 Only set once.
140156 """
141- assert not value is None
157+ assert value is not None
142158 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.
144160 # Confirm text is ISO-8601.
145- check_value = dateutil .parser .isoparse (str_value )
161+ _ = dateutil .parser .isoparse (str_value )
146162 literal_time = rdflib .Literal (str_value , datatype = NS_XSD .dateTime )
147163 self .graph .add ((self .n_process_facet , NS_UCO_OBSERVABLE .exitTime , literal_time ))
148164 self ._exit_time = value
@@ -153,8 +169,12 @@ def n_process_facet(self) -> rdflib.URIRef:
153169 Created on first access.
154170 """
155171 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+ )
158178 self .graph .add ((self .node , NS_UCO_CORE .hasFacet , self ._n_process_facet ))
159179 return self ._n_process_facet
160180
@@ -178,7 +198,7 @@ def build_process_object(
178198 ns_base : rdflib .Namespace ,
179199 gnu_time_log : str ,
180200 mtime : typing .Optional [str ] = None ,
181- prefix_slug : typing .Optional [str ] = None
201+ prefix_slug : typing .Optional [str ] = None ,
182202) -> ProcessUCOObject :
183203 """
184204 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(
194214 case_utils .local_uuid .configure ()
195215
196216 process_object_kwargs = dict ()
197- if not prefix_slug is None :
217+ if prefix_slug is not None :
198218 process_object_kwargs ["prefix_slug" ] = prefix_slug
199219 process_object = ProcessUCOObject (graph , ns_base , ** process_object_kwargs )
200220
201- if not mtime is None :
221+ if mtime is not None :
202222 process_object .exit_time = mtime
203223
204224 process_object .populate_from_gnu_time_log (gnu_time_log )
205225
206226 return process_object
207227
228+
208229argument_parser = argparse .ArgumentParser (epilog = __doc__ )
209230argument_parser .add_argument ("--base-prefix" , default = "http://example.org/kb/" )
210231argument_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+
215248
216249def main () -> None :
217250 args = argument_parser .parse_args ()
@@ -228,12 +261,14 @@ def main() -> None:
228261 if args .done_log :
229262 with open (args .done_log , "r" ) as mtime_fh :
230263 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 )
232265
233- #_logger.debug("args.output_format = %r." % args.output_format)
266+ # _logger.debug("args.output_format = %r." % args.output_format)
234267 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 )
235271
236- graph .serialize (destination = args .out_graph )
237272
238273if __name__ == "__main__" :
239274 main ()
0 commit comments