diff --git a/kalite/distributed/management/commands/screenshots.py b/kalite/distributed/management/commands/screenshots.py index 908a976d43..c7f34d00f3 100644 --- a/kalite/distributed/management/commands/screenshots.py +++ b/kalite/distributed/management/commands/screenshots.py @@ -1,10 +1,8 @@ -import errno +#TODO-BLOCKER(MCGallaspy): Better Exception handling in this file. import glob import json import os -import re from optparse import make_option -from selenium import webdriver from selenium.webdriver.common.keys import Keys from django.conf import settings @@ -18,7 +16,8 @@ from fle_utils.general import ensure_dir from kalite.testing.base import KALiteBrowserTestCase -from kalite.testing.mixins import FacilityMixins, BrowserActionMixins +from kalite.testing.mixins.facility_mixins import FacilityMixins +from kalite.testing.mixins.browser_mixins import BrowserActionMixins from kalite.distributed.management.commands.katest import unregister_distributed_server USER_TYPE_ADMIN = "admin" @@ -56,6 +55,11 @@ class Command(BaseCommand): dest='no_del', default=None, help='Don\'t delete existing screenshots.'), + make_option('--lang', + action='store', + dest='language', + default=None, + help='Specify the language of the session, set by the "set_default_language" api endpoint.'), ) def handle(self, *args, **options): @@ -210,10 +214,32 @@ def __init__(self, *args, **kwargs): self.browser.set_window_size(1024, 768) self.browser.implicitly_wait(15) + # After initializing the server (with setUp) and a browser, set the language + self.set_session_language(kwargs['language']) + self.loginfo("==> Browser %s successfully setup with live_server_url %s." % (self.browser.name, self.live_server_url,)) self.loginfo("==> Saving screenshots to %s ..." % (settings.SCREENSHOTS_OUTPUT_PATH,)) + + def set_session_language(self, lang_code): + """ Uses the "set_default_language" api endpoint to set the language for the session. + The language pack should already be downloaded, or the behavior is undefined. + TODO: Handle the case when the language pack is not downloaded. + + :param lang_code: A string with the language code or None. Value None is a no-op + """ + if not lang_code: + return + self.browser.get(self.live_server_url + reverse("homepage")) + self.browser_wait_for_js_object_exists("$") + data = json.dumps({"lang": lang_code}) + self.browser.execute_script("window.SUCCESS=false; $.ajax({type: \"POST\", url: \"%s\", data: '%s', contentType: \"application/json\", success: function(){window.SUCCESS=true}})" % (reverse("set_default_language"), data)) + self.browser_wait_for_js_condition("window.SUCCESS") + # Ensure the changes are loaded + self.browser.get(self.live_server_url + reverse("homepage")) + + def validate_json_keys(self, shot): """ Validates a json item if keys are valid or not, raises an exception if keys are found. diff --git a/kalitectl.py b/kalitectl.py index d22ef93b22..9c3d156cce 100644 --- a/kalitectl.py +++ b/kalitectl.py @@ -63,7 +63,8 @@ ] + sys.path # KALITE_DIR not set, so called from some other source else: - sys.path = ['python-packages', 'kalite'] + sys.path + filedir = os.path.dirname(__file__) + sys.path = [os.path.join(filedir, 'python-packages'), os.path.join(filedir, 'kalite')] + sys.path from django.core.management import ManagementUtility, get_commands diff --git a/sphinx-docs/README.md b/sphinx-docs/README.md index 745714d274..0563e54946 100644 --- a/sphinx-docs/README.md +++ b/sphinx-docs/README.md @@ -21,9 +21,18 @@ Then, install following python packages: pip install sphinx ``` -## Builiding Documentation +## Building Documentation Change your current working directory to `sphinx-docs` directory under `ka-lite` root and execute following command: ``` make html ``` If the above command executes successfully, docs should be found under `sphinx-docs/_build` directory inside `ka-lite`. + +## Building translated docs +1. Make sure you have the sphinx-intl dependency (run the command `pip install -r requirements` in this directory). +2. Extract the translatable messages into pot files using the command `make gettext`. The pot files are then found in `_build/locale directory`. +3. Setup po files for your target language with the command `sphinx-intl update -p _build/locale -l xx`, where xx is the language code of your target language (i.e. "de" or "eo"). (Note: running the `update_pot` command from the central server will generate po files and grab them for uploading.) +4. Translate the po files in `locale/xx/LC_MESSAGES`. TODO: Automate this. +5. Build the mo files with `sphinx-intl build` +6. Make the docs in the target language with the command `make -e SPHINXOPTS="-Dlanguage='xx'" html` +7. Party time! diff --git a/sphinx-docs/conf.py b/sphinx-docs/conf.py index 6b4ac5c829..ae785e76a9 100644 --- a/sphinx-docs/conf.py +++ b/sphinx-docs/conf.py @@ -288,3 +288,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = {'http://docs.python.org/': None} +# sphinx-intl options +locale_dirs = ['locale/'] +gettext_compact = False diff --git a/sphinx-docs/requirements.txt b/sphinx-docs/requirements.txt index 0bd349f553..6b2415330d 100644 --- a/sphinx-docs/requirements.txt +++ b/sphinx-docs/requirements.txt @@ -1,2 +1,6 @@ +sphinx +sphinx-intl +# Required for sphinx-intl in python 2.6 +ordereddict # Required by screenshots.py to take screenshots in headless manner pyvirtualdisplay diff --git a/sphinx-docs/screenshot.py b/sphinx-docs/screenshot.py index a42e9ab6d1..f9a921c537 100644 --- a/sphinx-docs/screenshot.py +++ b/sphinx-docs/screenshot.py @@ -25,11 +25,11 @@ USER_ROLES = ["guest", "coach", "admin", "learner"] SS_DUMP_DIR = ".screenshot_dump" OUTPUT_PATH = os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), SS_DUMP_DIR)) -MANAGE_PATH = os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"..","kalite","manage.py")) +KALITECTL_PATH = os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "kalitectl.py")) # Formatted from subprocess.Popen # Trying to import call_command to execute a Django mgmt command gets you # into a weird import hell, probably because of import_all_child_modules... -SCREENSHOT_COMMAND = [sys.executable, MANAGE_PATH, "screenshots"] +SCREENSHOT_COMMAND = [sys.executable, KALITECTL_PATH, "manage", "screenshots"] SCREENSHOT_COMMAND_OPTS = ["-v", "0", "--output-dir", OUTPUT_PATH] # These keys are css styles but they need to be camelCased FOCUS_CSS_STYLES = { "borderStyle": "solid", @@ -52,9 +52,17 @@ def purge_screenshots(app, env, docname): def process_screenshots(app, env): if not hasattr(env, 'screenshot_all_screenshots'): return - + # Don't bother building screenshots if we're just collecting messages. + # Just checks if we invoked the build command with "gettext" in there somewhere + if "gettext" in sys.argv: + return all_args = map(lambda x: x['from_str_arg'], env.screenshot_all_screenshots) - subprocess = Popen(SCREENSHOT_COMMAND + SCREENSHOT_COMMAND_OPTS + ["--from-str", json.dumps(all_args)]) + # If building in a different language, start the server in a different language + command = SCREENSHOT_COMMAND + SCREENSHOT_COMMAND_OPTS + ["--from-str", json.dumps(all_args)] + language = env.config.language + if language: + command += ["--lang", language] + subprocess = Popen(command) subprocess.wait() if display: display.stop() @@ -219,7 +227,6 @@ def run(self): Build language can be accessed from the BuildEnvironment. """ self.env = self.state.document.settings.env - language = self.env.config.language return_nodes = [] if not hasattr(self.env, 'screenshot_all_screenshots'): self.env.screenshot_all_screenshots = []