def test_extract_mapping_from_files(self): mapping1 = burton.StringMapping(filename = "1.txt") mapping1.add_mapping(u"SomeKey", u"Translation for some key") mapping2 = burton.StringMapping(filename = "2.txt") mapping2.add_mapping( u"SomeOtherKey", u"Translation for the other string", ) mapping2.add_mapping( u"IgnoredString", u"This is an ignored string", ) mapping2.add_mapping( u"IgnoredString2", u"IgnoredString{0}", ) individual_file_mappings = [ mapping1, mapping2 ] extractor = burton.parser.Base() extractor.extract_mapping_from_filename = mock.Mock(side_effect = lambda(filename): individual_file_mappings.pop() ) final_mapping = extractor.extract_string_mapping_from_files( [ "file1", "file2" ], [ "IgnoredString", "IgnoredString{0}" ], ) self.assertEquals( final_mapping.string_mapping_dict, { u"SomeKey" : u"Translation for some key", u"SomeOtherKey" : u"Translation for the other string", } )
def test_combine_string_mappings(self): reference_mapping = burton.StringMapping() mapping1 = burton.StringMapping(filename = "1.txt") mapping1.add_mapping("SomeKey", "Translation for some key") mapping2 = burton.StringMapping(filename = "2.txt") mapping2.add_mapping( "SomeOtherKey", "Translation for the other string", ) reference_mapping.combine_with(mapping1) reference_mapping.combine_with(mapping2) self.assertEquals( reference_mapping.get_string("SomeKey"), "Translation for some key" ) self.assertEquals(reference_mapping.get_filenames("SomeKey"), ["1.txt"]) self.assertEquals( reference_mapping.get_string("SomeOtherKey"), "Translation for the other string" ) self.assertEquals( reference_mapping.get_filenames("SomeOtherKey"), [ "2.txt" ] )
def extract_string_mapping_from_files(self, filenames, strings_to_ignore=[]): logger = logging.getLogger(burton.logger_name) reference_mapping = burton.StringMapping() for filename in set(self._filter_filenames(filenames)): logger.debug("Extracting string mapping from " + filename) reference_mapping.combine_with( self.extract_mapping_from_filename(filename)) strings_to_remove = [] for string in reference_mapping: replaced_key, params = replace_params(string) if replaced_key in strings_to_ignore: strings_to_remove.append(string) elif reference_mapping.get_string(string) is not None: replaced_value, params = \ replace_params(reference_mapping.get_string(string)) if replaced_value in strings_to_ignore: strings_to_remove.append(string) for string in strings_to_remove: reference_mapping.delete_mapping(string) return reference_mapping
def extract_mapping_from_filename(self, filename): string_mapping = burton.StringMapping(filename=filename) for string in self.extract_strings_from_filename(filename): string_mapping.add_mapping(string, string) return string_mapping
def test_add_mapping(self): reference_mapping = burton.StringMapping() reference_mapping.add_mapping(u"SomeKey", u"Translation for some key") self.assertEquals( reference_mapping.get_string(u"SomeKey"), u"Translation for some key" )
def test_get_accessors(self): reference_mapping = burton.StringMapping(filename = "1.txt") reference_mapping.add_mapping(u"SomeKey", u"Translation for some key") self.assertEquals( reference_mapping.get_string(u"SomeKey"), u"Translation for some key" ) self.assertEquals(reference_mapping.get_filenames(u"SomeKey"), ["1.txt"])
def extract_mapping_from_filename(self, filename): string_mapping = burton.StringMapping(filename=filename) def _add_mapping(key, value): string_mapping.add_mapping(key, value) self._parse(filename, _add_mapping) return string_mapping
def extract_mapping_from_filename(self, filename): string_mapping = burton.StringMapping(filename=filename) tree = lxml.etree.fromstring(self._read_file(filename)) def _add_mapping(str, category, node): string_mapping.add_mapping(str, str) self._parse(tree, _add_mapping) return string_mapping
def extract_mapping_from_filename(self, filename): string_mapping = burton.StringMapping(filename = filename) def _add_mapping(key, value, line): if key is not None and value is not None and \ key not in RC.string_file_infos: string_mapping.add_mapping(key, value) self._parse(filename, _add_mapping) return string_mapping
def extract_mapping_from_filename(self, filename): string_mapping = burton.StringMapping(filename=filename) tree = lxml.etree.fromstring(self._read_file(filename)) def _add_mapping(key, value, node): string_mapping.add_mapping(key + '-' + filename, value) self._parse(tree, filename, _add_mapping) return string_mapping
def test_iter(self): reference_mapping = burton.StringMapping(filename = "1.txt") reference_mapping.add_mapping(u"SomeKey", u"Translation for some key") reference_mapping.add_mapping( u"SomeOtherKey", u"Translation for the other string", ) actual_return_values = [] expected_return_values = [ u"SomeKey", u"SomeOtherKey" ] for key in reference_mapping: actual_return_values.append(key) actual_return_values.sort() self.assertEquals(actual_return_values, expected_return_values)
def extract_mapping_from_filename(self, filename, strip_keys=True): string_mapping = burton.StringMapping(filename=filename) def _add_mapping(key, value): if strip_keys and key and key[0] == '"': key = key[1:-1] if value and value[0] == '"': value = value[1:-1] string_mapping.add_mapping(key, value) self._parse(filename, _add_mapping) return string_mapping
def extract_string_mapping_from_files(self, filenames, strings_to_ignore=[]): return_mapping = burton.StringMapping() return_mapping.add_mapping("Shared String", "Shared String mapping") return_mapping.add_mapping("ignore_this", "ignore_this mapping") for file_index in range(1, len(filenames) + 1): key = self.parser_name + str(file_index) return_mapping.add_mapping(key, key + " mapping") for string in strings_to_ignore: return_mapping.delete_mapping(string) return return_mapping
def test_run(self, create_localized_resources_func, update_translation_file_func, create_db_instance_func, check_for_unmapped_strings_func, extract_mapping_func, extract_strings_func, update_base_localizations_func, create_vcs_class_func, config_logger_func, setup_default_logger_func, create_config_instance_func, isdir_func, chdir_func, exit_func): ran_all_tests = False test_db_name = "burton_test.sql" platform_string = "Test-platform" xlf_repo_path = "submodule" config_dict = { burton.Config.use_vcs: False, burton.Config.vcs_class: "burton.vcs.NoOp", burton.Config.commit_vcs: False, burton.Config.root_path: os.getcwd(), burton.Config.database_path: test_db_name, burton.Config.platform: platform_string, burton.Config.output_languages: ["French"], burton.Config.logging_level: "info", burton.Config.source_path: "foo", burton.Config.xlf_repo_path: xlf_repo_path } isdir_func.return_value = True def _config_get(key): return config_dict[key] conf = mock.Mock() conf._platform_queue = collections.deque([platform_string]) def _num_remaining_platforms(): return len(conf._platform_queue) def _parse_next(): return_value = conf._parse_config_file() if len(conf._platform_queue) > 0: conf._platform_queue.popleft() return return_value conf.num_remaining_platforms.side_effect = _num_remaining_platforms conf.parse_config_file_for_next_platform.side_effect = _parse_next conf.get.side_effect = _config_get conf.get_string_to_ignore.return_value = ["Ignore1", "Ignore2"] conf.parse_command_line_options.return_value = True conf._parse_config_file.return_value = False create_config_instance_func.return_value = conf chdir_directories = [] def _chdir(directory): chdir_directories.append(directory) chdir_func.side_effect = _chdir try: burton.run() conf._parse_config_file.return_value = True vcs_class = mock.Mock() create_vcs_class_func.return_value = vcs_class mapping = burton.StringMapping() mapping.add_mapping("String1", "Mapping1") extract_strings_func.return_value = ["String1"] extract_mapping_func.return_value = mapping mock_db = mock.Mock() mock_db.get_all_native_translations.return_value = ["Mapping1"] mock_db.get_native_translations_for_platform.return_value = [ "Mapping1" ] create_db_instance_func.return_value = mock_db conf._platform_queue = collections.deque([platform_string]) burton.run() self.assertTrue(create_db_instance_func.called) self.assertTrue(mock_db.connect.called) self.assertFalse(mock_db.update_from_vcs.called) cwd = os.getcwd() self.assertEquals(chdir_directories, [cwd, cwd, cwd, "foo", cwd]) mock_db.write_string_mapping_for_platform.assert_called_with( platform_string, mapping.string_mapping_dict, ) update_translation_file_func.assert_called_with( conf, ["Mapping1"], ["Mapping1"], "French", vcs_class) create_localized_resources_func.assert_called_with( conf, ["Mapping1"], vcs_class) update_base_localizations_func.assert_called_with(conf, vcs_class) self.assertTrue(mock_db.disconnect.called) self.assertFalse(vcs_class.commit_changes.called) self.assertFalse(vcs_class.upload_changes.called) config_dict[burton.Config.use_vcs] = True conf._platform_queue = collections.deque([platform_string]) burton.run() self.assertTrue(mock_db.update_from_vcs.called) config_dict[burton.Config.commit_vcs] = True conf._platform_queue = collections.deque([platform_string]) burton.run() self.assertTrue(vcs_class.commit_changes.called) self.assertTrue(vcs_class.upload_changes.called) def _throw_exception(conf, native_translations, vcs_class): raise Exception("Sample Exception") create_localized_resources_func.side_effect = _throw_exception captured_log = testfixtures.LogCapture() burton.logging_handler.max_level = logging.ERROR conf._platform_queue = collections.deque([platform_string]) burton.run() captured_log.check( (burton.logger_name, "INFO", "Running for platform Test-platform"), (burton.logger_name, 'INFO', 'Writing string mapping to database'), (burton.logger_name, 'ERROR', 'Sample Exception'), (burton.logger_name, 'ERROR', 'Reverting checkout'), (burton.logger_name, "INFO", "Finished running for platform Test-platform")) captured_log.uninstall() exit_func.assert_called_with(1) ran_all_tests = True except Exception as e: print e self.assertFalse(True) finally: if os.path.exists(test_db_name): os.remove(test_db_name) self.assertTrue(ran_all_tests)