Skip to content

Commit

Permalink
Move common constants to the top of did.utils
Browse files Browse the repository at this point in the history
Plus a couple of minor adjustments.
Update the `isort` pre-commit hook.
  • Loading branch information
psss committed Jan 30, 2023
1 parent ae92aba commit dcda209
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions did/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -156,15 +153,15 @@ def separator(self):
try:
return self.parser.get("general", "separator")
except (NoOptionError, NoSectionError):
return "~"
return DEFAULT_SEPARATOR

@property
def separator_width(self):
""" Number of separator characters to use for the report """
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) """
Expand Down
17 changes: 13 additions & 4 deletions did/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -210,15 +210,21 @@ 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
for user in users:
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)
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions did/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
# Constants
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Default maximum width
MAX_WIDTH = 79

# Default separator character
DEFAULT_SEPARATOR = "~"

# Coloring
COLOR_ON = 1
COLOR_OFF = 0
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dcda209

Please sign in to comment.