Files
Lake Chan 86f098cadc Migrate Config from dict to object (#2464)
* refactor: migrate config from dict to object

* fix: type hint import

* fix: BottleConfig.load()

* fix: config loading in BackupManager.import_backup()

* fix: Remove unused import

* fix: more guard code, type hints and Config object usage

* fix: a potential crash when SteamManager.get_app_config() failed

* Revert "fix: Remove unused import"

This reverts commit 2d48a4e08a.

* add: new config field

* fix: Config object value read

* misc: remove review TODO comments

* adapt to new BottleConfig object

* fix RunAsync usage

* add: BottleConfig._fix()

* fix: type hint and Config object usage

* fix: typo in FSR setting default value

* adapt to new BottleConfig object

* remove unnecessary DictCompat method

* add: BottleConfig._filter()

* fix: TemplateManager.new()

* misc: critical guard code

* fix: typo in commit b7ed61e678

* misc: more type hinting

* Revert "remove unnecessary DictCompat method"

This reverts commit 265633e733.

* fix crash when user choose Cancel in ProtonAlertDialog

* fix force stop all programs not work

* adapt NVAPIComponent.check_bottle_nvngx to Config object

* Better DictCompatMixIn

* InstallerManager.__set_parameters incorrect adopt

* fix typo in cbe1dc760b

* adopt LibraryManager.download_thumbnail to Config object

* serialize Config object to yaml automatically

* fix translation and remove workaround in 0830fddbc9

* fix missing locale for Chinese

* comment

* fix incorrect variable init position

* remove 'in' extensions and some bugfix

* fix executable filemode

Co-authored-by: Mirko Brombin <send@mirko.pm>
2023-01-20 17:02:40 +01:00

79 lines
2.1 KiB
Python

import os
import hashlib
class Diff:
"""
This class is no more used by the application, it's just a
reference for future implementations.
"""
__ignored = [
"dosdevices",
"users",
"bottle.yml",
"storage"
]
@staticmethod
def hashify(path: str) -> dict:
"""
Hash (SHA-1) all files in a directory and return
them in a dictionary. Here we use SHA-1 instead of
better ones like SHA-256 because we only need to
compare the file hashes, it's faster, and it's
not a security risk.
"""
_files = {}
if path[-1] != os.sep:
'''
Be sure to add a trailing slash at the end of the path to
prevent the correct path name in the result.
'''
path += os.sep
for root, dirs, files in os.walk(path):
dirs[:] = [d for d in dirs if d not in Diff.__ignored]
for f in files:
if f in Diff.__ignored:
continue
with open(os.path.join(root, f), "rb") as fr:
_hash = hashlib.sha1(fr.read()).hexdigest()
_key = os.path.join(root, f)
_key = _key.replace(path, "")
_files[_key] = _hash
return _files
@staticmethod
def file_hashify(path: str) -> str:
"""Hash (SHA-1) a file and return it."""
with open(path, "rb") as fr:
_hash = hashlib.sha1(fr.read()).hexdigest()
return _hash
@staticmethod
def compare(parent: dict, child: dict) -> dict:
"""
Compare two hashes dictionaries and return the
differences (added, removed, changed).
"""
added = []
changed = []
removed = [f for f in parent if f not in child]
for f in child:
if f not in parent:
added.append(f)
elif parent[f] != child[f]:
changed.append(f)
return {
"added": added,
"removed": removed,
"changed": changed
}