Last active
June 14, 2024 14:42
-
-
Save r0x0d/b02a03da66f73f0cfd200ff39ffa0eb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from argparse import Namespace | |
import copy | |
import pprint | |
class BaseConfig: | |
def __init__(self): | |
self.debug = False | |
self._username = None | |
self.config_file = None | |
self._password = None | |
self.no_rhsm = False | |
self.enablerepo = [] | |
self.disablerepo = [] | |
self.pool = None | |
self.rhsm_hostname = None | |
self.rhsm_port = None | |
self.rhsm_prefix = None | |
self.autoaccept = None | |
self.auto_attach = None | |
self.restart = None | |
self._activation_key = None | |
self._org = None | |
self.arch = None | |
self.no_rpm_va = False | |
self.eus = False | |
self.els = False | |
self.activity = None | |
# Settings | |
self.incomplete_rollback = None | |
self.tainted_kernel_module_check_skip = None | |
self.outdated_package_check_skip = None | |
self.allow_older_version = None | |
self.allow_unavailable_kmods = None | |
self.configure_host_metering = None | |
self.skip_kernel_currency_check = None | |
def _normalize_opts(self, opts): | |
unparsed_opts = copy.copy(opts) | |
unparsed_opts["activation_key"] = opts.pop("activationkey", None) | |
unparsed_opts["auto_accept"] = opts.pop("y", False) | |
unparsed_opts["activity"] = opts.pop("command", "convert") | |
return unparsed_opts | |
def set_cli_opts(self, opts): | |
"""Set ToolOpts data using dict with values from config file. | |
:param supported_opts: Supported options in config file | |
""" | |
normalized_opts = self._normalize_opts(opts) | |
for key, value in normalized_opts.items(): | |
if value and hasattr(self, key): | |
setattr(self, key, value) | |
class ToolOpts(BaseConfig): | |
def __init__(self, opts): | |
super(ToolOpts, self).__init__() | |
self.substitutions = Substitutions().options_from_config_files() | |
self.set_cli_opts(opts) | |
@property | |
def username(self): | |
# Check for username in either config file or cli (through substitutions) | |
return self._username if self._username else self.substitutions.get("username") | |
@username.setter | |
def username(self, value): | |
self._username = value | |
@property | |
def password(self): | |
return self._password if self._password else self.substitutions.get("password") | |
@password.setter | |
def password(self, value): | |
self._password = value | |
@property | |
def activation(self): | |
return self._activation_key if self._activation_key else self.substitutions.get("activation_key") | |
@username.setter | |
def activation_key(self, value): | |
self._activation_key = value | |
@property | |
def org(self): | |
return self._org if self._org else self.substitutions.get("org") | |
@username.setter | |
def org(self, value): | |
self._org = value | |
class Substitutions(dict): | |
mock_data = {'username': 'test', 'allow_incomplete_rollback': '1'} | |
def options_from_config_files(self): | |
for key, value in self.mock_data.items(): | |
self[key] = value | |
return self | |
# parsed_opts is the output of the CLI parsed from argparse | |
parsed_opts = Namespace( | |
activationkey=None, auto_attach=False, command='analyze', config_file=None, | |
debug=True, disablerepo=None, els=False, enablerepo=None, eus=False, | |
no_rhsm=False, no_rpm_va=False, org=None, password='c2r_pswd2', | |
pool='2c9435a18dcbf599018e0db3afc547f3', restart=False, | |
serverurl='subscription.rhsm.stage.redhat.com', username='c2r_main2', | |
y=True | |
) | |
# Initialize the ToolOpts class with the namespace above converted to a dict | |
tool_opts = ToolOpts(opts=vars(parsed_opts)) | |
pprint.pprint(tool_opts.__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment