Skip to content

Commit e249a7d

Browse files
committed
clean script_tools and timestamp for old py2isms
1 parent eeec84e commit e249a7d

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

lib/vsc/utils/script_tools.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __init__(self, options, run_prologue=True, excepthook=None, **kwargs):
125125
"""
126126

127127
options_ = _merge_options(options)
128-
super(ExtendedSimpleOption, self).__init__(options_, **kwargs)
128+
super().__init__(options_, **kwargs)
129129

130130
self.nagios_reporter = None
131131
self.lockfile = None
@@ -223,11 +223,11 @@ def critical_exception_handler(self, tp, value, traceback):
223223
"""
224224
self.log.exception("unhandled exception detected: %s - %s", tp, value)
225225
self.log.debug("traceback %s", traceback)
226-
message = "Script failure: %s - %s" % (tp, value)
226+
message = f"Script failure: {tp} - {value}"
227227
self.critical(message)
228228

229229

230-
class CLI(object):
230+
class CLI:
231231
"""
232232
Base class to implement cli tools that require timestamps, nagios checks, etc.
233233
"""
@@ -271,13 +271,13 @@ def make_options(self, defaults=None):
271271
# insert default timestamp value file based on name
272272
if TIMESTAMP_FILE_OPTION in options:
273273
tsopt = list(options[TIMESTAMP_FILE_OPTION])
274-
tsopt[-1] = os.path.join(self.CACHE_DIR, "%s.timestamp" % self.name)
274+
tsopt[-1] = os.path.join(self.CACHE_DIR, f"{self.name}.timestamp")
275275
options[TIMESTAMP_FILE_OPTION] = tuple(tsopt)
276276

277277
options.update(self.CLI_OPTIONS)
278278

279279
if TIMESTAMP_FILE_OPTION not in options and self.TIMESTAMP_MANDATORY:
280-
raise Exception("no mandatory %s option defined" % (TIMESTAMP_FILE_OPTION,))
280+
raise ValueError(f"no mandatory {TIMESTAMP_FILE_OPTION} option defined")
281281

282282
return ExtendedSimpleOption(options)
283283

@@ -332,7 +332,7 @@ def do(self, dry_run): #pylint: disable=unused-argument
332332
self.thresholds can be used to pass the thresholds during epilogue
333333
"""
334334
logging.error("`do` method not implemented")
335-
raise Exception("Not implemented")
335+
raise NotImplementedError("Not implemented")
336336
return "Not implemented"
337337

338338
def make_time(self):
@@ -375,14 +375,13 @@ def post(self, errors, current_time=None):
375375
_, timestamp = convert_timestamp(current_time)
376376
write_timestamp(self.options.timestamp_file, timestamp)
377377
except Exception as err:
378-
txt = "Writing timestamp %s to %s failed: %s" % (timestamp, self.options.timestamp_file, err)
378+
txt = f"Writing timestamp {timestamp} to {self.options.timestamp_file} failed: {err}"
379379
self.critical_exception(txt, err)
380380

381381
def final(self):
382382
"""
383383
Run as finally block in main
384384
"""
385-
pass
386385

387386
def main(self):
388387
"""
@@ -406,14 +405,14 @@ def main(self):
406405

407406
self.post(errors)
408407

409-
self.fulloptions.epilogue("%s complete" % msg, self.thresholds)
408+
self.fulloptions.epilogue(f"{msg} complete", self.thresholds)
410409

411410

412411

413412
class NrpeCLI(CLI):
414413

415414
def __init__(self, name=None, default_options=None):
416-
super(NrpeCLI, self).__init__(name=name, default_options=default_options)
415+
super().__init__(name=name, default_options=default_options)
417416

418417
def ok(self, msg):
419418
"""

lib/vsc/utils/timestamp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: latin-1 -*-
21
#
32
# Copyright 2009-2023 Ghent University
43
#
@@ -61,7 +60,7 @@ def convert_to_datetime(timestamp=None):
6160
return datetime.datetime.utcnow().replace(tzinfo=utc)
6261

6362
if isinstance(timestamp, int):
64-
timestamp = "%010d" % timestamp
63+
timestamp = f"{int(timestamp):010}"
6564
if isinstance(timestamp, datetime.datetime):
6665
if timestamp.tzinfo is None:
6766
timestamp = timestamp.replace(tzinfo=utc)
@@ -77,7 +76,7 @@ def convert_to_datetime(timestamp=None):
7776
elif len(timestamp) == 8:
7877
date_format = "%Y%m%d"
7978
else:
80-
raise Exception("invalid format provided %s" % timestamp)
79+
raise ValueError(f"invalid format provided {timestamp}")
8180
timestamp = datetime.datetime.strptime(timestamp, date_format)
8281

8382
return timestamp.replace(tzinfo=utc)

0 commit comments

Comments
 (0)