Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
refactor: improve Version comparison logic (#10)
Browse files Browse the repository at this point in the history
Signed-off-by: César Román <thecesrom@gmail.com>
  • Loading branch information
cesarcoatl committed Mar 22, 2022
1 parent e0e6d54 commit 0fdb0e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ signature-mutators=

# List of additional names supposed to be defined in builtins. Remember that
# you should avoid defining new builtins when possible.
additional-builtins=long,unicode
additional-builtins=basestring,long,unicode

# Tells whether unused global variables should be treated as a violation.
allow-global-unused-variables=yes
Expand Down
78 changes: 29 additions & 49 deletions src/com/inductiveautomation/ignition/common/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,34 @@

import re

from java.lang import Object
from java.lang import IllegalArgumentException, Object


class Version(Object):
dev = True
snapshot = False

def __init__(self, major=0, minor=0, rev=0, build=0, beta=0, rc=0):
"""Version initializer.
Args:
major (int): Major number.
minor (int): Minor number.
rev (int): Revision number.
build (int): Build number.
beta (int): Beta number.
rc (int): Release Candidate number.
"""
self.major = major
self.minor = minor
self.rev = rev
self.build = build
self.beta = beta
self.rc = rc

def __eq__(self, other, strict=False):
ret = 0
version_1 = [self.major, self.minor, self.rev]
version_2 = [other.major, other.minor, other.rev]

if strict:
version_1.extend((self.build, self.beta, self.rc))
version_2.extend((other.build, other.beta, other.rc))
for i in range(max(len(version_1), len(version_2))):
v_1 = version_1[i] if i < len(version_1) else 0
v_2 = version_2[i] if i < len(version_2) else 0
if v_1 > v_2:
ret = 1
elif v_1 < v_2:
ret = -1
return ret
def __eq__(self, other):
return self._compare(other, True) == 0

def __str__(self):
return self.toString()

def compareTo(self, that):
"""Compares two Versions.
Note that this comparison is stricter than we want for Gateway
restores or project imports. For those, isFutureVersion().
Args:
that (Version): The version to compare.
def _compare(self, that, strict=False):
this = self.toTuple(strict)
that = that.toTuple(strict)
return (this > that) - (this < that)

Returns:
int: 0 if self and that are equal, -1 if that is greater
than self, or 1 if self is greater than that.
"""
return self.__eq__(that, True)
def compareTo(self, that):
return self._compare(that, True) if isinstance(that, type(self)) else None

def exists(self):
pass
Expand Down Expand Up @@ -101,24 +70,28 @@ def isDev(self):
return self.dev

def isFutureVersion(self, arg):
other = self.parse(arg)
return self.__eq__(other) == -1
cls = type(self)
if isinstance(arg, basestring):
that = self.parse(arg)
elif isinstance(arg, cls):
that = arg
else:
raise TypeError("isFutureVersion(): 1st arg can't be coerced to String.")

return self._compare(that) == -1

def isSnapshot(self):
return self.snapshot

@staticmethod
def parse(s):
sem_ver = [int(i) for i in re.findall(r"-?\d+", s)]
return Version(sem_ver[0], sem_ver[1], sem_ver[2])
if len(sem_ver) < 3:
raise IllegalArgumentException('Invalid version: "{}"'.format(s))
build_number = sem_ver[3] if len(sem_ver) == 4 else 0
return Version(sem_ver[0], sem_ver[1], sem_ver[2], build_number)

def toParseableString(self):
"""Returns the version as a compact, parseable (non-XML) string
that can be parsed with the 1-string constructor of this class.
Returns:
string: Compact, parseable (non-XML) string.
"""
return "{}.{}.{}.{}".format(self.major, self.minor, self.rev, self.build)

def toString(self):
Expand All @@ -134,3 +107,10 @@ def toString(self):
return "{}.{}.{} (b{})".format(self.major, self.minor, self.rev, self.build)

return "{}.{}.{}".format(self.major, self.minor, self.rev)

def toTuple(self, strict=False):
return (
(self.major, self.minor, self.rev, self.build, self.beta, self.rc)
if strict
else (self.major, self.minor, self.rev)
)

0 comments on commit 0fdb0e4

Please sign in to comment.