1616
1717
1818def lreplace_extracted_comments (
19- filenames , match , replacement , dry_run = False , quiet = False
19+ filenames ,
20+ match = None ,
21+ replacement = None ,
22+ django_translators = False ,
23+ dry_run = False ,
24+ quiet = False ,
2025):
2126 """Replace the beginning of the extracted comments which starts with the
2227 string passed in the argument ``match``.
@@ -28,12 +33,16 @@ def lreplace_extracted_comments(
2833 filenames : list
2934 Set of file names to replace.
3035
31- match : str
36+ match : str, optional
3237 The start of the extracted comment content to be replaced.
3338
34- replacement : str
39+ replacement : str, optional
3540 The replacement for the beginning of the matching extracted comments.
3641
42+ django_translators : bool, optional
43+ Convenient parameter to pass ``Translators: `` as ``-m`` parameter and
44+ an empty string as replacement.
45+
3746 dry_run : bool, optional
3847 Don't do the replacements, just writes to stderr the location of them.
3948 You can use it with ``quiet=True`` to check if the strings has been
@@ -48,7 +57,14 @@ def lreplace_extracted_comments(
4857
4958 int: 0 if no a extracted comment has been replaced, 1 otherwise.
5059 """
51- regex = re .compile (rf"^#\.\s{ re .escape (match )} " )
60+ if not django_translators and not match and not replacement :
61+ raise ValueError ("You need to specify a match and replacement" )
62+
63+ if django_translators :
64+ regex = re .compile (r"^#\.\sTranslators:\s" )
65+ replacement = ""
66+ else :
67+ regex = re .compile (rf"^#\.\s{ re .escape (match )} " )
5268
5369 exitcode = 0
5470 for filename in filenames :
@@ -105,7 +121,7 @@ def main():
105121 "-m" ,
106122 "--match" ,
107123 dest = "match" ,
108- required = True ,
124+ required = False ,
109125 metavar = "MATCH" ,
110126 help = "The string to match at the beginning of the extracted comments to"
111127 " replace it." ,
@@ -114,16 +130,26 @@ def main():
114130 "-r" ,
115131 "--replacement" ,
116132 dest = "replacement" ,
117- required = True ,
133+ required = False ,
118134 metavar = "REPL" ,
119135 help = "The substitution to be used to replace the matching string." ,
120136 )
137+ parser .add_argument (
138+ "--django-translators" ,
139+ action = "store_true" ,
140+ dest = "django_translators" ,
141+ required = False ,
142+ help = (
143+ "Pass 'Translators: ' as '-m' argument and an empty string as replacement."
144+ ),
145+ )
121146 args = parser .parse_args ()
122147
123148 return lreplace_extracted_comments (
124149 args .filenames ,
125- args .match ,
126- args .replacement ,
150+ match = args .match ,
151+ replacement = args .replacement ,
152+ django_translators = args .django_translators ,
127153 dry_run = args .dry_run ,
128154 quiet = args .quiet ,
129155 )
0 commit comments