Skip to content

Commit

Permalink
change default units from s and d to seconds and days
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Jan 10, 2025
1 parent dab84ba commit d669d3c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
6 changes: 4 additions & 2 deletions assimilate/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down Expand Up @@ -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())}."
)
Expand Down
4 changes: 2 additions & 2 deletions assimilate/overdue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
44 changes: 23 additions & 21 deletions assimilate/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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'):
Expand All @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions tests/assimilate.nt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -4341,7 +4341,7 @@ assimilate:
stderr:
matches regex:
> test
> 100800 s
> 100800 seconds
> .*
> .*
> .*
Expand All @@ -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: .*
Expand Down

0 comments on commit d669d3c

Please sign in to comment.