# Path: styles/models.py
# class LegendItemName(models.Model):
#     __metaclass__ = TransMeta
# 
#     slug = models.SlugField(max_length=200, unique=True)
#     name = models.CharField(verbose_name=_('name'), max_length=200, null=True, blank=True)
#     group = models.CharField(max_length=200, null=True, blank=True)
#     order = models.PositiveIntegerField(null=True, blank=True)
#     image = models.ImageField(verbose_name=_('image'), upload_to='legend/', height_field='height',
#                               width_field='width', null=True, blank=True)
#     image_highres = models.ImageField(verbose_name=_('highres image'), upload_to='legend/',
#                                       height_field='height_highres', width_field='width_highres',
#                                       null=True, blank=True)
#     height = models.PositiveIntegerField(null=True, blank=True)
#     width = models.PositiveIntegerField(null=True, blank=True)
#     height_highres = models.PositiveIntegerField(null=True, blank=True)
#     width_highres = models.PositiveIntegerField(null=True, blank=True)
# 
#     class Meta:
#         translate = ('name', 'image', 'image_highres',)
# 
#     def __unicode__(self):
#         return self.slug
# 
#     def render_names(self, font_size=12, scale_factor=1):
#         font_size *= scale_factor
#         height = font_size + font_size/2
#         width = max(font_size*len(self.name)*2/3, 150)
#         for code in LANG_CODES:
#             activate(code)
#             image = getattr(self, 'image_%s' % code)
#             image_highres = getattr(self, 'image_highres_%s' % code)
#             filename = 'name_%s_%s.png' % (code, self.slug)
#             if scale_factor >= 2:
#                 filename = 'highres_%s' % filename
#             directory = 'media/legend'
#             tmppath = os.path.join(directory, 'tmp', filename)
#             path = os.path.join(directory, filename)
#             fo = file(tmppath + '.svg', 'w')
#             fo.write('<?xml version="1.0" encoding="UTF-8"?>\n')
#             fo.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
#                      '"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n')
#             fo.write('<svg width="%i" height="%i" xmlns="http://www.w3.org/2000/svg">\n' % (width, height))
#             fo.write('    <text fill="black" text-anchor="left" font-family="Dejavu Sans" x="%i" y="%i" font-size="%i"'
#                      ' >%s</text>' % (2, height-font_size/2, font_size, self.name.encode('utf-8')))
#             fo.write('</svg>')
#             fo.close()
#             system("rsvg-convert -o %s %s" % (tmppath, tmppath + '.svg'))
#             remove(tmppath + '.svg')
#             if scale_factor >= 2:
#                 if image_highres:
#                     if os.path.exists(image_highres.path):
#                         remove(image_highres.path)
#                     image_highres.delete()
#                 image_highres.save(path, File(open(tmppath)))
#             else:
#                 if image:
#                     if os.path.exists(image.path):
#                         remove(image.path)
#                     image.delete()
#                 image.save(path, File(open(tmppath)))
#             self.save()
#             remove(tmppath)


import csv
import logging
from django.conf import settings
from styles.models import LegendItemName
# Django imports

# Local imports

LANG_CODES = [code for code, name in settings.LANGUAGES]

logger = logging.getLogger(__name__)


def dump_translation_file(lang_code):
    filename = _get_locale_filename(lang_code)
    fields = ['slug', 'name_en', 'name_%s' % lang_code]
    _write_csv(filename, fields)


def load_translation_file(lang_code):
    filename = _get_locale_filename(lang_code)
    name_field = 'name_%s' % lang_code
    with open(filename, 'r') as f:
        reader = csv.DictReader(f)
        line = 2
        for row_dict in reader:
            new_value = row_dict[name_field]
            if new_value:
                try:
                    lin = LegendItemName.objects.get(slug=row_dict['slug'])
                    if getattr(lin, name_field) != new_value:
                        setattr(lin, name_field, new_value)
                        lin.save()
                        logger.info('Updating LegendItemName(slug=%s), %s = %s' % (row_dict['slug'], name_field, new_value))

