absurdfarce commented on code in PR #1273:
URL: 
https://github.com/apache/cassandra-python-driver/pull/1273#discussion_r2892673299


##########
cassandra/util.py:
##########
@@ -1692,54 +1692,43 @@ def __repr__(self):
             self.lower_bound, self.upper_bound, self.value
         )
 
+VERSION_REGEX = 
re.compile("(\\d+)\\.(\\d+)(\\.\\d+)?(\\.\\d+)?([~\\-]\\w[.\\w]*(?:-\\w[.\\w]*)*)?(\\+[.\\w]+)?")
 
 @total_ordering
 class Version(object):
-    """
-    Internal minimalist class to compare versions.
-    A valid version is: <int>.<int>.<int>.<int or str>.
-
-    TODO: when python2 support is removed, use packaging.version.
-    """
-
-    _version = None
-    major = None
-    minor = 0
-    patch = 0
-    build = 0
-    prerelease = 0
 
     def __init__(self, version):
         self._version = version
-        if '-' in version:
-            version_without_prerelease, self.prerelease = version.split('-', 1)
-        else:
-            version_without_prerelease = version
-        parts = list(reversed(version_without_prerelease.split('.')))
-        if len(parts) > 4:
-            prerelease_string = "-{}".format(self.prerelease) if 
self.prerelease else ""
-            log.warning("Unrecognized version: {}. Only 4 components plus 
prerelease are supported. "
-                        "Assuming version as {}{}".format(version, 
'.'.join(parts[:-5:-1]), prerelease_string))
+
+        match = VERSION_REGEX.match(version)
+        if not match:
+            raise ValueError("Version string did not match expected format")
+
+        self.major = int(match[1])
+        self.minor = int(match[2])
 
         try:
-            self.major = int(parts.pop())
-        except ValueError as e:
-            raise ValueError(
-                "Couldn't parse version {}. Version should start with a 
number".format(version))\
-                .with_traceback(e.__traceback__)
+            self.patch = self._cleanup_int(match[3])
+        except:
+            self.patch = 0
+
         try:
-            self.minor = int(parts.pop()) if parts else 0
-            self.patch = int(parts.pop()) if parts else 0
+            self.build = self._cleanup_int(match[4])
+        except:
+            self.build = 0
 
-            if parts:  # we have a build version
-                build = parts.pop()
-                try:
-                    self.build = int(build)
-                except ValueError:
-                    self.build = build
-        except ValueError:
-            assumed_version = "{}.{}.{}.{}-{}".format(self.major, self.minor, 
self.patch, self.build, self.prerelease)
-            log.warning("Unrecognized version {}. Assuming version as 
{}".format(version, assumed_version))
+        try:
+            self.prerelease = self._cleanup_str(match[5])
+        except:
+            self.prerelease = 0
+
+    # Trim off the leading '.' characters and convert the discovered value to 
an integer
+    def _cleanup_int(self, s):
+        return int(s[1:]) if s else 0
+
+    # Trim off the leading '.' or '~' characters and just return the string 
directly
+    def _cleanup_str(self, str):
+        return str[1:] if str else 0

Review Comment:
   Sure, shadowing is bad



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to