diff --git a/assimilate/command.py b/assimilate/command.py index 2ecd75a..98d56e2 100644 --- a/assimilate/command.py +++ b/assimilate/command.py @@ -55,7 +55,7 @@ ) from .utilities import ( gethostname, output, pager, process_cmdline, read_latest, table, to_date, - to_days, to_seconds, two_columns, update_latest, when, + to_days, to_seconds, two_columns, update_latest, when, UnknownConversion ) @@ -1299,9 +1299,11 @@ def gen_message(cmd): try: with Quantity.prefs(spacer=" "): return cmdline["--message"].format(**replacements) + except UnknownConversion as e: + raise Error(e, culprit = "--message") except KeyError as e: raise Error( - f"‘{e.args[0]}’ is an unknown key.", + f"‘{e!s}’ is an unknown key.", culprit = "--message", codicil = f"Choose from: {conjoin(replacements.keys())}." ) diff --git a/assimilate/overdue.py b/assimilate/overdue.py index cdc986e..61fc63e 100644 --- a/assimilate/overdue.py +++ b/assimilate/overdue.py @@ -129,7 +129,7 @@ # as_seconds {{{2 def as_seconds(arg, units=None): arg = as_string(arg) - return Quantity(arg, units or 'h').scale('s') + return Quantity(arg, units or 'h').scale('seconds') # SCHEMA {{{1 validate_settings = Schema( @@ -179,7 +179,7 @@ def get_local_data(description, config, path, max_age): locked = path.parent.glob('lock.*') delta = now - mtime - age = Quantity(24 * 60 * 60 * delta.days + delta.seconds, 's') + age = Quantity(24 * 60 * 60 * delta.days + delta.seconds, 'seconds') overdue = truth(age > max_age) locked = truth(locked) yield dict( diff --git a/assimilate/utilities.py b/assimilate/utilities.py index 67b78f0..e3a0f9f 100644 --- a/assimilate/utilities.py +++ b/assimilate/utilities.py @@ -28,7 +28,7 @@ Error, conjoin, cull, error, full_stop, join, narrate, os_error, warn, output as output_raw, terminate ) -from quantiphy import Quantity, UnitConversion, QuantiPhyError +from quantiphy import Quantity, UnitConversion, QuantiPhyError, UnknownConversion from .shlib import Run, set_prefs as set_shlib_prefs set_shlib_prefs(use_inform=True, log_cmd=True) @@ -289,18 +289,20 @@ def process_cmdline(*args, **kwargs): terminate(3) # time conversions {{{1 -UnitConversion('s', 'sec second seconds') -UnitConversion('s', 'm min minute minutes', 60) -UnitConversion('s', 'h hr hour hours', 60*60) -UnitConversion('s', 'D d day days', 24*60*60) -UnitConversion('s', 'W w week weeks', 7*24*60*60) -UnitConversion('s', 'M month months', 30*24*60*60) -UnitConversion('s', 'Y y year years', 365*24*60*60) -UnitConversion('d', 'm min minute minutes', 1/60/24) -UnitConversion('d', 'h hr hour hours', 1/24) -UnitConversion('d', 'W w week weeks', 7) -UnitConversion('d', 'M month months', 30) -UnitConversion('d', 'Y y year years', 365) +UnitConversion('seconds', 's sec second') +UnitConversion('seconds', 'm min minute minutes', 60) +UnitConversion('seconds', 'h hr hour hours', 60*60) +UnitConversion('seconds', 'D d day days', 24*60*60) +UnitConversion('seconds', 'W w week weeks', 7*24*60*60) +UnitConversion('seconds', 'M month months', 30*24*60*60) +UnitConversion('seconds', 'Y y year years', 365*24*60*60) +UnitConversion('days', 's sec second seconds', 1/60/60/24) +UnitConversion('days', 'm min minute minutes', 1/60/24) +UnitConversion('days', 'h hr hour hours', 1/24) +UnitConversion('days', 'D d day', 1) +UnitConversion('days', 'W w week weeks', 7) +UnitConversion('days', 'M month months', 30) +UnitConversion('days', 'Y y year years', 365) Quantity.set_prefs(ignore_sf=True, spacer='') # to_seconds() {{{2 @@ -309,25 +311,25 @@ def to_seconds(time_spec, default_units='d'): # be a relative time format (Ny, NM, Nw, Nd, Nm, Ns). # If an absolute format is given, then the return value is the number of # seconds in from now to the given date (is positive if date is in past). - # A Quantity with units of 's' is returned. + # A Quantity with units of 'seconds' is returned. try: target = arrow.get(time_spec, tzinfo='local') - return Quantity((arrow.now() - target).total_seconds(), 's') + return Quantity((arrow.now() - target).total_seconds(), 'seconds') except arrow.parser.ParserError: - return Quantity(time_spec, default_units, scale='s') + return Quantity(time_spec, default_units, scale='seconds') # to_days() {{{2 -def to_days(time_spec, default_units='s'): +def to_days(time_spec, default_units='seconds'): # The time_spec may be an absolute format (an arrow date format) or it may # be a relative time format (Ny, NM, Nw, Nd, Nm, Ns). # If an absolute format is given, then the return value is the number of # seconds in from now to the given date (is positive if date is in past). - # A Quantity with units of 's' is returned. + # A Quantity with units of 'days' is returned. try: target = arrow.get(time_spec, tzinfo='local') - return Quantity((arrow.now() - target).total_seconds(), 's', scale='d') + return Quantity((arrow.now() - target).total_seconds(), 'seconds', scale='days') except arrow.parser.ParserError: - return Quantity(time_spec, default_units, scale='d') + return Quantity(time_spec, default_units, scale='days') # to_date() {{{2 def to_date(time_spec, default_units='d'): @@ -338,7 +340,7 @@ def to_date(time_spec, default_units='d'): return arrow.get(time_spec, tzinfo='local') except arrow.parser.ParserError as e: try: - seconds = Quantity(time_spec, default_units, scale='s') + seconds = Quantity(time_spec, default_units, scale='seconds') return arrow.now().shift(seconds=-seconds) except QuantiPhyError: codicil = join( diff --git a/tests/assimilate.nt b/tests/assimilate.nt index 3914cb0..1e7c955 100644 --- a/tests/assimilate.nt +++ b/tests/assimilate.nt @@ -4233,23 +4233,23 @@ assimilate: run: assimilate due --since-backup 0 --message '⟪config⟫: ⟪cmd⟫ ⟪action⟫ ⟪since⟫ ⟪elapsed⟫' checks: stdout: - matches regex: ^test: backup backup 0d .* + matches regex: ^test: backup backup 0 days .* status: 1 agony — due --since-check ❬since❭ --message ❬msg❭ {{{2: - run: assimilate due --since-check 0h --message '⟪config⟫: ⟪cmd⟫ ⟪action⟫ ⟪since:0.1ph⟫ ⟪elapsed⟫' + run: assimilate due --since-check 0h --message '⟪config⟫: ⟪cmd⟫ ⟪action⟫ ⟪since:0.1phours⟫ ⟪elapsed⟫' checks: stdout: - matches regex: ^test: check check 0h .* + matches regex: ^test: check check 0 hours .* status: 1 resume — due --since-squeeze ❬since❭ --message ❬msg❭ {{{2: - run: assimilate due --since-squeeze 0Y --message '⟪config⟫: ⟪cmd⟫ ⟪action⟫ ⟪since:0.0ps⟫ ⟪elapsed⟫' + run: assimilate due --since-squeeze 0Y --message '⟪config⟫: ⟪cmd⟫ ⟪action⟫ ⟪since:0.0pseconds⟫ ⟪elapsed⟫' checks: stdout: - matches regex: ^test: prune squeeze \d+s .* + matches regex: ^test: prune squeeze \d+ seconds .* status: 1 @@ -4341,7 +4341,7 @@ assimilate: stderr: matches regex: > test - > 100800 s + > 100800 seconds > .* > .* > .* @@ -4358,7 +4358,7 @@ assimilate: > path: /{run_dir}/.local/share/assimilate/test.latest.nt > mtime: .* > age: .* - > max_age: 100800 s + > max_age: 100800 seconds > overdue: no > locked: no > updated: .*