Beispiel #1
0
 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
Beispiel #2
0
 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
Beispiel #3
0
 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
Beispiel #4
0
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)
        )
Beispiel #5
0
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)
        )
Beispiel #6
0
 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)
Beispiel #7
0
    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
Beispiel #8
0
 def test_parentdir_chain(self):
     self.assertEqual(
         render_template(
             "{{ value|parentdir|parentdir }}", value="foo/bar/weblate/test.po"
         ),
         "weblate/test.po",
     )
Beispiel #9
0
    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
Beispiel #10
0
 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
Beispiel #11
0
 def test_parentdir_chain(self):
     self.assertEqual(
         render_template(
             '{{ value|parentdir|parentdir }}',
             value='foo/bar/weblate/test.po'
         ),
         'weblate/test.po'
     )
Beispiel #12
0
 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,
     )
Beispiel #13
0
    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)
Beispiel #14
0
 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,
     )
Beispiel #15
0
 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())
     )
Beispiel #16
0
 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)
Beispiel #17
0
 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)
Beispiel #18
0
    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)
Beispiel #19
0
    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)
Beispiel #20
0
    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)
Beispiel #21
0
 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)
Beispiel #22
0
    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
Beispiel #23
0
 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)
Beispiel #24
0
    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
Beispiel #25
0
    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
Beispiel #26
0
 def test_parentdir(self):
     self.assertEqual(
         render_template('{{ value|parentdir }}', value='weblate/test.po'),
         'test.po')
Beispiel #27
0
 def test_stripext(self):
     self.assertEqual(
         render_template('{{ value|stripext }}', value='weblate/test.po'),
         'weblate/test',
     )
Beispiel #28
0
 def test_dirname(self):
     self.assertEqual(
         render_template('{{ value|dirname }}', value='weblate/test.po'),
         'weblate')
Beispiel #29
0
 def test_replace(self):
     self.assertEqual(
         render_template('{% replace "a-string-with-dashes" "-" " " %}'),
         'a string with dashes',
     )
Beispiel #30
0
 def test_stripext(self):
     self.assertEqual(
         render_template('{{ value|stripext }}', value='weblate/test.po'),
         'weblate/test'
     )
Beispiel #31
0
 def test_float(self):
     self.assertEqual(
         render_template('{{ number }}', number=1.1),
         '1.1'
     )
Beispiel #32
0
 def test_dirname(self):
     self.assertEqual(
         render_template('{{ value|dirname }}', value='weblate/test.po'),
         'weblate'
     )
Beispiel #33
0
 def test_parentdir(self):
     self.assertEqual(
         render_template("{{ value|parentdir }}", value="weblate/test.po"), "test.po"
     )
Beispiel #34
0
 def test_stripext(self):
     self.assertEqual(
         render_template("{{ value|stripext }}", value="weblate/test.po"),
         "weblate/test",
     )
Beispiel #35
0
 def test_dirname(self):
     self.assertEqual(
         render_template("{{ value|dirname }}", value="weblate/test.po"), "weblate"
     )
Beispiel #36
0
 def test_float(self):
     self.assertEqual(render_template("{{ number }}", number=1.1), "1.1")
Beispiel #37
0
 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()
Beispiel #38
0
 def test_parentdir_chain(self):
     self.assertEqual(
         render_template('{{ value|parentdir|parentdir }}',
                         value='foo/bar/weblate/test.po'),
         'weblate/test.po',
     )
Beispiel #39
0
 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)
Beispiel #40
0
 def test_replace(self):
     self.assertEqual(
         render_template('{% replace "a-string-with-dashes" "-" " " %}'),
         'a string with dashes'
     )
Beispiel #41
0
 def get_merge_message(self):
     return render_template(settings.DEFAULT_PULL_MESSAGE,
                            component=self.component).split("\n\n", 1)
Beispiel #42
0
 def test_float(self):
     self.assertEqual(render_template('{{ number }}', number=1.1), '1.1')
Beispiel #43
0
    def get_merge_message(self):
        from weblate.utils.render import render_template

        return render_template(settings.DEFAULT_PULL_MESSAGE,
                               component=self.component)
Beispiel #44
0
 def test_parentdir(self):
     self.assertEqual(
         render_template('{{ value|parentdir }}', value='weblate/test.po'),
         'test.po'
     )