[damned-lies] Allow use of Domain.pot_params for extra xgettext parameters



commit 9dd93c3e9d463c16fb57cd99c3f92c89f64f85ce
Author: Claude Paroz <claude 2xlibre net>
Date:   Fri Jan 14 15:31:47 2022 +0100

    Allow use of Domain.pot_params for extra xgettext parameters

 stats/admin.py                      |  5 -----
 stats/migrations/0010_pot_method.py |  2 +-
 stats/models.py                     | 37 +++++++++++++++++++++++--------------
 3 files changed, 24 insertions(+), 20 deletions(-)
---
diff --git a/stats/admin.py b/stats/admin.py
index 466766fb..149a39ef 100644
--- a/stats/admin.py
+++ b/stats/admin.py
@@ -31,11 +31,6 @@ class DomainForm(forms.ModelForm):
             self.fields['branch_from'].queryset = Branch.objects.none()
             self.fields['branch_to'].queryset = Branch.objects.none()
 
-    def clean(self):
-        cleaned_data = super().clean()
-        if cleaned_data['pot_params'] and cleaned_data['pot_method'] not in ('shell', 'url'):
-            raise forms.ValidationError("pot_params can only be set for shell or url pot methods.")
-
 
 class BranchInline(admin.TabularInline):
     model = Branch
diff --git a/stats/migrations/0010_pot_method.py b/stats/migrations/0010_pot_method.py
index 69ce78be..2e05f01a 100644
--- a/stats/migrations/0010_pot_method.py
+++ b/stats/migrations/0010_pot_method.py
@@ -21,6 +21,6 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='domain',
             name='pot_params',
-            field=models.CharField(blank=True, help_text='Only for shell or URL methods', max_length=100),
+            field=models.CharField(blank=True, help_text="pot_method='url': URL, pot_method='shell': shell 
command, pot_method='gettext': optional params", max_length=100),
         ),
     ]
diff --git a/stats/models.py b/stats/models.py
index e917a88f..b7eefdb7 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -684,7 +684,8 @@ class Domain(models.Model):
     )
     pot_method = models.CharField(max_length=20, choices=POT_METHOD_CHOICES, default='auto')
     pot_params = models.CharField(
-        max_length=100, blank=True, help_text="Only for shell or URL methods"
+        max_length=100, blank=True,
+        help_text="pot_method='url': URL, pot_method='shell': shell command, pot_method='gettext': optional 
params"
     )
     extra_its_dirs = models.TextField(
         blank=True,
@@ -875,12 +876,26 @@ class Domain(models.Model):
         return potfile, ()
 
     def get_xgettext_command(self, branch):
-        xgettext_args = [
-            '--directory', str(branch.co_path),
-            '--from-code', 'utf-8',
-            '--add-comments',
-            '--output', '%s.pot' % self.potbase(),
-        ]
+        # Note: the command will be run from the po directory.
+        xgettext_args = []
+        vcs_path = branch.co_path / self.base_dir
+        if self.pot_params:
+            xgettext_args.extend(self.pot_params.split())
+        for opt, value in [
+            ('--directory', str(branch.co_path)),
+            ('--from-code', 'utf-8'),
+            ('--add-comments', ''),
+            ('--output', f'{self.potbase()}.pot'),
+        ]:
+            if opt not in xgettext_args:
+                xgettext_args.extend([opt, value] if value != '' else [opt])
+        if (vcs_path / 'POTFILES.in').exists():
+            xgettext_args = ['--files-from', 'POTFILES.in'] + xgettext_args
+        elif (vcs_path / 'POTFILES').exists():
+            xgettext_args = ['--files-from', 'POTFILES'] + xgettext_args
+        else:
+            raise RuntimeError(f"No POTFILES file found in {self.base_dir}")
+
         if not os.path.exists(utils.ITS_DATA_DIR):
             utils.collect_its_data()
         env = {'GETTEXTDATADIRS': os.path.dirname(utils.ITS_DATA_DIR)}
@@ -889,13 +904,7 @@ class Domain(models.Model):
                 [env['GETTEXTDATADIRS']] + [str(branch.co_path / path)
                                             for path in self.extra_its_dirs.split(':')]
             )
-        vcs_path = branch.co_path / self.base_dir
-        if (vcs_path / 'POTFILES.in').exists():
-            xgettext_args = ['--files-from', 'POTFILES.in'] + xgettext_args
-        elif (vcs_path / 'POTFILES').exists():
-            xgettext_args = ['--files-from', 'POTFILES'] + xgettext_args
-        else:
-            raise RuntimeError(f"No POTFILES file found in {self.base_dir}")
+
         # Parse and use content from: "XGETTEXT_OPTIONS = --keyword=_ --keyword=N_"
         makefile = utils.MakefileWrapper.find_file(branch, [vcs_path], file_name='Makevars')
         if makefile:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]