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

@ -1,6 +1,7 @@
#!/usr/bin/env python3.7
import csv
from datetime import datetime
from itertools import chain
from pathlib import Path
import sys
@ -398,7 +399,7 @@ def kimai():
pass
def _get_kimai_mapping_file(path):
def _get_kimai_mapping_file(path, category_search=None):
try:
return open(path)
except FileNotFoundError:
@ -431,7 +432,7 @@ def _get_kimai_mapping_file(path):
@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('api_key')
@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')
@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('--category-search', help='Category search string')
@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')
output = f'kimai_{timestamp}.csv'
mapping_file = _get_kimai_mapping_file(mapping_path)
mapping_reader = csv.reader(mapping_file)
next(mapping_reader)
if type(mapping_path) == tuple:
mapping_files = []
for mapping_path_item in mapping_path:
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 = {
'{0}:{1}'.format(row[0], row[1]): [row[2], row[3], row[4], row[5]]
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_writer = csv.writer(output_file)
@ -623,8 +635,8 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
"Description",
"Exported",
"Tags",
"Hourly rate",
"Fixed rate"
"HourlyRate",
"FixedRate"
])
for fact in results: