]> git.ipfire.org Git - location/libloc.git/commitdiff
importer: Try to make parsing blocks faster
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 12 Aug 2022 15:38:08 +0000 (15:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 12 Aug 2022 15:38:08 +0000 (15:38 +0000)
This patch changes the order of some operations and drops a few
redundant ones (potentiall).

That should allow us to parse blocked data faster.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/location/importer.py

index ea1333cf1c5f86e4222bbc99b159c702c14f436b..a5776f7094bbc7f1ae691ae1d49c11880e5bcecb 100644 (file)
@@ -200,6 +200,10 @@ def iterate_over_blocks(f, charsets=("utf-8", "latin1")):
        block = []
 
        for line in f:
+               # Skip commented lines
+               if line.startswith(b"#") or line.startswith(b"%"):
+                       continue
+
                # Convert to string
                for charset in charsets:
                        try:
@@ -209,24 +213,17 @@ def iterate_over_blocks(f, charsets=("utf-8", "latin1")):
                        else:
                                break
 
-               # Skip commented lines
-               if line.startswith("#") or line.startswith("%"):
-                       continue
-
-               # Strip line-endings
-               line = line.rstrip()
-
                # Remove any comments at the end of line
                line, hash, comment = line.partition("#")
 
-               if comment:
-                       # Strip any whitespace before the comment
-                       line = line.rstrip()
+               # Strip any whitespace at the end of the line
+               line = line.rstrip()
 
-                       # If the line is now empty, we move on
-                       if not line:
-                               continue
+               # If we cut off some comment and the line is empty, we can skip it
+               if comment and not line:
+                       continue
 
+               # If the line has some content, keep collecting it
                if line:
                        block.append(line)
                        continue