stouputils 1.12.2__py3-none-any.whl → 1.13.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. stouputils/__main__.py +11 -6
  2. stouputils/continuous_delivery/pypi.py +39 -1
  3. stouputils/continuous_delivery/pypi.pyi +9 -0
  4. stouputils/ctx.py +408 -408
  5. stouputils/data_science/config/set.py +125 -125
  6. stouputils/data_science/models/keras_utils/callbacks/model_checkpoint_v2.py +31 -31
  7. stouputils/data_science/utils.py +285 -285
  8. stouputils/installer/__init__.py +18 -18
  9. stouputils/installer/linux.py +144 -144
  10. stouputils/installer/main.py +223 -223
  11. stouputils/installer/windows.py +136 -136
  12. stouputils/py.typed +1 -1
  13. stouputils/stouputils/__init__.pyi +15 -0
  14. stouputils/stouputils/_deprecated.pyi +12 -0
  15. stouputils/stouputils/all_doctests.pyi +46 -0
  16. stouputils/stouputils/applications/__init__.pyi +2 -0
  17. stouputils/stouputils/applications/automatic_docs.pyi +106 -0
  18. stouputils/stouputils/applications/upscaler/__init__.pyi +3 -0
  19. stouputils/stouputils/applications/upscaler/config.pyi +18 -0
  20. stouputils/stouputils/applications/upscaler/image.pyi +109 -0
  21. stouputils/stouputils/applications/upscaler/video.pyi +60 -0
  22. stouputils/stouputils/archive.pyi +67 -0
  23. stouputils/stouputils/backup.pyi +109 -0
  24. stouputils/stouputils/collections.pyi +86 -0
  25. stouputils/stouputils/continuous_delivery/__init__.pyi +5 -0
  26. stouputils/stouputils/continuous_delivery/cd_utils.pyi +129 -0
  27. stouputils/stouputils/continuous_delivery/github.pyi +162 -0
  28. stouputils/stouputils/continuous_delivery/pypi.pyi +53 -0
  29. stouputils/stouputils/continuous_delivery/pyproject.pyi +67 -0
  30. stouputils/stouputils/continuous_delivery/stubs.pyi +39 -0
  31. stouputils/stouputils/ctx.pyi +211 -0
  32. stouputils/stouputils/decorators.pyi +242 -0
  33. stouputils/stouputils/image.pyi +172 -0
  34. stouputils/stouputils/installer/__init__.pyi +5 -0
  35. stouputils/stouputils/installer/common.pyi +39 -0
  36. stouputils/stouputils/installer/downloader.pyi +24 -0
  37. stouputils/stouputils/installer/linux.pyi +39 -0
  38. stouputils/stouputils/installer/main.pyi +57 -0
  39. stouputils/stouputils/installer/windows.pyi +31 -0
  40. stouputils/stouputils/io.pyi +213 -0
  41. stouputils/stouputils/parallel.pyi +211 -0
  42. stouputils/stouputils/print.pyi +136 -0
  43. stouputils/stouputils/version_pkg.pyi +15 -0
  44. {stouputils-1.12.2.dist-info → stouputils-1.13.1.dist-info}/METADATA +2 -2
  45. {stouputils-1.12.2.dist-info → stouputils-1.13.1.dist-info}/RECORD +47 -16
  46. {stouputils-1.12.2.dist-info → stouputils-1.13.1.dist-info}/WHEEL +0 -0
  47. {stouputils-1.12.2.dist-info → stouputils-1.13.1.dist-info}/entry_points.txt +0 -0