# Path: styles/models.py
# class LegendItemName(models.Model):
#     __metaclass__ = TransMeta
# 
#     slug = models.SlugField(max_length=200, unique=True)
#     name = models.CharField(verbose_name=_('name'), max_length=200, null=True, blank=True)
#     group = models.CharField(max_length=200, null=True, blank=True)
#     order = models.PositiveIntegerField(null=True, blank=True)
#     image = models.ImageField(verbose_name=_('image'), upload_to='legend/', height_field='height',
#                               width_field='width', null=True, blank=True)
#     image_highres = models.ImageField(verbose_name=_('highres image'), upload_to='legend/',
#                                       height_field='height_highres', width_field='width_highres',
#                                       null=True, blank=True)
#     height = models.PositiveIntegerField(null=True, blank=True)
#     width = models.PositiveIntegerField(null=True, blank=True)
#     height_highres = models.PositiveIntegerField(null=True, blank=True)
#     width_highres = models.PositiveIntegerField(null=True, blank=True)
# 
#     class Meta:
#         translate = ('name', 'image', 'image_highres',)
# 
#     def __unicode__(self):
#         return self.slug
# 
#     def render_names(self, font_size=12, scale_factor=1):
#         font_size *= scale_factor
#         height = font_size + font_size/2
#         width = max(font_size*len(self.name)*2/3, 150)
#         for code in LANG_CODES:
#             activate(code)
#             image = getattr(self, 'image_%s' % code)
#             image_highres = getattr(self, 'image_highres_%s' % code)
#             filename = 'name_%s_%s.png' % (code, self.slug)
#             if scale_factor >= 2:
#                 filename = 'highres_%s' % filename
#             directory = 'media/legend'
#             tmppath = os.path.join(directory, 'tmp', filename)
#             path = os.path.join(directory, filename)
#             fo = file(tmppath + '.svg', 'w')
#             fo.write('<?xml version="1.0" encoding="UTF-8"?>\n')
#             fo.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
#                      '"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n')
#             fo.write('<svg width="%i" height="%i" xmlns="http://www.w3.org/2000/svg">\n' % (width, height))
#             fo.write('    <text fill="black" text-anchor="left" font-family="Dejavu Sans" x="%i" y="%i" font-size="%i"'
#                      ' >%s</text>' % (2, height-font_size/2, font_size, self.name.encode('utf-8')))
#             fo.write('</svg>')
#             fo.close()
#             system("rsvg-convert -o %s %s" % (tmppath, tmppath + '.svg'))
#             remove(tmppath + '.svg')
#             if scale_factor >= 2:
#                 if image_highres:
#                     if os.path.exists(image_highres.path):
#                         remove(image_highres.path)
#                     image_highres.delete()
#                 image_highres.save(path, File(open(tmppath)))
#             else:
#                 if image:
#                     if os.path.exists(image.path):
#                         remove(image.path)
#                     image.delete()
#                 image.save(path, File(open(tmppath)))
#             self.save()
#             remove(tmppath)


# Path: styles/translation.py
import csv
import logging
from django.conf import settings
from styles.models import LegendItemName

# Django imports

# Local imports

LANG_CODES = [code for code, name in settings.LANGUAGES]

logger = logging.getLogger(__name__)


def dump_translation_file(lang_code):
    filename = _get_locale_filename(lang_code)
    fields = ['slug', 'name_en', 'name_%s' % lang_code]
    _write_csv(filename, fields)


def load_translation_file(lang_code):
    filename = _get_locale_filename(lang_code)
    name_field = 'name_%s' % lang_code
    with open(filename, 'r') as f:
        reader = csv.DictReader(f)
        line = 2
        for row_dict in reader:
            new_value = row_dict[name_field]
            if new_value:
                try:
                    lin = LegendItemName.objects.get(slug=row_dict['slug'])
                    if getattr(lin, name_field) != new_value:
                        setattr(lin, name_field, new_value)
                        lin.save()
                        logger.info('Updating LegendItemName(slug=%s), %s = %s' % (row_dict['slug'], name_field, new_value))

                except LegendItemName.DoesNotExist: