Commit 3d5e7950 authored by Julien Lepiller's avatar Julien Lepiller

Add yaml format initial support

parent aff36f32
......@@ -55,10 +55,10 @@ class JSONEntry(Entry):
class YAMLEntry(Entry):
def __init__(self, entry):
self.key = list(entry.keys())[0]
Entry.__init__(self, self.key, entry[self.key], False, False)
self.entry = entry
Entry.__init__(self, [entry['source_string']],
[entry['translation']], False, False)
def update(self, index, content):
Entry.update(self, index, content)
self.entry[self.key] = content
self.entry['translation'] = content
""" The Yaml format. """
from ruamel import yaml
from .entry import YAMLEntry
def yaml_rec_load(path, source, dest):
ans = []
for s in source:
path2 = list(path)
path2.append(s)
if isinstance(source[s], str):
ans.append({'path': path, 'id': s,
'source_string': str(source[s]),
'translation': str(dest[s])})
else:
ans.extend(yaml_rec_load(path2, source[s], dest[s]))
return ans
def yaml_rec_update(callback, source, old, new):
ans = {}
for i in new:
o = ''
s = ''
n = new[i]
try:
s = source[i]
except Exception:
pass
try:
o = old[i]
except Exception:
pass
if isinstance(n, str):
if o == '':
ans[i] = n
elif n == '':
ans[i] = o
else:
ans[i] = callback(s, o, n)
else:
ans[i] = yaml_rec_update(callback, s, o, n)
return ans
class YamlFormat:
def __init__(self, conf):
self.conf = conf
self.source = conf['source']
self.dest = conf['dest']
with open(self.source, 'rb') as sf:
with open(self.dest, 'rb') as df:
source = yaml.safe_load(sf)
dest = yaml.safe_load(df)
# TODO: check that Yaml files always are rooted with the language name
lang1 = list(source.keys())[0]
lang2 = list(dest.keys())[0]
self.contents = yaml_rec_load([lang2], source[lang1], dest[lang2])
def content(self):
return [YAMLEntry(x) for x in self.contents]
def save(self):
data = {}
for d in self.contents:
path = d['path']
curr = data
for p in path:
if p in curr:
curr = curr[p]
else:
curr[p] = {}
curr = curr[p]
curr[d['id']] = str(d['translation'])
with open(self.dest, 'wb') as f:
f.write(yaml.dump(data, allow_unicode=True, Dumper=yaml.RoundTripDumper).encode('utf8'))
def merge(self, older, callback):
pass
......@@ -4,6 +4,7 @@ import json
import os
import requests
from requests.auth import HTTPBasicAuth
from ..formats.yaml import YamlFormat
class TransifexProject:
def __init__(self, conf, name, lang, data={}):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment