예제 #1
0
 def discovery(self):
     # Handle old settings which did not have this set
     if "new_base_template" not in self.instance.configuration:
         self.instance.configuration["new_base_template"] = ""
     return ComponentDiscovery(
         self.instance.component,
         **ComponentDiscovery.extract_kwargs(self.instance.configuration))
예제 #2
0
 def setUp(self):
     super(ComponentDiscoveryTest, self).setUp()
     self.component = self.create_component()
     self.discovery = ComponentDiscovery(
         self.component,
         file_format='po',
         match=r'(?P<component>[^/]*)/(?P<language>[^/]*)\.po',
         name_template='{{ component|title }}',
         language_regex='^(?!xx).*$',
     )
예제 #3
0
    def test_perform(self):
        # Preview should not create anything
        created, matched, deleted = self.discovery.perform(preview=True)
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Create components
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Test second call does nothing
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 3)
        self.assertEqual(len(deleted), 0)

        # Remove some files
        repository = self.component.repository
        with repository.lock:
            repository.remove(
                ['po-link', 'second-po/cs.po', 'second-po/de.po'],
                'Remove some files'
            )

        # Create new discover as it caches matches
        discovery = ComponentDiscovery(
            self.component,
            r'(?P<component>[^/]*)/(?P<language>[^/]*)\.po',
            '{{ component|title }}',
            '^(?!xx).*$',
        )

        # Test component removal preview
        created, matched, deleted = discovery.perform(
            preview=True, remove=True
        )
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 2)

        # Test component removal
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 2)

        # Components should be now removed
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 0)
예제 #4
0
 def test_duplicates(self):
     # Create all components with desired name po
     discovery = ComponentDiscovery(
         self.component,
         r'[^/]*(?P<component>po)[^/]*/(?P<language>[^/]*)\.po',
         '{{ component|title }}',
         '^(?!xx).*$',
     )
     created, matched, deleted = discovery.perform()
     self.assertEqual(len(created), 3)
     self.assertEqual(len(matched), 0)
     self.assertEqual(len(deleted), 0)
예제 #5
0
    def test_perform(self):
        # Preview should not create anything
        created, matched, deleted = self.discovery.perform(preview=True)
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Create components
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Test second call does nothing
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 3)
        self.assertEqual(len(deleted), 0)

        # Remove some files
        repository = self.component.repository
        with repository.lock:
            repository.remove(
                ["po-link", "second-po/cs.po", "second-po/de.po"],
                "Remove some files")

        # Create new discover as it caches matches
        discovery = ComponentDiscovery(
            self.component,
            file_format="po",
            match=r"(?P<component>[^/]*)/(?P<language>[^/]*)\.po",
            name_template="{{ component|title }}",
            language_regex="^(?!xx).*$",
        )

        # Test component removal preview
        created, matched, deleted = discovery.perform(preview=True,
                                                      remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 2)

        # Test component removal
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 2)

        # Components should be now removed
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 0)
예제 #6
0
 def test_multi_language(self):
     discovery = ComponentDiscovery(
         self.component,
         match=r'localization/(?P<language>[^/]*)/'
         r'(?P<component>[^/]*)\.(?P=language)\.po',
         name_template='{{ component }}',
         file_format='po',
     )
     created, matched, deleted = discovery.perform()
     self.assertEqual(len(created), 1)
     self.assertEqual(created[0][0]['mask'], 'localization/*/component.*.po')
     self.assertEqual(len(matched), 0)
     self.assertEqual(len(deleted), 0)
예제 #7
0
    def test_perform(self):
        # Preview should not create anything
        created, matched, deleted = self.discovery.perform(preview=True)
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Create components
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Test second call does nothing
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 3)
        self.assertEqual(len(deleted), 0)

        # Second discovery with restricted component match
        discovery = ComponentDiscovery(
            self.component,
            r'(?P<component>po)/(?P<language>[^/]*)\.po',
            '{{ component|title }}',
            '^(?!xx).*$',
        )

        # Test component removal preview
        created, matched, deleted = discovery.perform(
            preview=True, remove=True
        )
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 3)

        # Test component removal
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 3)

        # Components should be now removed
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)
예제 #8
0
 def setUp(self):
     super(ComponentDiscoveryTest, self).setUp()
     self.component = self.create_component()
     self.discovery = ComponentDiscovery(
         self.component,
         r'(?P<component>[^/]*)/(?P<language>[^/]*)\.po',
         '{{ component|title }}',
         '^(?!xx).*$',
     )
예제 #9
0
 def discovery(self):
     return ComponentDiscovery(
         self._addon.instance.component,
         self.cleaned_data['match'],
         self.cleaned_data['name_template'],
         self.cleaned_data['language_regex'],
         self.cleaned_data['base_file_template'],
         self.cleaned_data['new_base_template'],
     )
예제 #10
0
    def get_discovery(self, component, path=None):
        """Return discovery object after doing basic sanity check."""
        if self.discovery is not None:
            self.discovery.component = component
        else:
            self.discovery = ComponentDiscovery(
                component,
                self.filemask,
                self.name_template,
                self.language_regex,
                self.base_file_template,
                self.file_format,
                path=path
            )
            self.logger.info(
                'Found %d matching files',
                len(self.discovery.matched_files)
            )

            if not self.discovery.matched_files:
                raise CommandError('Your mask did not match any files!')

            self.logger.info(
                'Found %d components',
                len(self.discovery.matched_components)
            )
            langs = set()
            for component in self.discovery.matched_components.values():
                langs.update(component['languages'])
            self.logger.info('Found %d languages', len(langs))

            # Do some basic sanity check on languages
            if Language.objects.filter(code__in=langs).count() == 0:
                raise CommandError(
                    'None of matched languages exists, maybe you have '
                    'mixed * and ** in the mask?'
                )
        return self.discovery
