Skip to content

Commit

Permalink
chore: drop yapf and favor of the ruff formatter (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Nov 10, 2023
1 parent fd85dc5 commit f13de60
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 332 deletions.
2 changes: 1 addition & 1 deletion .woodpecker/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ steps:
- git fetch --depth=2147483647
- pip install poetry poetry-dynamic-versioning -qq
- poetry install
- poetry run yapf -dr ./${CI_REPO_NAME//-/}
- poetry run ruff format --check --diff ./${CI_REPO_NAME//-/}
environment:
PY_COLORS: "1"

Expand Down
456 changes: 196 additions & 260 deletions poetry.lock

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions prometheuspvesd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _cli_args(self):
"--config",
dest="config_file",
action="store",
help="location of configuration file"
help="location of configuration file",
)
parser.add_argument(
"-o", "--output", dest="output_file", action="store", help="output file"
Expand All @@ -73,22 +73,22 @@ def _cli_args(self):
dest="loop_delay",
action="store",
type=int,
help="delay between discovery runs"
help="delay between discovery runs",
)
parser.add_argument(
"--no-service",
dest="service",
action="store_false",
default=None,
help="run discovery only once"
help="run discovery only once",
)
parser.add_argument(
"-f",
"--log-format",
dest="logging.format",
metavar="LOG_FORMAT",
action="store",
help="used log format"
help="used log format",
)
parser.add_argument(
"-v", dest="logging.level", action="append_const", const=-1, help="increase log level"
Expand All @@ -113,15 +113,16 @@ def _get_config(self):
except ValueError as e:
self.log.sysexit_with_message(f"Can not set log level.\n{e!s}")

required = [("pve.server", config.config["pve"]["server"]),
("pve.user", config.config["pve"]["user"])]
required = [
("pve.server", config.config["pve"]["server"]),
("pve.user", config.config["pve"]["user"]),
]
for name, value in required:
if not value:
self.log.sysexit_with_message(f"Option '{name}' is required but not set")

if (
not config.config["pve"]["password"]
and not (config.config["pve"]["token_name"] and config.config["pve"]["token_value"])
if not config.config["pve"]["password"] and not (
config.config["pve"]["token_name"] and config.config["pve"]["token_value"]
):
self.log.sysexit_with_message(
"Either 'pve.password' or 'pve.token_name' and 'pve.token_value' "
Expand All @@ -144,7 +145,7 @@ def _fetch(self):
)
start_http_server(
self.config.config["metrics"]["port"],
addr=self.config.config["metrics"]["address"]
addr=self.config.config["metrics"]["address"],
)

while True:
Expand Down
10 changes: 6 additions & 4 deletions prometheuspvesd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

try:
from proxmoxer import ProxmoxAPI

HAS_PROXMOXER = True
except ImportError:
HAS_PROXMOXER = False
Expand Down Expand Up @@ -53,15 +54,15 @@ def _auth(self):
token_name=self.config.config["pve"]["token_name"],
token_value=self.config.config["pve"]["token_value"],
verify_ssl=to_bool(self.config.config["pve"]["verify_ssl"]),
timeout=self.config.config["pve"]["auth_timeout"]
timeout=self.config.config["pve"]["auth_timeout"],
)

return ProxmoxAPI(
self.config.config["pve"]["server"],
user=self.config.config["pve"]["user"],
password=self.config.config["pve"]["password"],
verify_ssl=to_bool(self.config.config["pve"]["verify_ssl"]),
timeout=self.config.config["pve"]["auth_timeout"]
timeout=self.config.config["pve"]["auth_timeout"],
)
except requests.RequestException as e:
PVE_REQUEST_COUNT_ERROR_TOTAL.inc()
Expand Down Expand Up @@ -98,5 +99,6 @@ def get_agent_info(self, pve_node, pve_type, vmid):

def get_network_interfaces(self, pve_node, vmid):
self.logger.debug(f"fetching network interfaces for {vmid} on {pve_node}")
return self._do_request(pve_node, "qemu", vmid, "agent",
"network-get-interfaces")["result"]
return self._do_request(pve_node, "qemu", vmid, "agent", "network-get-interfaces")[
"result"
]
55 changes: 29 additions & 26 deletions prometheuspvesd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,130 +34,130 @@ class Config:
"metrics.enabled": {
"default": True,
"env": "METRICS_ENABLED",
"type": environs.Env().bool
"type": environs.Env().bool,
},
"metrics.address": {
"default": "127.0.0.1",
"env": "METRICS_ADDRESS",
"type": environs.Env().str
"type": environs.Env().str,
},
"metrics.port": {
"default": 8000,
"env": "METRICS_PORT",
"type": environs.Env().int
"type": environs.Env().int,
},
"config_file": {
"default": "",
"env": "CONFIG_FILE",
"type": environs.Env().str
"type": environs.Env().str,
},
"logging.level": {
"default": "WARNING",
"env": "LOG_LEVEL",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"logging.format": {
"default": "console",
"env": "LOG_FORMAT",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"output_file": {
"default": default_output_file,
"env": "OUTPUT_FILE",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"output_file_mode": {
"default": "0640",
"env": "OUTPUT_FILE_MODE",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"loop_delay": {
"default": 300,
"env": "LOOP_DELAY",
"file": True,
"type": environs.Env().int
"type": environs.Env().int,
},
"service": {
"default": True,
"env": "SERVICE",
"file": True,
"type": environs.Env().bool
"type": environs.Env().bool,
},
"exclude_state": {
"default": [],
"env": "EXCLUDE_STATE",
"file": True,
"type": environs.Env().list
"type": environs.Env().list,
},
"exclude_vmid": {
"default": [],
"env": "EXCLUDE_VMID",
"file": True,
"type": environs.Env().list
"type": environs.Env().list,
},
"include_vmid": {
"default": [],
"env": "INCLUDE_VMID",
"file": True,
"type": environs.Env().list
"type": environs.Env().list,
},
"exclude_tags": {
"default": [],
"env": "EXCLUDE_TAGS",
"file": True,
"type": environs.Env().list
"type": environs.Env().list,
},
"include_tags": {
"default": [],
"env": "INCLUDE_TAGS",
"file": True,
"type": environs.Env().list
"type": environs.Env().list,
},
"pve.server": {
"default": "",
"env": "PVE_SERVER",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"pve.user": {
"default": "",
"env": "PVE_USER",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"pve.password": {
"default": "",
"env": "PVE_PASSWORD",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"pve.token_name": {
"default": "",
"env": "PVE_TOKEN_NAME",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"pve.token_value": {
"default": "",
"env": "PVE_TOKEN_VALUE",
"file": True,
"type": environs.Env().str
"type": environs.Env().str,
},
"pve.auth_timeout": {
"default": 5,
"env": "PVE_AUTH_TIMEOUT",
"file": True,
"type": environs.Env().int
"type": environs.Env().int,
},
"pve.verify_ssl": {
"default": True,
"env": "PVE_VERIFY_SSL",
"file": True,
"type": environs.Env().bool
"type": environs.Env().bool,
},
}

Expand Down Expand Up @@ -245,7 +245,8 @@ def _set_config(self):
try:
file_dict = ruamel.yaml.YAML(typ="safe", pure=True).load(s)
except (
ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError
ruamel.yaml.composer.ComposerError,
ruamel.yaml.scanner.ScannerError,
) as e:
message = f"{e.context} {e.problem}"
raise prometheuspvesd.exception.ConfigError(
Expand Down Expand Up @@ -287,17 +288,19 @@ def _validate(self, config):
schema_error = "Failed validating '{validator}' in schema{schema}\n{message}".format(
validator=e.validator,
schema=format_as_index(list(e.relative_schema_path)[:-1], 0),
message=e.message
message=e.message,
)
raise prometheuspvesd.exception.ConfigError("Configuration error", schema_error) from e

return True

def _add_dict_branch(self, tree, vector, value):
key = vector[0]
tree[key] = value \
if len(vector) == 1 \
tree[key] = (
value
if len(vector) == 1
else self._add_dict_branch(tree[key] if key in tree else {}, vector[1:], value)
)
return tree


Expand Down
15 changes: 6 additions & 9 deletions prometheuspvesd/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ def _filter(self, pve_list):
):
continue

if (
len(self.config.config["include_tags"]) > 0 and (
bool(obj["tags"]) is False # continue if tags is not set
or set(obj["tags"].split(",")).isdisjoint(self.config.config["include_tags"])
)
if len(self.config.config["include_tags"]) > 0 and (
bool(obj["tags"]) is False # continue if tags is not set
or set(obj["tags"].split(",")).isdisjoint(self.config.config["include_tags"])
):
continue

Expand All @@ -134,9 +132,8 @@ def _filter(self, pve_list):
if str(obj["vmid"]) in self.config.config["exclude_vmid"]:
continue

if (
isinstance(obj["tags"], str)
and not set(obj["tags"].split(",")).isdisjoint(self.config.config["exclude_tags"])
if isinstance(obj["tags"], str) and not set(obj["tags"].split(",")).isdisjoint(
self.config.config["exclude_tags"]
):
continue

Expand Down Expand Up @@ -183,7 +180,7 @@ def propagate(self):
config = self.client.get_instance_config(node, pve_type, vmid)

try:
description = (config["description"])
description = config["description"]
except KeyError:
description = None
except Exception as e: # noqa
Expand Down
2 changes: 1 addition & 1 deletion prometheuspvesd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def strtobool(value):
"f": False,
"false": False,
"off": False,
"0": False
"0": False,
}

try:
Expand Down
Loading

0 comments on commit f13de60

Please sign in to comment.