Skip to content

Commit 92780a3

Browse files
committed
Add type signatures to case_gnu_time program
Signed-off-by: Alex Nelson <[email protected]>
1 parent 9b794fe commit 92780a3

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

case_gnu_time/__init__.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import datetime
2222
import logging
2323
import os
24+
import typing
2425

2526
import dateutil
2627
import dateutil.parser
27-
import rdflib
28+
import dateutil.relativedelta
29+
import rdflib.util
2830

2931
import case_utils
3032

@@ -36,7 +38,7 @@
3638
NS_XSD = rdflib.namespace.XSD
3739

3840
class ProcessUCOObject(object):
39-
def __init__(self, graph, ns_base, **kwargs):
41+
def __init__(self, graph, ns_base, **kwargs) -> None:
4042
"""
4143
Initializing a ProcessUCOObject will create one triple in the graph. To add data to the new node, call populate_from_gnu_time_log().
4244
"""
@@ -51,12 +53,12 @@ def __init__(self, graph, ns_base, **kwargs):
5153
self._node = rdflib.URIRef(ns_base[prefix_slug + case_utils.local_uuid.local_uuid()])
5254
self.graph.add((self.node, NS_RDF.type, NS_UCO_OBSERVABLE.Process))
5355

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
5860

59-
def populate_from_gnu_time_log(self, gnu_time_log):
61+
def populate_from_gnu_time_log(self, gnu_time_log) -> None:
6062
"""
6163
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.
6264
"""
@@ -106,7 +108,7 @@ def populate_from_gnu_time_log(self, gnu_time_log):
106108
self.created_time = created_time_datetime.isoformat()
107109

108110
@property
109-
def bnode_process(self):
111+
def bnode_process(self) -> rdflib.BNode:
110112
"""
111113
Created on first access.
112114
"""
@@ -117,11 +119,11 @@ def bnode_process(self):
117119
return self._bnode_process
118120

119121
@property
120-
def created_time(self):
122+
def created_time(self) -> typing.Optional[str]:
121123
return self._created_time
122124

123125
@created_time.setter
124-
def created_time(self, value):
126+
def created_time(self, value: str) -> None:
125127
"""
126128
Only set once.
127129
"""
@@ -132,24 +134,22 @@ def created_time(self, value):
132134
check_value = dateutil.parser.isoparse(str_value)
133135
self.graph.add((self.bnode_process, NS_UCO_OBSERVABLE.observableCreatedTime, rdflib.Literal(str_value, datatype=NS_XSD.dateTime)))
134136
self._created_time = value
135-
return self._created_time
136137

137138
@property
138-
def exit_status(self):
139+
def exit_status(self) -> typing.Optional[int]:
139140
return self._exit_status
140141

141142
@exit_status.setter
142-
def exit_status(self, value):
143+
def exit_status(self, value: int) -> None:
143144
assert isinstance(value, int)
144145
self.graph.add((self.bnode_process, NS_UCO_OBSERVABLE.exitStatus, rdflib.Literal(value)))
145-
return self._exit_status
146146

147147
@property
148-
def exit_time(self):
148+
def exit_time(self) -> typing.Optional[str]:
149149
return self._exit_time
150150

151151
@exit_time.setter
152-
def exit_time(self, value):
152+
def exit_time(self, value: str) -> None:
153153
"""
154154
Only set once.
155155
"""
@@ -161,16 +161,15 @@ def exit_time(self, value):
161161
literal_time = rdflib.Literal(str_value, datatype=NS_XSD.dateTime)
162162
self.graph.add((self.bnode_process, NS_UCO_OBSERVABLE.exitTime, literal_time))
163163
self._exit_time = value
164-
return self._exit_time
165164

166165
@property
167-
def node(self):
166+
def node(self) -> rdflib.URIRef:
168167
"""
169168
Read-only property.
170169
"""
171170
return self._node
172171

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:
174173
"""
175174
This function builds a Process UCO Object from a file that contains the output of GNU Time's --verbose flag.
176175
@@ -204,7 +203,7 @@ def build_process_object(graph, ns_base, gnu_time_log, mtime=None, prefix_slug=N
204203
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.")
205204
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.")
206205

207-
def main():
206+
def main() -> None:
208207
args = argument_parser.parse_args()
209208
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
210209

@@ -221,9 +220,9 @@ def main():
221220
process_object = build_process_object(graph, NS_BASE, args.gnu_time_log, mtime_str)
222221

223222
#_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)
225224

226-
graph.serialize(destination=args.out_graph, format=output_format)
225+
graph.serialize(destination=args.out_graph)
227226

228227
if __name__ == "__main__":
229228
main()

0 commit comments

Comments
 (0)