2019-12-27 10:28:25 +00:00
|
|
|
#!/usr/bin/env python3.7
|
|
|
|
|
|
|
|
import sqlite3
|
|
|
|
import click
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
HAMSTER_FILE = Path.home() / '.local/share/hamster-applet/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):
|
|
|
|
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)
|
|
|
|
|
|
|
|
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):
|
2019-12-28 09:33:35 +00:00
|
|
|
""" List / search categories """
|
2019-12-27 10:28:25 +00:00
|
|
|
results = get_categories(search=search)
|
|
|
|
|
|
|
|
for r in results:
|
|
|
|
click.echo('@{0[0]}: {0[1]}'.format(r))
|
|
|
|
|
|
|
|
|
2019-12-28 09:33:35 +00:00
|
|
|
@categories.command('delete')
|
2019-12-27 10:28:25 +00:00
|
|
|
@click.argument('ids', nargs=-1)
|
2019-12-28 09:33:35 +00:00
|
|
|
def delete_categories(ids):
|
|
|
|
""" Delete categories specified by IDS """
|
2019-12-27 10:28:25 +00:00
|
|
|
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):
|
2019-12-28 09:33:35 +00:00
|
|
|
""" Show activities for categories specified by IDS """
|
2019-12-27 10:28:25 +00:00
|
|
|
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')
|
|
|
|
def list_activities(search):
|
2019-12-28 09:33:35 +00:00
|
|
|
""" List / search activities """
|
|
|
|
|
2019-12-27 10:28:25 +00:00
|
|
|
results = get_activities(search=search)
|
|
|
|
|
|
|
|
for r in results:
|
2019-12-28 09:33:35 +00:00
|
|
|
click.echo('@{0[3]}: {0[2]} » {0[0]}: {0[1]}'.format(r))
|
2019-12-27 10:28:25 +00:00
|
|
|
|
|
|
|
|
2019-12-28 09:33:35 +00:00
|
|
|
@activities.command('delete')
|
2019-12-27 10:28:25 +00:00
|
|
|
@click.argument('ids', nargs=-1)
|
|
|
|
def delete_activities(ids):
|
2019-12-28 09:33:35 +00:00
|
|
|
""" Delete activities specified by IDS """
|
|
|
|
|
2019-12-27 10:28:25 +00:00
|
|
|
results = get_activities(ids)
|
|
|
|
|
|
|
|
click.secho('Deleting:', fg='red')
|
|
|
|
|
|
|
|
for r in results:
|
2019-12-28 09:33:35 +00:00
|
|
|
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))
|
2019-12-27 10:28:25 +00:00
|
|
|
|
|
|
|
click.confirm('Do you want to continue?', abort=True)
|
|
|
|
|
2019-12-28 09:33:35 +00:00
|
|
|
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')
|
|
|
|
|
2020-09-21 18:17:59 +00:00
|
|
|
|
2019-12-27 10:28:25 +00:00
|
|
|
@activities.command()
|
|
|
|
@click.argument('category_id')
|
|
|
|
@click.argument('ids', nargs=-1)
|
|
|
|
def move(category_id, ids):
|
2019-12-28 09:33:35 +00:00
|
|
|
""" Move activities to another category """
|
2019-12-27 10:28:25 +00:00
|
|
|
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')
|
|
|
|
|
2019-12-28 09:33:35 +00:00
|
|
|
|
|
|
|
@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[0]}: {0[1]}" to "@{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')
|
|
|
|
|
|
|
|
|
2020-09-21 18:17:59 +00:00
|
|
|
@cli.command()
|
|
|
|
def hamster():
|
|
|
|
click.echo('🐹')
|
|
|
|
|
|
|
|
|
2019-12-27 10:28:25 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|