diff options
author | Alasdair Colley <ac@brede.(none)> | 2011-11-01 15:19:11 +0000 |
---|---|---|
committer | Alasdair Colley <ac@brede.(none)> | 2011-11-01 15:19:11 +0000 |
commit | f1975e80eec071e565cb43d2602b1fca2c3aae7c (patch) | |
tree | 2949b6c5d93a98050dcbdf51b876549348493dd5 | |
parent | de00dd55cb358388f35a6b950c95e68f48fa8192 (diff) |
can now add new users to config
-rwxr-xr-x | perc | 121 | ||||
-rw-r--r-- | perc.cfg | 93 |
2 files changed, 114 insertions, 100 deletions
@@ -31,6 +31,8 @@ # - Admins can create and remove custom events # - Allows creation and removal of custom temporal events (stored in perc.cfg) # - Maintains a list of admins allowed to perform admin actions +# 3.4: +# - Ability to add new users added import copy import decimal @@ -43,7 +45,7 @@ import sys from datetime import datetime, date, timedelta, time PROG = "perc" -VERSION = "3.3" +VERSION = "3.4" CONFIG_FILE = "perc.cfg" @@ -52,9 +54,12 @@ NOW = datetime.now() MODES = ["user", "admin"] USER = None +DEFAULT_USER = None CONFIG = {} CONFIG_IS_DIRTY = False +EVENT_DATETIME_FORMAT = "%d-%m-%Y %H:%M" + MONDAY = 0 TUESDAY = 1 WEDNESDAY = 2 @@ -75,16 +80,6 @@ OUTPUT_TYPES = ["perc", "bar", "ram", "pint"] VERB_FORMS = ["is", "is not", "should be", "could be", "would be", "ought to be", "might be", "may become"] -main_parser = optparse.OptionParser("{0} <finish> [options] | {0} <start> <finish> [options]".format(PROG)) -main_parser.add_option("-v", "--version", action="store_true", dest="version", help="Display the version of perc") -main_parser.add_option("-s", "--start", action="store", dest="start", help="The start time of your particular day") -main_parser.add_option("-f", "--finish", action="store", dest="finish", help="The finish time of your particular day") -main_parser.add_option("-n", "--nick", action="store", dest="nick", help="Nickname of someone (e.g. fuckwad, l_bratch, bikeman or something)") -main_parser.add_option("-o", "--output", action="store", type="choice", choices=OUTPUT_TYPES, dest="output", help="Output display type. Can be a simple percentage or different types of chart. Options are %s" % ", ".join(OUTPUT_TYPES)) -main_parser.add_option("-m", "--mobile", action="store_true", default=False, dest="mobile", help="Indicate this should be displayed appropriately for a mobile device (i.e. smaller character display width)") -main_parser.add_option("-a", "--add-event", action="store", dest="add_event") -main_parser.add_option("-r", "--remove-event", action="store", dest="remove_event") - def clamp(value, min_value, max_value): if value < min_value: return min_value @@ -175,14 +170,7 @@ def parseTime(timestr): except ValueError: pass - for key, value in CONFIG["FORMAT"]["date"].items(): - try: - t = datetime.strptime(timestr, value) - return NOW.replace(year=t.year, month=t.month, day=t.day, hour=t.hour, minute=t.minute, second=t.second, microsecond=t.microsecond), value - except ValueError: - pass - - print "Not a correctly formatted time/date: '%s'" % timestr + print "Not a correctly formatted time: '%s'" % timestr sys.exit(1) def isImportantTemporalEvent(event): @@ -251,7 +239,7 @@ def parseTemporalEvent(event, user): start_formatted = "Halloween %d" % start.year finish_formatted = "Halloween %d" % finish.year elif event == "lunch": - if NOW < lunch_start: + if NOW < user.lunch.start: start = work_start finish = user.lunch.start @@ -262,10 +250,10 @@ def parseTemporalEvent(event, user): finish_formatted = finish.strftime(user.format) - start_formatted = start.strftime(format) + start_formatted = start.strftime(user.format) elif event in CONFIG["CUSTOM_EVENTS"]: - start = datetime.strptime(CONFIG["CUSTOM_EVENTS"][event]["start"], "%Y-%m-%d %H:%M") - finish = datetime.strptime(CONFIG["CUSTOM_EVENTS"][event]["finish"], "%Y-%m-%d %H:%M") + start = datetime.strptime(CONFIG["CUSTOM_EVENTS"][event]["start"], EVENT_DATETIME_FORMAT) + finish = datetime.strptime(CONFIG["CUSTOM_EVENTS"][event]["finish"], EVENT_DATETIME_FORMAT) format = CONFIG["CUSTOM_EVENTS"][event]["format"] start_formatted = start.strftime(format).format(name=event) @@ -323,8 +311,8 @@ def addEvent(name, start, finish, format="{name} %Y"): print "Event added: '%s'" % name event = { - "start": start.strftime("%Y-%m-%d %H:%M"), - "finish": finish.strftime("%Y-%m-%d %H:%M"), + "start": start.strftime(EVENT_DATETIME_FORMAT), + "finish": finish.strftime(EVENT_DATETIME_FORMAT), "format": format } @@ -421,25 +409,36 @@ class Output(object): class TimeRange(object): - def __init__(self, start, finish, formatter): + def __init__(self, start, finish, user): self._start = start self._finish = finish - self.formatter = formatter + self.user = user def __datetimeGetter(self, name): return datetime.combine(NOW.date(), datetime.strptime(getattr(self, name), "%H:%M").time()) + def __datetimeSetter(self, name, value): + self._data[name] = value + @property def start(self): return self.__datetimeGetter("_start") + @start.setter + def start(self, value): + self.__datetimeSetter("_start", value) + @property def finish(self): return self.__datetimeGetter("_finish") + @finish.setter + def finish(self, value): + self.__datetimeSetter("_finish", value) + @property def format(self): - return self.formatter.format + return self.user.format def toDict(self): return { "start": self._start, "finish": self._finish } @@ -450,11 +449,12 @@ class User(object): Name = property(fget=lambda self: self._name) - def __init__(self, name, data=None): - self._name = name - if data: - self.data = self.__parseData(data) - else: + def __init__(self, name): + self._name = name.lower() + + try: + self.data = self.__parseData(CONFIG["DEFAULTS"][name]) + except KeyError: self.data = {} def __parseData(self, data): @@ -484,22 +484,25 @@ class User(object): print "Cannot edit default user" sys.exit(1) - CONFIG["DEFAULTS"][self.Name] = self.__convertToDict(self.data) + CONFIG["DEFAULTS"][self.Name] = self.__convertToDict() + print "User updated in config: '%s'" % self.Name + + global CONFIG_IS_DIRTY CONFIG_IS_DIRTY = True def __datetimeGetter(self, name): try: return datetime.combine(NOW.date(), datetime.strptime(self.data[name], "%H:%M").time()) except KeyError: - return CONFIG["DEFAULTS"]["default"][name] + return getattr(DEFAULT_USER, name) def __defaultSetter(self, name, value): - self._data[name] = value + self.data[name] = value def __defaultDeleter(self, name): try: - del self._data[name] + del self.data[name] except KeyError: pass @@ -507,7 +510,7 @@ class User(object): try: return self.data[name] except KeyError: - return CONFIG["DEFAULTS"]["default"][name] + return getattr(DEFAULT_USER, name) def __getattr__(self, name): if name in User.PROPERTIES: @@ -545,7 +548,7 @@ def admin(args): parser.add_option("-r", "--remove-event", action="store", dest="remove_event", help="Remove a named event from %s" % PROG) parser.add_option("-s", "--start", action="store", dest="start", help="Used in conjunction with -a/--add-event option. The start date/time of the event.") parser.add_option("-f", "--finish", action="store", dest="finish", help="Used in conjunction with -a/--add-event option. The finish date/time of the event.") - parser.add_option("-p", "--format", action="store", default="{name} %Y", dest="format", help="Used in conjunction with -a/--add-event option. The format of the printed event date/time, uses python strftime formatting and accepts {name} keyword to display name of event.") + parser.add_option("-p", "--format", action="store", default="", dest="format", help="Used in conjunction with -a/--add-event option. The format of the printed event date/time, uses python strftime formatting and accepts {name} keyword to display name of event.") parser.add_option("-o", "--output", action="store", type="choice", choices=OUTPUT_TYPES, dest="output", help="The output display type for the user. Options are %s" % ", ".join(OUTPUT_TYPES)) (options, args) = parser.parse_args(args) @@ -555,7 +558,36 @@ def admin(args): sys.exit(0) if options.user: - pass + user = User(options.user) + + if options.lunch: + lunch_start = "13:00" + lunch_finish = "14:00" + + if options.start: + lunch_start, f = parseTime(options.start) + lunch_start = lunch_start.strftime("%H:%M") + if options.finish: + lunch_finish, f = parseTime(options.finish) + lunch_finish = lunch_finish.strftime("%H:%M") + + lunch = TimeRange(lunch_start, lunch_finish, user) + user.lunch = lunch + else: + if options.start: + start, f = parseTime(options.start) + user.start = start.strftime("%H:%M") + + if options.finish: + finish, f = parseTime(options.finish) + user.finish = finish.strftime("%H:%M") + + if options.format: + user.format = options.format + if options.output: + user.output = options.output + + user.commit() else: if options.add_event: if not options.start or not options.finish: @@ -565,7 +597,7 @@ def admin(args): name = options.add_event start, f = parseDatetime(options.start) finish, f = parseDatetime(options.finish) - format = options.format + format = options.format if options.format else "{name} %Y" addEvent(name, start, finish, format) @@ -601,7 +633,7 @@ def user(args): user = USER if options.nick: nick = options.nick.lower() - user = User(nick, CONFIG["DEFAULTS"].get(nick)) + user = User(nick) start, finish, ratio, output_type = parseUserArgs(args, user) @@ -616,12 +648,15 @@ def main(): if "perc" in args: args.remove("perc") + global DEFAULT_USER + DEFAULT_USER = User("default") + # Set the user variable to the user who ran the script global USER USER = User("default") if args: nick = args.pop(0).lower() - USER = User(nick, CONFIG["DEFAULTS"].get(nick)) + USER = User(nick) # Obtain the mode if set, otherwise default to 'user' mode = "user" @@ -1,14 +1,14 @@ { "FORMAT": { "date": { - "slashes": "%Y/%m/%d", - "hyphens": "%Y-%m-%d" + "slashes": "%d/%m/%Y", + "hyphens": "%d-%m-%Y" }, "time": { + "military": "%H%M", "12hour": "%I:%M%p", - "civilian": "%H:%M", "12houronly": "%I%p", - "military": "%H%M" + "civilian": "%H:%M" } }, "RATIO_SIGFIGS": 3, @@ -32,143 +32,122 @@ "DEFAULTS": { "daveg": { "start": "07:30", - "format": "%H:%M", "finish": "18:00", + "output": "bar", "lunch": { "start": "12:30", "finish": "13:00" }, - "output": "bar" + "format": "%H:%M" }, "bikeman": { "start": "09:00", - "format": "%I%p", "finish": "17:00", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%I%p" }, "wjoe": { "start": "09:00", - "format": "%I%p", "finish": "17:00", + "output": "bar", "lunch": { "start": "12:00", "finish": "13:00" }, - "output": "bar" + "format": "%I%p" }, "default": { "start": "09:00", - "format": "%H:%M", "finish": "17:00", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%H:%M" }, - "l_bratch": { - "start": "08:30", - "format": "%H:%M", - "finish": "17:00", + "jagw": { + "start": "09:00", + "finish": "18:00", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%I%p" }, "bottlecap": { "start": "09:00", - "format": "%H:%M", "finish": "17:30", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%H:%M" }, "carpnet": { "start": "10:00", - "format": "%H:%M", "finish": "19:00", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%H:%M" }, "miniwork": { "start": "09:00", - "format": "%H:%M", "finish": "17:30", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%H:%M" }, "otherlw": { "start": "10:00", - "format": "%H:%M", "finish": "19:00", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%H:%M" }, - "jagw": { - "start": "09:00", - "format": "%I%p", - "finish": "18:00", + "l_bratch": { + "start": "08:30", + "finish": "17:00", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%H:%M" }, "sbeans": { "start": "09:00", - "format": "%H:%M", "finish": "17:30", + "output": "bar", "lunch": { "start": "13:00", "finish": "14:00" }, - "output": "bar" + "format": "%H:%M" } }, "OUTPUT": { "PERCENTAGE": "{ratio:.2%} {finish} ({start} start)", "BAR": { - "character": "-", + "length_mobile": 30, "length": 40, - "length_mobile": 30 + "character": "-" } }, - "CUSTOM_EVENTS": { - "Test": { - "start": "1900-01-01 17:00", - "finish": "1900-01-01 18:00", - "format": "{name} %Y" - }, - "tg": { - "start": "2010-04-24 10:00", - "finish": "2011-04-04 12:00", - "format": "{name} %Y" - }, - "intense": { - "start": "2011-09-04 18:00", - "finish": "2011-11-11 20:00", - "format": "{name} (%y-%m-%d %H:%M)" - }, - "Skyrim": { - "start": "2011-11-01 00:00", - "finish": "2011-11-11 00:00", - "format": "{name} %Y" - } - } -}
\ No newline at end of file + "CUSTOM_EVENTS": {} +} |