Skip to content

Commit 2012f9f

Browse files
HugoCLSCdependabot[bot]
authored andcommitted
Refactor/tests compliance (#117)
* Build(deps): Bump requests from 2.32.3 to 2.32.4 in /scripts (#112) Bumps [requests](https://github.com/psf/requests) from 2.32.3 to 2.32.4. - [Release notes](https://github.com/psf/requests/releases) - [Changelog](https://github.com/psf/requests/blob/main/HISTORY.md) - [Commits](psf/requests@v2.32.3...v2.32.4) --- updated-dependencies: - dependency-name: requests dependency-version: 2.32.4 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Removed unused method * Remove unused imports * Remove unused import, f-string without placeholders * Remove unused import * Removed unused module * Removed f-string without placeholder * Removed unused code on method * Removed placeholder file for new module * Removed assigned variable but never used * Removed unused import * Comment unused variable * Removed unused code line * Removed unused file * Explicity re-raise with from * Removed unused code line * Refactor text box painting * Removed unused import * Removed unused code line * Removed unused module * Removed unused module placeholder * Added docstrings * Add docstrings to methods * Add docstring to methods and class * Add docstring to methods * Removed unused module * Added docstrings * Added docstrings * Added docstrings, deleted commented code * Added docstrings, deleted commented code * Added docstrings, deleted commented code * Added docstrings, deleted commented code * deleted commented code * Added docstrings, deleted commented code * Deleted code from unused window * Added docstring to methods, deleted unused code * Added docstring to methods, deleted unused code * Add docsctring, delete commented code * Add docsctring * Add docsctring, delete unused code * Add docstring, change method name * Add docstring, change method name, delete unused code * Add docstring, delete unused code * Add docstring, delete unused code, changed method name * Add docstring, changed method name * Add docstring, changed method name * Add docstring, changed method name * Add docstring, changed method name * Add docstring, changed method name * Formatting * Add docstring * Add docstring, delete unused code, changed method name * Add docstring, delete unused code, changed method name * Add docstring, changed method name * Add docstring, changed method name * Add docstring, delete commented code, change method name * Add docstring, delete commented code, change method name * Let troubleshoot page decide where it wants to go * Add docstring, change method name * Add docstring, delete unused and untested code * Add docstring * Add docstring * Add docstring, delete unused code * Add docstring * Add docstring * Add docstring * Add docstring * Add docstring * Add docstrings * Add docstring, change method name * Add docstring * Add docstring, delete unused code * Add docstring, delete unused code * Add docstring * Change list item docstring * Add docstring * Add docstring * Deleted unused module * Add docstring * Deleted unused method * Deleted unused code line * Delete unused code line * Delete unused code line * Security patch subprocess shell = True, security issue * Remove argument from method * Ruff formatting * Ruff formatting * Surpress Reviewed security issues * Formatting --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Hugo Costa <hugo.santos.costa@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 81aba62 commit 2012f9f

68 files changed

Lines changed: 1303 additions & 2462 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎BlocksScreen/BlocksScreen.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@
2222
RESET = "\033[0m"
2323

2424

25-
def setup_working_dir(): ...
26-
27-
2825
def setup_app_loggers():
29-
ql = logger.create_logger(name="logs/BlocksScreen.log", level=logging.DEBUG)
26+
"""Setup logger"""
27+
_ = logger.create_logger(name="logs/BlocksScreen.log", level=logging.DEBUG)
3028
_logger = logging.getLogger(name="logs/BlocksScreen.log")
3129
_logger.info("============ BlocksScreen Initializing ============")
3230

3331

3432
def show_splash(window: typing.Optional[QtWidgets.QWidget] = None):
33+
"""Show splash screen on app initialization"""
3534
logo = QtGui.QPixmap("BlocksScreen/BlocksScreen/lib/ui/resources/logoblocks.png")
3635
splash = QtWidgets.QSplashScreen(pixmap=logo)
3736
splash.setGeometry(QtCore.QRect(0, 0, 400, 200))

‎BlocksScreen/configfile.py‎

Lines changed: 88 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@
4747

4848

4949
class Sentinel(enum.Enum):
50+
"""Sentinel value to signify missing condition, absence of value"""
51+
5052
MISSING = object
5153

5254

5355
class ConfigError(Exception):
56+
"""Exception raised when Configfile errors exist"""
57+
5458
def __init__(self, msg) -> None:
5559
super().__init__(msg)
5660
self.msg = msg
@@ -59,24 +63,10 @@ def __init__(self, msg) -> None:
5963
class BlocksScreenConfig:
6064
config = configparser.ConfigParser(
6165
allow_no_value=True,
62-
# interpolation=configparser.ExtendedInterpolation(),
63-
# delimiters=(":"),
64-
# inline_comment_prefixes=("#"),
65-
# comment_prefixes=("#", "#~#"),
66-
# empty_lines_in_values=True,
6766
)
68-
6967
update_pending: bool = False
70-
7168
_instance = None
7269

73-
# def __new__(
74-
# cls, *args, **kwargs
75-
# ) -> BlocksScreenConfig: # Singleton pattern
76-
# if not cls._instance:
77-
# cls._instance = super(BlocksScreenConfig, cls).__new__(cls)
78-
# return cls._instance
79-
8070
def __init__(
8171
self, configfile: typing.Union[str, pathlib.Path], section: str
8272
) -> None:
@@ -93,24 +83,41 @@ def __contains__(self, key):
9383
return key in self.config
9484

9585
def sections(self) -> typing.List[str]:
86+
"""Returns list of all sections"""
9687
return self.config.sections()
9788

9889
def get_section(
9990
self, section: str, fallback: typing.Optional[T] = None
10091
) -> BlocksScreenConfig:
92+
"""Get configfile section"""
10193
if not self.config.has_section(section):
102-
raise configparser.NoSectionError(
103-
f"No section with name: {section}"
104-
)
94+
raise configparser.NoSectionError(f"No section with name: {section}")
10595
return BlocksScreenConfig(self.configfile, section)
10696

10797
def get_options(self) -> list:
98+
"""Get section options"""
10899
return self.config.options(self.section)
109100

110101
def has_section(self, section: str) -> bool:
102+
"""Check if config file has a section
103+
104+
Args:
105+
section (str): section name
106+
107+
Returns:
108+
bool: true if section exists, false otherwise
109+
"""
111110
return bool(self.config.has_section(section))
112111

113112
def has_option(self, option: str) -> bool:
113+
"""Check if section has a option
114+
115+
Args:
116+
option (str): option name
117+
118+
Returns:
119+
bool: true if section exists, false otherwise
120+
"""
114121
return bool(self.config.has_option(self.section, option))
115122

116123
def get(
@@ -119,26 +126,50 @@ def get(
119126
parser: type = str,
120127
default: typing.Union[Sentinel, str, T] = Sentinel.MISSING,
121128
) -> typing.Union[Sentinel, str]:
129+
"""Get option value
130+
131+
Args:
132+
option (str): option name
133+
parser (type, optional): bool, float, int. Defaults to str.
134+
default (typing.Union[Sentinel, str, T], optional): Default value for specified option. Defaults to Sentinel.MISSING.
135+
136+
Returns:
137+
typing.Union[Sentinel, str]: Requested option. Defaults to the specified default value
138+
"""
122139
return parser(
123-
self.config.get(
124-
section=self.section, option=option, fallback=default
125-
)
140+
self.config.get(section=self.section, option=option, fallback=default)
126141
)
127142

128143
def getint(
129144
self,
130145
option: str,
131146
default: typing.Union[Sentinel, int] = Sentinel.MISSING,
132147
) -> typing.Union[Sentinel, int]:
133-
return self.config.getint(
134-
section=self.section, option=option, fallback=default
135-
)
148+
"""Get option value
149+
150+
Args:
151+
option (str): option name
152+
default (typing.Union[Sentinel, int], optional): Default value for specified option. Defaults to Sentinel.MISSING.
153+
154+
Returns:
155+
typing.Union[Sentinel, int]: Requested option.
156+
"""
157+
return self.config.getint(section=self.section, option=option, fallback=default)
136158

137159
def getfloat(
138160
self,
139161
option: str,
140162
default: typing.Union[Sentinel, float] = Sentinel.MISSING,
141163
) -> typing.Union[Sentinel, float]:
164+
"""Get the value for the specified option
165+
166+
Args:
167+
option (str): option name
168+
default (typing.Union[Sentinel, float], optional): Default value for specified option. Defaults to Sentinel.MISSING.
169+
170+
Returns:
171+
typing.Union[Sentinel, float]: _description_
172+
"""
142173
return self.config.getfloat(
143174
section=self.section, option=option, fallback=default
144175
)
@@ -148,6 +179,15 @@ def getboolean(
148179
option: str,
149180
default: typing.Union[Sentinel, bool] = Sentinel.MISSING,
150181
) -> typing.Union[Sentinel, bool]:
182+
"""Get option value
183+
184+
Args:
185+
option (str): option name
186+
default (typing.Union[Sentinel, bool], optional): Default value for specified option. Defaults to Sentinel.MISSING.
187+
188+
Returns:
189+
typing.Union[Sentinel, bool]: _description_
190+
"""
151191
return self.config.getboolean(
152192
section=self.section, option=option, fallback=default
153193
)
@@ -156,9 +196,7 @@ def _find_section_index(self, section: str) -> int:
156196
try:
157197
return self.raw_config.index("[" + section + "]")
158198
except ValueError as e:
159-
raise configparser.Error(
160-
f'Section "{section}" does not exist: {e}'
161-
)
199+
raise configparser.Error(f'Section "{section}" does not exist: {e}')
162200

163201
def _find_section_limits(self, section: str) -> typing.Tuple:
164202
try:
@@ -189,6 +227,14 @@ def _find_option_index(
189227
)
190228

191229
def add_section(self, section: str) -> None:
230+
"""Add a section to configuration file
231+
232+
Args:
233+
section (str): section name
234+
235+
Raises:
236+
configparser.DuplicateSectionError: Exception thrown when section is duplicated
237+
"""
192238
try:
193239
with self.file_lock:
194240
sec_string = f"[{section}]"
@@ -209,16 +255,21 @@ def add_section(self, section: str) -> None:
209255
except configparser.DuplicateSectionError as e:
210256
logging.error(f'Section "{section}" already exists. {e}')
211257
except configparser.Error as e:
212-
logging.error(
213-
f'Unable to add "{section}" section to configuration: {e}'
214-
)
258+
logging.error(f'Unable to add "{section}" section to configuration: {e}')
215259

216260
def add_option(
217261
self,
218262
section: str,
219263
option: str,
220264
value: typing.Union[str, None] = None,
221265
) -> None:
266+
"""Add option with a value to a section
267+
268+
Args:
269+
section (str): section name
270+
option (str): option name
271+
value (typing.Union[str, None], optional): value for the specified option. Defaults to None.
272+
"""
222273
try:
223274
with self.file_lock:
224275
section_start, section_end = self._find_section_limits(section)
@@ -239,13 +290,12 @@ def add_option(
239290
)
240291

241292
def save_configuration(self) -> None:
293+
"""Save teh configuration to file"""
242294
try:
243295
if not self.update_pending:
244296
return
245297
with self.file_lock:
246-
self.configfile.write_text(
247-
"\n".join(self.raw_config), encoding="utf-8"
248-
)
298+
self.configfile.write_text("\n".join(self.raw_config), encoding="utf-8")
249299
sio = io.StringIO()
250300
sio.writelines(self.raw_config)
251301
self.config.write(sio)
@@ -257,13 +307,8 @@ def save_configuration(self) -> None:
257307
finally:
258308
self.update_pending = False
259309

260-
def _do_save(self, data) -> bool:
261-
try:
262-
return True
263-
except Exception as e:
264-
return False
265-
266310
def load_config(self):
311+
"""Load configuration file"""
267312
try:
268313
self.raw_config.clear()
269314
self.config.clear() # Reset configparser
@@ -330,20 +375,17 @@ def _parse_file(self) -> typing.Tuple[typing.List[str], typing.Dict]:
330375

331376

332377
def get_configparser() -> BlocksScreenConfig:
378+
"""Loads configuration from file and returns that configuration"""
333379
wanted_target = os.path.join(DEFAULT_CONFIGFILE_PATH, "BlocksScreen.cfg")
334380
fallback = os.path.join(WORKING_DIR, "BlocksScreen.cfg")
335381
configfile = (
336382
wanted_target
337383
if check_file_on_path(DEFAULT_CONFIGFILE_PATH, "BlocksScreen.cfg")
338384
else fallback
339385
)
340-
try:
341-
config_object = BlocksScreenConfig(
342-
configfile=configfile, section="server"
343-
)
344-
config_object.load_config()
345-
if not config_object.has_section("server"):
346-
raise ConfigError("Section [server] is missing from configuration")
347-
except ConfigError:
386+
config_object = BlocksScreenConfig(configfile=configfile, section="server")
387+
config_object.load_config()
388+
if not config_object.has_section("server"):
348389
logging.error("Error loading configuration file for the application.")
390+
raise ConfigError("Section [server] is missing from configuration")
349391
return BlocksScreenConfig(configfile=configfile, section="server")

0 commit comments

Comments
 (0)