def get_resource_files(self, resource): """Get a dict for all files assigned to a resource. First we calculate the files matching the file expression and then we apply all translation excpetions. The resulting dict will be in this format: { 'en': 'path/foo/en/bar.po', 'de': 'path/foo/de/bar.po', 'es': 'path/exceptions/es.po'} NOTE: All paths are relative to the root of the project """ tr_files = {} if self.config.has_section(resource): try: file_filter = self.config.get(resource, "file_filter") except configparser.NoOptionError: file_filter = "$^" source_lang = self.config.get(resource, "source_lang") source_file = self.get_source_file(resource) expr_re = utils.regex_from_filefilter(file_filter, self.root) expr_rec = re.compile(expr_re) for f_path in utils.files_in_project(self.root): match = expr_rec.match(posix_path(f_path)) if match: lang = match.group(1) if lang != source_lang: f_path = os.path.relpath(f_path, self.root) if f_path != source_file: tr_files.update({lang: f_path}) for (name, value) in self.config.items(resource): if name.startswith("trans."): value = native_path(value) lang = name.split('.')[1] # delete language which has same file if value in list(tr_files.values()): keys = [] for k, v in six.iteritems(tr_files): if v == value: keys.append(k) if len(keys) == 1: del tr_files[keys[0]] else: raise Exception("Your configuration seems wrong. " "You have multiple languages " "pointing to the same file.") # Add language with correct file tr_files.update({lang: value}) return tr_files return None
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 expr_re = utils.regex_from_filefilter(expression, curpath) expr_rec = re.compile(expr_re) 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 in utils.files_in_project(curpath): match = expr_rec.match(posix_path(f_path)) if match: lang = match.group(1) 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)) # Eval 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()
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 expr_re = utils.regex_from_filefilter(expression, curpath) expr_rec = re.compile(expr_re) 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 in files_in_project(curpath): match = expr_rec.match(posix_path(f_path)) if match: lang = match.group(1) 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) root_dir = os.path.abspath(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)) # Eval 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()