Skip to content

Commit

Permalink
Improve adding custom measures
Browse files Browse the repository at this point in the history
When adding measures of a new unit, now can contorl description (optional)
  • Loading branch information
DrorHarari committed Nov 9, 2019
1 parent e772d5c commit cb9614a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
10 changes: 8 additions & 2 deletions cvt.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
# * It is useful for extending and experimenting with Cvt
# * If you have generally useful measurements to add - let me know on Cvt's github

# Adding a custom unit to existing measurement is possible by adding a section to this
# file in the following format:
# Adding a custom measure is possible by adding a section to this file in the
# following format:
#
# [measure/{measure name}]
# desc = {short-description of the measure}

# Adding a custom unit to existing measurement is possible by adding a section to
# this file in the following format:
#
# [unit/{measure name}/{unit name}]
# factor = {expression to multiple the main unit by to get this unit}
Expand Down
64 changes: 37 additions & 27 deletions cvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Cvt(kp.Plugin):

ITEM_LABEL_PREFIX = "Cvt: "
UNIT_SECTION_PREFIX = "unit/"
MEASURE_SECTION_PREFIX = "measure/"

CVTDEF_FILE = "cvtdefs.json"
CVTDEF_LOCALE_FILE = "cvtdefs-{}.json"
Expand Down Expand Up @@ -89,33 +90,42 @@ def read_setting_defs(self):
defs = { "measures": { } }
measures = defs["measures"]
for section in self.settings.sections():
if not section.lower().startswith(self.UNIT_SECTION_PREFIX):
continue

measure_name, unit_name = section[len(self.UNIT_SECTION_PREFIX):].strip().split("/", 1)
if not measure_name in measures:
measures[measure_name] = { "units": { }}
measure = measures[measure_name]

if not unit_name in measure["units"]:
measure["units"][unit_name] = { "aliases": [] }
unit = measure["units"][unit_name]

unit["factor"] = self.settings.get_stripped("factor", section=section, fallback="1.0")

offset = self.settings.get_stripped("offset", section=section, fallback=None)
if offset:
unit["offset"] = self.settings.get_stripped("offset", section=section, fallback=None)

inverse = self.settings.get_bool("inverse", section=section, fallback=None)
if inverse:
unit["inverse"] = self.settings.get_bool("inverse", section=section, fallback=None)

aliases = self.settings.get_stripped("aliases", section=section, fallback=None)
if aliases:
unit["aliases"] = unit["aliases"] + self.settings.get_stripped('aliases', section=section, fallback=None).split(",")

self.dbg(f"Added unit {unit_name} for measure {measure_name} as:\n{repr(unit)}")
if section.lower().startswith(self.UNIT_SECTION_PREFIX):
measure_name, unit_name = section[len(self.UNIT_SECTION_PREFIX):].strip().split("/", 1)
if not measure_name in measures:
measures[measure_name] = {
"desc": f"Convert units of area {measure_name}",
"units": { }
}
measure = measures[measure_name]

if not unit_name in measure["units"]:
measure["units"][unit_name] = { "aliases": [] }
unit = measure["units"][unit_name]

unit["factor"] = self.settings.get_stripped("factor", section=section, fallback="1.0")

offset = self.settings.get_stripped("offset", section=section, fallback=None)
if offset:
unit["offset"] = self.settings.get_stripped("offset", section=section, fallback=None)

inverse = self.settings.get_bool("inverse", section=section, fallback=None)
if inverse:
unit["inverse"] = self.settings.get_bool("inverse", section=section, fallback=None)

aliases = self.settings.get_stripped("aliases", section=section, fallback=None)
if aliases:
unit["aliases"] = unit["aliases"] + self.settings.get_stripped('aliases', section=section, fallback=None).split(",")

self.dbg(f"Added unit {unit_name} for measure {measure_name} as:\n{repr(unit)}")
elif section.lower().startswith(self.MEASURE_SECTION_PREFIX):
measure_name = section[len(self.MEASURE_SECTION_PREFIX):].strip()
if not measure_name in measures:
measures[measure_name] = { "units": { }}
measure = measures[measure_name]
measure["desc"] = self.settings.get_stripped("desc", section=section, fallback=f"Convert units of area {measure_name}")

self.dbg(f"Added configured measure {measure_name}")

return defs

Expand Down

0 comments on commit cb9614a

Please sign in to comment.