Skip to content

Commit dd46965

Browse files
committed
- Fixed logging issue causing multiple handlers to log at once.
- Updated requirements. - Fixed issues due to new version of PyLoadBar. - Added code in `main.py` to automatically add program to Python path upon run. - Greatly expanded method documentation. - Improved source code formatting across entire repo. - Improved README content. - Fixed several import bugs. Signed-off-by: schlopp96 <71921821+schlopp96@users.noreply.github.com>
1 parent 8d85039 commit dd46965

File tree

9 files changed

+175
-98
lines changed

9 files changed

+175
-98
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66

77
## About
88

9-
- **V**alheim **B**epInEx **P**atcher is a CLI application for patching the Unity modding plugin, [_**BepInEx**_](https://github.com/BepInEx/BepInEx) to its latest release (whether LTS or experimental).
9+
- _**V**alheim **B**epInEx **P**atcher_ is a CLI application for patching the Unity modding plugin, [_**BepInEx**_](https://github.com/BepInEx/BepInEx) to its latest release (whether LTS or experimental).
1010

11-
- **VBP** was originally created as a solution for an issue that occurs while using the [**_Vortex_**](https://) modding tool to mod the game **Valheim**, whilst having **BepInEx** installed.
11+
- _**VBPatcher**_ was originally created as a solution for an issue that occurs while using the [**_Vortex_**](https://) modding tool to mod the game _**Valheim**_, whilst having _**BepInEx**_ installed.
1212

13-
- When opening **Vortex** to begin modding Valheim, the modding tool automatically downloads what it _incorrectly_ perceives to be the "latest" version of **BepInEx** (a necessary requirement for the vast majority of mods available) which is generally incorrect, and often takes a long time to be fixed.
13+
- When opening **_Vortex_** to begin modding **_Valheim_**, the modding tool automatically downloads what it _incorrectly_ perceives to be the "latest" version of _**BepInEx**_ (a necessary requirement for the vast majority of mods available) which is generally incorrect, and often takes a long time to be fixed.
1414

15-
- Unfortunately, this means if you were using an experimental/newer/different build of **BepInEx**, it has been overwritten by whatever build **Vortex** installed.
15+
- Unfortunately, this means if you were using an experimental/newer/different build of _**BepInEx**_, it has been overwritten by whatever build _**Vortex**_ installed.
1616

17-
- This occurs _each and every time_ you open **Vortex**, and became a major annoyance for me, so I decided to create a quick solution to make this problem less annoying.
17+
- This occurs _each and every time_ you open _**Vortex**_, and became a major annoyance for me, so I decided to create a quick solution to make this problem less annoying.
1818

1919
---
2020

2121
## Installation
2222

2323
### Using PIP _(Recommended)_
2424

25-
- To install VBPatcher using `pip`, enter the following:
25+
- To install _**VBPatcher**_ using `pip`, enter the following:
2626

2727
```python
28-
pip install VBPatcher
28+
python -m pip install VBPatcher
2929
```
3030

3131
### Manual Installation _(**NOT** Recommended)_
@@ -46,12 +46,12 @@
4646

4747
1. Open the application, which can be run within a terminal using the following command:
4848

49-
```psh
50-
PS> vbpatcher
51-
```
49+
```psh
50+
PS > vbpatcher
51+
```
5252

53-
- You can also run **VBP** from the main program file:
54-
- `~/VBPatcher/main.py`.
53+
- You can also run **VBP** from the main program file:
54+
- `~/VBPatcher/main.py`.
5555

5656
2. Once the script is run, you will be presented with an option menu, allowing you to choose whether to:
5757

@@ -106,7 +106,7 @@
106106
- Within a terminal, open **VBP** with the following command:
107107

108108
```psh
109-
PS> vbpatcher
109+
PS > vbpatcher
110110
```
111111

112112
- Or optionally within a python environment:

VBPatcher/applogger/applogger.py

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
import logging
22

3+
from VBPatcher.globalvars.globalvars import _logFile
4+
5+
36
class _LogGenerator():
47
"""Wrapper for application logging.
58
69
- Uses built-in Python `logging` module.
10+
11+
- Class methods:
12+
- `debug(self, msg) -> None`
13+
- Logs a message with level `DEBUG`.
14+
15+
- `info(self, msg) -> None`
16+
- Logs a message with level `INFO`.
17+
18+
- `warning(self, msg) -> None`
19+
- Logs a message with level `WARNING`.
20+
21+
- `error(self, msg) -> None`
22+
- Logs a message with level `ERROR`.
23+
24+
- `critical(self, msg) -> None`
25+
- Logs a message with level `CRITICAL`.
726
"""
827

928
CRITICAL = 50
@@ -15,12 +34,12 @@ class _LogGenerator():
1534
DEBUG = 10
1635
NOTSET = 0
1736

18-
def __init__(
19-
self,
20-
log_file: str = __name__,
21-
log_format: str = '[%(asctime)s - %(levelname)s] : %(message)s',
22-
date_fmt: str = "%Y-%m-%d %H:%M:%S",
23-
log_lvl=INFO):
37+
def __init__(self,
38+
name: str,
39+
log_file: str,
40+
log_fmt: str = '[%(asctime)s - %(levelname)s] : %(message)s',
41+
date_fmt: str = "%Y-%m-%d %H:%M:%S",
42+
level=INFO):
2443
"""Initialize logger instance.
2544
2645
- For the `log_lvl` parameter, the level of logging can be any of the following:
@@ -35,37 +54,79 @@ def __init__(
3554
3655
---
3756
38-
:param log_file: assign specific name to logger, defaults to `__name__`.
39-
:type log_file: str, optional
40-
:param log_format: Initialize the formatter either with the specified format string, or a default as described above, defaults to '[%(asctime)s - %(levelname)s] : %(message)s'
41-
:type log_format: str, optional
57+
:param name: Name of logger.
58+
:type name: str
59+
:param log_file: file to create log entries.
60+
:type log_file: str
61+
:param log_fmt: Initialize the formatter either with the specified format string, or a default as described above, defaults to '[%(asctime)s - %(levelname)s] : %(message)s'
62+
:type log_fmt: str, optional
4263
:param date_fmt: set date formatting, defaults to "%Y-%m-%d %H:%M:%S"
4364
:type date_fmt: str, optional
44-
:param log_lvl: Set the logging level of this logger. Level must be an int or a str, defaults to `INFO`.
45-
:type log_lvl: int, optional
65+
:param level: Set the logging level of this logger. Level must be an int or a str, defaults to `INFO`.
66+
:type level: int, optional
4667
"""
47-
self.logger = logging.getLogger(log_file)
48-
self.log_format = log_format
68+
69+
self.name = name
70+
self.logger = logging.getLogger(name)
71+
self.log_format = log_fmt
4972
self.datefmt = date_fmt
50-
self.log_lvl = log_lvl
51-
self.formatter = logging.Formatter(log_format, datefmt=date_fmt)
73+
self.log_lvl = level
74+
self.formatter = logging.Formatter(log_fmt, datefmt=date_fmt)
5275
self.log_file = log_file
5376
self.fhandler = logging.FileHandler(log_file)
5477
self.logger.addHandler(self.fhandler)
5578
self.fhandler.setFormatter(self.formatter)
56-
self.logger.setLevel(log_lvl)
79+
self.logger.setLevel(level)
5780

58-
def debug(self, msg):
81+
def debug(self, msg) -> None:
82+
"""Logs a message with level `DEBUG`.
83+
84+
:param msg: message to be logged.
85+
:type msg: str
86+
:return: creates log entry.
87+
:rtype: None
88+
"""
5989
return self.logger.debug(msg)
6090

6191
def info(self, msg):
92+
"""Logs a message with level `INFO`.
93+
94+
:param msg: message to be logged.
95+
:type msg: str
96+
:return: creates log entry.
97+
:rtype: None
98+
"""
6299
return self.logger.info(msg)
63100

64101
def warning(self, msg):
102+
"""Logs a message with level `WARNING`.
103+
104+
:param msg: message to be logged.
105+
:type msg: str
106+
:return: creates log entry.
107+
:rtype: None
108+
"""
65109
return self.logger.warning(msg)
66110

67111
def error(self, msg):
112+
"""Logs a message with level `ERROR`.
113+
114+
:param msg: message to be logged.
115+
:type msg: str
116+
:return: creates log entry.
117+
:rtype: None
118+
"""
68119
return self.logger.error(msg)
69120

70121
def critical(self, msg):
122+
"""Logs a message with level `CRITICAL`.
123+
124+
:param msg: message to be logged.
125+
:type msg: str
126+
:return: creates log entry.
127+
:rtype: None
128+
"""
71129
return self.logger.critical(msg)
130+
131+
132+
logger = _LogGenerator(__name__, _logFile)

VBPatcher/downloader/downloader.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@
44
import sys
55
from zipfile import ZipFile
66

7-
import VBPatcher.applogger.applogger
8-
import VBPatcher.globalvars.globalvars
97
import requests
108
import tqdm
9+
import VBPatcher.globalvars.globalvars
10+
from VBPatcher.applogger.applogger import logger
11+
1112

12-
logger = VBPatcher.applogger.applogger._LogGenerator(VBPatcher.globalvars.globalvars._logFile)
1313
class _Downloader:
1414
"""Wrapper containing patch-file update functionality.
1515
1616
- Class Methods:
1717
- `dl_stable(self, url) -> BufferedWriter`
1818
- Download latest BepInEx stable release.
19+
1920
- `dl_dev(self, url) -> BufferedWriter`
2021
- Download latest BepInEx development build.
22+
2123
- `unzip_patch(self, filename, stable) -> None`
2224
- Unzip downloaded patch files before deleting patch `.zip` archive.
25+
2326
- `_UpdatePatcher(self) -> None`
2427
- Process to retrieve latest available patch files using class methods.
2528
"""
2629
def __init__(self) -> None:
2730
pass
2831

29-
def dl_stable(self, url):
32+
def _dl_stable(self, url):
3033
"""Download zip containing latest BepInEx stable release.
3134
3235
:return: zip archive containing patch files.
@@ -60,7 +63,7 @@ def dl_stable(self, url):
6063
print('\nCancelled update-check.\n')
6164
break
6265

63-
def dl_dev(self, url):
66+
def _dl_dev(self, url):
6467
"""Download zip archive containing latest BepInEx development build.
6568
6669
:return: zip archive containing patch files.
@@ -94,7 +97,7 @@ def dl_dev(self, url):
9497
break
9598

9699
def _unzip_patch(self, filename, stable: bool) -> None:
97-
"""Unzip downloaded patch files before deleting patch `.zip` archive.
100+
"""Unzip downloaded patch files and cleanup leftover files.
98101
99102
---
100103
@@ -108,12 +111,13 @@ def _unzip_patch(self, filename, stable: bool) -> None:
108111
logger.info('Unzipping patch files...')
109112
print('Unzipping patch files...')
110113
try:
111-
if stable:
114+
if stable: # Unzip stable release patch files
112115
with ZipFile(filename) as archive:
113116
archive.extractall(path='./patch-files/stable')
114117
os.unlink('./patch-files/stable/doorstop_config.ini')
115118
os.unlink(f'./patch-files/stable/BepInEx_stable_{VBPatcher.globalvars.globalvars.b_stable}.zip')
116-
else:
119+
120+
else: # Unzip development build patch files
117121
with ZipFile(filename) as archive:
118122
archive.extractall(path='./patch-files/development')
119123
os.unlink('./patch-files/development/doorstop_config.ini')
@@ -127,19 +131,22 @@ def _unzip_patch(self, filename, stable: bool) -> None:
127131
print(f'\nEncountered error while attempting to unzip archive...\n>> Exception: {err}\n')
128132

129133
def _UpdatePatcher(self) -> None:
130-
"""Process to retrieve latest available patch files using class methods.
134+
"""Retrieve latest available patch files.
131135
132136
---
133137
134-
:return: most recent release/build patch files.
138+
:return: download most recent release/build patch files.
135139
:rtype: None
136140
"""
137-
self.dl_stable(VBPatcher.globalvars.globalvars.url_stable)
141+
# Retrieve stable release patch files
142+
self._dl_stable(VBPatcher.globalvars.globalvars.url_stable)
138143
self._unzip_patch(f'./patch-files/stable/BepInEx_stable_{VBPatcher.globalvars.globalvars.b_stable}.zip', True)
139-
self.dl_dev(VBPatcher.globalvars.globalvars.url_dev)
144+
145+
# Retrieve development build patch files
146+
self._dl_dev(VBPatcher.globalvars.globalvars.url_dev)
140147
self._unzip_patch(f'./patch-files/development/BepInEx_dev_{VBPatcher.globalvars.globalvars.b_dev}.zip', False)
141148

142149
logger.info('Completed Patcher Update!\n>> Patches ready for deployment!\n')
143150
print('\nCompleted Patcher Update!\n>> Patches ready for deployment!\n')
144151
print('Press anything to continue...')
145-
m.getch()
152+
m.getch() # Wait for user to press any key to continue

VBPatcher/globalvars/globalvars.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import datetime
22

3-
43
p_stable: str = r'.\patch-files\stable'
54
url_stable = 'https://github.com/BepInEx/BepInEx/releases/download/v5.4.19/BepInEx_x64_5.4.19.0.zip'
65
b_stable: str = url_stable[53:60] #release version
@@ -15,4 +14,4 @@
1514

1615
_datefmt: str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
1716

18-
_textborder: str = "=".ljust((61), "=")
17+
_textborder: str = "=".ljust((61), "=")

VBPatcher/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
#!/usr/bin/env python3
22

3+
import sys
34
from os import chdir
45
from os.path import dirname
56
from time import sleep
67
from typing import NoReturn
78

8-
from PyLoadBar import load
9+
import PyLoadBar
910

11+
sys.path.insert(0, dirname(dirname(__file__))) # Ensure main module can be found by Python.
1012
chdir(dirname(__file__))
1113

1214
import VBPatcher.globalvars.globalvars
13-
from VBPatcher.applogger.applogger import _LogGenerator
15+
from VBPatcher.applogger.applogger import logger
1416
from VBPatcher.downloader.downloader import _Downloader
1517
from VBPatcher.patching.patching import _Patcher
1618
from VBPatcher.subprocessing.subprocessing import _exitPatcher, _openValheim
1719
from VBPatcher.validation.validation import _Validate
1820

1921
__version__: str = '0.6.0'
2022

21-
logger = _LogGenerator(VBPatcher.globalvars.globalvars._logFile)
23+
2224
DL = _Downloader()
2325
Patcher = _Patcher()
2426
Validations = _Validate()
27+
bar = PyLoadBar.PyLoadBar()
2528

2629
#$ ====================================================================================================== $#
2730

@@ -52,7 +55,7 @@ def main() -> None | NoReturn:
5255
case '5': _openValheim()
5356
case '6':
5457
logger.info('BepInEx patching process cancelled...\n>> Preparing to exit...\n')
55-
load('\nBepInEx patching process cancelled', 'Preparing to exit...', enable_display=False)
58+
bar.load('\nBepInEx patching process cancelled', 'Preparing to exit...', enable_display=False)
5659
return _exitPatcher()
5760
case _:
5861
logger.warning(f'Invalid Input:\n>> "{choosePatch}"\n\n>> Must ONLY enter:\n>> [1] for stable release {VBPatcher.globalvars.globalvars.b_stable}\n>> [2] for development build {VBPatcher.globalvars.globalvars.b_dev}\n>> [3] for FULL upgrade (apply both patches in order of release)\n>> [4] to update available patch versions/builds\n>> [5] to open Valheim\n>> [6] to exit program\n')

0 commit comments

Comments
 (0)