489 lines
11 KiB
Python
Executable File
489 lines
11 KiB
Python
Executable File
#!/usr/bin/env python3.7
|
|
|
|
import sys
|
|
import click
|
|
import csv
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
import sqlite3
|
|
|
|
HAMSTER_DIR = Path.home() / '.local/share/hamster-applet'
|
|
HAMSTER_FILE = HAMSTER_DIR / 'hamster.db'
|
|
|
|
conn = sqlite3.connect(HAMSTER_FILE)
|
|
c = conn.cursor()
|
|
|
|
|
|
def get_categories(ids=None, search=None):
|
|
sql = '''
|
|
SELECT
|
|
id, name
|
|
FROM
|
|
categories '''
|
|
|
|
args = []
|
|
|
|
if ids is not None:
|
|
sql = sql + 'WHERE id IN ({seq})'.format(
|
|
seq=','.join(['?'] * len(ids))
|
|
)
|
|
args = args + list(ids)
|
|
|
|
if search is not None:
|
|
sql = sql + " WHERE name LIKE ?"
|
|
search = '%{0}%'.format(search)
|
|
args.append(search)
|
|
|
|
results = c.execute(sql, args)
|
|
|
|
results = c.fetchall()
|
|
|
|
return results
|
|
|
|
|
|
def get_activities(ids=None, search=None, category_search=None):
|
|
sql = '''
|
|
SELECT
|
|
activities.id, activities.name, categories.name, categories.id
|
|
FROM
|
|
activities
|
|
LEFT JOIN
|
|
categories
|
|
ON
|
|
activities.category_id = categories.id '''
|
|
|
|
args = []
|
|
|
|
if ids is not None:
|
|
sql = sql + 'WHERE activities.id IN ({seq})'.format(
|
|
seq=','.join(['?'] * len(ids))
|
|
)
|
|
args = args + list(ids)
|
|
|
|
if search is not None:
|
|
sql = sql + " WHERE activities.name LIKE ?"
|
|
search = '%{0}%'.format(search)
|
|
args.append(search)
|
|
|
|
if category_search is not None:
|
|
sql = sql + " WHERE categories.name LIKE ?"
|
|
category_search = '%{0}%'.format(category_search)
|
|
args.append(category_search)
|
|
|
|
results = c.execute(sql, args)
|
|
results = c.fetchall()
|
|
|
|
return results
|
|
|
|
|
|
@click.group()
|
|
def cli():
|
|
pass
|
|
|
|
|
|
@cli.group()
|
|
def categories():
|
|
pass
|
|
|
|
|
|
@categories.command('list')
|
|
@click.option('--search', help='Search string')
|
|
def list_categories(search):
|
|
""" List / search categories """
|
|
results = get_categories(search=search)
|
|
|
|
for r in results:
|
|
click.echo('@{0[0]}: {0[1]}'.format(r))
|
|
|
|
|
|
@categories.command('delete')
|
|
@click.argument('ids', nargs=-1)
|
|
def delete_categories(ids):
|
|
""" Delete categories specified by IDS """
|
|
click.secho('Deleting:', fg='red')
|
|
|
|
results = get_categories(ids)
|
|
|
|
for r in results:
|
|
sql = 'SELECT COUNT(id) FROM activities WHERE category_id = ?'
|
|
count = c.execute(sql, (r[0],)).fetchone()[0]
|
|
click.echo('@{0[0]}: {0[1]} ({1} activities)'.format(r, count))
|
|
|
|
click.confirm('Do you want to continue?', abort=True)
|
|
|
|
sql = 'DELETE FROM categories '
|
|
|
|
sql = sql + 'WHERE id IN ({seq})'.format(
|
|
seq=','.join(['?'] * len(ids))
|
|
)
|
|
|
|
c.execute(sql, ids)
|
|
conn.commit()
|
|
|
|
click.secho('Deleted {0} categories'.format(len(ids)), fg='green')
|
|
|
|
|
|
@categories.command('activities')
|
|
@click.argument('ids', nargs=-1)
|
|
def list_category_activities(ids):
|
|
""" Show activities for categories specified by IDS """
|
|
sql = '''
|
|
SELECT
|
|
activities.id, activities.name, categories.name
|
|
FROM
|
|
activities
|
|
LEFT JOIN
|
|
categories
|
|
ON
|
|
activities.category_id = categories.id
|
|
WHERE
|
|
categories.id IN ({seq})
|
|
'''.format(
|
|
seq=','.join(['?'] * len(ids))
|
|
)
|
|
|
|
results = c.execute(sql, ids)
|
|
|
|
for r in results:
|
|
click.echo('@{0[0]}: {0[2]} » {0[1]}'.format(r))
|
|
|
|
|
|
@cli.group()
|
|
def activities():
|
|
pass
|
|
|
|
|
|
@activities.command('list')
|
|
@click.option('--search', help='Search string')
|
|
@click.option('--csv/--no-csv', 'csv_output', default=False, help='CSV output')
|
|
def list_activities(search, csv_output):
|
|
""" List / search activities """
|
|
|
|
results = get_activities(search=search)
|
|
|
|
if csv_output:
|
|
csv_writer = csv.writer(sys.stdout)
|
|
|
|
for r in results:
|
|
if csv_output:
|
|
csv_writer.writerow([r[3], r[2], r[0], r[1]])
|
|
else:
|
|
click.echo('@{0[3]}: {0[2]} » {0[0]}: {0[1]}'.format(r))
|
|
|
|
|
|
@activities.command('delete')
|
|
@click.argument('ids', nargs=-1)
|
|
def delete_activities(ids):
|
|
""" Delete activities specified by IDS """
|
|
|
|
results = get_activities(ids)
|
|
|
|
click.secho('Deleting:', fg='red')
|
|
|
|
for r in results:
|
|
sql = "SELECT COUNT(id) FROM facts WHERE activity_id = ?"
|
|
count = c.execute(sql, (r[0],)).fetchone()[0]
|
|
click.echo('@{0[0]}: {0[2]} » {0[1]} ({1} facts)'.format(r, count))
|
|
|
|
click.confirm('Do you want to continue?', abort=True)
|
|
|
|
sql = 'DELETE FROM activities '
|
|
|
|
sql = sql + 'WHERE id IN ({seq})'.format(
|
|
seq=','.join(['?'] * len(ids))
|
|
)
|
|
|
|
c.execute(sql, ids)
|
|
conn.commit()
|
|
|
|
click.secho('Deleted {0} activities'.format(len(ids)), fg='green')
|
|
|
|
|
|
@activities.command()
|
|
@click.argument('category_id')
|
|
@click.argument('ids', nargs=-1)
|
|
def move(category_id, ids):
|
|
""" Move activities to another category """
|
|
category = get_categories((category_id,))[0]
|
|
results = get_activities(ids)
|
|
|
|
click.secho('Moving to "@{0[0]}: {0[1]}":'.format(category), fg='green')
|
|
|
|
for r in results:
|
|
click.secho('@{0[3]}: {0[2]} » @{0[0]}: {0[1]}'.format(r), fg='blue')
|
|
|
|
click.confirm('Do you want to continue?', abort=True)
|
|
|
|
sql = '''
|
|
UPDATE
|
|
activities
|
|
SET
|
|
category_id = ?
|
|
'''
|
|
|
|
sql = sql + 'WHERE id IN ({seq})'.format(
|
|
seq=','.join(['?'] * len(ids))
|
|
)
|
|
|
|
c.execute(sql, (category[0], *ids))
|
|
conn.commit()
|
|
|
|
click.secho('Moved {0} activities'.format(len(ids)), fg='green')
|
|
|
|
|
|
@activities.command()
|
|
@click.argument('ids', nargs=-1)
|
|
def list_facts(ids):
|
|
""" Show facts for activities """
|
|
|
|
results = get_activities(ids)
|
|
|
|
for r in results:
|
|
click.secho(
|
|
'@{0[0]}: {0[1]}'.format(r), fg='green'
|
|
)
|
|
|
|
sql = '''
|
|
SELECT
|
|
start_time,
|
|
activities.name
|
|
FROM
|
|
facts
|
|
LEFT JOIN
|
|
activities
|
|
ON
|
|
facts.activity_id = activities.id
|
|
WHERE
|
|
activities.id = ?
|
|
'''
|
|
|
|
results = c.execute(sql, (r[0],))
|
|
|
|
for r in results:
|
|
click.secho('@{0[0]}, {0[1]}'.format(r), fg='blue')
|
|
|
|
@activities.command()
|
|
@click.argument('from_id')
|
|
@click.argument('to_id')
|
|
def move_facts(from_id, to_id):
|
|
""" Move facts from one activity to another """
|
|
from_activity = get_activities((from_id,))[0]
|
|
to_activity = get_activities((to_id,))[0]
|
|
|
|
click.secho(
|
|
'Moving facts from "@{0[2]} » @{0[0]}: {0[1]}" to "@{1[2]} » @{1[0]}: {1[1]}"'.format(
|
|
from_activity, to_activity
|
|
), fg='green'
|
|
)
|
|
|
|
sql = '''
|
|
SELECT
|
|
start_time,
|
|
activities.name
|
|
FROM
|
|
facts
|
|
LEFT JOIN
|
|
activities
|
|
ON
|
|
facts.activity_id = activities.id
|
|
WHERE
|
|
activities.id = ?
|
|
'''
|
|
|
|
results = c.execute(sql, (from_id,))
|
|
|
|
for r in results:
|
|
click.secho('@{0[0]}, {0[1]}'.format(r), fg='blue')
|
|
|
|
click.confirm('Do you want to continue?', abort=True)
|
|
|
|
c.execute(
|
|
'UPDATE facts SET activity_id = ? WHERE activity_id = ?',
|
|
(to_id, from_id)
|
|
)
|
|
|
|
conn.commit()
|
|
|
|
click.secho('Moved {0} facts'.format(results.rowcount), fg='green')
|
|
|
|
|
|
@activities.command()
|
|
def find_duplicates():
|
|
""" Show activities which are not unique in their categories """
|
|
|
|
sql = '''
|
|
SELECT
|
|
categories.id,
|
|
categories.name,
|
|
activities.id,
|
|
activities.name,
|
|
COUNT(activities.id) c
|
|
FROM
|
|
activities
|
|
LEFT JOIN
|
|
categories
|
|
ON
|
|
activities.category_id = categories.id
|
|
GROUP BY
|
|
activities.name,
|
|
activities.category_id
|
|
HAVING c > 1
|
|
'''
|
|
|
|
results = c.execute(sql)
|
|
|
|
for r in results:
|
|
click.secho('@{0[0]}: {0[1]} » @{0[2]}: {0[3]} ({0[4]})'.format(r), fg='blue')
|
|
|
|
|
|
@cli.group()
|
|
def export():
|
|
pass
|
|
|
|
|
|
@export.command()
|
|
@click.option('--mapping', help='Mapping file (default ~/.local/share/hamster/mapping.kimai.csv)')
|
|
@click.option('--output', help='Output file (default kimai.csv)')
|
|
@click.option('--category-search', help='Category search string')
|
|
@click.argument('username')
|
|
def kimai(username, mapping=None, output=None, category_search=None):
|
|
"""
|
|
Export time tracking data in Kimai format
|
|
"""
|
|
|
|
if mapping is None:
|
|
mapping = HAMSTER_DIR / 'mapping.kimai.csv'
|
|
if output is None:
|
|
output = 'kimai.csv'
|
|
|
|
try:
|
|
mapping_file = open(mapping)
|
|
mapping_reader = csv.reader(mapping_file)
|
|
except FileNotFoundError:
|
|
click.confirm(
|
|
'Mapping file {} not found, create it?:'.format(mapping),
|
|
abort=True
|
|
)
|
|
mapping_file = open(mapping, 'w')
|
|
mapping_writer = csv.writer(mapping_file)
|
|
|
|
mapping_writer.writerow([
|
|
'FROM category',
|
|
'FROM activity',
|
|
'TO Customer',
|
|
'TO Project',
|
|
'TO Activity'
|
|
])
|
|
|
|
results = get_activities(category_search=category_search)
|
|
|
|
for r in results:
|
|
mapping_writer.writerow([
|
|
r[2], r[1]
|
|
])
|
|
|
|
mapping_file.close()
|
|
return
|
|
|
|
next(mapping_reader)
|
|
|
|
mapping = {
|
|
'{0}:{1}'.format(row[0], row[1]): [row[2], row[3], row[4]]
|
|
for row in mapping_reader
|
|
}
|
|
|
|
mapping_file.close()
|
|
|
|
output_file = open(output, 'w')
|
|
output_writer = csv.writer(output_file)
|
|
|
|
args = []
|
|
|
|
sql = '''
|
|
SELECT
|
|
facts.id, facts.start_time, facts.end_time, facts.description,
|
|
activities.id, activities.name,
|
|
categories.name, categories.id
|
|
FROM
|
|
facts
|
|
LEFT JOIN
|
|
activities ON facts.activity_id = activities.id
|
|
LEFT JOIN
|
|
categories ON activities.category_id = categories.id '''
|
|
|
|
if category_search is not None:
|
|
sql = sql + " WHERE categories.name LIKE ?"
|
|
category_search = '%{0}%'.format(category_search)
|
|
args.append(category_search)
|
|
|
|
results = c.execute(sql, args)
|
|
results = c.fetchall()
|
|
|
|
output_writer.writerow([
|
|
"Date",
|
|
"From",
|
|
"To",
|
|
"Duration",
|
|
"Rate",
|
|
"User",
|
|
"Customer",
|
|
"Project",
|
|
"Activity",
|
|
"Description",
|
|
"Exported",
|
|
"Tags",
|
|
"Hourly rate",
|
|
"Fixed rate"
|
|
])
|
|
|
|
for fact in results:
|
|
k = '{0}:{1}'.format(fact[6], fact[5])
|
|
try:
|
|
mapping_ = mapping[k]
|
|
except KeyError:
|
|
click.secho("Can't find mapping for '{0}', skipping".format(k), fg='yellow')
|
|
continue
|
|
|
|
if fact[1] is None or fact[2] is None:
|
|
click.secho("Missing duration data '{0}-{1}', skipping".format(
|
|
fact[1],
|
|
fact[2]
|
|
), fg='yellow')
|
|
continue
|
|
|
|
date_start, date_end = (
|
|
datetime.strptime(fact[2].split('.')[0], '%Y-%m-%d %H:%M:%S'),
|
|
datetime.strptime(fact[1].split('.')[0], '%Y-%m-%d %H:%M:%S')
|
|
)
|
|
duration = (
|
|
date_start - date_end
|
|
).seconds
|
|
|
|
output_writer.writerow([
|
|
date_start.strftime('%Y-%m-%d'),
|
|
date_start.strftime('%H:%M'),
|
|
'', # To (time)
|
|
duration,
|
|
'', # Rate
|
|
username,
|
|
mapping_[0],
|
|
mapping_[1],
|
|
mapping_[2],
|
|
fact[3] or '',
|
|
'0', # Exported
|
|
'', # Tags
|
|
'', # Hourly rate
|
|
'', # Fixed rate
|
|
])
|
|
|
|
output_file.close()
|
|
|
|
|
|
@cli.command()
|
|
def hamster():
|
|
click.echo('🐹')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
cli()
|