Skip to content

Commit d93c271

Browse files
committed
Refactors, Renaming, & Format Changes
--- - Renamed multiple vars/classes across entire project. - Placed `_get_stable_assets` & `_get_dev_assets` into `_PatcherAssets` class. - Improvements to method docstrings. - Changed default log msg format. - Improved consistency of console/log strings. - Added patch version to download message. - Removed unnecessary exception vars from error handling. - Changes to "./README.md". Signed-off-by: schlopp96 <[email protected]>
1 parent 90c07de commit d93c271

File tree

8 files changed

+198
-181
lines changed

8 files changed

+198
-181
lines changed

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@
6767
6868
2. Once the script is run, you will be presented with an option menu, allowing you to choose from the following commands:
6969
70-
- Patch BepInEx to the latest stable release.
71-
- Patch BepInEx to the latest developmental/experimental "bleeding-edge" build.
72-
- Patch BepInEx first with the latest stable release, and **then** with the latest experimental build to ensure a safe installation.
73-
- Check for and download new releases/builds of BepInEx.
74-
- Start Valheim.
75-
- Exit the application.
70+
- **[1].** Patch BepInEx to the latest stable release.
71+
- **[2].** Patch BepInEx to the latest developmental/experimental "bleeding-edge" build.
72+
- **[3].** Patch BepInEx first with the latest stable release, and **then** with the latest experimental build to ensure a safe installation.
73+
- **[4].** Check for and download new releases/builds of BepInEx.
74+
- **[5].** Start Valheim.
75+
- **[6].** Exit the application.
7676
7777
3. Once an option is chosen, you will then be asked to confirm that the correct option/location is chosen.
7878
@@ -84,13 +84,7 @@
8484
8585
6. If you choose to **NOT** run the game, the patcher will return to the main menu.
8686
87-
- **_NOTE:_**
88-
89-
- BepInEx will list its current version as the last stable build number, even if a bleeding-edge build is installed. It will still work all the same.
90-
91-
- If you wish to verify, you can either compare the files contained in the patch to the ones you have on your machine using a diff tool, or simply side-to-side by eye.
92-
93-
- **_Note that you can also find the latest bleeding-edge-builds of BepInEx [here](https://builds.bepis.io/projects/bepinex_be)._**
87+
- **_Note that you can also find the latest bleeding-edge-builds of BepInEx [here](https://builds.bepis.io/projects/bepinex_be)._**
9488
9589
---
9690

VBPatcher/appglobals/globals.py

Lines changed: 82 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,99 @@
55
from requests import Response
66

77

8-
def _get_stable_assets(url: str, mode: int) -> str:
9-
"""Send GET request to retrieve BepInEx stable release assets, depending on value passed to :param:`mode`.
10-
11-
- If :param:`mode` == `1`, get patch archive download link.
12-
- If :param:`mode` != `1`, get patch release version number as string.
8+
class _PatcherAssets:
9+
"""Wrapper for retrieving necessary patcher assets.
1310
1411
---
1512
16-
:param url: URL for the new :class:`Request` object.
17-
:type url: :class:`str` | :class:`PathLike`
18-
:param mode: specifies values to retrieve from response.
19-
:type mode: :class:`int`
20-
:return: response content from GET request.
21-
:rtype: :class:`str`
13+
Contained Methods:
14+
15+
- :func:`_get_stable_assets(url, mode) -> str`
16+
- Send GET request to retrieve BepInEx stable build assets, depending on value passed to :param:`mode`.
17+
- Static method.
18+
19+
- :func:`_get_dev_assets(url, mode) -> str`
20+
- Send GET request to retrieve BepInEx dev/bleeding-edge build assets, depending on value passed to :param:`mode`.
21+
- Static method.
2222
"""
2323

24-
r: Response = req.get(url, 'html.parser') # Send GET request.
25-
r.raise_for_status() # Raise exception if response is not 200.
24+
@staticmethod
25+
def _get_stable_assets(url: str, mode: int) -> str:
26+
"""Send GET request to retrieve BepInEx stable build assets, depending on value passed to :param:`mode`.
2627
27-
dl_link: str = r.json()['assets'][1][
28-
'browser_download_url'] # Get download link.
29-
patch_ver: str = r.json()['assets'][1]['name'][12:20] # Get patch version.
28+
- If :param:`mode` == `1`, get patch archive download link.
29+
- If :param:`mode` != `1`, get patch version number as string.
3030
31-
return dl_link if mode == 1 else patch_ver
31+
---
3232
33+
:param url: URL for the new :class:`Request` object.
34+
:type url: :class:`str` | :class:`PathLike`
35+
:param mode: specifies which values to retrieve from response.
36+
:type mode: :class:`int`
37+
:return: response content from GET request.
38+
:rtype: :class:`str`
39+
"""
3340

34-
def _get_dev_assets(url: str, mode: int) -> str:
35-
"""Send GET request to retrieve BepInEx dev/bleeding-edge release assets, depending on value passed to :param:`mode`.
41+
r: Response = req.get(url, 'html.parser') # Send GET request.
42+
r.raise_for_status() # Raise exception if response is not 200.
3643

37-
- If :param:`mode` == `1`, get patch archive download link.
38-
- If :param:`mode` != `1`, get patch version number as string.
44+
dl_link: str = r.json()['assets'][1][
45+
'browser_download_url'] # Get download link.
46+
patch_ver: str = r.json()['assets'][1]['name'][
47+
12:20] # Get patch version.
3948

40-
---
49+
return dl_link if mode == 1 else patch_ver
4150

42-
:param url: URL for the new :class:`Request` object.
43-
:type url: :class:`str` | :class:`PathLike`
44-
:param mode: specifies values to retrieve from response.
45-
:type mode: :class:`int`
46-
:return: response content from GET request.
47-
:rtype: :class:`str`
48-
"""
51+
@staticmethod
52+
def _get_dev_assets(url: str, mode: int) -> str:
53+
"""Send GET request to retrieve BepInEx dev/bleeding-edge build assets, depending on value passed to :param:`mode`.
4954
50-
retrieved: int = 0
55+
- If :param:`mode` == `1`, get patch archive download link.
56+
- If :param:`mode` != `1`, get patch version number as string.
5157
52-
r: Response = req.get(url) # Send GET request.
53-
r.raise_for_status() # Raise exception if error code is returned.
58+
---
5459
55-
soup = bs4.BeautifulSoup(r.content, 'html.parser') # Parse response HTML.
56-
results = soup.find_all(
57-
'div', class_="artifacts-list"
58-
) # Get all <div> tags with class="artifacts-list".
60+
:param url: URL for the new :class:`Request` object.
61+
:type url: :class:`str` | :class:`PathLike`
62+
:param mode: specifies values to retrieve from response.
63+
:type mode: :class:`int`
64+
:return: response content from GET request.
65+
:rtype: :class:`str`
66+
"""
5967

60-
for result in results:
61-
if retrieved < 1: # Only get 2nd <div> tag.
62-
link = result.find_all('a')[1]['href'] # Get download link.
63-
retrieved += 1
68+
retrieved: int = 0
6469

65-
dl_link = (f'{url[:26]}{link}') # Combine URL and download link.
70+
r: Response = req.get(url) # Send GET request.
71+
r.raise_for_status() # Raise exception if error code is returned.
6672

67-
return dl_link if mode == 1 else dl_link[93:100]
73+
soup = bs4.BeautifulSoup(r.content,
74+
'html.parser') # Parse response HTML.
75+
results = soup.find_all(
76+
'div', class_="artifacts-list"
77+
) # Get all <div> tags with class="artifacts-list".
78+
79+
for result in results:
80+
if retrieved < 1: # Only get 2nd <div> tag.
81+
link = result.find_all('a')[1]['href'] # Get download link.
82+
retrieved += 1
83+
84+
dl_link = (f'{url[:26]}{link}') # Combine URL and download link.
85+
86+
return dl_link if mode == 1 else dl_link[93:100]
6887

6988

7089
class _Getch:
71-
"""Gets a single character from standard input. Does not echo to the screen."""
90+
"""Retrieve single character from standard input. Does not echo to the screen.
91+
92+
- If system is running on Windows-based OS, initialize :class:`_GetchWindows` class.
93+
- If system is running on UNIX-based OS, initialize :class:`_GetchUnix` class.
94+
"""
7295

7396
def __init__(self):
7497
try:
75-
self.impl = _GetchWindows()
98+
self.impl = _GetchWindows() # Windows compatible.
7699
except ImportError:
77-
self.impl = _GetchUnix()
100+
self.impl = _GetchUnix() # UNIX compatible
78101

79102
def __call__(self):
80103
char = self.impl()
@@ -86,11 +109,7 @@ def __call__(self):
86109

87110

88111
class _GetchUnix:
89-
"""UNIX-compatible `getch` method for reading single character from standard input. Does not echo to screen."""
90-
91-
def __init__(self):
92-
import sys
93-
import tty
112+
"""UNIX-compatible `getch()` method for reading single character from standard input. Does not echo to screen."""
94113

95114
def __call__(self):
96115
import sys
@@ -107,42 +126,40 @@ def __call__(self):
107126

108127

109128
class _GetchWindows:
110-
"""Windows-compatible `getch` method for reading single character from standard input. Does not echo to screen."""
111-
112-
def __init__(self):
113-
import msvcrt
129+
"""Windows-compatible `getch()` method for reading single character from standard input. Does not echo to screen."""
114130

115131
def __call__(self):
116132
import msvcrt
117133
return msvcrt.getch()
118134

119135

120-
getch = _Getch()
136+
__version__: str = '0.9.0' # Current program version number
121137

122-
__version__: str = '0.9.0'
138+
getch: _Getch = _Getch() # Initialized `_Getch` instance.
123139

124-
p_stable: str = r'.\patch-files\stable' # Stable patch file location
140+
patch_stable: str = r'.\patch-files\stable' # Stable patch file location
125141

126-
url_stable: str = _get_stable_assets(
142+
url_stable: str = _PatcherAssets._get_stable_assets(
127143
'http://api.github.com/repos/BepInEx/BepInEx/releases/latest',
128144
1) # stable release download link
129145

130-
b_stable: str = _get_stable_assets(
146+
ver_stable: str = _PatcherAssets._get_stable_assets(
131147
'http://api.github.com/repos/BepInEx/BepInEx/releases/latest',
132148
2) # stable release version
133149

134-
p_dev: str = r'.\patch-files\development' # Development patch file location
150+
patch_dev: str = r'.\patch-files\development' # Development patch file location
135151

136-
url_dev: str = _get_dev_assets(
152+
url_dev: str = _PatcherAssets._get_dev_assets(
137153
'https://builds.bepinex.dev/projects/bepinex_be',
138154
1) # dev/bleeding-edge build download link
139155

140-
b_dev: str = _get_dev_assets('https://builds.bepinex.dev/projects/bepinex_be',
141-
2) # dev/bleeding-edge build number
156+
ver_dev: str = _PatcherAssets._get_dev_assets(
157+
'https://builds.bepinex.dev/projects/bepinex_be',
158+
2) # dev/bleeding-edge build number
142159

143-
p_targetDir: str = r'C:\Program Files (x86)\Steam\steamapps\common\Valheim' # target directory to patch
160+
patch_targetDir: str = r'C:\Program Files (x86)\Steam\steamapps\common\Valheim' # target directory to patch
144161

145-
logFile: str = r'.\logs\VBPatcherLog.log' # Log file path
162+
log_fh: str = r'.\logs\VBPatcherLog.log' # Log file path
146163

147164
datefmt: str = datetime.now().strftime(
148165
"%Y-%m-%d %H:%M:%S"

VBPatcher/apploggers/loggers.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from VBPatcher.appglobals.globals import logFile
3+
from VBPatcher.appglobals.globals import log_fh
44

55

66
class _LogGenerator():
@@ -37,15 +37,13 @@ class _LogGenerator():
3737
DEBUG = 10
3838
NOTSET = 0
3939

40-
def __init__(
41-
self,
42-
name: str,
43-
log_file: str,
44-
log_format:
45-
str = '[ %(levelname)s - %(asctime)s - %(name)s ] : %(message)s',
46-
datefmt: str = "%Y-%m-%d %H:%M:%S",
47-
level: int = INFO,
48-
stream: bool = False):
40+
def __init__(self,
41+
name: str,
42+
log_file: str,
43+
log_format: str = '[ %(asctime)s - %(name)s ] : %(message)s',
44+
datefmt: str = "%Y-%m-%d %H:%M:%S",
45+
level: int = INFO,
46+
stream: bool = False):
4947
"""Initialize logger instance.
5048
5149
- For the :param:`log_lvl` parameter, the level of logging can be any of the following:
@@ -138,7 +136,7 @@ def error(self, msg) -> None:
138136
:return: creates log entry.
139137
:rtype: `None`
140138
"""
141-
return self.logger.error(msg)
139+
return self.logger.error(msg, exc_info=True)
142140

143141
def critical(self, msg) -> None:
144142
"""Logs a message with level `CRITICAL`.
@@ -153,7 +151,7 @@ def critical(self, msg) -> None:
153151
return self.logger.critical(msg)
154152

155153

156-
logger: _LogGenerator = _LogGenerator('MAIN', logFile) # Main logging instance
154+
logger: _LogGenerator = _LogGenerator('MAIN', log_fh) # Main logging instance
157155

158156
logger_stream: _LogGenerator = _LogGenerator(
159-
'STDOUT', logFile, stream=True) # Stdout streaming logging instance
157+
'STDOUT', log_fh, stream=True) # Stdout streaming logging instance

0 commit comments

Comments
 (0)