Skip to content

Commit

Permalink
Github project support, plus small tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
reinhardt committed Mar 9, 2022
1 parent 0058d60 commit 1c96d66
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 25 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def read(*rnames):
install_requires=[
"jira",
"pyactiveresource",
"github3api",
"pystache",
"python-harvest-redux",
"setuptools",
Expand Down
86 changes: 64 additions & 22 deletions src/octodon/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import
# from __future__ import absolute_import
import os
import argparse
import pystache
Expand Down Expand Up @@ -33,23 +33,10 @@ def __init__(self, config, spent_on, new_session=False, *args):
ticket_patterns.append(self.redmine_class.ticket_pattern)
if self.jira_class:
ticket_patterns.append(self.jira_class.ticket_pattern)
if self.github_class:
ticket_patterns.append(self.github_class.ticket_pattern)

if config.get("main", "source") == "hamster":
from octodon.hamster import HamsterTimeLog

self.time_log = HamsterTimeLog(ticket_patterns=ticket_patterns)
elif config.get("main", "source") == "orgmode":
from octodon.orgmode import OrgModeTimeLog

filename = config.get("orgmode", "filename")
self.time_log = OrgModeTimeLog(filename, ticket_patterns=ticket_patterns)
elif config.get("main", "source") == "plaintext":
from octodon.clockwork import ClockWorkTimeLog

log_path = config.get("plaintext", "log_path")
self.time_log = ClockWorkTimeLog(
ticket_patterns=ticket_patterns, log_path=log_path
)
self.time_log = get_time_log(config, ticket_patterns)

self.editor = config.get("main", "editor")

Expand Down Expand Up @@ -139,6 +126,15 @@ def harvest_class(self):
else:
return None

@property
def github_class(self):
if self.config.has_section("github"):
from octodon.github import Github

return Github
else:
return None

@property
def jira(self):
jira = getattr(self, "_jira", None)
Expand All @@ -156,6 +152,21 @@ def jira(self):
)
return jira

@property
def github(self):
github = getattr(self, "_github", None)
if github is None and self.github_class is not None:
if self.config.has_option("github", "token_command"):
cmd = self.config.get("github", "token_command")
token = subprocess.check_output(cmd.split(" ")).strip().decode("utf-8")
self.config.set("github", "token", token)
self._github = github = self.github_class(
self.config.get("github", "token"),
self.config.get("github", "organization"),
int(self.config.get("github", "project_num")),
)
return github

@property
def redmine(self):
redmine = getattr(self, "_redmine", None)
Expand Down Expand Up @@ -217,7 +228,10 @@ def tracking(self):
tracking = getattr(self, "_tracking", None)
if tracking is None:
self._tracking = tracking = Tracking(
redmine=self.redmine, jira=self.jira, harvest=self.harvest,
redmine=self.redmine,
jira=self.jira,
harvest=self.harvest,
github=self.github,
)
return tracking

Expand Down Expand Up @@ -317,7 +331,7 @@ def do_total(self, *args):
print(format_spent_time(get_time_sum(bookings)))

def do_list(self, arg):
""" Print the current bookings or save them to a file.
"""Print the current bookings or save them to a file.
Subcommands: show \tsave
"""
args = filter(None, arg.split(" "))
Expand Down Expand Up @@ -349,7 +363,8 @@ def do_list(self, arg):
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,
"Could not open {0}: {1}".format(filename, fnfe),
file=sys.stderr,
)
return

Expand Down Expand Up @@ -436,6 +451,10 @@ def do_book(self, *args):
self.do_jira()
self.do_harvest()

def do_flush(self, *args):
""" Executes summary, book, list save in this order """
self.cmdqueue.extend(["summary", "book", "list save"])

def do_fetch(self, *args):
""" EXPERIMENTAL. Freshly fetch bookings from source. """
old_bookings = self.bookings[:]
Expand Down Expand Up @@ -483,6 +502,25 @@ def get_config(cfgfile=None):
return config


def get_time_log(config, ticket_patterns=[]):
time_log = None
if config.get("main", "source") == "hamster":
from octodon.hamster import HamsterTimeLog

time_log = HamsterTimeLog(ticket_patterns=ticket_patterns)
elif config.get("main", "source") == "orgmode":
from octodon.orgmode import OrgModeTimeLog

filename = config.get("orgmode", "filename")
time_log = OrgModeTimeLog(filename, ticket_patterns=ticket_patterns)
elif config.get("main", "source") == "plaintext":
from octodon.clockwork import ClockWorkTimeLog

log_path = config.get("plaintext", "log_path")
time_log = ClockWorkTimeLog(ticket_patterns=ticket_patterns, log_path=log_path)
return time_log


def main():
parser = argparse.ArgumentParser(
description="Extract time tracking data "
Expand Down Expand Up @@ -539,11 +577,15 @@ def main():
else:
raise Exception("unrecognized date format: {0}".format(args.date))

if args.command and args.command != "shell":
if args.command == "total":
time_log = get_time_log(config)
bookings = time_log.get_timeinfo(date=spent_on)
print(format_spent_time(get_time_sum(bookings)))
elif args.command and args.command != "shell":
octodon = Octodon(config, spent_on, new_session=True)
octodon.onecmd(args.command)
else:
octodon = Octodon(config, spent_on, new_session=args.new_session)
if args.command != "shell":
octodon.cmdqueue.extend(["edit", "summary", "book", "list save"])
octodon.cmdqueue.extend(["edit"])
octodon.cmdloop()
13 changes: 10 additions & 3 deletions src/octodon/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@

class Tracking(object):
def __init__(
self, redmine=None, jira=None, harvest=None, project_history_file=None,
self,
redmine=None,
jira=None,
harvest=None,
github=None,
project_history_file=None,
):
self.redmine = redmine
self.jira = jira
self.harvest = harvest
self.trackers = list(filter(None, [self.jira, self.redmine]))
self.github = github
self.trackers = list(filter(None, [self.jira, self.redmine, self.github]))
self._projects = []
self._issue_to_project = {}
if project_history_file is None:
Expand All @@ -39,7 +45,8 @@ def get_issue_title(self, issue_id):
)
except (ConnectionError, socket.error):
print(
"Could not find issue " + str(issue_id), file=sys.stderr,
"Could not find issue " + str(issue_id),
file=sys.stderr,
)
if issue is None:
continue
Expand Down

0 comments on commit 1c96d66

Please sign in to comment.