Apply linter steam roller

pull/613/head
Tim Head 2019-03-07 23:29:34 +01:00
rodzic be2d22da09
commit 6e2a718d15
1 zmienionych plików z 66 dodań i 22 usunięć

Wyświetl plik

@ -9,8 +9,11 @@
# functors take a version string and return true if it passes its constraints. # functors take a version string and return true if it passes its constraints.
import re import re
from enum import Enum
import semver import semver
# Main algorithm: # Main algorithm:
# Create an AbstractMatcher instance from the constraint string, and check it # Create an AbstractMatcher instance from the constraint string, and check it
# against each version in the versions_list, returning the first success. # against each version in the versions_list, returning the first success.
@ -21,60 +24,91 @@ def find_semver_match(constraint, versions_list):
return vstr return vstr
return None return None
def str_to_version(vstr): def str_to_version(vstr):
return tuple([int(n) for n in vstr.split('.')]) return tuple([int(n) for n in vstr.split(".")])
# --- Matcher interface ------------------------------------------- # --- Matcher interface -------------------------------------------
class AbstractMatcher: class AbstractMatcher:
def match(self, v): def match(self, v):
pass pass
class SemverMatcher(AbstractMatcher): class SemverMatcher(AbstractMatcher):
""" Match a version tuple to a given constraint_str using the `semver` package. """ """ Match a version tuple to a given constraint_str using the `semver` package. """
def __init__(self, constraint_str): def __init__(self, constraint_str):
self.constraint_str = constraint_str self.constraint_str = constraint_str
def match(self, v): def match(self, v):
while len(v) < 3: while len(v) < 3:
v = v+(0,) v = v + (0,)
v_str = '.'.join(map(str, v)) v_str = ".".join(map(str, v))
return semver.match(v_str, self.constraint_str) return semver.match(v_str, self.constraint_str)
def __eq__(self, rhs): def __eq__(self, rhs):
return self.constraint_str == rhs.constraint_str return self.constraint_str == rhs.constraint_str
def __repr__(self): def __repr__(self):
return self.constraint_str return self.constraint_str
# --- Custom matcher for julia-specific SemVer handling: ---------
from enum import Enum # --- Custom matcher for julia-specific SemVer handling: ---------
class Exclusivity(Enum): class Exclusivity(Enum):
EXCLUSIVE = 1 EXCLUSIVE = 1
INCLUSIVE = 2 INCLUSIVE = 2
class VersionRange(AbstractMatcher): class VersionRange(AbstractMatcher):
""" Match a version tuple between lower and upper bounds. """ """Match a version tuple between lower and upper bounds"""
def __init__(self, lower, upper, upper_exclusivity): def __init__(self, lower, upper, upper_exclusivity):
self.lower = lower self.lower = lower
self.upper = upper self.upper = upper
self.upper_exclusivity = upper_exclusivity self.upper_exclusivity = upper_exclusivity
def match(self, v): def match(self, v):
if self.upper_exclusivity == Exclusivity.EXCLUSIVE: if self.upper_exclusivity == Exclusivity.EXCLUSIVE:
return self.lower <= v < self.upper return self.lower <= v < self.upper
else: else:
return self.lower <= v <= self.upper return self.lower <= v <= self.upper
def __eq__(self, rhs): def __eq__(self, rhs):
return self.lower == rhs.lower and self.upper == rhs.upper and self.upper_exclusivity == rhs.upper_exclusivity return (
self.lower == rhs.lower
and self.upper == rhs.upper
and self.upper_exclusivity == rhs.upper_exclusivity
)
def __repr__(self): def __repr__(self):
return ("["+".".join(map(str, self.lower)) +"-"+ ".".join(map(str, self.upper)) + return (
(")" if self.upper_exclusivity == Exclusivity.EXCLUSIVE else "]")) "["
+ ".".join(map(str, self.lower))
+ "-"
+ ".".join(map(str, self.upper))
+ (")" if self.upper_exclusivity == Exclusivity.EXCLUSIVE else "]")
)
# Helpers # Helpers
def major(v): return v[0] def major(v):
def minor(v): return v[1] if len(v) >= 2 else 0 return v[0]
def patch(v): return v[2] if len(v) >= 3 else 0
def minor(v):
return v[1] if len(v) >= 2 else 0
def patch(v):
return v[2] if len(v) >= 3 else 0
# --- main constraint parser function ------------------------------------ # --- main constraint parser function ------------------------------------
def create_semver_matcher(constraint_str): def create_semver_matcher(constraint_str):
""" """
Returns a derived-class instance of AbstractMatcher that matches version Returns a derived-class instance of AbstractMatcher that matches version
@ -85,9 +119,9 @@ def create_semver_matcher(constraint_str):
if not first_digit: if not first_digit:
# Invalid version string (no numbers in it) # Invalid version string (no numbers in it)
return "" return ""
constraint = str_to_version(constraint_str[first_digit.start():]) constraint = str_to_version(constraint_str[first_digit.start() :])
comparison_symbol = constraint_str[0:first_digit.start()].strip() comparison_symbol = constraint_str[0 : first_digit.start()].strip()
# Default to "^" search if no matching mode specified (up to next major version) # Default to "^" search if no matching mode specified (up to next major version)
if (first_digit.start() == 0) or (comparison_symbol == "^"): if (first_digit.start() == 0) or (comparison_symbol == "^"):
@ -96,25 +130,35 @@ def create_semver_matcher(constraint_str):
# non-zero number is actually a major number: # non-zero number is actually a major number:
# https://docs.julialang.org/en/latest/stdlib/Pkg/#Caret-specifiers-1 # https://docs.julialang.org/en/latest/stdlib/Pkg/#Caret-specifiers-1
# So we need to handle it separately by bumping the first non-zero number. # So we need to handle it separately by bumping the first non-zero number.
for i,n in enumerate(constraint): for i, n in enumerate(constraint):
if n != 0 or i == len(constraint)-1: # (using the last existing number handles situations like "^0.0" or "^0") if (
upper = constraint[0:i] + (n+1,) n != 0 or i == len(constraint) - 1
): # (using the last existing number handles situations like "^0.0" or "^0")
upper = constraint[0:i] + (n + 1,)
break break
return VersionRange(constraint, upper, Exclusivity.EXCLUSIVE) return VersionRange(constraint, upper, Exclusivity.EXCLUSIVE)
else: else:
return VersionRange(constraint, (major(constraint)+1,), Exclusivity.EXCLUSIVE) return VersionRange(
constraint, (major(constraint) + 1,), Exclusivity.EXCLUSIVE
)
# '~' matching (only allowed to bump the last present number by one) # '~' matching (only allowed to bump the last present number by one)
if (comparison_symbol == "~"): if comparison_symbol == "~":
return VersionRange(constraint, constraint[:-1] +(constraint[-1]+1,), Exclusivity.INCLUSIVE) return VersionRange(
constraint,
constraint[:-1] + (constraint[-1] + 1,),
Exclusivity.INCLUSIVE,
)
# Use semver package's comparisons for everything else: # Use semver package's comparisons for everything else:
# semver requires three version numbers # semver requires three version numbers
if len(constraint) < 3: if len(constraint) < 3:
while len(constraint) < 3: while len(constraint) < 3:
constraint = constraint+(0,) constraint = constraint + (0,)
constraint_str = constraint_str[0:first_digit.start()] + ".".join(map(str, constraint)) constraint_str = constraint_str[0 : first_digit.start()] + ".".join(
map(str, constraint)
)
# Convert special comparison strings to format accepted by `semver` library. # Convert special comparison strings to format accepted by `semver` library.
constraint_str = constraint_str.replace("", ">=").replace("", "<=") constraint_str = constraint_str.replace("", ">=").replace("", "<=")