Skip to content

Commit b97ef51

Browse files
committed
added more type hints
Signed-off-by: Zen <[email protected]>
1 parent 8e1c968 commit b97ef51

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

ugrd/initramfs_dict.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ def __init__(self, *args, **kwargs):
3232
else:
3333
super().__setitem__(parameter, default_type())
3434

35-
def import_args(self, args: dict):
35+
def import_args(self, args: dict) -> None:
3636
"""
3737
Imports data from an argument dict
3838
"""
3939
for arg, value in args.items():
4040
self.logger.warning("Importing argument '%s' with value: %s" % (arg, value))
4141
self[arg] = value
4242

43-
def __setitem__(self, key, value):
43+
def __setitem__(self, key: str, value) -> None:
4444
# If the type is registered, use the appropriate update function
4545
if expected_type := self.builtin_parameters.get(key, self['custom_parameters'].get(key)):
4646
self.logger.log(5, "[%s] Expected type: %s" % (key, expected_type))
@@ -74,7 +74,7 @@ def __setitem__(self, key, value):
7474
raise ValueError("Detected undefined parameter type '%s' with value: %s" % (key, value))
7575

7676
@handle_plural
77-
def _process_custom_parameters(self, parameter_name, parameter_type):
77+
def _process_custom_parameters(self, parameter_name: str, parameter_type: type) -> None:
7878
"""
7979
Updates the custom_parameters attribute.
8080
Sets the initial value of the parameter based on the type.
@@ -92,7 +92,7 @@ def _process_custom_parameters(self, parameter_name, parameter_type):
9292
super().__setitem__(parameter_name, 0)
9393

9494
@handle_plural
95-
def _process_imports(self, import_type: str, import_value: dict):
95+
def _process_imports(self, import_type: str, import_value: dict) -> None:
9696
"""
9797
Processes imports in a module, importing the functions and adding them to the appropriate list
9898
"""
@@ -123,7 +123,7 @@ def _process_imports(self, import_type: str, import_value: dict):
123123
self.logger.debug("Registered config processing function: %s" % function.__name__)
124124

125125
@handle_plural
126-
def _process_mod_depends(self, module):
126+
def _process_mod_depends(self, module: str) -> None:
127127
"""
128128
Processes module dependencies
129129
"""
@@ -134,7 +134,7 @@ def _process_mod_depends(self, module):
134134
self['mod_depends'].append(module)
135135

136136
@handle_plural
137-
def _process_modules(self, module):
137+
def _process_modules(self, module: str) -> None:
138138
"""
139139
processes a single module into the config
140140
takes list with decorator
@@ -174,15 +174,15 @@ def _process_modules(self, module):
174174

175175
self['modules'].append(module)
176176

177-
def verify_deps(self):
177+
def verify_deps(self) -> None:
178178
""" Verifies that all module dependencies are met """
179179
for module in self['mod_depends']:
180180
if module not in self['modules']:
181181
raise KeyError(f"Required module '{module}' not found in config")
182182

183183
self.logger.info("Verified module depndencies: %s" % self['mod_depends'])
184184

185-
def verify_mask(self):
185+
def verify_mask(self) -> None:
186186
"""
187187
Processes masked imports
188188
"""
@@ -193,5 +193,5 @@ def verify_mask(self):
193193
self.logger.warning("Masking import: %s" % function.__name__)
194194
self['imports'][mask_hook].remove(function)
195195

196-
def __str__(self):
196+
def __str__(self) -> str:
197197
return pretty_print(self)

ugrd/initramfs_generator.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
__author__ = "desultory"
3-
__version__ = "0.9.0"
3+
__version__ = "0.9.1"
44

55
from tomllib import load
6+
from typing import Union
67
from pathlib import Path
7-
from subprocess import run
8+
from subprocess import run, CompletedProcess
89

910
from ugrd.zen_custom import loggify, pretty_print
1011
from ugrd.initramfs_dict import InitramfsConfigDict
@@ -25,7 +26,7 @@ def __init__(self, config='/etc/ugrd/config.toml', *args, **kwargs):
2526
self.config_dict.verify_mask()
2627
self.config_dict.import_args(kwargs)
2728

28-
def load_config(self):
29+
def load_config(self) -> None:
2930
"""
3031
Loads the config from the specified toml file.
3132
Populates self.config_dict with the config.
@@ -49,7 +50,7 @@ def load_config(self):
4950
else:
5051
raise KeyError("Required parameter '%s' not found in config" % parameter)
5152

52-
def clean_build_dir(self):
53+
def clean_build_dir(self) -> None:
5354
"""
5455
Cleans the build directory
5556
"""
@@ -66,7 +67,7 @@ def clean_build_dir(self):
6667
else:
6768
self.logger.info("Build dir is not present, not cleaning: %s" % self.build_dir)
6869

69-
def build_structure(self):
70+
def build_structure(self) -> None:
7071
"""
7172
builds the initramfs structure.
7273
Cleans the build dir first if clean is set
@@ -78,7 +79,7 @@ def build_structure(self):
7879
self._run_hook('build_pre', return_output=False)
7980
self._run_hook('build_tasks', return_output=False)
8081

81-
def _run_func(self, function, external=False, return_output=True):
82+
def _run_func(self, function: str, external=False, return_output=True) -> list[str]:
8283
"""
8384
Runs a function, returning the output in a list
8485
"""
@@ -102,9 +103,10 @@ def _run_func(self, function, external=False, return_output=True):
102103
self.logger.debug("[%s] Function returned no output" % function.__name__)
103104
return []
104105

