🚨 Updated lints (#308)

This commit is contained in:
Nikhil Badyal
2023-08-25 15:36:50 +05:30
committed by GitHub
parent 09b815cb21
commit 77377cdd48
26 changed files with 404 additions and 487 deletions
+39 -78
View File
@@ -3,49 +3,42 @@ import concurrent
import hashlib
import pathlib
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, List
from typing import Dict, List, Self
from loguru import logger
from src.config import RevancedConfig
from src.downloader.sources import apk_sources
from src.exceptions import DownloadFailure, PatchingFailed
from src.exceptions import DownloadError, PatchingFailedError, UnknownError
from src.utils import slugify
class APP(object):
class APP:
"""Patched APK."""
def __init__(self, app_name: str, config: RevancedConfig):
def __init__(self: Self, app_name: str, config: RevancedConfig) -> None:
"""Initialize APP.
Args:
----
app_name (str): Name of the app.
config (RevancedConfig): Configuration object.
"""
from src.patches import Patches
self.app_name = app_name
self.app_version = config.env.str(f"{app_name}_VERSION".upper(), None)
self.experiment = False
self.cli_dl = config.env.str(f"{app_name}_CLI_DL".upper(), config.global_cli_dl)
self.patches_dl = config.env.str(
f"{app_name}_PATCHES_DL".upper(), config.global_patches_dl
)
self.integrations_dl = config.env.str(
f"{app_name}_INTEGRATIONS_DL".upper(), config.global_integrations_dl
)
self.patches_json_dl = config.env.str(
f"{app_name}_PATCHES_JSON_DL".upper(), config.global_patches_json_dl
)
self.exclude_request: List[str] = config.env.list(
f"{app_name}_EXCLUDE_PATCH".upper(), []
)
self.include_request: List[str] = config.env.list(
f"{app_name}_INCLUDE_PATCH".upper(), []
)
self.patches_dl = config.env.str(f"{app_name}_PATCHES_DL".upper(), config.global_patches_dl)
self.integrations_dl = config.env.str(f"{app_name}_INTEGRATIONS_DL".upper(), config.global_integrations_dl)
self.patches_json_dl = config.env.str(f"{app_name}_PATCHES_JSON_DL".upper(), config.global_patches_json_dl)
self.exclude_request: List[str] = config.env.list(f"{app_name}_EXCLUDE_PATCH".upper(), [])
self.include_request: List[str] = config.env.list(f"{app_name}_INCLUDE_PATCH".upper(), [])
self.resource: Dict[str, str] = {}
self.no_of_patches: int = 0
self.keystore_name = config.env.str(
f"{app_name}_KEYSTORE_FILE_NAME".upper(), config.global_keystore_name
)
self.archs_to_build = config.env.list(
f"{app_name}_ARCHS_TO_BUILD".upper(), config.global_archs_to_build
)
self.keystore_name = config.env.str(f"{app_name}_KEYSTORE_FILE_NAME".upper(), config.global_keystore_name)
self.archs_to_build = config.env.list(f"{app_name}_ARCHS_TO_BUILD".upper(), config.global_archs_to_build)
self.download_file_name = ""
self.download_dl = config.env.str(f"{app_name}_DL".upper(), "")
self.download_patch_resources(config)
@@ -53,7 +46,7 @@ class APP(object):
env_package_name = config.env.str(f"{app_name}_PACKAGE_NAME".upper(), None)
self.package_name = env_package_name or Patches.get_package_name(app_name)
def download_apk_for_patching(self, config: RevancedConfig) -> None:
def download_apk_for_patching(self: Self, config: RevancedConfig) -> None:
"""Download apk to be patched."""
from src.downloader.download import Downloader
from src.downloader.factory import DownloaderFactory
@@ -61,31 +54,22 @@ class APP(object):
if self.download_dl:
logger.info("Downloading apk to be patched using provided dl")
self.download_file_name = f"{self.app_name}.apk"
Downloader(config).direct_download(
self.download_dl, self.download_file_name
)
Downloader(config).direct_download(self.download_dl, self.download_file_name)
else:
logger.info("Downloading apk to be patched by scrapping")
try:
if not self.download_source:
self.download_source = apk_sources[self.app_name].format(
self.package_name
)
except KeyError:
raise DownloadFailure(
f"App {self.app_name} not supported officially yet. Please provide download "
"source in env."
)
downloader = DownloaderFactory.create_downloader(
config=config, apk_source=self.download_source
)
self.download_file_name, self.download_dl = downloader.download(
self.app_version, self
)
self.download_source = apk_sources[self.app_name].format(self.package_name)
except KeyError as key:
msg = f"App {self.app_name} not supported officially yet. Please provide download source in env."
raise DownloadError(
msg,
) from key
downloader = DownloaderFactory.create_downloader(config=config, apk_source=self.download_source)
self.download_file_name, self.download_dl = downloader.download(self.app_version, self)
def get_output_file_name(self) -> str:
"""The function returns a string representing the output file name for
an APK file appended with version.
def get_output_file_name(self: Self) -> str:
"""The function returns a string representing the output file name.
Returns
-------
@@ -93,32 +77,14 @@ class APP(object):
"""
return f"Re-{self.app_name}-{slugify(self.app_version)}-output.apk"
def set_recommended_version(self, version: str, exp: bool = False) -> None:
"""The function sets the recommended version and experiment flag for an
app.
Parameters
----------
version : str
The version parameter is a string that represents the recommended version of the app.
exp : bool, optional
The "exp" parameter is a boolean flag that indicates whether the specified version is for an
experimental or regular release. If "exp" is set to True, it means the version is for an
experimental release. If "exp" is set to False or not provided, it means the version is for
"""
self.app_version = version
self.experiment = exp
def __str__(self: "APP") -> str:
"""Returns the str representation of the app."""
attrs = vars(self)
return ", ".join([f"{key}: {value}" for key, value in attrs.items()])
@staticmethod
def download(
url: str, config: RevancedConfig, assets_filter: str, file_name: str = ""
) -> str:
"""The `download` function downloads a file from a given URL using a
specified configuration and filters the assets based on a given filter.
def download(url: str, config: RevancedConfig, assets_filter: str, file_name: str = "") -> str:
"""The `download` function downloads a file from a given URL & filters the assets based on a given filter.
Parameters
----------
@@ -156,9 +122,8 @@ class APP(object):
Downloader(config).direct_download(url, file_name)
return file_name
def download_patch_resources(self, config: RevancedConfig) -> None:
"""The function `download_patch_resources` downloads various resources
for patching in parallel using a ThreadPoolExecutor.
def download_patch_resources(self: Self, config: RevancedConfig) -> None:
"""The function `download_patch_resources` downloads various resources req. for patching.
Parameters
----------
@@ -177,10 +142,7 @@ class APP(object):
# Using a ThreadPoolExecutor for parallelism
with ThreadPoolExecutor(4) as executor:
futures = {
resource_name: executor.submit(self.download, *args)
for resource_name, *args in download_tasks
}
futures = {resource_name: executor.submit(self.download, *args) for resource_name, *args in download_tasks}
# Wait for all tasks to complete
concurrent.futures.wait(futures.values())
@@ -189,13 +151,12 @@ class APP(object):
for resource_name, future in futures.items():
try:
self.resource[resource_name] = future.result()
except Exception as e:
raise PatchingFailed(e) from e
except UnknownError as e:
raise PatchingFailedError(e) from e
@staticmethod
def generate_filename(url: str) -> str:
"""The function `generate_filename` takes a URL as input and returns a
hashed version of the URL as the filename.
"""The function `generate_filename` takes URL as input and returns a hashed version of the URL as the filename.
Parameters
----------