Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
teleyinex committed Jun 5, 2017
1 parent 7026158 commit 8c4a6e3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
104 changes: 58 additions & 46 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c4a6e3

Please sign in to comment.