Browse Source

Better options for the translation commands

sh4nks 8 years ago
parent
commit
42ca53679d
4 changed files with 75 additions and 31 deletions
  1. 2 1
      .travis.yml
  2. 4 1
      babel.cfg
  3. 49 19
      flaskbb/cli.py
  4. 20 10
      flaskbb/utils/translations.py

+ 2 - 1
.travis.yml

@@ -7,9 +7,10 @@ python:
 install:
 install:
   - "pip install -r requirements-dev.txt"
   - "pip install -r requirements-dev.txt"
   - "pip install coveralls"
   - "pip install coveralls"
+  - "pip install -e ."
 # command to run tests
 # command to run tests
 script:
 script:
-  - python manage.py compile_translations
+  - flaskbb translations compile
   - py.test --cov=flaskbb --cov-report=term-missing tests
   - py.test --cov=flaskbb --cov-report=term-missing tests
 after_success:
 after_success:
   - coveralls
   - coveralls

+ 4 - 1
babel.cfg

@@ -1,4 +1,7 @@
+[ignore: .tox/**]
+[ignore: .venv/**]
 [ignore: **/plugins/**]
 [ignore: **/plugins/**]
-[python: **.py]
+[ignore: **/node_modules/**]
+[python: **/flaskbb/**.py]
 [jinja2: **/templates/**.html]
 [jinja2: **/templates/**.html]
 extensions=jinja2.ext.autoescape,jinja2.ext.with_
 extensions=jinja2.ext.autoescape,jinja2.ext.with_

+ 49 - 19
flaskbb/cli.py

@@ -22,14 +22,16 @@ from flask_migrate import upgrade as upgrade_database
 
 
 from flaskbb import create_app
 from flaskbb import create_app
 from flaskbb._compat import iteritems
 from flaskbb._compat import iteritems
-from flaskbb.extensions import db, whooshee
+from flaskbb.extensions import db, whooshee, plugin_manager
 from flaskbb.utils.populate import (create_test_data, create_welcome_forum,
 from flaskbb.utils.populate import (create_test_data, create_welcome_forum,
                                     create_user, create_default_groups,
                                     create_user, create_default_groups,
                                     create_default_settings, insert_bulk_data,
                                     create_default_settings, insert_bulk_data,
                                     update_settings_from_fixture)
                                     update_settings_from_fixture)
 from flaskbb.utils.translations import (add_translations, compile_translations,
 from flaskbb.utils.translations import (add_translations, compile_translations,
                                         update_translations,
                                         update_translations,
-                                        add_plugin_translations)
+                                        add_plugin_translations,
+                                        compile_plugin_translations,
+                                        update_plugin_translations)
 
 
 _email_regex = r"[^@]+@[^@]+\.[^@]+"
 _email_regex = r"[^@]+@[^@]+\.[^@]+"
 
 
@@ -56,17 +58,17 @@ class EmailType(click.ParamType):
     supported values.  All of these values have to be strings.
     supported values.  All of these values have to be strings.
     See :ref:`choice-opts` for an example.
     See :ref:`choice-opts` for an example.
     """
     """
-    name = 'email'
+    name = "email"
 
 
     def convert(self, value, param, ctx):
     def convert(self, value, param, ctx):
         # Exact match
         # Exact match
         if re.match(_email_regex, value):
         if re.match(_email_regex, value):
             return value
             return value
         else:
         else:
-            self.fail(('invalid email: %s' % value), param, ctx)
+            self.fail(("invalid email: %s" % value), param, ctx)
 
 
     def __repr__(self):
     def __repr__(self):
-        return 'email'
+        return "email"
 
 
 
 
 @click.group(cls=FlaskGroup, create_app=create_app)
 @click.group(cls=FlaskGroup, create_app=create_app)
@@ -175,29 +177,57 @@ def translations():
               help="The plugin for which a language should be added.")
               help="The plugin for which a language should be added.")
 @click.argument("lang")
 @click.argument("lang")
 def new_translation(lang, plugin):
 def new_translation(lang, plugin):
-    """Adds a new language to the translations. 'lang' is the language code
-    of the language, like, 'de_AT'."""
+    """Adds a new language to the translations. "lang" is the language code
+    of the language, like, "de_AT"."""
     if plugin:
     if plugin:
+        if plugin not in plugin_manager.all_plugins:
+            raise FlaskBBCLIError("Plugin {} not found.".format(plugin),
+                                  fg="red")
         click.secho("[+] Adding new language {} for plugin {}..."
         click.secho("[+] Adding new language {} for plugin {}..."
-                    .format(lang, plugin), fg='cyan')
+                    .format(lang, plugin), fg="cyan")
         add_plugin_translations(plugin, lang)
         add_plugin_translations(plugin, lang)
     else:
     else:
-        click.secho("[+] Adding new language {}...".format(lang), fg='cyan')
+        click.secho("[+] Adding new language {}...".format(lang), fg="cyan")
         add_translations(lang)
         add_translations(lang)
 
 
 
 
 @translations.command("update")
 @translations.command("update")
-def update_translation():
+@click.option("is_all", "--all", "-a", default=True, is_flag=True,
+              help="Updates the plugin translations as well.")
+@click.option("--plugin", "-p", type=click.STRING,
+              help="The plugin for which the translations should be updated.")
+def update_translation(is_all, plugin):
     """Updates all translations."""
     """Updates all translations."""
-    click.secho("[+] Updating language files...", fg='cyan')
-    update_translations()
+    if plugin is not None:
+        if plugin not in plugin_manager.all_plugins:
+            raise FlaskBBCLIError("Plugin {} not found.".format(plugin),
+                                  fg="red")
+
+        click.secho("[+] Updating language files for plugin {}..."
+                    .format(plugin), fg="cyan")
+        update_plugin_translations(plugin)
+    else:
+        click.secho("[+] Updating language files...", fg="cyan")
+        update_translations(include_plugins=is_all)
 
 
 
 
 @translations.command("compile")
 @translations.command("compile")
-def compile_translation(lang):
+@click.option("is_all", "--all", "-a", default=True, is_flag=True,
+              help="Compiles the plugin translations as well.")
+@click.option("--plugin", "-p", type=click.STRING,
+              help="The plugin for which the translations should be compiled.")
+def compile_translation(is_all, plugin):
     """Compiles all translations."""
     """Compiles all translations."""
-    click.secho("[+] Compiling language files...", fg='cyan')
-    compile_translations()
+    if plugin is not None:
+        if plugin not in plugin_manager.all_plugins:
+            raise FlaskBBCLIError("Plugin {} not found.".format(plugin),
+                                  fg="red")
+        click.secho("[+] Compiling language files for plugin {}..."
+                    .format(plugin), fg="cyan")
+        compile_plugin_translations(plugin)
+    else:
+        click.secho("[+] Compiling language files...", fg="cyan")
+        compile_translations(include_plugins=is_all)
 
 
 
 
 @cli.group()
 @cli.group()
@@ -210,21 +240,21 @@ def plugins():
 @click.argument("plugin")
 @click.argument("plugin")
 def new_plugin(plugin):
 def new_plugin(plugin):
     """Creates a new plugin based on the plugin template."""
     """Creates a new plugin based on the plugin template."""
-    click.secho("[+] Creating new Plugin {}...".format(plugin), fg='cyan')
+    click.secho("[+] Creating new Plugin {}...".format(plugin), fg="cyan")
 
 
 
 
 @plugins.command("install")
 @plugins.command("install")
 @click.argument("plugin")
 @click.argument("plugin")
 def install_plugin(plugin):
 def install_plugin(plugin):
-    """Installs a new plugin from FlaskBB's Plugin Repository."""
-    click.secho("[+] Installing plugin {}...".format(plugin), fg='cyan')
+    """Installs a new plugin from FlaskBB"s Plugin Repository."""
+    click.secho("[+] Installing plugin {}...".format(plugin), fg="cyan")
 
 
 
 
 @plugins.command("uninstall")
 @plugins.command("uninstall")
 @click.argument("plugin")
 @click.argument("plugin")
 def uninstall_plugin(plugin):
 def uninstall_plugin(plugin):
     """Uninstalls a plugin from FlaskBB."""
     """Uninstalls a plugin from FlaskBB."""
-    click.secho("[+] Uninstalling plugin {}...".format(plugin), fg='cyan')
+    click.secho("[+] Uninstalling plugin {}...".format(plugin), fg="cyan")
 
 
 
 
 @plugins.command("list")
 @plugins.command("list")

+ 20 - 10
flaskbb/utils/translations.py

@@ -78,8 +78,12 @@ class FlaskBBDomain(Domain):
         return translations
         return translations
 
 
 
 
-def update_translations():
-    """Updates all translations."""
+def update_translations(include_plugins=False):
+    """Updates all translations.
+
+    :param include_plugins: If set to `True` it will also update the
+                            translations for all plugins.
+    """
 
 
     # update flaskbb translations
     # update flaskbb translations
     translations_folder = os.path.join(current_app.root_path, "translations")
     translations_folder = os.path.join(current_app.root_path, "translations")
@@ -90,9 +94,10 @@ def update_translations():
     subprocess.call(["pybabel", "update", "-i", source_file,
     subprocess.call(["pybabel", "update", "-i", source_file,
                      "-d", translations_folder])
                      "-d", translations_folder])
 
 
-    # updates all plugin translations too
-    for plugin in plugin_manager.all_plugins:
-        update_plugin_translations(plugin)
+    if include_plugins:
+        # updates all plugin translations too
+        for plugin in plugin_manager.all_plugins:
+            update_plugin_translations(plugin)
 
 
 
 
 def add_translations(translation):
 def add_translations(translation):
@@ -107,16 +112,21 @@ def add_translations(translation):
                      "-d", translations_folder, "-l", translation])
                      "-d", translations_folder, "-l", translation])
 
 
 
 
-def compile_translations():
-    """Compiles all translations."""
+def compile_translations(include_plugins=False):
+    """Compiles all translations.
+
+    :param include_plugins: If set to `True` it will also compile the
+                            translations for all plugins.
+    """
 
 
     # compile flaskbb translations
     # compile flaskbb translations
     translations_folder = os.path.join(current_app.root_path, "translations")
     translations_folder = os.path.join(current_app.root_path, "translations")
     subprocess.call(["pybabel", "compile", "-d", translations_folder])
     subprocess.call(["pybabel", "compile", "-d", translations_folder])
 
 
-    # compile all plugin translations
-    for plugin in plugin_manager.all_plugins:
-        compile_plugin_translations(plugin)
+    if include_plugins:
+        # compile all plugin translations
+        for plugin in plugin_manager.all_plugins:
+            compile_plugin_translations(plugin)
 
 
 
 
 def add_plugin_translations(plugin, translation):
 def add_plugin_translations(plugin, translation):