Skip to content

Instantly share code, notes, and snippets.

@julienmalik
Created February 16, 2016 14:35
Show Gist options
  • Save julienmalik/e6544ad26e82bae4381c to your computer and use it in GitHub Desktop.
Save julienmalik/e6544ad26e82bae4381c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import yaml
import pprint
import copy
import collections
def make_translated_view(lang, o):
t = type(o)
if t is dict:
return DictTranslateView(lang, o)
elif t is list:
return ListTranslateView(lang, o)
else:
return o
class ListTranslateViewIterator(object):
def __init__(self, lang, l):
self.lang = lang
self.proxified_list = l
self.it = iter(l)
def next(self):
return make_translated_view(self.lang, self.it.next())
def __iter__(self):
return self
class ListTranslateView(collections.Sequence):
def __init__(self, lang, l):
self.lang=lang
self.proxified_list = l
def __len__(self):
return len(self.proxified_list)
def __getitem__(self, i):
return make_translated_view(self.lang, self.proxified_list[i])
def __iter__(self):
return ListTranslateViewIterator(self.lang, self.proxified_list)
def __str__(self):
return "[%s]" % (", ".join(["%s" % x for x in self]))
class DictTranslateViewIterator(object):
def __init__(self, lang, d):
self.lang = lang
self.proxified_dict = d
self.it = iter(d)
def next(self):
return make_translated_view(self.lang, self.it.next())
def __iter__(self):
return self
class DictTranslateView(collections.Mapping):
def __init__(self, lang, d):
self.lang=lang
self.proxified_dict = d
def __len__(self):
return len(self.proxified_dict)
def __getitem__(self, key):
if key in ('title', 'sub_title', 'text'):
try:
return self.proxified_dict[key][self.lang]
except(KeyError, TypeError):
# KeyError : when text is actually a dict (e.g. full-picture)
# TypeError : when value is a string and not a locales dict (monolingual case)
pass
return make_translated_view(self.lang, self.proxified_dict[key])
def __iter__(self):
return DictTranslateViewIterator(self.lang, self.proxified_dict)
def __str__(self):
return "{%s}" % (", ".join(["'%s': '%s'" % (k,v) for (k,v) in self.iteritems()]))
def test():
settings_monolang="""
title: Salut
sub_title: Ca roule ?
date: 2016-01-24
cover: truc.jpg
sections:
- type: full-picture
image: truc.jpg
text:
title: Un truc
sub_title: inutile
date: 2016-01-24
- type: text
text: Un autre truc
- type: pictures-group
images:
-
- truc.jpg
- truc.jpg
"""
settings_multilang="""
title:
fr: Salut
es: Ola
sub_title:
fr: Ca va ?
es: Que tal?
date: 2016-01-24
cover: truc.jpg
sections:
- type: full-picture
image: truc.jpg
text:
title:
fr: Un truc
es: Una cosa
sub_title:
fr: inutile
es: innecesaria
date: 2016-01-24
- type: text
text:
fr: Un autre truc
es: Una otra cosa
- type: pictures-group
images:
-
- truc.jpg
- truc.jpg
"""
pp = pprint.PrettyPrinter(indent=4)
mono = yaml.safe_load(settings_monolang)
pp.pprint(mono)
multi = yaml.safe_load(settings_multilang)
pp.pprint(multi)
# DONE
print 'should print french'
print DictTranslateView("fr", multi)
# DONE
print 'should print spanish'
print DictTranslateView("es", multi)
# DONE
print 'should print french because mono is french only'
print DictTranslateView("es", mono)
# DONE
print 'should print french because None is bad key'
print DictTranslateView(None, mono)
# TODO
print 'multi contains only fr and es translations, asking for en : undefined behavior ?'
print DictTranslateView("en", multi)
# TODO : unicode fails to print via str()...
settings_monolang_unicode="""
title: Ohé
sub_title: Ca roule ?
date: 2016-01-24
cover: truc.jpg
sections:
- type: full-picture
image: truc.jpg
text:
title: Un truc
sub_title: inutilisé
date: 2016-01-24
- type: text
text: Un autre truc
- type: pictures-group
images:
-
- truc.jpg
- truc.jpg
"""
d = DictTranslateView(None, yaml.safe_load(settings_monolang_unicode))
print d["title"] # OK
print d["sections"][0]["text"]["sub_title"] # OK
#print d # wtf ???
# TODO : unicode fails to print via str()...
settings_multilang_unicode="""
title:
fr: Ohé
es: Ola
sub_title:
fr: Ca va ?
es: Que tal?
date: 2016-01-24
cover: truc.jpg
sections:
- type: full-picture
image: truc.jpg
text:
title:
fr: Un truc
es: Una cosa
sub_title:
fr: inutilisé
es: innecesaria
date: 2016-01-24
- type: text
text:
fr: Un autre truc
es: Una otra cosa
- type: pictures-group
images:
-
- truc.jpg
- truc.jpg """
d = DictTranslateView("fr", yaml.safe_load(settings_multilang_unicode))
print d["title"] # OK
print d["sections"][0]["text"]["sub_title"] # OK
#print d # wtf ???
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment