]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Ensure `path` is correctly bound for extract callback (flake8 B023)
authorAarni Koskela <akx@iki.fi>
Wed, 11 Jan 2023 10:49:43 +0000 (12:49 +0200)
committerAarni Koskela <akx@iki.fi>
Wed, 18 Jan 2023 19:07:51 +0000 (21:07 +0200)
babel/messages/frontend.py

index c7e921d09a4e30361b9edcd323c2ac4128566e31..a18d00d9fe916f4327a39260b2e801982eec70cf 100644 (file)
@@ -472,6 +472,27 @@ class extract_messages(Command):
         else:
             self.directory_filter = None
 
+    def _build_callback(self, path: str):
+        def callback(filename: str, method: str, options: dict):
+            if method == 'ignore':
+                return
+
+            # If we explicitly provide a full filepath, just use that.
+            # Otherwise, path will be the directory path and filename
+            # is the relative path from that dir to the file.
+            # So we can join those to get the full filepath.
+            if os.path.isfile(path):
+                filepath = path
+            else:
+                filepath = os.path.normpath(os.path.join(path, filename))
+
+            optstr = ''
+            if options:
+                opt_values = ", ".join(f'{k}="{v}"' for k, v in options.items())
+                optstr = f" ({opt_values})"
+            self.log.info('extracting messages from %s%s', filepath, optstr)
+        return callback
+
     def run(self):
         mappings = self._get_mappings()
         with open(self.output_file, 'wb') as outfile:
@@ -483,25 +504,7 @@ class extract_messages(Command):
                               header_comment=(self.header_comment or DEFAULT_HEADER))
 
             for path, method_map, options_map in mappings:
-                def callback(filename, method, options):
-                    if method == 'ignore':
-                        return
-
-                    # If we explicitly provide a full filepath, just use that.
-                    # Otherwise, path will be the directory path and filename
-                    # is the relative path from that dir to the file.
-                    # So we can join those to get the full filepath.
-                    if os.path.isfile(path):
-                        filepath = path
-                    else:
-                        filepath = os.path.normpath(os.path.join(path, filename))
-
-                    optstr = ''
-                    if options:
-                        opt_values = ", ".join(f'{k}="{v}"' for k, v in options.items())
-                        optstr = f" ({opt_values})"
-                    self.log.info('extracting messages from %s%s', filepath, optstr)
-
+                callback = self._build_callback(path)
                 if os.path.isfile(path):
                     current_dir = os.getcwd()
                     extracted = check_and_call_extract_file(