From dcda20913b1a9ed99e8b49ed0f7762cdfd828cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pl=C3=ADchal?= Date: Mon, 30 Jan 2023 15:00:29 +0100 Subject: [PATCH] Move common constants to the top of `did.utils` Plus a couple of minor adjustments. Update the `isort` pre-commit hook. --- .pre-commit-config.yaml | 2 +- did/base.py | 9 +++------ did/cli.py | 17 +++++++++++++---- did/utils.py | 10 ++++++++-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bf606336..bb812552 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: - --max-line-length=88 - repo: https://github.com/PyCQA/isort - rev: "5.11.4" + rev: "5.12.0" hooks: - id: isort diff --git a/did/base.py b/did/base.py index 58437550..1d917789 100644 --- a/did/base.py +++ b/did/base.py @@ -17,7 +17,7 @@ from dateutil.relativedelta import relativedelta as delta from did import utils -from did.utils import log +from did.utils import DEFAULT_SEPARATOR, MAX_WIDTH, log # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Constants @@ -26,9 +26,6 @@ # Config file location CONFIG = os.path.expanduser("~/.did") -# Default maximum width -MAX_WIDTH = 79 - # Today's date TODAY = datetime.date.today() @@ -156,7 +153,7 @@ def separator(self): try: return self.parser.get("general", "separator") except (NoOptionError, NoSectionError): - return "~" + return DEFAULT_SEPARATOR @property def separator_width(self): @@ -164,7 +161,7 @@ def separator_width(self): try: return int(self.parser.get("general", "separator_width")) except (NoOptionError, NoSectionError): - return 79 + return MAX_WIDTH def sections(self, kind=None): """ Return all sections (optionally of given kind only) """ diff --git a/did/cli.py b/did/cli.py index 469e6d20..1d585693 100644 --- a/did/cli.py +++ b/did/cli.py @@ -53,7 +53,7 @@ def __init__(self, arguments=None): try: width = did.base.Config().width except did.base.ConfigFileError: - width = did.base.MAX_WIDTH + width = did.utils.MAX_WIDTH # Time & user selection group = self.parser.add_argument_group("Select") @@ -210,7 +210,10 @@ def main(arguments=None): print(header) team_stats = UserStats(options=options) if options.merge: - utils.header("Total Report", did.base.Config().separator_width, did.base.Config().separator) + utils.header( + "Total Report", + separator=did.base.Config().separator, + separator_width=did.base.Config().separator_width) utils.item("Users: {0}".format(len(users)), options=options) # Check individual user stats @@ -218,7 +221,10 @@ def main(arguments=None): if options.merge: utils.item(user, 1, options=options) else: - utils.header(user, did.base.Config().separator_width, did.base.Config().separator) + utils.header( + user, + separator=did.base.Config().separator, + separator_width=did.base.Config().separator_width) user_stats = UserStats(user=user, options=options) user_stats.check() team_stats.merge(user_stats) @@ -227,7 +233,10 @@ def main(arguments=None): # Display merged team report if options.merge or options.total: if options.total: - utils.header("Total Report", did.base.Config().separator_width, did.base.Config().separator) + utils.header( + "Total Report", + separator=did.base.Config().separator, + separator_width=did.base.Config().separator_width) team_stats.show() # Return all gathered stats objects diff --git a/did/utils.py b/did/utils.py index 13dfa026..d166795f 100644 --- a/did/utils.py +++ b/did/utils.py @@ -15,6 +15,12 @@ # Constants # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Default maximum width +MAX_WIDTH = 79 + +# Default separator character +DEFAULT_SEPARATOR = "~" + # Coloring COLOR_ON = 1 COLOR_OFF = 0 @@ -157,12 +163,12 @@ def load_components(*paths, **kwargs): return num_loaded -def header(text, separator_width=79, separator="~"): +def header(text, separator=DEFAULT_SEPARATOR, separator_width=MAX_WIDTH): """ Show text as a header. """ print("\n{0}\n {1}\n{0}".format(separator_width * separator, text)) -def shorted(text, width=79): +def shorted(text, width=MAX_WIDTH): """ Shorten text, make sure it's not cut in the middle of a word """ if len(text) <= width: return text