extensions: Make extensions conform with PEP8

This commit is contained in:
Thomas Lauf 2017-03-01 20:42:32 +01:00
parent 97ab53afeb
commit 310f48973e
4 changed files with 114 additions and 111 deletions

View file

@ -5,23 +5,23 @@ import json
# Skip the configuration settings. # Skip the configuration settings.
for line in sys.stdin: for line in sys.stdin:
if line == '\n': if line == '\n':
break; break;
# Extract the JSON. # Extract the JSON.
doc = '' doc = ''
for line in sys.stdin: for line in sys.stdin:
doc += line doc += line
total_active_time = 0 total_active_time = 0
j = json.loads(doc) j = json.loads(doc)
for object in j: for object in j:
line = '"%s",' % object['start'] line = '"%s",' % object['start']
line +='"%s"' % (object['end'] if 'end' in object else '') line +='"%s"' % (object['end'] if 'end' in object else '')
if 'tags' in object: if 'tags' in object:
for tag in object['tags']: for tag in object['tags']:
line += ',"%s"' % tag line += ',"%s"' % tag
print line print line

View file

@ -3,4 +3,4 @@
import sys import sys
for line in sys.stdin: for line in sys.stdin:
print line.strip() print line.strip()

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
################################################################################ ###############################################################################
# #
# Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez. # Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez.
# #
@ -23,7 +23,7 @@
# #
# http://www.opensource.org/licenses/mit-license.php # http://www.opensource.org/licenses/mit-license.php
# #
################################################################################ ###############################################################################
import sys import sys
import json import json
@ -45,28 +45,28 @@ print(json.dumps(new))
tags = [new['description']] tags = [new['description']]
if 'project' in new: if 'project' in new:
project = new['project'] project = new['project']
tags.append(project) tags.append(project)
if '.' in project: if '.' in project:
tags.extend([tag for tag in project.split('.')]) tags.extend([tag for tag in project.split('.')])
if 'tags' in new: if 'tags' in new:
tags.extend(new['tags']) tags.extend(new['tags'])
combined = ' '.join(['"%s"' % tag for tag in tags]).encode('utf-8').strip() combined = ' '.join(['"%s"' % tag for tag in tags]).encode('utf-8').strip()
# Started task. # Started task.
if 'start' in new and not 'start' in old: if 'start' in new and not 'start' in old:
os.system('timew start ' + combined.decode() + ' :yes') os.system('timew start ' + combined.decode() + ' :yes')
# Stopped task. # Stopped task.
elif not 'start' in new and 'start' in old: elif not 'start' in new and 'start' in old:
os.system('timew stop ' + combined.decode() + ' :yes') os.system('timew stop ' + combined.decode() + ' :yes')
# TI-51 - in the taskwarrior hook, deleting a task doesn't stop the watch # TI-51 - in the taskwarrior hook, deleting a task doesn't stop the watch
# #
# Broadened to: Any task that is active, with a non-pending status should not # Broadened to: Any task that is active, with a non-pending status should not
# be tracked. # be tracked.
elif 'start' in new and new['status'] != 'pending': elif 'start' in new and new['status'] != 'pending':
os.system('timew stop ' + combined.decode() + ' :yes') os.system('timew stop ' + combined.decode() + ' :yes')

View file