105-
def _run_funcs(self, functions, external=False, return_output=True):
106+
def _run_funcs(self, functions: list[str], external=False, return_output=True) -> list[str]:
106107
"""
107108
Runs a list of functions
109+
Returns the output if return_output is set
108110
"""
109111
self.logger.debug("Running functions: %s" % functions)
110112
out = []
@@ -116,7 +118,7 @@ def _run_funcs(self, functions, external=False, return_output=True):
116118
self._run_func(function, external=external, return_output=return_output)
117119
return out
118120

119-
def _run_hook(self, hook, return_output=True):
121+
def _run_hook(self, hook: str, return_output=True) -> list[str]:
120122
"""
121123
Runs a hook for imported functions
122124
"""
@@ -135,15 +137,15 @@ def _run_hook(self, hook, return_output=True):
135137
self.logger.debug("[%s] Hook output: %s" % (hook, out))
136138
return out
137139

138-
def _run_init_hook(self, level):
140+
def _run_init_hook(self, level: str) -> list[str]:
139141
"""
140142
Runs the specified init hook, returning the output
141143
"""
142144
out = ['\n\n# Begin %s' % level]
143145
out += self._run_hook(level)
144146
return out
145147

146-
def generate_init_main(self):
148+
def generate_init_main(self) -> list[str]:
147149
"""
148150
Generates the main init file.
149151
Just runs each hook in self.init_types and returns the output
@@ -158,7 +160,7 @@ def generate_init_main(self):
158160
self.logger.debug("No output from init hook: %s" % init_type)
159161
return out
160162

161-
def generate_init(self):
163+
def generate_init(self) -> None:
162164
"""
163165
Generates the init file
164166
"""
@@ -181,7 +183,7 @@ def generate_init(self):
181183
self._write('init', init, 0o755)
182184
self.logger.debug("Final config:\n%s" % pretty_print(self.config_dict))
183185

184-
def generate_structure(self):
186+
def generate_structure(self) -> None:
185187
"""
186188
Generates the initramfs directory structure
187189
"""
@@ -195,7 +197,7 @@ def generate_structure(self):
195197

196198
self._mkdir(target_dir)
197199

198-
def pack_build(self):
200+
def pack_build(self) -> None:
199201
"""
200202
Packs the initramfs based on self.config_dict['imports']['pack']
201203
"""
@@ -204,7 +206,7 @@ def pack_build(self):
204206
else:
205207
self.logger.warning("No pack functions specified, the final build is present in: %s" % self.build_dir)
206208

207-
def _mkdir(self, path):
209+
def _mkdir(self, path: Path) -> None:
208210
"""
209211
Creates a directory, chowns it as self.config_dict['_file_owner_uid']
210212
"""
@@ -230,7 +232,7 @@ def _mkdir(self, path):
230232

231233
self._chown(path_dir)
232234

233-
def _chown(self, path):
235+
def _chown(self, path: Path) -> None:
234236
"""
235237
Chowns a file or directory as self.config_dict['_file_owner_uid']
236238
"""
@@ -246,7 +248,7 @@ def _chown(self, path):
246248
else:
247249
self.logger.debug("[%s] Set file owner: %s" % (path, self.config_dict['_file_owner_uid']))
248250

249-
def _write(self, file_name, contents, chmod_mask=0o644, in_build_dir=True):
251+
def _write(self, file_name: Union[Path, str], contents: list[str], chmod_mask=0o644, in_build_dir=True) -> None:
250252
"""
251253
Writes a file and owns it as self.config_dict['_file_owner_uid']
252254
Sets the passed chmod
@@ -278,7 +280,7 @@ def _write(self, file_name, contents, chmod_mask=0o644, in_build_dir=True):
278280

279281
self._chown(file_path)
280282

281-
def _copy(self, source, dest=None, in_build_dir=True):
283+
def _copy(self, source: Union[Path, str], dest=None, in_build_dir=True) -> None:
282284
"""
283285
Copies a file, chowns it as self.config_dict['_file_owner_uid']
284286
"""
@@ -314,7 +316,7 @@ def _copy(self, source, dest=None, in_build_dir=True):
314316

315317
self._chown(dest_path)
316318

317-
def _rotate_old(self, file_name: Path, sequence=0):
319+
def _rotate_old(self, file_name: Path, sequence=0) -> None:
318320
"""
319321
Copies a file to file_name.old then file_nane.old.n, where n is the next number in the sequence
320322
"""
@@ -359,7 +361,7 @@ def _rotate_old(self, file_name: Path, sequence=0):
359361
self.logger.info("[%d] Cycling file: %s -> %s" % (sequence, file_name, target_file))
360362
file_name.rename(target_file)
361363

362-
def _get_build_path(self, path):
364+
def _get_build_path(self, path: Union[Path, str]) -> Path:
363365
"""
364366
Returns the build path
365367
"""
@@ -371,7 +373,7 @@ def _get_build_path(self, path):
371373
else:
372374
return self.build_dir / path
373375

374-
def _symlink(self, source, target, in_build_dir=True):
376+
def _symlink(self, source: Union[Path, str], target: Union[Path, str], in_build_dir=True) -> None:
375377
"""
376378
Creates a symlink
377379
"""
@@ -393,7 +395,7 @@ def _symlink(self, source, target, in_build_dir=True):
393395
self.logger.debug("Creating symlink: %s -> %s" % (source, target))
394396
symlink(source, target)
395397

396-
def _run(self, args):
398+
def _run(self, args: list[str]) -> CompletedProcess:
397399
"""
398400
Runs a command, returns the object
399401
"""

0 commit comments

Comments
 (0)