Support multiple mapping files

This commit is contained in:
3wc 2023-07-08 13:13:07 +01:00
parent 40c0312e4c
commit f858d4a5f0
2 changed files with 25 additions and 13 deletions

View File

@ -53,7 +53,7 @@ hamster](https://github.com/projecthamster/hamster-lib) is ready.
1. Run `python -m hamstertools activities list --csv` to get a dump of all activities 1. Run `python -m hamstertools activities list --csv` to get a dump of all activities
2. Review the export to check for any duplicates, typoes, etc. 2. Review the export to check for any duplicates, typoes, etc.
3. Use e.g. `python -m hamstertools activities move-facts ...` to clean them up 3. Use e.g. `python -m hamstertools activities move-facts ...` to clean them up
4. Run `python -m hamstertools kimai import --mapping-path mapping.kimai.csv --show-missing --after 2021-09-07 --category-search auto 3wordchant` to find entries in your time tracking which don't yet have a mapping to Kimai. You might notice some extra tidying needed here. 4. Run `python -m hamstertools kimai import --mapping-path data/mapping.2023.kimai.csv --show-missing --after 2023-09-07 --category-search auto 3wordchant` to find entries in your time tracking which don't yet have a mapping to Kimai. You might notice some extra tidying needed here.
5. Run `python -m hamstertools kimai sync --mapping-path mapping.kimai.csv --just-errors --ignore-activities 3wordchant $(pass users/calix/logins/kimai.autonomic.zone/api)` to find items in your mapping file which don't exist on Kimai 5. Run `python -m hamstertools kimai sync --mapping-path data/mapping.2023.kimai.csv --just-errors --ignore-activities 3wordchant $(pass users/calix/logins/kimai.autonomic.zone/api)` to find items in your mapping file which don't exist on Kimai
5. Run `python -m hamstertools kimai import --mapping-path mapping.kimai.csv --after 2021-09-07 --category-search auto 3wordchant --output kimai_$(date +%F).csv` to generate a CSV of your time entries 5. Run `python -m hamstertools kimai import --mapping-path data/mapping.2023.kimai.csv --after 2023-09-07 --category-search auto 3wordchant --output data/kimai_$(date +%F).csv` to generate a CSV of your time entries
6. Review your timesheet -- NB it might not be sorted in date order by default - to make sure it has the expected entries, and remove any entries already in Kimai. 6. Review your timesheet -- NB it might not be sorted in date order by default - to make sure it has the expected entries, and remove any entries already in Kimai.

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3.7 #!/usr/bin/env python3.7
import csv import csv
from datetime import datetime from datetime import datetime
from itertools import chain
from pathlib import Path from pathlib import Path
import sys import sys
@ -398,7 +399,7 @@ def kimai():
pass pass
def _get_kimai_mapping_file(path): def _get_kimai_mapping_file(path, category_search=None):
try: try:
return open(path) return open(path)
except FileNotFoundError: except FileNotFoundError:
@ -431,7 +432,7 @@ def _get_kimai_mapping_file(path):
@kimai.command() @kimai.command()
@click.option('--mapping-path', help='Mapping file (default ~/.local/share/hamster/mapping.kimai.csv)') @click.option('--mapping-path', help='Mapping file (default ~/.local/share/hamster/mapping.kimai.csv)', multiple=True)
@click.argument('username') @click.argument('username')
@click.argument('api_key') @click.argument('api_key')
@click.option('--just-errors', 'just_errors', is_flag=True, help='Only display errors') @click.option('--just-errors', 'just_errors', is_flag=True, help='Only display errors')
@ -546,7 +547,7 @@ def sync(username, api_key, just_errors, ignore_activities, mapping_path=None):
@kimai.command('import') @kimai.command('import')
@click.option('--mapping-path', help='Mapping file (default ~/.local/share/hamster/mapping.kimai.csv)') @click.option('--mapping-path', help='Mapping file (default ~/.local/share/hamster/mapping.kimai.csv)', multiple=True)
@click.option('--output', help='Output file (default kimai.csv)') @click.option('--output', help='Output file (default kimai.csv)')
@click.option('--category-search', help='Category search string') @click.option('--category-search', help='Category search string')
@click.option('--after', help='Only show time entries after this date') @click.option('--after', help='Only show time entries after this date')
@ -564,17 +565,28 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
timestamp = datetime.now().strftime('%F') timestamp = datetime.now().strftime('%F')
output = f'kimai_{timestamp}.csv' output = f'kimai_{timestamp}.csv'
mapping_file = _get_kimai_mapping_file(mapping_path) if type(mapping_path) == tuple:
mapping_reader = csv.reader(mapping_file) mapping_files = []
for mapping_path_item in mapping_path:
next(mapping_reader) mapping_file = _get_kimai_mapping_file(mapping_path_item, category_search)
next(mapping_file)
mapping_files.append(mapping_file)
mapping_reader = csv.reader(chain(*mapping_files))
else:
mapping_file = _get_kimai_mapping_file(mapping_path, category_search)
next(mapping_file)
mapping_reader = csv.reader(mapping_file)
mapping = { mapping = {
'{0}:{1}'.format(row[0], row[1]): [row[2], row[3], row[4], row[5]] '{0}:{1}'.format(row[0], row[1]): [row[2], row[3], row[4], row[5]]
for row in mapping_reader for row in mapping_reader
} }
mapping_file.close() if type(mapping_path) == tuple:
for mapping_file in mapping_files:
mapping_file.close()
else:
mapping_file.close()
output_file = open(output, 'w') output_file = open(output, 'w')
output_writer = csv.writer(output_file) output_writer = csv.writer(output_file)
@ -623,8 +635,8 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
"Description", "Description",
"Exported", "Exported",
"Tags", "Tags",
"Hourly rate", "HourlyRate",
"Fixed rate" "FixedRate"
]) ])
for fact in results: for fact in results: