]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Reduce code duplication.
authorBruno Haible <bruno@clisp.org>
Sun, 7 Aug 2022 15:29:16 +0000 (17:29 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 7 Aug 2022 21:04:21 +0000 (23:04 +0200)
* pygnulib/GLModuleSystem.py (GLModule): Declare two regexes are class
variables.

ChangeLog
pygnulib/GLModuleSystem.py

index acb82e903cb82a650656c5ec725f55462bc82cb1..a8d020edabdad0fbe78d0a1b384709c369e4b287 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2022-08-07  Bruno Haible  <bruno@clisp.org>
 
+       gnulib-tool.py: Reduce code duplication.
+       * pygnulib/GLModuleSystem.py (GLModule): Declare two regexes are class
+       variables.
+
        gnulib-tool.py: Implement option --find.
        * pygnulib/GLModuleSystem.py (GLModuleSystem.file_is_module): New
        method.
index 3383b22b28fedadb9bdffc446af827111220ec5e..33495f9c07f067419dacf3035003003b840ae63e 100644 (file)
@@ -177,6 +177,15 @@ class GLModule(object):
     path. GLModule can get all information about module, get its dependencies,
     files, etc.'''
 
+    section_label_regex = '(?:Description:|Comment:|Status:|Notice:|Applicability:|\
+Files:|Depends-on:|configure\\.ac-early:|configure\\.ac:|Makefile\\.am:|\
+Include:|Link:|License:|Maintainer:)'
+
+    section_label_pattern = \
+        re.compile('^(Description|Comment|Status|Notice|Applicability|'
+                   + 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
+                   + 'Makefile\\.am|Include|Link|License|Maintainer):$')
+
     def __init__(self, config, module, patched=False):
         '''GLModule.__init__(config, module[, patched]) -> GLModule
 
@@ -202,9 +211,6 @@ class GLModule(object):
         self.modulesystem = GLModuleSystem(self.config)
         with codecs.open(module, 'rb', 'UTF-8') as file:
             self.content = file.read()
-        self.regex = '(?:Description:|Comment:|Status:|Notice:|Applicability:|\
-Files:|Depends-on:|configure\\.ac-early:|configure\\.ac:|Makefile\\.am:|\
-Include:|Link:|License:|Maintainer:)'
 
     def __eq__(self, module):
         '''x.__eq__(y) <==> x==y'''
@@ -379,7 +385,7 @@ Include:|Link:|License:|Maintainer:)'
             if section not in self.content:
                 result = ''
             else:  # if section in self.content
-                pattern = '^%s[\t ]*(.*?)%s' % (section, self.regex)
+                pattern = '^%s[\t ]*(.*?)%s' % (section, GLModule.section_label_regex)
                 pattern = re.compile(pattern, re.S | re.M)
                 result = pattern.findall(self.content)
                 if type(result) is list:
@@ -400,7 +406,7 @@ Include:|Link:|License:|Maintainer:)'
             if section not in self.content:
                 result = ''
             else:  # if section in self.content
-                pattern = '^%s[\t ]*(.*?)%s' % (section, self.regex)
+                pattern = '^%s[\t ]*(.*?)%s' % (section, GLModule.section_label_regex)
                 pattern = re.compile(pattern, re.S | re.M)
                 result = pattern.findall(self.content)
                 if type(result) is list:
@@ -427,11 +433,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -456,11 +458,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -483,11 +481,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -521,11 +515,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -555,11 +545,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -599,11 +585,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -626,11 +608,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -667,11 +645,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -765,11 +739,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -793,11 +763,7 @@ Include:|Link:|License:|Maintainer:)'
                 lines = [ '%s\n' % line
                           for line in snippet.split('\n') ]
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]
@@ -838,7 +804,7 @@ Include:|Link:|License:|Maintainer:)'
             if section not in self.content:
                 result = ''
             else:  # if section in self.content
-                pattern = '^%s[\t ]*(.*?)%s' % (section, self.regex)
+                pattern = '^%s[\t ]*(.*?)%s' % (section, GLModule.section_label_regex)
                 pattern = re.compile(pattern, re.S | re.M)
                 result = pattern.findall(self.content)
                 if type(result) is list:
@@ -865,11 +831,7 @@ Include:|Link:|License:|Maintainer:)'
                           for line in snippet.split('\n') ]
                 parts = list()
                 for line in lines:
-                    regex = '^(Description|Comment|Status|Notice|Applicability|'
-                    regex += 'Files|Depends-on|configure\\.ac-early|configure\\.ac|'
-                    regex += 'Makefile\\.am|Include|Link|License|Maintainer):$'
-                    pattern = re.compile(regex)
-                    findflag = pattern.findall(line)
+                    findflag = GLModule.section_label_pattern.findall(line)
                     if findflag:
                         break
                     parts += [line]