Skip to content

Commit

Permalink
Merge pull request #73 from timotoots/converter
Browse files Browse the repository at this point in the history
Converter
  • Loading branch information
brunoproduit authored Nov 12, 2018
2 parents b513854 + 3cc4725 commit b1d6221
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
36 changes: 26 additions & 10 deletions ckanext-converter/ckanext/converter/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,45 @@ def filter(resource):
file_path = FILESTORAGE_PATH + "/resources/" + file_id[:3] + "/" +\
file_id[3:6] + "/" + file_id[6:]

# Validate()
# ----------------------------------------

# Switch-Case for filetype differenciation
if file_type=='XML':
return xml_to_csv(file_path)
return xml_to_csv(file_id, file_path)
elif file_type=='JSON':
return json_to_csv(file_path)
return json_to_csv(file_id, file_path)
elif file_type=='CSV':
return file_path
else:
print "error"

# ----------------------------------------
# Convert()
# ----------------------------------------
# Complete()
# ----------------------------------------
# Validate()
# ----------------------------------------



# Convert JSON to CSV
def json_to_csv(json):
return json
def json_to_csv(file_id, file_path):
return file_path

# Convert XML to CSV
def xml_to_csv(xml):
tree = ET.parse(xml)
def xml_to_csv(file_id, file_path):
tree = ET.parse(file_path)
root = tree.getroot()
Resident_data = open('/tmp/ResidentData.csv', 'w')
Resident_data = open('/tmp/' + file_id + '.csv', 'w')
csvwriter = csv.writer(Resident_data)
for i in root:
row = []
for j in i:
j=j.text
csvwriter.writerow(i)
return open('/tmp/ResidentData.csv', 'r').read(100)
if j.text:
row.append(j.text.encode('utf-8'))
csvwriter.writerow(row)
return FILESTORAGE_PATH + "/converted/" + file_id[:3] + "/" +\
file_id[3:6] + "/" + file_id[6:]

4 changes: 2 additions & 2 deletions ckanext-converter/ckanext/converter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class ConverterPlugin(plugins.SingletonPlugin):

# Filter received resource after upload
def after_create(self, context, resource):
print filter.filter(resource)
filter.filter(resource)

# Not needed, so not changed
def before_create(self, context, resource):
pass
pass

# Not needed, so not changed
def before_update(self, context, current, resource):
Expand Down

0 comments on commit b1d6221

Please sign in to comment.