-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileUtils.py
34 lines (29 loc) · 1.38 KB
/
fileUtils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json, csv
from shutil import copyfile
from os import path, listdir
from os.path import isfile, getsize
def getDictFromJSON(jsonFileName):
jsonFile=open(jsonFileName, encoding='utf-8').read()
# check empty file use an empty object to avoid parsing error
if jsonFile == '':
jsonFile = '{}'
dict = json.loads(jsonFile)
return dict
def saveDictToJSON(dict, jsonFileName):
with open(jsonFileName, 'w', encoding='utf-8') as outfile:
json.dump(dict, outfile, indent = 4, ensure_ascii=False, sort_keys=True)
def getLocaleFromFileName(fileName):
return path.splitext(path.basename(fileName))[0]
def exportToFile(sourceDict, targetDict, sourceLocale, targetLocale, onlyMissing=False):
if onlyMissing:
targetFileName = sourceLocale + '_' + targetLocale + '_missing.csv'
else:
targetFileName = sourceLocale + '_' + targetLocale + '.csv'
fieldNames = ['key', sourceLocale, targetLocale]
with open(targetFileName, 'w', newline='') as csvFile:
writer = csv.DictWriter(csvFile, fieldnames=fieldNames)
writer.writeheader()
for key, source in sourceDict.items():
target = targetDict.get(key, '')
if target == '' or (target != '' and not onlyMissing):
writer.writerow({'key': key, sourceLocale: source, targetLocale: target})