Changeset 58:b24ccde3e229 in stamper


Ignore:
Timestamp:
Nov 10, 2014, 12:33:51 PM (10 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Use a config file for the setup of certain parameters, instead of
hardcoded global variables (ugh!)

Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • bin/stamp

    r35 r58  
    33import sys
    44from datetime import datetime
    5 from stamper import DATETIME_FORMAT, Stamper
     5from stamper import Stamper
    66
    77
     
    2424    if len(sys.argv) == 1:
    2525        # stamp!
    26         s.stamp(datetime.today().strftime(DATETIME_FORMAT),
     26        s.stamp(datetime.today().strftime(s.datetime_format),
    2727                None, None, None)
    2828
     
    3535        customer = sys.argv[1]
    3636        description = ' '.join(sys.argv[2:])
    37         s.stamp(current_start, datetime.today().strftime(DATETIME_FORMAT),
     37        s.stamp(current_start, datetime.today().strftime(s.datetime_format),
    3838                customer, description)
    3939
  • stamper/__init__.py

    r27 r58  
    1 from stamper import DATETIME_FORMAT
    21from stamper import Stamper
  • stamper/stamper.py

    r56 r58  
    1010
    1111from .http import HTTPClient
    12 
    13 
    14 STAMPS_FILE = os.path.expanduser('~/.workstamps.json')
    15 CHARTS_DIR = os.path.expanduser('~/.workstamps-charts')
    16 DATE_FORMAT = '%Y-%m-%d'
    17 TIME_FORMAT = '%H:%M:%S'
    18 DATETIME_FORMAT = '%Y-%m-%d %H:%M'
    19 HOURS_DAY = 8
    20 SECS_DAY = HOURS_DAY * 60 * 60
    21 REMOTE_BASE_URL = 'https://localhost'
    22 # IMPORTANT: keep the trailing / on these _URL variables
    23 REMOTE_LOGIN_URL = '/stamper/login/'
    24 REMOTE_PUSH_URL = '/stamper/listen/'
    25 REMOTE_USER = None
    26 REMOTE_PASSWORD = None
     12from .config import Config
    2713
    2814
    2915class Stamper(object):
    3016
    31     def __init__(self, stamps_file=STAMPS_FILE, charts_dir=CHARTS_DIR):
    32         self.stamps_file = stamps_file
    33         self.charts_dir = charts_dir
     17    def __init__(self, config_file=None):
     18        self.config = Config(config_file)
     19        self.stamps_file = os.path.expanduser(
     20            self.config.get('stamps', 'path'))
     21        self.charts_dir = os.path.expanduser(
     22            self.config.get('charts', 'path'))
     23        self.date_format = self.config.get('stamps', 'date_format')
     24        self.time_format = self.config.get('stamps', 'time_format')
     25        self.datetime_format = self.config.get('stamps', 'datetime_format')
     26        self.hours_day = int(self.config.get('sum', 'hours_day'))
     27        self.collector = self.config.get('collector')
    3428        self.ensure_stamps_file()
    3529        self.ensure_charts_dir()
     
    9690
    9791    def worktime(self, start, end):
    98         worktime = (datetime.strptime(end, DATETIME_FORMAT) -
    99                     datetime.strptime(start, DATETIME_FORMAT))
     92        worktime = (datetime.strptime(end, self.datetime_format) -
     93                    datetime.strptime(start, self.datetime_format))
    10094        return worktime.seconds
    10195
     
    126120        if '--' in stamp_filter:
    127121            filter_from, filter_to = stamp_filter.split('--')
    128             filter_from = datetime.strptime(filter_from, DATE_FORMAT)
    129             filter_to = datetime.strptime(filter_to, DATE_FORMAT)
     122            filter_from = datetime.strptime(filter_from, self.date_format)
     123            filter_to = datetime.strptime(filter_to, self.date_format)
    130124
    131125        elif stamp_filter.startswith('*'):
    132             filter_to = datetime.strptime(stamp_filter, '*'+DATE_FORMAT)
     126            filter_to = datetime.strptime(stamp_filter, '*'+self.date_format)
    133127            filter_to = filter_to.replace(hour=0, minute=0, second=0)
    134128
    135129        elif stamp_filter.endswith('*'):
    136             filter_from = datetime.strptime(stamp_filter, DATE_FORMAT+'*')
     130            filter_from = datetime.strptime(stamp_filter, self.date_format+'*')
    137131            filter_from = filter_from.replace(hour=0, minute=0, second=0)
    138132
     
    167161            # maybe they are giving us a fixed date
    168162            try:
    169                 filter_from = datetime.strptime(stamp_filter, DATE_FORMAT)
     163                filter_from = datetime.strptime(stamp_filter, self.date_format)
    170164            except:
    171165                # nothing to be used as a filter, go on, printing a warning
     
    192186            # customer will be None for "start" stamps, having no end time
    193187            if customer:
    194                 start = datetime.strptime(stamp['start'], DATETIME_FORMAT)
    195                 end = datetime.strptime(stamp['end'], DATETIME_FORMAT)
     188                start = datetime.strptime(stamp['start'], self.datetime_format)
     189                end = datetime.strptime(stamp['end'], self.datetime_format)
    196190                if filter_from and start < filter_from:
    197191                    # if there is a filter setting a starting date for the
     
    219213                    # it
    220214                    continue
    221                 start = datetime.strptime(stamp['start'], DATETIME_FORMAT)
     215                start = datetime.strptime(stamp['start'], self.datetime_format)
    222216                start_day = start.strftime('%Y-%m-%d')
    223                 end = datetime.strptime(stamp['end'], DATETIME_FORMAT)
     217                end = datetime.strptime(stamp['end'], self.datetime_format)
    224218                if filter_from and start < filter_from:
    225219                    # if there is a filter setting a starting date for the
     
    255249        filter_from, filter_to = self.validate_filter(stamp_filter)
    256250        for stamp in self.stamps:
    257             start = datetime.strptime(stamp['start'], DATETIME_FORMAT)
     251            start = datetime.strptime(stamp['start'], self.datetime_format)
    258252            start_day = start.strftime('%Y-%m-%d')
    259253            if filter_from and start < filter_from:
     
    284278        filter_from, filter_to = self.validate_filter(stamp_filter)
    285279        chart = pygal.Bar(title='Work hours per day',
    286                           range=(0, HOURS_DAY),
     280                          range=(0, self.hours_day),
    287281                          x_title='Days',
    288282                          y_title='Work hours',
     
    380374                totalSecs, sec = divmod(seconds, 60)
    381375                hr, min = divmod(totalSecs, 60)
    382                 totalDays, remaining = divmod(seconds, SECS_DAY)
     376                totalDays, remaining = divmod(seconds,
     377                                              (self.hours_day * 60 * 60))
    383378                remainingMin, remainingSec = divmod(remaining, (60))
    384379                remainingHr, remainingMin = divmod(remainingMin, (60))
    385380                print('----- %d:%02d:%02d -----' % (hr, min, sec))
    386381                print('--- %d days, remaining: %d:%02d (%d hours/day) ---' % (
    387                     totalDays, remainingHr, remainingMin, HOURS_DAY
     382                    totalDays, remainingHr, remainingMin, self.hours_day
    388383                ))
    389384
     
    444439                if customer and customer != stamp['customer']:
    445440                    continue
    446                 start = datetime.strptime(stamp['start'], DATETIME_FORMAT)
     441                start = datetime.strptime(stamp['start'], self.datetime_format)
    447442                start_day = start.strftime('%Y-%m-%d')
    448                 end = datetime.strptime(stamp['end'], DATETIME_FORMAT)
     443                end = datetime.strptime(stamp['end'], self.datetime_format)
    449444                if filter_from and start < filter_from:
    450445                    # if there is a filter setting a starting date for the
     
    458453
    459454        stamps = json.dumps(stamps, indent=4)
    460         http_client = HTTPClient(REMOTE_BASE_URL)
    461         http_client.post(REMOTE_LOGIN_URL, {'username': REMOTE_USER,
    462                                             'password': REMOTE_PASSWORD})
    463         http_client.post(REMOTE_PUSH_URL, {'stamps': stamps})
     455        http_client = HTTPClient(self.collector['base_url'])
     456        http_client.post(self.collector['login_path'],
     457                         {'username': self.collector['user'],
     458                          'password': self.collector['password']})
     459        http_client.post(self.collector['listen_path'], {'stamps': stamps})
Note: See TracChangeset for help on using the changeset viewer.