Changeset 58:b24ccde3e229 in stamper
- Timestamp:
- Nov 10, 2014, 12:33:51 PM (10 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
bin/stamp
r35 r58 3 3 import sys 4 4 from datetime import datetime 5 from stamper import DATETIME_FORMAT,Stamper5 from stamper import Stamper 6 6 7 7 … … 24 24 if len(sys.argv) == 1: 25 25 # stamp! 26 s.stamp(datetime.today().strftime( DATETIME_FORMAT),26 s.stamp(datetime.today().strftime(s.datetime_format), 27 27 None, None, None) 28 28 … … 35 35 customer = sys.argv[1] 36 36 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), 38 38 customer, description) 39 39 -
stamper/__init__.py
r27 r58 1 from stamper import DATETIME_FORMAT2 1 from stamper import Stamper -
stamper/stamper.py
r56 r58 10 10 11 11 from .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 12 from .config import Config 27 13 28 14 29 15 class Stamper(object): 30 16 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') 34 28 self.ensure_stamps_file() 35 29 self.ensure_charts_dir() … … 96 90 97 91 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)) 100 94 return worktime.seconds 101 95 … … 126 120 if '--' in stamp_filter: 127 121 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) 130 124 131 125 elif stamp_filter.startswith('*'): 132 filter_to = datetime.strptime(stamp_filter, '*'+ DATE_FORMAT)126 filter_to = datetime.strptime(stamp_filter, '*'+self.date_format) 133 127 filter_to = filter_to.replace(hour=0, minute=0, second=0) 134 128 135 129 elif stamp_filter.endswith('*'): 136 filter_from = datetime.strptime(stamp_filter, DATE_FORMAT+'*')130 filter_from = datetime.strptime(stamp_filter, self.date_format+'*') 137 131 filter_from = filter_from.replace(hour=0, minute=0, second=0) 138 132 … … 167 161 # maybe they are giving us a fixed date 168 162 try: 169 filter_from = datetime.strptime(stamp_filter, DATE_FORMAT)163 filter_from = datetime.strptime(stamp_filter, self.date_format) 170 164 except: 171 165 # nothing to be used as a filter, go on, printing a warning … … 192 186 # customer will be None for "start" stamps, having no end time 193 187 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) 196 190 if filter_from and start < filter_from: 197 191 # if there is a filter setting a starting date for the … … 219 213 # it 220 214 continue 221 start = datetime.strptime(stamp['start'], DATETIME_FORMAT)215 start = datetime.strptime(stamp['start'], self.datetime_format) 222 216 start_day = start.strftime('%Y-%m-%d') 223 end = datetime.strptime(stamp['end'], DATETIME_FORMAT)217 end = datetime.strptime(stamp['end'], self.datetime_format) 224 218 if filter_from and start < filter_from: 225 219 # if there is a filter setting a starting date for the … … 255 249 filter_from, filter_to = self.validate_filter(stamp_filter) 256 250 for stamp in self.stamps: 257 start = datetime.strptime(stamp['start'], DATETIME_FORMAT)251 start = datetime.strptime(stamp['start'], self.datetime_format) 258 252 start_day = start.strftime('%Y-%m-%d') 259 253 if filter_from and start < filter_from: … … 284 278 filter_from, filter_to = self.validate_filter(stamp_filter) 285 279 chart = pygal.Bar(title='Work hours per day', 286 range=(0, HOURS_DAY),280 range=(0, self.hours_day), 287 281 x_title='Days', 288 282 y_title='Work hours', … … 380 374 totalSecs, sec = divmod(seconds, 60) 381 375 hr, min = divmod(totalSecs, 60) 382 totalDays, remaining = divmod(seconds, SECS_DAY) 376 totalDays, remaining = divmod(seconds, 377 (self.hours_day * 60 * 60)) 383 378 remainingMin, remainingSec = divmod(remaining, (60)) 384 379 remainingHr, remainingMin = divmod(remainingMin, (60)) 385 380 print('----- %d:%02d:%02d -----' % (hr, min, sec)) 386 381 print('--- %d days, remaining: %d:%02d (%d hours/day) ---' % ( 387 totalDays, remainingHr, remainingMin, HOURS_DAY382 totalDays, remainingHr, remainingMin, self.hours_day 388 383 )) 389 384 … … 444 439 if customer and customer != stamp['customer']: 445 440 continue 446 start = datetime.strptime(stamp['start'], DATETIME_FORMAT)441 start = datetime.strptime(stamp['start'], self.datetime_format) 447 442 start_day = start.strftime('%Y-%m-%d') 448 end = datetime.strptime(stamp['end'], DATETIME_FORMAT)443 end = datetime.strptime(stamp['end'], self.datetime_format) 449 444 if filter_from and start < filter_from: 450 445 # if there is a filter setting a starting date for the … … 458 453 459 454 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.