diff options
Diffstat (limited to 'lib/inx')
| -rw-r--r-- | lib/inx/__init__.py | 1 | ||||
| -rwxr-xr-x | lib/inx/extensions.py | 43 | ||||
| -rw-r--r-- | lib/inx/generate.py | 11 | ||||
| -rwxr-xr-x | lib/inx/inputs.py | 18 | ||||
| -rw-r--r-- | lib/inx/outputs.py | 18 | ||||
| -rw-r--r-- | lib/inx/utils.py | 52 |
6 files changed, 143 insertions, 0 deletions
diff --git a/lib/inx/__init__.py b/lib/inx/__init__.py new file mode 100644 index 00000000..32b8bfae --- /dev/null +++ b/lib/inx/__init__.py @@ -0,0 +1 @@ +from generate import generate_inx_files diff --git a/lib/inx/extensions.py b/lib/inx/extensions.py new file mode 100755 index 00000000..d1a0c7f3 --- /dev/null +++ b/lib/inx/extensions.py @@ -0,0 +1,43 @@ +import pyembroidery + +from .utils import build_environment, write_inx_file +from .outputs import pyembroidery_output_formats +from ..extensions import extensions, Input, Output +from ..commands import LAYER_COMMANDS, OBJECT_COMMANDS, GLOBAL_COMMANDS, COMMANDS + + +def layer_commands(): + # We purposefully avoid using commands.get_command_description() here. We + # want to call _() on the description inside the actual template so that + # we use the translation language selected in build_environment(). + return [(command, COMMANDS[command]) for command in LAYER_COMMANDS] + + +def global_commands(): + return [(command, COMMANDS[command]) for command in GLOBAL_COMMANDS] + + +def object_commands(): + return [(command, COMMANDS[command]) for command in OBJECT_COMMANDS] + + +def pyembroidery_debug_formats(): + for format in pyembroidery.supported_formats(): + if 'writer' in format and format['category'] != 'embroidery': + yield format['extension'], format['description'] + + +def generate_extension_inx_files(): + env = build_environment() + + for extension in extensions: + if extension is Input or extension is Output: + continue + + name = extension.name() + template = env.get_template('%s.inx' % name) + write_inx_file(name, template.render(formats=pyembroidery_output_formats(), + debug_formats=pyembroidery_debug_formats(), + layer_commands=layer_commands(), + object_commands=object_commands(), + global_commands=global_commands())) diff --git a/lib/inx/generate.py b/lib/inx/generate.py new file mode 100644 index 00000000..941596de --- /dev/null +++ b/lib/inx/generate.py @@ -0,0 +1,11 @@ +from .inputs import generate_input_inx_files +from .outputs import generate_output_inx_files +from .extensions import generate_extension_inx_files +from .utils import iterate_inx_locales + + +def generate_inx_files(): + for locale in iterate_inx_locales(): + generate_input_inx_files() + generate_output_inx_files() + generate_extension_inx_files() diff --git a/lib/inx/inputs.py b/lib/inx/inputs.py new file mode 100755 index 00000000..d40ffeaf --- /dev/null +++ b/lib/inx/inputs.py @@ -0,0 +1,18 @@ +import pyembroidery + +from .utils import build_environment, write_inx_file + + +def pyembroidery_input_formats(): + for format in pyembroidery.supported_formats(): + if 'reader' in format and format['category'] == 'embroidery': + yield format['extension'], format['description'] + + +def generate_input_inx_files(): + env = build_environment() + template = env.get_template('input.inx') + + for format, description in pyembroidery_input_formats(): + name = "input_%s" % format.upper() + write_inx_file(name, template.render(format=format, description=description)) diff --git a/lib/inx/outputs.py b/lib/inx/outputs.py new file mode 100644 index 00000000..aef0c8b5 --- /dev/null +++ b/lib/inx/outputs.py @@ -0,0 +1,18 @@ +import pyembroidery + +from .utils import build_environment, write_inx_file + + +def pyembroidery_output_formats(): + for format in pyembroidery.supported_formats(): + if 'writer' in format and format['category'] == 'embroidery': + yield format['extension'], format['description'] + + +def generate_output_inx_files(): + env = build_environment() + template = env.get_template('output.inx') + + for format, description in pyembroidery_output_formats(): + name = "output_%s" % format.upper() + write_inx_file(name, template.render(format=format, description=description)) diff --git a/lib/inx/utils.py b/lib/inx/utils.py new file mode 100644 index 00000000..54b37c58 --- /dev/null +++ b/lib/inx/utils.py @@ -0,0 +1,52 @@ +import os +import gettext +from os.path import dirname +from jinja2 import Environment, FileSystemLoader + +from ..i18n import translation as default_translation, locale_dir, N_ + + +_top_path = dirname(dirname(dirname(os.path.realpath(__file__)))) +inx_path = os.path.join(_top_path, "inx") +template_path = os.path.join(_top_path, "templates") + +current_translation = default_translation +current_locale = "en_US" + + +def build_environment(): + env = Environment( + loader=FileSystemLoader(template_path), + autoescape=True, + extensions=['jinja2.ext.i18n'] + ) + + env.install_gettext_translations(current_translation) + env.globals["locale"] = current_locale + + return env + + +def write_inx_file(name, contents): + inx_file_name = "inkstitch_%s_%s.inx" % (name, current_locale) + with open(os.path.join(inx_path, inx_file_name), 'w') as inx_file: + print >> inx_file, contents.encode("utf-8") + + +def iterate_inx_locales(): + global current_translation, current_locale + + locales = sorted(os.listdir(locale_dir)) + for locale in locales: + translation = gettext.translation("inkstitch", locale_dir, languages=[locale], fallback=True) + + # L10N If you translate this string, that will tell Ink/Stitch to + # generate menu items for this language in Inkscape's "Extensions" + # menu. + magic_string = N_("Generate INX files") + translated_magic_string = translation.gettext(magic_string) + + if translated_magic_string != magic_string or locale == "en_US": + current_translation = translation + current_locale = locale + yield locale |
