diff --git a/README.md b/README.md index fa48ef3..293add8 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ options, please check the **--help** command: Adding tasks is very simple. You can have your tasks in three formats: * JSON + * Excel (xlsx from 2010. It imports the first sheet) * CSV * PO (any po file that you want to translate) * PROPERTIES (any PROPERTIES file that you want to translate) diff --git a/helpers.py b/helpers.py index 18dc595..c5ab989 100644 --- a/helpers.py +++ b/helpers.py @@ -134,58 +134,70 @@ def _update_project(config, task_presenter, results, raise +def _load_data(data_file, data_type): + """Load data from CSV, JSON, Excel, ..., formats.""" + raw_data = data_file.read() + if data_type is None: + data_type = data_file.name.split('.')[-1] + # Data list to process + data = [] + # JSON type + if data_type == 'json': + data = json.loads(raw_data) + return data + # CSV type + elif data_type == 'csv': + csv_data = StringIO(raw_data) + reader = csv.DictReader(csv_data, delimiter=',') + for line in reader: + data.append(line) + return data + elif data_type in ['xlsx', 'xlsm', 'xltx', 'xltm']: + excel_data = StringIO(raw_data) + wb = openpyxl.load_workbook(excel_data) + ws = wb.active + # First headers + headers = [] + for row in ws.iter_rows(max_row=1): + for cell in row: + tmp = '_'.join(cell.value.split(" ")).lower() + headers.append(tmp) + # Simulate DictReader + for row in ws.iter_rows(row_offset=1): + values = [] + for cell in row: + values.append(cell.value) + tmp = dict(itertools.izip(headers, values)) + if len(values) == len(headers) and not row_empty(values): + data.append(tmp) + return data + # PO type + elif data_type == 'po': + po = polib.pofile(raw_data) + for entry in po.untranslated_entries(): + data.append(entry.__dict__) + return data + # PROPERTIES type (used in Java and Firefox extensions) + elif data_type == 'properties': + lines = raw_data.split('\n') + for l in lines: + if l: + var_id, string = l.split('=') + tmp = dict(var_id=var_id, string=string) + data.append(tmp) + return data + else: + return data + + def _add_tasks(config, tasks_file, tasks_type, priority, redundancy): """Add tasks to a project.""" try: project = find_project_by_short_name(config.project['short_name'], config.pbclient, config.all) - tasks = tasks_file.read() - if tasks_type is None: - tasks_type = tasks_file.name.split('.')[-1] - # Data list to process - data = [] - # JSON type - if tasks_type == 'json': - data = json.loads(tasks) - # CSV type - elif tasks_type == 'csv': - csv_data = StringIO(tasks) - reader = csv.DictReader(csv_data, delimiter=',') - for line in reader: - data.append(line) - elif tasks_type in ['xlsx', 'xlsm', 'xltx', 'xltm']: - excel_data = StringIO(tasks) - wb = openpyxl.load_workbook(excel_data) - ws = wb.active - # First headers - headers = [] - for row in ws.iter_rows(max_row=1): - for cell in row: - tmp = '_'.join(cell.value.split(" ")).lower() - headers.append(tmp) - # Simulate DictReader - for row in ws.iter_rows(row_offset=1): - values = [] - for cell in row: - values.append(cell.value) - tmp = dict(itertools.izip(headers, values)) - if len(values) == len(headers) and not row_empty(values): - data.append(tmp) - # PO type - elif tasks_type == 'po': - po = polib.pofile(tasks) - for entry in po.untranslated_entries(): - data.append(entry.__dict__) - # PROPERTIES type (used in Java and Firefox extensions) - elif tasks_type == 'properties': - lines = tasks.split('\n') - for l in lines: - if l: - var_id, string = l.split('=') - tmp = dict(var_id=var_id, string=string) - data.append(tmp) - else: + data = _load_data(tasks_file, tasks_type) + if len(data) == 0: return ("Unknown format for the tasks file. Use json, csv, po or " "properties.") # Check if for the data we have to auto-throttle task creation