@@ -1,136 +1,136 @@
1
- """ Installer module for Windows specific functions.
2
-
3
- Provides Windows specific implementations for checking administrator privileges,
4
- determining appropriate installation paths (global/local), and modifying
5
- the user's PATH environment variable.
6
- """
7
- # Imports
8
- import os
9
-
10
- from ..decorators import LogLevels, handle_error
11
- from ..io import clean_path
12
- from ..print import debug, info, warning
13
- from .common import ask_install_type, prompt_for_path
14
-
15
-
16
- # Functions
17
- @handle_error(message="Failed to add to PATH (Windows)", error_log=LogLevels.WARNING_TRACEBACK)
18
- def add_to_path_windows(install_path: str) -> bool | None:
19
- """ Add install_path to the User PATH environment variable on Windows.
20
-
21
- Args:
22
- install_path (str): The path to add to the User PATH environment variable.
23
-
24
- Returns:
25
- bool | None: True if the path was added to the User PATH environment variable, None otherwise.
26
- """
27
- # Convert install_path to a Windows path if it's not already
28
- install_path = install_path.replace("/", "\\")
29
- os.makedirs(install_path, exist_ok=True)
30
-
31
- # Get current user PATH
32
- import winreg
33
- with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment", 0, winreg.KEY_READ | winreg.KEY_WRITE) as key:
34
-
35
- # Get the number of values in the registry key
36
- num_values = winreg.QueryInfoKey(key)[1]
37
-
38
- # Find the index of the 'Path' value
39
- path_index = -1
40
- for i in range(num_values):
41
- if winreg.EnumValue(key, i)[0] == 'Path':
42
- path_index = i
43
- break
44
-
45
- # Get the current path value
46
- current_path: str = winreg.EnumValue(key, path_index)[1]
47
-
48
- # Check if path is already present
49
- if install_path not in current_path.split(';'):
50
- new_path: str = f"{current_path};{install_path}"
51
- winreg.SetValueEx(key, "Path", 0, winreg.REG_EXPAND_SZ, new_path)
52
- debug(f"Added '{install_path}' to user PATH. Please restart your terminal for changes to take effect.")
53
- else:
54
- debug(f"'{install_path}' is already in user PATH.")
55
- return True
56
-
57
-
58
- def check_admin_windows() -> bool:
59
- """ Check if the script is running with administrator privileges on Windows. """
60
- try:
61
- import ctypes
62
- return ctypes.windll.shell32.IsUserAnAdmin() != 0
63
- except Exception:
64
- return False
65
-
66
-
67
- @handle_error(message="Failed to get installation path (Windows)", error_log=LogLevels.ERROR_TRACEBACK)
68
- def get_install_path_windows(
69
- program_name: str,
70
- ask_global: int = 0,
71
- add_path: bool = True,
72
- append_to_path: str = "",
73
- default_global: str = os.environ.get("ProgramFiles", "C:\\Program Files")
74
- ) -> str:
75
- """ Get the installation path for the program
76
-
77
- Args:
78
- program_name (str): The name of the program to install.
79
- ask_global (int): 0 = ask for anything, 1 = install globally, 2 = install locally
80
- add_path (bool): Whether to add the program to the PATH environment variable. (Only if installed globally)
81
- append_to_path (str): String to append to the installation path when adding to PATH.
82
- (ex: "bin" if executables are in the bin folder)
83
- default_global (str): The default global installation path.
84
- (Default is "C:\\Program Files" which is the most common location for executables on Windows)
85
-
86
- Returns:
87
- str: The installation path.
88
- """
89
- # Default path is located in the current working directory
90
- default_local_path: str = clean_path(os.path.join(os.getcwd(), program_name))
91
-
92
- # Define default global path (used in prompt even if not chosen initially)
93
- default_global_path: str = clean_path(os.path.join(default_global, program_name))
94
-
95
- # Ask user for installation type (global/local)
96
- install_type: str = ask_install_type(ask_global, default_local_path, default_global_path)
97
-
98
- # If the user wants to install globally,
99
- if install_type == 'g':
100
-
101
- # Check if the user has admin privileges,
102
- if not check_admin_windows():
103
-
104
- # If the user doesn't have admin privileges, fallback to local
105
- warning(
106
- f"Global installation requires administrator privileges. Please re-run as administrator.\n"
107
- f"Install locally instead to '{default_local_path}'? (Y/n): "
108
- )
109
- if input().lower() == 'n':
110
- info("Installation cancelled.")
111
- return ""
112
- else:
113
- # Fallback to local path if user agrees
114
- return prompt_for_path(
115
- f"Falling back to local installation path: {default_local_path}.",
116
- default_local_path
117
- )
118
-
119
- # If the user has admin privileges,
120
- else:
121
- # Ask it user wants to override the default global install path
122
- install_path: str = prompt_for_path(
123
- f"Default global installation path is {default_global_path}.",
124
- default_global_path
125
- )
126
- if add_path:
127
- add_to_path_windows(os.path.join(install_path, append_to_path))
128
- return install_path
129
-
130
- # Local install
131
- else: # install_type == 'l'
132
- return prompt_for_path(
133
- f"Default local installation path is {default_local_path}.",
134
- default_local_path
135
- )
136
-
1
+ """ Installer module for Windows specific functions.
2
+
3
+ Provides Windows specific implementations for checking administrator privileges,
4
+ determining appropriate installation paths (global/local), and modifying
5
+ the user's PATH environment variable.
6
+ """
7
+ # Imports
8
+ import os
9
+
10
+ from ..decorators import LogLevels, handle_error
11
+ from ..io import clean_path
12
+ from ..print import debug, info, warning
13
+ from .common import ask_install_type, prompt_for_path
14
+
15
+
16
+ # Functions
17
+ @handle_error(message="Failed to add to PATH (Windows)", error_log=LogLevels.WARNING_TRACEBACK)
18
+ def add_to_path_windows(install_path: str) -> bool | None:
19
+ """ Add install_path to the User PATH environment variable on Windows.
20
+
21
+ Args:
22
+ install_path (str): The path to add to the User PATH environment variable.
23
+
24
+ Returns:
25
+ bool | None: True if the path was added to the User PATH environment variable, None otherwise.
26
+ """
27
+ # Convert install_path to a Windows path if it's not already
28
+ install_path = install_path.replace("/", "\\")
29
+ os.makedirs(install_path, exist_ok=True)
30
+
31
+ # Get current user PATH
32
+ import winreg
33
+ with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment", 0, winreg.KEY_READ | winreg.KEY_WRITE) as key:
34
+
35
+ # Get the number of values in the registry key
36
+ num_values = winreg.QueryInfoKey(key)[1]
37
+
38
+ # Find the index of the 'Path' value
39
+ path_index = -1
40
+ for i in range(num_values):
41
+ if winreg.EnumValue(key, i)[0] == 'Path':
42
+ path_index = i
43
+ break
44
+
45
+ # Get the current path value
46
+ current_path: str = winreg.EnumValue(key, path_index)[1]
47
+
48
+ # Check if path is already present
49
+ if install_path not in current_path.split(';'):
50
+ new_path: str = f"{current_path};{install_path}"
51
+ winreg.SetValueEx(key, "Path", 0, winreg.REG_EXPAND_SZ, new_path)
52
+ debug(f"Added '{install_path}' to user PATH. Please restart your terminal for changes to take effect.")
53
+ else:
54
+ debug(f"'{install_path}' is already in user PATH.")
55
+ return True
56
+
57
+
58
+ def check_admin_windows() -> bool:
59
+ """ Check if the script is running with administrator privileges on Windows. """
60
+ try:
61
+ import ctypes
62
+ return ctypes.windll.shell32.IsUserAnAdmin() != 0
63
+ except Exception:
64
+ return False
65
+
66
+
67
+ @handle_error(message="Failed to get installation path (Windows)", error_log=LogLevels.ERROR_TRACEBACK)
68
+ def get_install_path_windows(
69
+ program_name: str,
70
+ ask_global: int = 0,
71
+ add_path: bool = True,
72
+ append_to_path: str = "",
73
+ default_global: str = os.environ.get("ProgramFiles", "C:\\Program Files")
74
+ ) -> str:
75
+ """ Get the installation path for the program
76
+
77
+ Args:
78
+ program_name (str): The name of the program to install.
79
+ ask_global (int): 0 = ask for anything, 1 = install globally, 2 = install locally
80
+ add_path (bool): Whether to add the program to the PATH environment variable. (Only if installed globally)
81
+ append_to_path (str): String to append to the installation path when adding to PATH.
82
+ (ex: "bin" if executables are in the bin folder)
83
+ default_global (str): The default global installation path.
84
+ (Default is "C:\\Program Files" which is the most common location for executables on Windows)
85
+
86
+ Returns:
87
+ str: The installation path.
88
+ """
89
+ # Default path is located in the current working directory
90
+ default_local_path: str = clean_path(os.path.join(os.getcwd(), program_name))
91
+
92
+ # Define default global path (used in prompt even if not chosen initially)
93
+ default_global_path: str = clean_path(os.path.join(default_global, program_name))
94
+
95
+ # Ask user for installation type (global/local)
96
+ install_type: str = ask_install_type(ask_global, default_local_path, default_global_path)
97
+
98
+ # If the user wants to install globally,
99
+ if install_type == 'g':
100
+
101
+ # Check if the user has admin privileges,
102
+ if not check_admin_windows():
103
+
104
+ # If the user doesn't have admin privileges, fallback to local
105
+ warning(
106
+ f"Global installation requires administrator privileges. Please re-run as administrator.\n"
107
+ f"Install locally instead to '{default_local_path}'? (Y/n): "
108
+ )
109
+ if input().lower() == 'n':
110
+ info("Installation cancelled.")
111
+ return ""
112
+ else:
113
+ # Fallback to local path if user agrees
114
+ return prompt_for_path(
115
+ f"Falling back to local installation path: {default_local_path}.",
116
+ default_local_path
117
+ )
118
+
119
+ # If the user has admin privileges,
120
+ else:
121
+ # Ask it user wants to override the default global install path
122
+ install_path: str = prompt_for_path(
123
+ f"Default global installation path is {default_global_path}.",
124
+ default_global_path
125
+ )
126
+ if add_path:
127
+ add_to_path_windows(os.path.join(install_path, append_to_path))
128
+ return install_path
129
+
130
+ # Local install
131
+ else: # install_type == 'l'
132
+ return prompt_for_path(
133
+ f"Default local installation path is {default_local_path}.",
134
+ default_local_path
135
+ )
136
+
stouputils/py.typed CHANGED
@@ -1 +1 @@
1
-
1
+
@@ -0,0 +1,15 @@
1
+ from ._deprecated import *
2
+ from .all_doctests import *
3
+ from .archive import *
4
+ from .backup import *
5
+ from .collections import *
6
+ from .continuous_delivery import *
7
+ from .ctx import *
8
+ from .decorators import *
9
+ from .image import *
10
+ from .io import *
11
+ from .parallel import *
12
+ from .print import *
13
+ from .version_pkg import *
14
+
15
+ __version__: str
@@ -0,0 +1,12 @@
1
+ from .decorators import LogLevels as LogLevels, deprecated as deprecated
2
+ from .io import csv_dump as csv_dump, csv_load as csv_load, json_dump as json_dump, json_load as json_load
3
+ from typing import Any
4
+
5
+ def super_csv_dump(*args: Any, **kwargs: Any) -> Any:
6
+ ''' Deprecated function, use "csv_dump" instead. '''
7
+ def super_csv_load(*args: Any, **kwargs: Any) -> Any:
8
+ ''' Deprecated function, use "csv_load" instead. '''
9
+ def super_json_dump(*args: Any, **kwargs: Any) -> Any:
10
+ ''' Deprecated function, use "json_dump" instead. '''
11
+ def super_json_load(*args: Any, **kwargs: Any) -> Any:
12
+ ''' Deprecated function, use "json_load" instead. '''
@@ -0,0 +1,46 @@
1
+ from . import decorators as decorators
2
+ from .decorators import measure_time as measure_time
3
+ from .io import clean_path as clean_path, relative_path as relative_path
4
+ from .print import error as error, info as info, progress as progress, warning as warning
5
+ from doctest import TestResults as TestResults
6
+ from types import ModuleType
7
+
8
+ def launch_tests(root_dir: str, strict: bool = True) -> int:
9
+ ''' Main function to launch tests for all modules in the given directory.
10
+
11
+ \tArgs:
12
+ \t\troot_dir\t\t\t\t(str):\t\t\tRoot directory to search for modules
13
+ \t\tstrict\t\t\t\t\t(bool):\t\t\tModify the force_raise_exception variable to True in the decorators module
14
+
15
+ \tReturns:
16
+ \t\tint: The number of failed tests
17
+
18
+ \tExamples:
19
+ \t\t>>> launch_tests("unknown_dir")
20
+ \t\tTraceback (most recent call last):
21
+ \t\t\t...
22
+ \t\tValueError: No modules found in \'unknown_dir\'
23
+
24
+ \t.. code-block:: python
25
+
26
+ \t\t> if launch_tests("/path/to/source") > 0:
27
+ \t\t\tsys.exit(1)
28
+ \t\t[PROGRESS HH:MM:SS] Importing module \'module1\'\ttook 0.001s
29
+ \t\t[PROGRESS HH:MM:SS] Importing module \'module2\'\ttook 0.002s
30
+ \t\t[PROGRESS HH:MM:SS] Importing module \'module3\'\ttook 0.003s
31
+ \t\t[PROGRESS HH:MM:SS] Importing module \'module4\'\ttook 0.004s
32
+ \t\t[INFO HH:MM:SS] Testing 4 modules...
33
+ \t\t[PROGRESS HH:MM:SS] Testing module \'module1\'\ttook 0.005s
34
+ \t\t[PROGRESS HH:MM:SS] Testing module \'module2\'\ttook 0.006s
35
+ \t\t[PROGRESS HH:MM:SS] Testing module \'module3\'\ttook 0.007s
36
+ \t\t[PROGRESS HH:MM:SS] Testing module \'module4\'\ttook 0.008s
37
+ \t'''
38
+ def test_module_with_progress(module: ModuleType, separator: str) -> TestResults:
39
+ """ Test a module with testmod and measure the time taken with progress printing.
40
+
41
+ \tArgs:
42
+ \t\tmodule\t\t(ModuleType):\tModule to test
43
+ \t\tseparator\t(str):\t\t\tSeparator string for alignment in output
44
+ \tReturns:
45
+ \t\tTestResults: The results of the tests
46
+ \t"""
@@ -0,0 +1,2 @@
1
+ from .automatic_docs import *
2
+ from .upscaler import *
@@ -0,0 +1,106 @@
1
+ from ..continuous_delivery import version_to_float as version_to_float
2
+ from ..decorators import LogLevels as LogLevels, handle_error as handle_error, simple_cache as simple_cache
3
+ from ..io import clean_path as clean_path, json_dump as json_dump, super_open as super_open
4
+ from ..print import info as info
5
+ from collections.abc import Callable as Callable
6
+
7
+ REQUIREMENTS: list[str]
8
+
9
+ def check_dependencies(html_theme: str) -> None:
10
+ ''' Check for each requirement if it is installed.
11
+
12
+ \tArgs:
13
+ \t\thtml_theme (str): HTML theme to use for the documentation, to check if it is installed (e.g. "breeze", "pydata_sphinx_theme", "furo", etc.)
14
+ \t'''
15
+ def get_sphinx_conf_content(project: str, project_dir: str, author: str, current_version: str, copyright: str, html_logo: str, html_favicon: str, html_theme: str = 'breeze', github_user: str = '', github_repo: str = '', version_list: list[str] | None = None, skip_undocumented: bool = True) -> str:
16
+ """ Get the content of the Sphinx configuration file.
17
+
18
+ \tArgs:
19
+ \t\tproject (str): Name of the project
20
+ \t\tproject_dir (str): Path to the project directory
21
+ \t\tauthor (str): Author of the project
22
+ \t\tcurrent_version (str): Current version
23
+ \t\tcopyright (str): Copyright information
24
+ \t\thtml_logo (str): URL to the logo
25
+ \t\thtml_favicon (str): URL to the favicon
26
+ \t\tgithub_user (str): GitHub username
27
+ \t\tgithub_repo (str): GitHub repository name
28
+ \t\tversion_list (list[str] | None): List of versions. Defaults to None
29
+ \t\tskip_undocumented (bool): Whether to skip undocumented members. Defaults to True
30
+
31
+ \tReturns:
32
+ \t\tstr: Content of the Sphinx configuration file
33
+ \t"""
34
+ def get_versions_from_github(github_user: str, github_repo: str, recent_minor_versions: int = 2) -> list[str]:
35
+ """ Get list of versions from GitHub gh-pages branch.
36
+ \tOnly shows detailed versions for the last N minor versions, and keeps only
37
+ \tthe latest patch version for older minor versions.
38
+
39
+ \tArgs:
40
+ \t\tgithub_user (str): GitHub username
41
+ \t\tgithub_repo (str): GitHub repository name
42
+ \t\trecent_minor_versions (int): Number of recent minor versions to show all patches for (-1 for all).
43
+
44
+ \tReturns:
45
+ \t\tlist[str]: List of versions, with 'latest' as first element
46
+ \t"""
47
+ def markdown_to_rst(markdown_content: str) -> str:
48
+ """ Convert markdown content to RST format.
49
+
50
+ \tArgs:
51
+ \t\tmarkdown_content (str): Markdown content
52
+
53
+ \tReturns:
54
+ \t\tstr: RST content
55
+ \t"""
56
+ def generate_index_rst(readme_path: str, index_path: str, project: str, github_user: str, github_repo: str, get_versions_function: Callable[[str, str, int], list[str]] = ..., recent_minor_versions: int = 2) -> None:
57
+ """ Generate index.rst from README.md content.
58
+
59
+ \tArgs:
60
+ \t\treadme_path (str): Path to the README.md file
61
+ \t\tindex_path (str): Path where index.rst should be created
62
+ \t\tproject (str): Name of the project
63
+ \t\tgithub_user (str): GitHub username
64
+ \t\tgithub_repo (str): GitHub repository name
65
+ \t\tget_versions_function (Callable[[str, str, int], list[str]]): Function to get versions from GitHub
66
+ \t\trecent_minor_versions (int): Number of recent minor versions to show all patches for. Defaults to 2
67
+ \t"""
68
+ def generate_documentation(source_dir: str, modules_dir: str, project_dir: str, build_dir: str) -> None:
69
+ """ Generate documentation using Sphinx.
70
+
71
+ \tArgs:
72
+ \t\tsource_dir (str): Source directory
73
+ \t\tmodules_dir (str): Modules directory
74
+ \t\tproject_dir (str): Project directory
75
+ \t\tbuild_dir (str): Build directory
76
+ \t"""
77
+ def generate_redirect_html(filepath: str) -> None:
78
+ """ Generate HTML content for redirect page.
79
+
80
+ \tArgs:
81
+ \t\tfilepath (str): Path to the file where the HTML content should be written
82
+ \t"""
83
+ def update_documentation(root_path: str, project: str, project_dir: str = '', author: str = 'Author', copyright: str = '2025, Author', html_logo: str = '', html_favicon: str = '', html_theme: str = 'breeze', github_user: str = '', github_repo: str = '', version: str | None = None, skip_undocumented: bool = True, recent_minor_versions: int = 2, get_versions_function: Callable[[str, str, int], list[str]] = ..., generate_index_function: Callable[..., None] = ..., generate_docs_function: Callable[..., None] = ..., generate_redirect_function: Callable[[str], None] = ..., get_conf_content_function: Callable[..., str] = ...) -> None:
84
+ ''' Update the Sphinx documentation.
85
+
86
+ \tArgs:
87
+ \t\troot_path (str): Root path of the project
88
+ \t\tproject (str): Name of the project
89
+ \t\tproject_dir (str): Path to the project directory (to be used with generate_docs_function)
90
+ \t\tauthor (str): Author of the project
91
+ \t\tcopyright (str): Copyright information
92
+ \t\thtml_logo (str): URL to the logo
93
+ \t\thtml_favicon (str): URL to the favicon
94
+ \t\thtml_theme (str): Theme to use for the documentation. Defaults to "breeze"
95
+ \t\tgithub_user (str): GitHub username
96
+ \t\tgithub_repo (str): GitHub repository name
97
+ \t\tversion (str | None): Version to build documentation for (e.g. "1.0.0", defaults to "latest")
98
+ \t\tskip_undocumented (bool): Whether to skip undocumented members. Defaults to True
99
+ \t\trecent_minor_versions (int): Number of recent minor versions to show all patches for. Defaults to 2
100
+
101
+ \t\tget_versions_function (Callable[[str, str, int], list[str]]): Function to get versions from GitHub
102
+ \t\tgenerate_index_function (Callable[..., None]): Function to generate index.rst
103
+ \t\tgenerate_docs_function (Callable[..., None]): Function to generate documentation
104
+ \t\tgenerate_redirect_function (Callable[[str], None]): Function to create redirect file
105
+ \t\tget_conf_content_function (Callable[..., str]): Function to get Sphinx conf.py content
106
+ \t'''
@@ -0,0 +1,3 @@
1
+ from .config import *
2
+ from .image import *
3
+ from .video import *
@@ -0,0 +1,18 @@
1
+ WAIFU2X_NCNN_VULKAN_RELEASES: dict[str, str]
2
+ FFMPEG_RELEASES: dict[str, str]
3
+ YOUTUBE_BITRATE_RECOMMENDATIONS: dict[str, dict[str, dict[int, int]]]
4
+
5
+ class Config:
6
+ """ Configuration class for the upscaler. """
7
+ JPG_QUALITY: int
8
+ VIDEO_FINAL_BITRATE: int
9
+ FFMPEG_EXECUTABLE: str
10
+ FFMPEG_ARGS: tuple[str, ...]
11
+ FFPROBE_EXECUTABLE: str
12
+ FFMPEG_CHECK_HELP_TEXT: str
13
+ UPSCALER_EXECUTABLE: str
14
+ UPSCALER_ARGS: tuple[str, ...]
15
+ UPSCALER_EXECUTABLE_HELP_TEXT: str
16
+ SLIGHTLY_FASTER_MODE: bool
17
+ upscaler_executable_checked: bool
18
+ ffmpeg_executable_checked: bool
@@ -0,0 +1,109 @@
1
+ from ...installer import check_executable as check_executable
2
+ from ...io import clean_path as clean_path
3
+ from ...parallel import multithreading as multithreading
4
+ from ...print import colored_for_loop as colored_for_loop, debug as debug, info as info
5
+ from .config import Config as Config, WAIFU2X_NCNN_VULKAN_RELEASES as WAIFU2X_NCNN_VULKAN_RELEASES
6
+ from tempfile import TemporaryDirectory
7
+
8
+ def convert_frame(frame_path: str, delete_png: bool = True) -> None:
9
+ ''' Convert a PNG frame to JPG format to take less space.
10
+
11
+ \tArgs:
12
+ \t\tframe_path (str): Path to the PNG frame to convert.
13
+ \t\tdelete_png (bool): Whether to delete the original PNG file after conversion.
14
+
15
+ \tReturns:
16
+ \t\tNone: This function doesn\'t return anything.
17
+
18
+ \tExample:
19
+ \t\t.. code-block:: python
20
+
21
+ \t\t\t> convert_frame("input.png", delete_png=True)
22
+ \t\t\t> # input.png will be converted to input.jpg and the original file will be deleted
23
+
24
+ \t\t\t> convert_frame("input.png", delete_png=False)
25
+ \t\t\t> # input.png will be converted to input.jpg and the original file will be kept
26
+ \t'''
27
+ def get_all_files(folder: str, suffix: str | tuple[str, ...] = '') -> list[str]:
28
+ ''' Get all files paths in a folder, with a specific suffix if provided.
29
+
30
+ \tArgs:
31
+ \t\tfolder (str): Path to the folder containing the files.
32
+ \t\tsuffix (str | tuple[str, ...]): Suffix of the files to get (e.g. ".png", ".jpg", etc.).
33
+
34
+ \tReturns:
35
+ \t\tlist[str]: List of all files paths in the folder.
36
+
37
+ \tExample:
38
+ \t\t>>> files: list[str] = get_all_files("some_folder", ".png")
39
+ \t\t>>> len(files)
40
+ \t\t0
41
+ \t'''
42
+ def create_temp_dir_for_not_upscaled(input_path: str, output_path: str) -> TemporaryDirectory[str] | None:
43
+ """ Creates a temporary directory containing only images that haven't been upscaled yet.
44
+
45
+ Args:
46
+ input_path (str): Path to the folder containing input images.
47
+ output_path (str): Path to the folder where upscaled images are saved.
48
+
49
+ Returns:
50
+ TemporaryDirectory[str] | None: A temporary directory object if there are images to process,
51
+ None if all images are already upscaled.
52
+ """
53
+ def check_upscaler_executable() -> None: ...
54
+ def upscale(input_path: str, output_path: str, upscale_ratio: int) -> None:
55
+ ''' Upscale an input image (or a directory of images) with the upscaler executable.
56
+
57
+ \tArgs:
58
+ \t\tinput_path (str): Path to the image to upscale (or a directory).
59
+ \t\toutput_path (str): Path to the output image (or a directory).
60
+ \t\tupscale_ratio (int): Upscaling ratio.
61
+
62
+ \tExample:
63
+ \t\t.. code-block:: python
64
+
65
+ \t\t\t> upscale("folder", "folder", 2)
66
+ \t\t\tTraceback (most recent call last):
67
+ \t\t\t\t...
68
+ \t\t\tAssertionError: Input and output paths cannot be the same, got \'folder\'
69
+
70
+ \t\t\t> upscale("stouputils", "stouputils/output.jpg", 2)
71
+ \t\t\tTraceback (most recent call last):
72
+ \t\t\t\t...
73
+ \t\t\tAssertionError: If input is a directory, output must be a directory too, got \'stouputils/output.jpg\'
74
+
75
+
76
+ \t\t\t> upscale("input.jpg", "output.jpg", 2)
77
+ \t\t\t> # The input.jpg will be upscaled to output.jpg with a ratio of 2
78
+
79
+ \t\t\t> upscale("input_folder", "output_folder", 2)
80
+ \t\t\t> # The input_folder will be upscaled to output_folder with a ratio of 2
81
+ \t'''
82
+ def upscale_images(images: list[str], output_folder: str, upscale_ratio: int, desc: str = 'Upscaling images') -> None:
83
+ """ Upscale multiple images from a list.
84
+
85
+ \tArgs:
86
+ \t\timages (list[str]): List of paths to the images to upscale.
87
+ \t\toutput_folder (str): Path to the output folder where the upscaled images will be saved.
88
+ \t\tupscale_ratio (int): Upscaling ratio.
89
+ \t\tdesc (str): Description of the function execution displayed in the progress bar.
90
+ \t\t\tNo progress bar will be displayed if desc is empty.
91
+
92
+ \tReturns:
93
+ \t\tNone: This function doesn't return anything.
94
+ \t"""
95
+ def upscale_folder(input_folder: str, output_folder: str, upscale_ratio: int, slightly_faster_mode: bool = True, desc: str = 'Upscaling folder') -> None:
96
+ """ Upscale all images in a folder.
97
+
98
+ \tArgs:
99
+ \t\tinput_folder (str): Path to the input folder containing the images to upscale.
100
+ \t\toutput_folder (str): Path to the output folder where the upscaled images will be saved.
101
+ \t\tupscale_ratio (int): Upscaling ratio.
102
+ \t\tslightly_faster_mode (bool): Whether to use the slightly faster mode (no progress bar),
103
+ \t\t\tone call to the upscaler executable.
104
+ \t\tdesc (str): Description of the function execution displayed in the progress bar.
105
+ \t\t\tNo progress bar will be displayed if desc is empty.
106
+
107
+ \tReturns:
108
+ \t\tNone: This function doesn't return anything.
109
+ \t"""
@@ -0,0 +1,60 @@
1
+ from ...installer import check_executable as check_executable
2
+ from ...io import clean_path as clean_path
3
+ from ...parallel import multithreading as multithreading
4
+ from ...print import colored_for_loop as colored_for_loop, debug as debug, error as error, info as info, warning as warning
5
+ from .config import Config as Config, FFMPEG_RELEASES as FFMPEG_RELEASES, YOUTUBE_BITRATE_RECOMMENDATIONS as YOUTUBE_BITRATE_RECOMMENDATIONS
6
+ from .image import convert_frame as convert_frame, get_all_files as get_all_files, upscale_folder as upscale_folder
7
+ from typing import Literal
8
+
9
+ def get_recommended_bitrate(resolution: tuple[int, int], frame_rate: int = 60, upload_type: Literal['SDR', 'HDR'] = 'SDR') -> int:
10
+ ''' Get the recommended bitrate (in kbps) for the output video based on the video resolution.
11
+
12
+ \tArgs:
13
+ \t\tresolution (tuple[int, int]): Video resolution (width, height).
14
+ \t\tframe_rate (int): Frame rate of the video, default is 60.
15
+ \t\tupload_type (Literal["SDR","HDR"]): Upload type from which the recommendation is made, default is "SDR".
16
+
17
+ \tReturns:
18
+ \t\tint: The recommended bitrate for the output video (in kbps)
19
+
20
+ \tSource: https://support.google.com/youtube/answer/1722171?hl=en#zippy=%2Cbitrate
21
+
22
+ \tExamples:
23
+ \t\t>>> # Valid examples
24
+ \t\t>>> get_recommended_bitrate((3840, 2160), 60, "SDR")
25
+ \t\t68000
26
+ \t\t>>> get_recommended_bitrate((1920, 1080), 60, "HDR")
27
+ \t\t15000
28
+ \t\t>>> get_recommended_bitrate((1920, 1080), 60, "SDR")
29
+ \t\t12000
30
+ \t\t>>> get_recommended_bitrate((1920, 1080), 30, "SDR")
31
+ \t\t8000
32
+
33
+ \t\t>>> # Invalid examples
34
+ \t\t>>> get_recommended_bitrate((1920, 1080), 60, "Ratio")
35
+ \t\tTraceback (most recent call last):
36
+ \t\t\t...
37
+ \t\tAssertionError: Invalid upload type: \'Ratio\'
38
+ \t\t>>> get_recommended_bitrate("1920x1080", 60, "SDR")
39
+ \t\tTraceback (most recent call last):
40
+ \t\t\t...
41
+ \t\tAssertionError: Invalid resolution: 1920x1080, must be a tuple of two integers
42
+ \t\t>>> get_recommended_bitrate((1920, 1080), -10, "SDR")
43
+ \t\tTraceback (most recent call last):
44
+ \t\t\t...
45
+ \t\tAssertionError: Invalid frame rate: -10, must be a positive integer
46
+ \t'''
47
+ def check_ffmpeg_executable() -> None: ...
48
+ def upscale_video(video_file: str, input_folder: str, progress_folder: str, output_folder: str) -> None:
49
+ """ Handles a video file. """
50
+ def video_upscaler_cli(input_folder: str, progress_folder: str, output_folder: str) -> None:
51
+ """ Upscales videos from an input folder and saves them to an output folder.
52
+
53
+ \tUses intermediate folders for extracted and upscaled frames within the progress folder.
54
+ \t**Handles resuming partially processed videos.**
55
+
56
+ \tArgs:
57
+ \t\tinput_folder (str): Path to the folder containing input videos.
58
+ \t\tprogress_folder (str): Path to the folder for storing intermediate files (frames).
59
+ \t\toutput_folder (str): Path to the folder where upscaled videos will be saved.
60
+ \t"""