common: Basic config and bustime code

pull/2/head
Mike Lang 6 years ago
parent a361ab7a63
commit 048277b003

@ -14,5 +14,14 @@ but a brief overview of the components:
* Cutter interacts with a database to perform cutting jobs
* Sheet Sync syncs specifc database columns to a google doc which is the primary operator interface.
### Usage
All components are built as docker images.
Components which access the disk expect a shared directory mounted at `/mnt`.
#### Configuration
Configuration is built into the docker images, and set during build by specifying a
YAML file `config.yaml` in the repository root directory.
See the provided example file for documentation of options.

@ -1,3 +1,65 @@
"""A place for common utilities between wubloader components"""
import datetime
import dateutil.parser
import yaml
class Config(object):
def load(self,
run_start_time,
):
self.run_start_time = dateutil.parser.parse(run_start_time)
CONF = Config()
def load_config(path="/etc/wubloader.yaml"):
with open(path) as f:
CONF.load(**yaml.safe_load(f))
def dt_to_bustime(dt):
"""Convert a datetime to bus time. Bus time is seconds since the start of the run
as defined in the config file."""
return (dt - CONF.run_start_time).total_seconds()
def bustime_to_dt(bustime):
"""Convert from bus time to a datetime"""
return CONF.run_start_time + datetime.timedelta(seconds=bustime)
def format_bustime(bustime, round="millisecond"):
"""Convert bustime to a human-readable string (-)H:MM:SS.fff, with the
ending cut off depending on the value of round:
"millisecond": (default) Round to the nearest millisecond.
"second": Round down to the current second.
"minute": Round down to the current minute.
Examples:
0:00:00.000
1:23:00
110:50
159:59:59.999
-10:30:01.100
Note that a negative value only indicates the number of hours after the start
is negative, the number of minutes/seconds is simply time past the hour.
eg. the bustime "-1:20:00" indicates the run begins in 40 minutes, not 80 minutes.
"""
whole_secs, fractional = divmod(bustime, 1)
total_mins, secs = divmod(whole_secs, 60)
hours, mins = divmod(total_mins, 60)
parts = "{}:{:02d}:{:02d}:{:.3f}".format(hours, mins, secs, fractional).split(":")
if round == "millisecond":
pass
elif round == "second":
parts = parts[:-1]
elif round == "minute":
parts = parts[:-2]
else:
raise ValueError("Bad rounding value: {!r}".format(round))
return ":".join(parts)

@ -5,5 +5,7 @@ setup(
version = "0.0.0",
py_modules = ["common.py"],
install_requires = [
"dateutil",
"PyYAML<4.0.0",
],
)

@ -0,0 +1,6 @@
# The timestamp of the beginning of the run.
# This defines 00:00 bustime.
# Should be in format YYYY-MM-DD hh:mm:ss.
# Example: 2018-11-09 18:00:00
run_start_time: 1970-01-01 00:00:00
Loading…
Cancel
Save