def test_resolve_locales(intercept_populated, temp_site): """Test if locales are resolved correctly. In this environment, no languages have to be added. """ api = XTMCloudAPI(_API_TOKEN_VALID) source = FileSource(str(temp_site)) source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) utils.resolve_locales(api, source)
def test_resolve_locales_adds_langs(intercept, temp_site): """Test if locales are resolved correctly. In this environment, new target languages are added. """ api = XTMCloudAPI(_API_TOKEN_VALID) source = FileSource(str(temp_site)) source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) exp_targets = {'de_DE'} utils.resolve_locales(api, source) assert exp_targets == api.get_target_languages(_PROJECT_ID)
def test_resolve_locales_raise_exception(intercept_populated, temp_site): """Test if locales are resolved correctly. In this environment, the API has more target languages configured than are available online. We except an exception to be raised. """ api = XTMCloudAPI(_API_TOKEN_VALID) api.add_target_languages(_PROJECT_ID, ['ro_RO']) source = FileSource(str(temp_site)) source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) exp_msg = ('The following languages are enabled in the API, but not ' "listed in locales: set(['ro_RO'])! Please remove them manually" ' from project number 1234 and then re-run the script!') exception_test(utils.resolve_locales, Exception, exp_msg, api, source)
def temp_site_no_target_files(tmpdir): out_dir = tmpdir.mkdir('temp_out_no_targets') site_dir = out_dir.join('test_site').strpath shutil.copytree(os.path.join(ROOTPATH, 'tests', 'test_site'), site_dir) with FileSource(str(site_dir)) as fs: fs.write_to_config('XTM', 'project_id', '9999') return site_dir
def crowdin_sync(source_dir, crowdin_api_key): with FileSource(source_dir) as source: config = source.read_config() defaultlocale = config.get('general', 'defaultlocale') crowdin_project_name = config.get('general', 'crowdin-project-name') crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) logger.info('Requesting project information...') project_info = crowdin_api.request('GET', 'info') page_strings = extract_strings(source, defaultlocale) local_locales = source.list_locales() - {defaultlocale} enabled_locales = {l['code'] for l in project_info['languages']} required_locales = configure_locales(crowdin_api, local_locales, enabled_locales, defaultlocale) remote_files, remote_directories = list_remote_files(project_info) local_files, local_directories = list_local_files(page_strings) # Avoid deleting all remote content if there was a problem listing local files if not local_files: logger.error( 'No existing strings found, maybe the project directory is ' 'not set up correctly? Aborting!') sys.exit(1) new_files = local_files - remote_files new_directories = local_directories - remote_directories create_directories(crowdin_api, new_directories) upload_new_files(crowdin_api, new_files, page_strings) upload_translations(crowdin_api, source_dir, new_files, required_locales) existing_files = local_files - new_files update_existing_files(crowdin_api, existing_files, page_strings) old_files = remote_files - local_files old_directories = remote_directories - local_directories remove_old_files(crowdin_api, old_files) remove_old_directories(crowdin_api, old_directories) download_translations(crowdin_api, source_dir, required_locales) logger.info('Crowdin sync completed.')
def test_map_locales(temp_site): """Test if a local website's languages are mapped to XTM's format.""" test_source = FileSource(str(temp_site)) exp_out = {'de_DE'} assert exp_out == utils.map_locales(test_source)
def test_get_files_to_upload(temp_site): """Generation of correct datatype for the XTMCloudAPI class.""" files = utils.get_files_to_upload(FileSource(temp_site)) assert 'translate.json' in files assert 'translate-not-enough.json' in files
def test_extract_unicode_strings(temp_site): """Test correct extraction of unicode strings for translation.""" with FileSource(temp_site) as fs: strings = utils.extract_strings(fs) assert '\u0376' in strings['translate-unicode']['simple']['message']
description= 'CMS development server created to test pages locally and on-the-fly') parser.add_argument('path', nargs='?', default=os.curdir) parser.add_argument( '-a', '--address', default='localhost', help='Address of the interface the server will listen on') parser.add_argument('-p', '--port', type=int, default=5000, help='TCP port the server will listen on') args = parser.parse_args() source = FileSource(args.path) address = args.address port = args.port try: from werkzeug.serving import ThreadedWSGIServer, run_simple # see https://github.com/mitsuhiko/werkzeug/pull/770 ThreadedWSGIServer.daemon_threads = True def run(*args, **kwargs): # The werkzeug logger must be configured before the # root logger. Also we must prevent it from propagating # messages, otherwise messages are logged twice. import logging logger = logging.getLogger('werkzeug')
def test_cache(output_pages): source = FileSource(os.path.join('test_site')) assert source.get_cache_dir() == os.path.join('test_site', 'cache')