@ -1,42 +1,45 @@
#!/usr/bin/env python #!/usr/bin/env python
################################################################################ ###############################################################################
## #
## Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez. # Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez.
## #
## Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal # of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights # in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is # copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions: # furnished to do so, subject to the following conditions:
## #
## The above copyright notice and this permission notice shall be included # The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software. # in all copies or substantial portions of the Software.
## #
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE. # SOFTWARE.
## #
## http:##www.opensource.org/licenses/mit-license.php # http://www.opensource.org/licenses/mit-license.php
## #
################################################################################ ###############################################################################
import sys import sys
import json import json
import datetime import datetime
def formatSeconds(seconds):
''' Convert seconds: 3661 def format_seconds(seconds):
To formatted: 1:01:01 """Convert seconds to a formatted string
'''
hours = int(seconds / 3600) Convert seconds: 3661
minutes = int(seconds % 3600) / 60 To formatted: 1:01:01
seconds = seconds % 60 """
return '%4d:%02d:%02d' % (hours, minutes, seconds) hours = int(seconds / 3600)
minutes = int(seconds % 3600) / 60
seconds = seconds % 60
return '%4d:%02d:%02d' % (hours, minutes, seconds)
DATEFORMAT = '%Y%m%dT%H%M%SZ' DATEFORMAT = '%Y%m%dT%H%M%SZ'
@ -46,87 +49,87 @@ header = 1
configuration = dict() configuration = dict()
body = '' body = ''
for line in sys.stdin: for line in sys.stdin:
if header: if header:
if line == '\n': if line == '\n':
header = 0 header = 0
else:
fields = line.strip().split(': ', 2)
if len(fields) == 2:
configuration[fields[0]] = fields[1]
else:
configuration[fields[0]] = ''
else: else:
fields = line.strip().split(': ', 2) body += line
if len(fields) == 2:
configuration[fields[0]] = fields[1]
else:
configuration[fields[0]] = ''
else:
body += line
# Sum the second tracked by tag. # Sum the second tracked by tag.
totals = dict() totals = dict()
untagged = None untagged = None
j = json.loads(body) j = json.loads(body)
for object in j: for object in j:
start = datetime.datetime.strptime(object['start'], DATEFORMAT) start = datetime.datetime.strptime(object['start'], DATEFORMAT)
if 'end' in object: if 'end' in object:
end = datetime.datetime.strptime(object['end'], DATEFORMAT) end = datetime.datetime.strptime(object['end'], DATEFORMAT)
else:
end = datetime.datetime.utcnow()
tracked = end - start
if 'tags' not in object or object['tags'] == []:
if untagged is None:
untagged = tracked
else: else:
untagged += tracked end = datetime.datetime.utcnow()
else:
for tag in object['tags']: tracked = end - start
if tag in totals:
totals[tag] += tracked if 'tags' not in object or object['tags'] == []:
else: if untagged is None:
totals[tag] = tracked untagged = tracked
else:
untagged += tracked
else:
for tag in object['tags']:
if tag in totals:
totals[tag] += tracked
else:
totals[tag] = tracked
# Determine largest tag width. # Determine largest tag width.
max_width = len('Total') max_width = len('Total')
for tag in totals: for tag in totals:
if len(tag) > max_width: if len(tag) > max_width:
max_width = len(tag) max_width = len(tag)
if 'temp.report.start' not in configuration: if 'temp.report.start' not in configuration:
print 'There is no data in the database' print 'There is no data in the database'
exit() exit()
start = datetime.datetime.strptime(configuration['temp.report.start'], DATEFORMAT) start = datetime.datetime.strptime(configuration['temp.report.start'], DATEFORMAT)
end = datetime.datetime.strptime(configuration['temp.report.end'], DATEFORMAT) end = datetime.datetime.strptime(configuration['temp.report.end'], DATEFORMAT)
if max_width > 0: if max_width > 0:
# Compose report header. # Compose report header.
print '\nTotal by Tag, for %s - %s\n' % (start, end) print '\nTotal by Tag, for %s - %s\n' % (start, end)
# Compose table header. # Compose table header.
if configuration['color'] == 'on': if configuration['color'] == 'on':
print '%-*s %10s' % (max_width, 'Tag', 'Total') print '%-*s %10s' % (max_width, 'Tag', 'Total')
else: else:
print '%-*s %10s' % (max_width, 'Tag', 'Total') print '%-*s %10s' % (max_width, 'Tag', 'Total')
print '-' * max_width, '----------' print '-' * max_width, '----------'
# Compose table rows. # Compose table rows.
grand_total = 0 grand_total = 0
for tag in sorted(totals): for tag in sorted(totals):
formatted = formatSeconds(totals[tag].seconds) formatted = format_seconds(totals[tag].seconds)
grand_total += totals[tag].seconds grand_total += totals[tag].seconds
print '%-*s %10s' % (max_width, tag, formatted) print '%-*s %10s' % (max_width, tag, formatted)
if untagged is not None: if untagged is not None:
formatted = formatSeconds(untagged.seconds) formatted = format_seconds(untagged.seconds)
grand_total += untagged.seconds grand_total += untagged.seconds
print '%-*s %10s' % (max_width, '', formatted) print '%-*s %10s' % (max_width, '', formatted)
# Compose total. # Compose total.
if configuration['color'] == 'on': if configuration['color'] == 'on':
print ' ' * max_width, ' ' print ' ' * max_width, ' '
else: else:
print ' ' * max_width, '----------' print ' ' * max_width, '----------'
print '%-*s %10s' % (max_width, 'Total', formatSeconds(grand_total)) print '%-*s %10s' % (max_width, 'Total', format_seconds(grand_total))
else: else:
print 'No data in the range %s - %s' % (start, end) print 'No data in the range %s - %s' % (start, end)