Skip to content

Commit

Permalink
Make linter satisfied
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Mar 15, 2021
1 parent 89512a7 commit 98c72e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/tribler-common/tribler_common/version_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class VersionError(Exception):
pass


# pylint: disable=too-many-instance-attributes
class TriblerVersion:
version_str: str
version_tuple: Tuple
Expand Down Expand Up @@ -186,12 +187,13 @@ class VersionHistory:
root_state_dir: Path
file_path: Path
file_data: Dict
versions: OrderedDict[Tuple[int, int], TriblerVersion]
versions: OrderedDict[Tuple[int, int], TriblerVersion] # pylint: disable=unsubscriptable-object
versions_by_number: List[TriblerVersion]
versions_by_time: List[TriblerVersion]
last_run_version: Optional[TriblerVersion]
code_version: TriblerVersion

# pylint: disable=too-many-branches
def __init__(self, root_state_dir: Path, code_version_id: Optional[str] = None):
if code_version_id is None:
code_version_id = tribler_core.version.version_id
Expand All @@ -201,21 +203,7 @@ def __init__(self, root_state_dir: Path, code_version_id: Optional[str] = None):
self.file_data = {"last_version": None, "history": {}}
self.versions = versions = OrderedDict()
if self.file_path.exists():
self.file_data = json.loads(self.file_path.read_text().strip())
if "history" not in self.file_data:
raise VersionError("Invalid history file structure")

# timestamps needs to be converted to float before sorting
history_items = [
(float(time_str), version_str) for time_str, version_str in self.file_data["history"].items()
]
for timestamp, version_str in sorted(history_items):
version = TriblerVersion(root_state_dir, version_str, timestamp)
# store only versions with directories:
if version.state_exists():
# eventually store only the latest launched version with the same major_minor tuple
self.add_version(version)

self.load(self.file_path)
elif (root_state_dir / "triblerd.conf").exists():
# Pre-7.4 versions of Tribler don't have history file
# and can by detected by presence of the triblerd.conf file in the root directory
Expand Down Expand Up @@ -268,6 +256,20 @@ def __repr__(self):
s = ','.join(str(v.major_minor) for v in self.versions_by_time)
return f'<{self.__class__.__name__}[{s}]>'

def load(self, file_path: Path):
self.file_data = json.loads(file_path.read_text().strip())
if "history" not in self.file_data:
raise VersionError("Invalid history file structure")

# timestamps needs to be converted to float before sorting
history_items = [(float(time_str), version_str) for time_str, version_str in self.file_data["history"].items()]
for timestamp, version_str in sorted(history_items):
version = TriblerVersion(self.root_state_dir, version_str, timestamp)
# store only versions with directories:
if version.state_exists():
# eventually store only the latest launched version with the same major_minor tuple
self.add_version(version)

def add_version(self, version):
self.versions[version.major_minor] = version
self.versions.move_to_end(version.major_minor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def test_copy_state_directory(tmpdir):
assert filecmp.cmp(src_dir / 'ec_multichain.pem', tgt_dir / 'ec_multichain.pem')


# pylint: disable=too-many-locals
def test_get_disposable_state_directories(tmpdir_factory):
tmpdir = tmpdir_factory.mktemp("scenario")
root_state_dir = Path(tmpdir)
Expand Down

0 comments on commit 98c72e7

Please sign in to comment.