21
21
import datetime
22
22
import logging
23
23
import os
24
+ import typing
24
25
25
26
import dateutil
26
27
import dateutil .parser
27
- import rdflib
28
+ import dateutil .relativedelta
29
+ import rdflib .util
28
30
29
31
import case_utils
30
32
36
38
NS_XSD = rdflib .namespace .XSD
37
39
38
40
class ProcessUCOObject (object ):
39
- def __init__ (self , graph , ns_base , ** kwargs ):
41
+ def __init__ (self , graph , ns_base , ** kwargs ) -> None :
40
42
"""
41
43
Initializing a ProcessUCOObject will create one triple in the graph. To add data to the new node, call populate_from_gnu_time_log().
42
44
"""
@@ -51,12 +53,12 @@ def __init__(self, graph, ns_base, **kwargs):
51
53
self ._node = rdflib .URIRef (ns_base [prefix_slug + case_utils .local_uuid .local_uuid ()])
52
54
self .graph .add ((self .node , NS_RDF .type , NS_UCO_OBSERVABLE .Process ))
53
55
54
- self ._bnode_process = None
55
- self ._created_time = None
56
- self ._exit_status = None
57
- self ._exit_time = None
56
+ self ._bnode_process : typing . Optional [ rdflib . BNode ] = None
57
+ self ._created_time : typing . Optional [ str ] = None
58
+ self ._exit_status : typing . Optional [ int ] = None
59
+ self ._exit_time : typing . Optional [ str ] = None
58
60
59
- def populate_from_gnu_time_log (self , gnu_time_log ):
61
+ def populate_from_gnu_time_log (self , gnu_time_log ) -> None :
60
62
"""
61
63
This method populates Process data from a GNU Time log file. If self.exit_time is not set before this method is called, it will be set by reading the modification time of gnu_time_log.
62
64
"""
@@ -106,7 +108,7 @@ def populate_from_gnu_time_log(self, gnu_time_log):
106
108
self .created_time = created_time_datetime .isoformat ()
107
109
108
110
@property
109
- def bnode_process (self ):
111
+ def bnode_process (self ) -> rdflib . BNode :
110
112
"""
111
113
Created on first access.
112
114
"""
@@ -117,11 +119,11 @@ def bnode_process(self):
117
119
return self ._bnode_process
118
120
119
121
@property
120
- def created_time (self ):
122
+ def created_time (self ) -> typing . Optional [ str ] :
121
123
return self ._created_time
122
124
123
125
@created_time .setter
124
- def created_time (self , value ) :
126
+ def created_time (self , value : str ) -> None :
125
127
"""
126
128
Only set once.
127
129
"""
@@ -132,24 +134,22 @@ def created_time(self, value):
132
134
check_value = dateutil .parser .isoparse (str_value )
133
135
self .graph .add ((self .bnode_process , NS_UCO_OBSERVABLE .observableCreatedTime , rdflib .Literal (str_value , datatype = NS_XSD .dateTime )))
134
136
self ._created_time = value
135
- return self ._created_time
136
137
137
138
@property
138
- def exit_status (self ):
139
+ def exit_status (self ) -> typing . Optional [ int ] :
139
140
return self ._exit_status
140
141
141
142
@exit_status .setter
142
- def exit_status (self , value ) :
143
+ def exit_status (self , value : int ) -> None :
143
144
assert isinstance (value , int )
144
145
self .graph .add ((self .bnode_process , NS_UCO_OBSERVABLE .exitStatus , rdflib .Literal (value )))
145
- return self ._exit_status
146
146
147
147
@property
148
- def exit_time (self ):
148
+ def exit_time (self ) -> typing . Optional [ str ] :
149
149
return self ._exit_time
150
150
151
151
@exit_time .setter
152
- def exit_time (self , value ) :
152
+ def exit_time (self , value : str ) -> None :
153
153
"""
154
154
Only set once.
155
155
"""
@@ -161,16 +161,15 @@ def exit_time(self, value):
161
161
literal_time = rdflib .Literal (str_value , datatype = NS_XSD .dateTime )
162
162
self .graph .add ((self .bnode_process , NS_UCO_OBSERVABLE .exitTime , literal_time ))
163
163
self ._exit_time = value
164
- return self ._exit_time
165
164
166
165
@property
167
- def node (self ):
166
+ def node (self ) -> rdflib . URIRef :
168
167
"""
169
168
Read-only property.
170
169
"""
171
170
return self ._node
172
171
173
- def build_process_object (graph , ns_base , gnu_time_log , mtime = None , prefix_slug = None ):
172
+ def build_process_object (graph , ns_base , gnu_time_log , mtime = None , prefix_slug = None ) -> ProcessUCOObject :
174
173
"""
175
174
This function builds a Process UCO Object from a file that contains the output of GNU Time's --verbose flag.
176
175
@@ -204,7 +203,7 @@ def build_process_object(graph, ns_base, gnu_time_log, mtime=None, prefix_slug=N
204
203
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." )
205
204
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." )
206
205
207
- def main ():
206
+ def main () -> None :
208
207
args = argument_parser .parse_args ()
209
208
logging .basicConfig (level = logging .DEBUG if args .debug else logging .INFO )
210
209
@@ -221,9 +220,9 @@ def main():
221
220
process_object = build_process_object (graph , NS_BASE , args .gnu_time_log , mtime_str )
222
221
223
222
#_logger.debug("args.output_format = %r." % args.output_format)
224
- output_format = args .output_format or case_utils .guess_format (args .out_graph )
223
+ output_format = args .output_format or rdflib . util .guess_format (args .out_graph )
225
224
226
- graph .serialize (destination = args .out_graph , format = output_format )
225
+ graph .serialize (destination = args .out_graph )
227
226
228
227
if __name__ == "__main__" :
229
228
main ()
0 commit comments