def matched_components(self): """Return list of matched components.""" result = {} for path, groups, mask in self.matches: if mask not in result: name = render_template(self.name_template, **groups) result[mask] = { "files": {path}, "languages": {groups["language"]}, "files_langs": {(path, groups["language"])}, "base_file": render_template(self.base_file_template, **groups), "new_base": render_template(self.new_base_template, **groups), "intermediate": render_template(self.intermediate_template, **groups), "mask": mask, "name": name, "slug": slugify(name), } else: result[mask]["files"].add(path) result[mask]["languages"].add(groups["language"]) result[mask]["files_langs"].add((path, groups["language"])) return result
def matched_components(self): """Return list of matched components.""" result = {} for path, groups, mask in self.matches: if mask not in result: name = render_template(self.name_template, **groups) result[mask] = { 'files': {path}, 'languages': {groups['language']}, 'files_langs': {(path, groups['language'])}, 'base_file': render_template( self.base_file_template, **groups ), 'new_base': render_template( self.new_base_template, **groups ), 'mask': mask, 'name': name, 'slug': slugify(name), } else: result[mask]['files'].add(path) result[mask]['languages'].add(groups['language']) result[mask]['files_langs'].add((path, groups['language'])) return result
def validate_render(value, **kwargs): """Validates rendered template.""" try: render_template(value, **kwargs) except Exception as err: raise ValidationError( _('Failed to render template: {}').format(err) )
def pre_commit(self, translation): filename = os.path.join( self.instance.component.full_path, render_template(self.instance.configuration['filename'], translation=translation)) content = render_template(self.instance.configuration['template'], translation=translation) dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) with open(filename, 'w') as handle: handle.write(content) translation.addon_commit_files.append(filename)
def render_repo_filename(self, template, translation): component = translation.component # Render the template filename = render_template(template, translation=translation) # Validate filename (not absolute or linking to parent dir) try: validate_filename(filename) except ValidationError: return None # Absolute path filename = os.path.join(component.full_path, filename) # Check if parent directory exists dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) # Validate if there is not a symlink out of the tree try: component.repository.resolve_symlinks(dirname) if os.path.exists(filename): component.repository.resolve_symlinks(filename) except ValueError: component.log_error('refused to write out of repository: %s', filename) return None return filename
def test_parentdir_chain(self): self.assertEqual( render_template( "{{ value|parentdir|parentdir }}", value="foo/bar/weblate/test.po" ), "weblate/test.po", )
def render_repo_filename(self, template, translation): component = translation.component # Render the template filename = render_template(template, translation=translation) # Validate filename (not absolute or linking to parent dir) try: validate_filename(filename) except ValidationError: return None # Absolute path filename = os.path.join(component.full_path, filename) # Check if parent directory exists dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) # Validate if there is not a symlink out of the tree try: component.repository.resolve_symlinks(dirname) if os.path.exists(filename): component.repository.resolve_symlinks(filename) except ValueError: component.log_error( 'refused to write out of repository: %s', filename ) return None return filename
def get_merge_message(self): parts = render_template( settings.DEFAULT_PULL_MESSAGE, component=self.component ).split("\n\n", 1) if len(parts) == 1: parts.append("") return parts
def test_parentdir_chain(self): self.assertEqual( render_template( '{{ value|parentdir|parentdir }}', value='foo/bar/weblate/test.po' ), 'weblate/test.po' )
def get_commit_message(self, component): return render_template( component.addon_message, # Compatibility with older hook_name=self.verbose, addon_name=self.verbose, component=component, )
def pre_commit(self, translation, author): self.run_script(translation=translation) if self.add_file: filename = os.path.join( self.instance.component.full_path, render_template(self.add_file, translation=translation)) translation.addon_commit_files.append(filename)
def get_commit_message(self, component): return render_template( component.addon_message, hook_name=self.verbose, project_name=component.project.name, component_name=component.name, url=get_site_url(component.get_absolute_url()) )
def pre_commit(self, translation, author): filename = os.path.join( self.instance.component.full_path, render_template( self.instance.configuration['filename'], translation=translation ) ) content = render_template( self.instance.configuration['template'], translation=translation ) dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) with open(filename, 'w') as handle: handle.write(content) translation.addon_commit_files.append(filename)
def pre_commit(self, translation, author): filename = self.render_repo_filename( self.instance.configuration['filename'], translation) if not filename: return content = render_template(self.instance.configuration['template'], translation=translation) with open(filename, 'w') as handle: handle.write(content) translation.addon_commit_files.append(filename)
def get_commit_message(self, author): """Format commit message based on project configuration.""" if self.commit_template == 'add': template = self.component.add_message self.commit_template = '' elif self.commit_template == 'delete': template = self.component.delete_message self.commit_template = '' else: template = self.component.commit_message return render_template(template, translation=self, author=author)
def pre_commit(self, translation, author): self.run_script(translation=translation) if self.add_file: filename = os.path.join( self.instance.component.full_path, render_template( self.add_file, translation=translation ) ) translation.addon_commit_files.append(filename)
def get_commit_message(self, author, template=None, **kwargs): """Format commit message based on project configuration.""" if template is None: if self.commit_template == "add": template = self.component.add_message self.commit_template = "" elif self.commit_template == "delete": template = self.component.delete_message self.commit_template = "" else: template = self.component.commit_message return render_template(template, translation=self, author=author, **kwargs)
def commit_and_push(self, component): repository = component.repository with repository.lock: if repository.needs_commit(): files = [t.filename for t in component.translation_set.all()] repository.commit(render_template( self.message, hook_name=self.verbose, project_name=component.project.name, component_name=component.name, url=get_site_url(component.get_absolute_url())), files=files) component.push_if_needed(None)
def get_commit_message(self, author): """Format commit message based on project configuration.""" if self.commit_template == 'add': template = self.component.add_message self.commit_template = '' elif self.commit_template == 'delete': template = self.component.delete_message self.commit_template = '' else: template = self.component.commit_message msg = render_template(template, translation=self, author=author) return msg
def commit_and_push(self, component): repository = component.repository with repository.lock: if repository.needs_commit(): files = [t.filename for t in component.translation_set.all()] repository.commit( render_template( self.message, hook_name=self.verbose, project_name=component.project.name, component_name=component.name, url=get_site_url(component.get_absolute_url()) ), files=files ) component.push_if_needed(None)
def get_commit_message(self, author): """Format commit message based on project configuration.""" template = self.component.commit_message if self.commit_message == '__add__': template = self.component.add_message self.commit_message = '' self.save() elif self.commit_message == '__delete__': template = self.component.delete_message self.commit_message = '' self.save() msg = render_template(template, self, author=author) if self.commit_message: msg = '{0}\n\n{1}'.format(msg, self.commit_message) self.commit_message = '' self.save() return msg
def test_parentdir(self): self.assertEqual( render_template('{{ value|parentdir }}', value='weblate/test.po'), 'test.po')
def test_stripext(self): self.assertEqual( render_template('{{ value|stripext }}', value='weblate/test.po'), 'weblate/test', )
def test_dirname(self): self.assertEqual( render_template('{{ value|dirname }}', value='weblate/test.po'), 'weblate')
def test_replace(self): self.assertEqual( render_template('{% replace "a-string-with-dashes" "-" " " %}'), 'a string with dashes', )
def test_stripext(self): self.assertEqual( render_template('{{ value|stripext }}', value='weblate/test.po'), 'weblate/test' )
def test_float(self): self.assertEqual( render_template('{{ number }}', number=1.1), '1.1' )
def test_dirname(self): self.assertEqual( render_template('{{ value|dirname }}', value='weblate/test.po'), 'weblate' )
def test_parentdir(self): self.assertEqual( render_template("{{ value|parentdir }}", value="weblate/test.po"), "test.po" )
def test_stripext(self): self.assertEqual( render_template("{{ value|stripext }}", value="weblate/test.po"), "weblate/test", )
def test_dirname(self): self.assertEqual( render_template("{{ value|dirname }}", value="weblate/test.po"), "weblate" )
def test_float(self): self.assertEqual(render_template("{{ number }}", number=1.1), "1.1")
def get_merge_message(self): lines = render_template( self.component.pull_message.strip(), component=self.component ).splitlines() return lines[0], "\n".join(lines[1:]).strip()
def test_parentdir_chain(self): self.assertEqual( render_template('{{ value|parentdir|parentdir }}', value='foo/bar/weblate/test.po'), 'weblate/test.po', )
def get_commit_message(self, author: str, template: str, **kwargs): """Format commit message based on project configuration.""" return render_template(template, translation=self, author=author, **kwargs)
def test_replace(self): self.assertEqual( render_template('{% replace "a-string-with-dashes" "-" " " %}'), 'a string with dashes' )
def get_merge_message(self): return render_template(settings.DEFAULT_PULL_MESSAGE, component=self.component).split("\n\n", 1)
def test_float(self): self.assertEqual(render_template('{{ number }}', number=1.1), '1.1')
def get_merge_message(self): from weblate.utils.render import render_template return render_template(settings.DEFAULT_PULL_MESSAGE, component=self.component)
def test_parentdir(self): self.assertEqual( render_template('{{ value|parentdir }}', value='weblate/test.po'), 'test.po' )