def test_project_files_scanning(self): """ Test that the project files filter works as expected. """ # XXX: This testcase depends on the current file structure # Test with a valid expression on directory-level expressions = [ "tests/project_dir/test_expressions/<lang>/test.txt", "./tests/project_dir/test_expressions/<lang>/test.txt" ] for expr in expressions: langs = [] for file, lang in utils.get_project_files(os.getcwd(), expr): langs.append(lang) self.assertTrue(file.endswith("{}/test.txt".format(lang))) self.assertListEqual(sorted(langs), sorted(["en", "es"])) # Test with a valid expression on file-level with different prefixes expressions = [ "tests/project_dir/test_expressions/bulk/1.<lang>.po", "tests/project_dir/test_expressions/bulk/2_<lang>.po", "tests/project_dir/test_expressions/bulk/3 <lang>.po" ] for expr in expressions: langs = [] for file, lang in utils.get_project_files(os.getcwd(), expr): langs.append(lang) self.assertTrue(file.endswith("{}.po".format(lang))) self.assertListEqual(sorted(langs), sorted(["en_SE", "en"])) # Test with an invalid expression that doesn't contain <lang> expression = "tests/project_dir/test_expressions/bulk/1.en.po" msg = r"File filter (.*) does not contain" with six.assertRaisesRegex(self, exceptions.MalformedConfigFile, msg): for file, lang in utils.get_project_files(os.getcwd(), expression): pass
def test_project_files_scanning(self): """ Test that the project files filter works as expected. """ # XXX: This testcase depends on the current file structure # Test with a valid expression on directory-level expressions = ["tests/project_dir/test_expressions/<lang>/test.txt", "./tests/project_dir/test_expressions/<lang>/test.txt"] for expr in expressions: langs = [] for file, lang in utils.get_project_files(os.getcwd(), expr): langs.append(lang) self.assertTrue(file.endswith("{}/test.txt".format(lang))) self.assertListEqual(sorted(langs), sorted(["en", "es"])) # Test with a valid expression on file-level with different prefixes expressions = ["tests/project_dir/test_expressions/bulk/1.<lang>.po", "tests/project_dir/test_expressions/bulk/2_<lang>.po", "tests/project_dir/test_expressions/bulk/3 <lang>.po"] for expr in expressions: langs = [] for file, lang in utils.get_project_files(os.getcwd(), expr): langs.append(lang) self.assertTrue(file.endswith("{}.po".format(lang))) self.assertListEqual(sorted(langs), sorted(["en_SE", "en"])) # Test with an invalid expression that doesn't contain <lang> expression = "tests/project_dir/test_expressions/bulk/1.en.po" msg = r"File filter (.*) does not contain" with six.assertRaisesRegex(self, exceptions.MalformedConfigFile, msg): for file, lang in utils.get_project_files(os.getcwd(), expression): pass
def _auto_local(path_to_tx, resource, source_language, expression, execute=False, source_file=None, regex=False): """Auto configure local project.""" # The path everything will be relative to curpath = os.path.abspath(os.curdir) # Force expr to be a valid regex expr (escaped) but keep <lang> intact if not expression: raise Exception("You need to specify an expression to define where " "translation files should be saved.") if not execute: logger.info("Only printing the commands which will be run if the " "--execute switch is specified.") # First, let's construct a dictionary of all matching files. # Note: Only the last matching file of a language will be stored. translation_files = {} for f_path, lang in utils.get_project_files(curpath, expression): if lang == source_language and not source_file: source_file = f_path else: translation_files[lang] = f_path if not source_file: raise Exception("Could not find a source language file. Please run " "set --source manually and then re-run this command " "or provide the source file with the -s flag.") if execute: logger.info("Updating source for resource %s ( %s -> %s )." % ( resource, source_language, os.path.relpath( source_file, path_to_tx) )) _set_source_file(path_to_tx, resource, source_language, os.path.relpath(source_file, path_to_tx)) else: logger.info('\ntx set --source -r %(res)s -l %(lang)s %(file)s\n' % { 'res': resource, 'lang': source_language, 'file': os.path.relpath(source_file, curpath)}) prj = project.Project(path_to_tx) if execute: try: prj.config.get("%s" % resource, "source_file") except configparser.NoSectionError: raise Exception("No resource with slug \"%s\" was found.\nRun " "'tx set auto-local -r %s \"expression\"' to " "do the initial configuration." % resource) # Now let's handle the translation files. if execute: logger.info("Updating file expression for resource %s ( %s )." % ( resource, expression)) # Evaluate file_filter relative to root dir file_filter = posix_path( os.path.relpath(os.path.join(curpath, expression), path_to_tx) ) prj.config.set("%s" % resource, "file_filter", file_filter) else: for (lang, f_path) in sorted(translation_files.items()): logger.info('tx set -r %(res)s -l %(lang)s %(file)s' % { 'res': resource, 'lang': lang, 'file': os.path.relpath(f_path, curpath)}) if execute: prj.save()