From db1fb81f6dc645a833a063817636804fbfddbcfd Mon Sep 17 00:00:00 2001 From: Tatarize Date: Thu, 20 Apr 2023 17:11:59 -0700 Subject: [PATCH] Split off minor sections of main package --- setup.cfg | 4 +- src/assets.py | 23 ++++++ src/plugin.py | 168 ++++++++++++++++++++++++++++++++++++++++++++ src/poboy.py | 189 +------------------------------------------------- 4 files changed, 195 insertions(+), 189 deletions(-) create mode 100644 src/assets.py create mode 100644 src/plugin.py diff --git a/setup.cfg b/setup.cfg index 3873b16..b8ccb2f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = poboy -version = 0.0.1 +version = 0.1.0 description = POboy long_description_content_type=text/markdown long_description = file: README.md @@ -36,4 +36,4 @@ universal=1 [options.entry_points] console_scripts = poboy = src.poboy:run console_scripts = babelmsg = src.babelmsg.frontend:main -meerk40t.plugins = Poboy = src.poboy:plugin +meerk40t.plugins = Poboy = src.plugin:plugin diff --git a/src/assets.py b/src/assets.py new file mode 100644 index 0000000..921290c --- /dev/null +++ b/src/assets.py @@ -0,0 +1,23 @@ +from wx.lib.embeddedimage import PyEmbeddedImage + +icons8_translation_50 = PyEmbeddedImage( + b"iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAABmJLR0QA/wD/AP+gvaeTAAAD" + b"oklEQVRogd2ay0sVURzHP+bNWrgpuPdKLYKeG20jImQRBZdoERTSwmiT1CK0FkGt7WlRQi2K" + b"3Bb9ARIu06VEi8RKe1EE3gjKIGtRRt4W8xtnmjsz5zEzV/MLB+ae8zvf7/nOnDmvubDM0ABc" + b"Az4ClRqlMtAv2jpoAnpUQVdraCCYrmiamJL43rjAsgTt0CBNCx14TyYOfhNT8jsS7t2pNVS6" + b"ReC5xLwE1iUlzApxusYmVIRZIkq3AEyi2Z0AVqTbrsVHkieSVd0CXtd6RQ26VpZ1jc3EEaY1" + b"X9gYMTazlI2AwYSY5qi1EvgufEVFrImu1hJFl3A70IrT2Ci4M/a0Bl/qw74u4TOJa42JGZKY" + b"GynqakOX8LbE3Y8o3yflP4H1KepqQ5dwK/BLYk8EyjYCX6SsL2VdbZgQnpLYeeA8kAM2AG8k" + b"fxSo1+Ap+nRtUuh+xvTOnBUjFeAJ8EGunwJrNTnywERCMxUC+xmbR7wf+Oar+wc4DNQp6hWA" + b"ks+MaoiOQuh+xtRIHnjgq/fbdz0JnCF8QdoIvMB5z0oh5aaoareukRxwEvgs8T+A0zjd6Rxe" + b"F3sdw9GHY6Zg2uoQGBupB7rw9ggVYATYFIjLAQeAo4H8PDCA92I22rQ6BMZG2n0xb4FOQ8FH" + b"UnfAsJ4KVl3rHnCM+OVJFFpwzOQD+TlgjaScBa/1O2KCAk7jW2Jien3aygVhCGpi5DreuxSF" + b"cbz5aNxCI1Mj7oucwzETNU+0ieYQ8F6u2wy1MjPiztY6K99B0ewELsn1XUO9zIyUcCa7CeKH" + b"2EZgFvgKrAK2if6sol4QVe12j0w7DEj8KOKNSCWqR6cgukVv0Jf3WPK6DXSrjPT7Mm3ThIYB" + b"F2NSZ5cvzx3BxpIYaRAzZZKZ0VkANkvsO/5dYOaBOSlrtjWSFCaEtyT2QkjZQym7mYFuqoSr" + b"gRmJbceb1d10XMpmJDYtXW3oEh5Bv5t2meiqNkG6cE2o+EaAPTjb4tmImCacg4tRYG9S3R40" + b"jvIDhKonshlnOTJH/Oi2W7jmpY61rjsMan2X0CEUuEP8kCKuDm/J0p9E1+Y4X2Ukh/fFWGcv" + b"c1FiPxG/bVDeQFMzKsJDUu4uSVTYgrcqPphAFzAzoyIclvI7KlEf3Nl/OIHuAvzH+ZNEHxik" + b"Pp5rYkF32X1DDIPJ5+FFfyJRMP3GvSSNGP1lQpB0P2ODnaIZ+UHJZkJMYz9jmy7HNcx0iZLW" + b"fsYkTYsJ3b9J/R/4C673TQnRnTmBAAAAAElFTkSuQmCC" +) diff --git a/src/plugin.py b/src/plugin.py new file mode 100644 index 0000000..a57ff5a --- /dev/null +++ b/src/plugin.py @@ -0,0 +1,168 @@ +import sys + +import re + +from src.poboy import PoboyWindow + + +def plugin(kernel, lifecycle): + if getattr(sys, "frozen", False): + # This plugin is source only. + return + if lifecycle == "register": + context = kernel.root + _ = kernel.translation + + @context.console_command("locale", output_type="locale", hidden=True) + def locale(channel, _, **kwargs): + return "locale", "en" + + @context.console_command( + "generate", input_type="locale", output_type="locale", hidden=True + ) + def generate_locale(channel, _, data=None, **kwargs): + return "locale", data + + @context.console_argument("locale", help="locale use for these opeations") + @context.console_command( + "change", input_type="locale", output_type="locale", hidden=True + ) + def change_locale(channel, _, data=None, locale=None, **kwargs): + if locale is None: + raise SyntaxError + channel("locale changed from %s to %s" % (data, locale)) + return "locale", locale + + @context.console_command("update", input_type="locale", hidden=True) + def update_locale(channel, _, data=None, **kwargs): + """ + This script updates the message.po structure with the original translation information. + + @param channel: + @param _: + @param data: + @param kwargs: + @return: + """ + if data == "en": + channel( + "Cannot update English since it is the default language and has no file" + ) + keys = dict() + translations = open( + "./locale/%s/LC_MESSAGES/meerk40t.po" % data, "r", encoding="utf-8" + ) + + file_lines = translations.readlines() + key = None + index = 0 + translation_header = [] + while index < len(file_lines): + # Header is defined as the first batch of uninterrupted lines in the file. + try: + if file_lines[index]: + translation_header.append(file_lines[index]) + else: + break + index += 1 + except IndexError: + break + + while index < len(file_lines): + try: + # Find msgid and all multi-lined message ids + if re.match('msgid "(.*)"', file_lines[index]): + m = re.match('msgid "(.*)"', file_lines[index]) + key = m.group(1) + index += 1 + if index >= len(file_lines): + break + while re.match('^"(.*)"$', file_lines[index]): + m = re.match('^"(.*)"$', file_lines[index]) + key += m.group(1) + index += 1 + + # find all message strings and all multi-line message strings + if re.match('msgstr "(.*)"', file_lines[index]): + m = re.match('msgstr "(.*)"', file_lines[index]) + value = [file_lines[index]] + if len(key) > 0: + keys[key] = value + index += 1 + while re.match('^"(.*)"$', file_lines[index]): + value.append(file_lines[index]) + if len(key) > 0: + keys[key] = value + index += 1 + index += 1 + except IndexError: + break + + template = open("./locale/messages.po", "r", encoding="utf-8") + lines = [] + + file_lines = list(template.readlines()) + index = 0 + template_header = [] + while index < len(file_lines): + # Header is defined as the first batch of uninterrupted lines in the file. + # We read the template header but do not use them. + try: + if file_lines[index]: + template_header.append(file_lines[index]) + else: + break + index += 1 + except IndexError: + break + + # Lines begins with the translation's header information. + lines.extend(translation_header) + while index < len(file_lines): + try: + # Attempt to locate message id + if re.match('msgid "(.*)"', file_lines[index]): + lines.append(file_lines[index]) + m = re.match('msgid "(.*)"', file_lines[index]) + key = m.group(1) + index += 1 + while re.match('^"(.*)"$', file_lines[index]): + lines.append(file_lines[index]) + key += m.group(1) + index += 1 + except IndexError: + pass + try: + # Attempt to locate message string + if re.match('msgstr "(.*)"', file_lines[index]): + if key in keys: + lines.extend(keys[key]) + index += 1 + while re.match('^"(.*)"$', file_lines[index]): + index += 1 + else: + lines.append(file_lines[index]) + index += 1 + while re.match('^"(.*)"$', file_lines[index]): + lines.append(file_lines[index]) + index += 1 + except IndexError: + pass + try: + # We append any line if it wasn't fully read by msgid and msgstr readers. + lines.append(file_lines[index]) + index += 1 + except IndexError: + break + + filename = "meerk40t.update" + channel("writing %s" % filename) + import codecs + + template = codecs.open(filename, "w", "utf8") + template.writelines(lines) + + try: + kernel.register("window/Translate", PoboyWindow) + except NameError: + pass diff --git a/src/poboy.py b/src/poboy.py index 36b7bbd..78ad1e6 100644 --- a/src/poboy.py +++ b/src/poboy.py @@ -9,8 +9,9 @@ import wx from babelmsg import pofile, mofile, extract, Catalog -from wx.lib.embeddedimage import PyEmbeddedImage from difflib import get_close_matches +from assets import icons8_translation_50 + PUNCTUATION = (".", "?", "!", ":", ";") TEMPLATE = "" @@ -32,192 +33,6 @@ ) -icons8_translation_50 = PyEmbeddedImage( - b"iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAABmJLR0QA/wD/AP+gvaeTAAAD" - b"oklEQVRogd2ay0sVURzHP+bNWrgpuPdKLYKeG20jImQRBZdoERTSw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CKuDm/J0p9E1+Y4X2Ukh/fFWGcv" - b"c1FiPxG/bVDeQFMzKsJDUu4uSVTYgrcqPphAFzAzoyIclvI7KlEf3Nl/OIHuAvzH+ZNEHxik" - b"Pp5rYkF32X1DDIPJ5+FFfyJRMP3GvSSNGP1lQpB0P2ODnaIZ+UHJZkJMYz9jmy7HNcx0iZLW" - b"fsYkTYsJ3b9J/R/4C673TQnRnTmBAAAAAElFTkSuQmCC" -) - - -def plugin(kernel, lifecycle): - if getattr(sys, "frozen", False): - # This plugin is source only. - return - if lifecycle == "register": - context = kernel.root - _ = kernel.translation - - @context.console_command("locale", output_type="locale", hidden=True) - def locale(channel, _, **kwargs): - return "locale", "en" - - @context.console_command( - "generate", input_type="locale", output_type="locale", hidden=True - ) - def generate_locale(channel, _, data=None, **kwargs): - return "locale", data - - @context.console_argument("locale", help="locale use for these opeations") - @context.console_command( - "change", input_type="locale", output_type="locale", hidden=True - ) - def change_locale(channel, _, data=None, locale=None, **kwargs): - if locale is None: - raise SyntaxError - channel("locale changed from %s to %s" % (data, locale)) - return "locale", locale - - @context.console_command("update", input_type="locale", hidden=True) - def update_locale(channel, _, data=None, **kwargs): - """ - This script updates the message.po structure with the original translation information. - - @param channel: - @param _: - @param data: - @param kwargs: - @return: - """ - if data == "en": - channel( - "Cannot update English since it is the default language and has no file" - ) - keys = dict() - translations = open( - "./locale/%s/LC_MESSAGES/meerk40t.po" % data, "r", encoding="utf-8" - ) - - file_lines = translations.readlines() - key = None - index = 0 - translation_header = [] - while index < len(file_lines): - # Header is defined as the first batch of uninterrupted lines in the file. - try: - if file_lines[index]: - translation_header.append(file_lines[index]) - else: - break - index += 1 - except IndexError: - break - - while index < len(file_lines): - try: - # Find msgid and all multi-lined message ids - if re.match('msgid "(.*)"', file_lines[index]): - m = re.match('msgid "(.*)"', file_lines[index]) - key = m.group(1) - index += 1 - if index >= len(file_lines): - break - while re.match('^"(.*)"$', file_lines[index]): - m = re.match('^"(.*)"$', file_lines[index]) - key += m.group(1) - index += 1 - - # find all message strings and all multi-line message strings - if re.match('msgstr "(.*)"', file_lines[index]): - m = re.match('msgstr "(.*)"', file_lines[index]) - value = [file_lines[index]] - if len(key) > 0: - keys[key] = value - index += 1 - while re.match('^"(.*)"$', file_lines[index]): - value.append(file_lines[index]) - if len(key) > 0: - keys[key] = value - index += 1 - index += 1 - except IndexError: - break - - template = open("./locale/messages.po", "r", encoding="utf-8") - lines = [] - - file_lines = list(template.readlines()) - index = 0 - template_header = [] - while index < len(file_lines): - # Header is defined as the first batch of uninterrupted lines in the file. - # We read the template header but do not use them. - try: - if file_lines[index]: - template_header.append(file_lines[index]) - else: - break - index += 1 - except IndexError: - break - - # Lines begins with the translation's header information. - lines.extend(translation_header) - while index < len(file_lines): - try: - # Attempt to locate message id - if re.match('msgid "(.*)"', file_lines[index]): - lines.append(file_lines[index]) - m = re.match('msgid "(.*)"', file_lines[index]) - key = m.group(1) - index += 1 - while re.match('^"(.*)"$', file_lines[index]): - lines.append(file_lines[index]) - key += m.group(1) - index += 1 - except IndexError: - pass - try: - # Attempt to locate message string - if re.match('msgstr "(.*)"', file_lines[index]): - if key in keys: - lines.extend(keys[key]) - index += 1 - while re.match('^"(.*)"$', file_lines[index]): - index += 1 - else: - lines.append(file_lines[index]) - index += 1 - while re.match('^"(.*)"$', file_lines[index]): - lines.append(file_lines[index]) - index += 1 - except IndexError: - pass - try: - # We append any line if it wasn't fully read by msgid and msgstr readers. - lines.append(file_lines[index]) - index += 1 - except IndexError: - break - - filename = "meerk40t.update" - channel("writing %s" % filename) - import codecs - - template = codecs.open(filename, "w", "utf8") - template.writelines(lines) - - try: - kernel.register("window/Translate", PoboyWindow) - except NameError: - pass - - def save(catalog, filename=None, write_mo=True): if filename is None: filename = catalog.filename