Skip to content

Commit

Permalink
Extended list command with subcommands and configurable file name.
Browse files Browse the repository at this point in the history
  • Loading branch information
reinhardt committed May 20, 2020
1 parent ab9124c commit 453a50e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
3 changes: 3 additions & 0 deletions octodon.cfg.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ vcs =
git
svn
source = plaintext
list-template-file = ~/octodon.md.tmpl
list-item-template = * {description}: {comments}
list-file = ~/octodon.md
project-mapping =
project-1 my-project-i
task-mapping =
Expand Down
70 changes: 50 additions & 20 deletions src/octodon/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def __init__(self, config, spent_on, new_session=False, *args):
with open(list_template_file, "r") as tmpl_file:
self.list_template = pystache.parse(tmpl_file.read())

self.list_file_name = None
if config.has_option("main", "list-file"):
self.list_file_name = os.path.expanduser(config.get("main", "list-file"))

self.redmine = None
if config.has_section("redmine"):
from octodon.redmine import Redmine
Expand Down Expand Up @@ -275,32 +279,58 @@ def do_total(self, *args):
bookings = self.time_log.get_timeinfo(date=self.spent_on)
print(format_spent_time(get_time_sum(bookings)))

def do_list(self, *args):
@contextmanager
def prepare_outfile(args):
if len(args) >= 1 and args[0]:
filename = os.path.expanduser(args[0])
try:
outfile = open(filename, "w")
except FileNotFoundError as fnfe:
print(
"Could not open {0}: {1}".format(filename, fnfe),
file=sys.stderr,
)
outfile = None
yield outfile
if outfile:
print("Printed to {}".format(filename), file=sys.stderr)
outfile.close()
def do_list(self, arg):
""" Print the current bookings or save them to a file.
Subcommands: show \tsave
"""
args = filter(None, arg.split(" "))
subcommand = next(args, "show")
filename = next(args, None)

if subcommand not in ["show", "save"]:
print("Unknown subcommand {}".format(subcommand))
return
if subcommand == "save":
if filename:
filename = os.path.expanduser(filename)
else:
yield sys.stdout
filename = self.list_file_name
if not filename:
print("Error: File name is required")
return
elif subcommand == "show":
if filename:
print("Subcommand '{}' takes no arguments".format(subcommand))
return
filename = "-"

with prepare_outfile(args) as outfile:
if outfile:
try:
with self.prepare_outfile(filename) as outfile:
if self.list_template:
print(self.get_templated_list(self.bookings), file=outfile)
else:
print(self.get_simple_list(self.bookings), file=outfile)
except FileNotFoundError as fnfe:
print(
"Could not open {0}: {1}".format(filename, fnfe), file=sys.stderr,
)
return

if filename != "-":
print("Printed to {}".format(filename), file=sys.stderr)

@contextmanager
def prepare_outfile(self, filename):
outfile = None
try:
if filename == "-":
outfile = sys.stdout
else:
outfile = open(filename, "w")
yield outfile
finally:
if filename != "-" and outfile is not None:
outfile.close()

def get_simple_list(self, bookings):
try:
Expand Down

0 comments on commit 453a50e

Please sign in to comment.