예제 #11
0
class ComponentDiscoveryTest(RepoTestCase):
    def setUp(self):
        super(ComponentDiscoveryTest, self).setUp()
        self.component = self.create_component()
        self.discovery = ComponentDiscovery(
            self.component,
            r'(?P<component>[^/]*)/(?P<language>[^/]*)\.po',
            '{{ component|title }}',
            '^(?!xx).*$',
        )

    def test_matched_files(self):
        self.assertEqual(
            sorted(self.discovery.matched_files),
            sorted([
                'po-link/cs.po',
                'po-link/de.po',
                'po-link/it.po',
                'po-mono/cs.po',
                'po-mono/de.po',
                'po-mono/en.po',
                'po-mono/it.po',
                'po/cs.po',
                'po/de.po',
                'po/it.po',
                'second-po/cs.po',
                'second-po/de.po',
            ])
        )

    def test_matched_components(self):
        self.assertEqual(
            self.discovery.matched_components,
            {
                'po/*.po': {
                    'files': {'po/cs.po', 'po/de.po', 'po/it.po'},
                    'files_langs': {
                        ('po/cs.po', 'cs'),
                        ('po/de.po', 'de'),
                        ('po/it.po', 'it'),
                    },
                    'languages': {'cs', 'de', 'it'},
                    'mask': 'po/*.po',
                    'name': 'Po',
                    'slug': 'po',
                    'base_file': '',
                    'new_base': '',
                },
                'po-link/*.po': {
                    'files': {
                        'po-link/cs.po', 'po-link/de.po', 'po-link/it.po'
                    },
                    'files_langs': {
                        ('po-link/cs.po', 'cs'),
                        ('po-link/de.po', 'de'),
                        ('po-link/it.po', 'it'),
                    },
                    'languages': {'cs', 'de', 'it'},
                    'mask': 'po-link/*.po',
                    'name': 'Po-Link',
                    'slug': 'po-link',
                    'base_file': '',
                    'new_base': '',
                },
                'po-mono/*.po': {
                    'files': {
                        'po-mono/cs.po', 'po-mono/de.po',
                        'po-mono/it.po', 'po-mono/en.po'
                    },
                    'files_langs': {
                        ('po-mono/cs.po', 'cs'),
                        ('po-mono/de.po', 'de'),
                        ('po-mono/it.po', 'it'),
                        ('po-mono/en.po', 'en'),
                    },
                    'languages': {'cs', 'de', 'it', 'en'},
                    'mask': 'po-mono/*.po',
                    'name': 'Po-Mono',
                    'slug': 'po-mono',
                    'base_file': '',
                    'new_base': '',
                },
                'second-po/*.po': {
                    'files': {'second-po/cs.po', 'second-po/de.po'},
                    'files_langs': {
                        ('second-po/cs.po', 'cs'),
                        ('second-po/de.po', 'de'),
                    },
                    'languages': {'cs', 'de'},
                    'mask': 'second-po/*.po',
                    'name': 'Second-Po',
                    'slug': 'second-po',
                    'base_file': '',
                    'new_base': '',
                },
            }
        )

    def test_perform(self):
        # Preview should not create anything
        created, matched, deleted = self.discovery.perform(preview=True)
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Create components
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)

        # Test second call does nothing
        created, matched, deleted = self.discovery.perform()
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 3)
        self.assertEqual(len(deleted), 0)

        # Remove some files
        repository = self.component.repository
        with repository.lock:
            repository.remove(
                ['po-link', 'second-po/cs.po', 'second-po/de.po'],
                'Remove some files'
            )

        # Create new discover as it caches matches
        discovery = ComponentDiscovery(
            self.component,
            r'(?P<component>[^/]*)/(?P<language>[^/]*)\.po',
            '{{ component|title }}',
            '^(?!xx).*$',
        )

        # Test component removal preview
        created, matched, deleted = discovery.perform(
            preview=True, remove=True
        )
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 2)

        # Test component removal
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 2)

        # Components should be now removed
        created, matched, deleted = discovery.perform(remove=True)
        self.assertEqual(len(created), 0)
        self.assertEqual(len(matched), 1)
        self.assertEqual(len(deleted), 0)

    def test_duplicates(self):
        # Create all components with desired name po
        discovery = ComponentDiscovery(
            self.component,
            r'[^/]*(?P<component>po)[^/]*/(?P<language>[^/]*)\.po',
            '{{ component|title }}',
            '^(?!xx).*$',
        )
        created, matched, deleted = discovery.perform()
        self.assertEqual(len(created), 3)
        self.assertEqual(len(matched), 0)
        self.assertEqual(len(deleted), 0)
예제 #12
0
파일: forms.py 프로젝트: nijel/weblate
 def discovery(self):
     return ComponentDiscovery(
         self._addon.instance.component,
         **ComponentDiscovery.extract_kwargs(self.cleaned_data)
     )