Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove fallback for BooleanOptionalAction (obsolete in python>=3.9) #163

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 6 additions & 52 deletions src/pve_exporter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,14 @@
Proxmox VE exporter for the Prometheus monitoring system.
"""

from argparse import ArgumentParser

from argparse import ArgumentParser, BooleanOptionalAction
import os
import yaml
from pve_exporter.http import start_http_server
from pve_exporter.config import config_from_yaml
from pve_exporter.config import config_from_env
from pve_exporter.collector import CollectorsOptions

try:
from argparse import BooleanOptionalAction
except ImportError:
from argparse import Action
# https://github.com/python/cpython/blob/master/Lib/argparse.py#L856
# pylint: disable=all
class BooleanOptionalAction(Action):
def __init__(self,
option_strings,
dest,
default=None,
type=None,
choices=None,
required=False,
help=None,
metavar=None):

_option_strings = []
for option_string in option_strings:
_option_strings.append(option_string)

if option_string.startswith('--'):
option_string = '--no-' + option_string[2:]
_option_strings.append(option_string)

if help is not None and default is not None:
help += f" (default: {default})"

super().__init__(
option_strings=_option_strings,
dest=dest,
nargs=0,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar)

def __call__(self, parser, namespace, values, option_string=None):
if option_string in self.option_strings:
setattr(namespace, self.dest, not option_string.startswith('--no-'))

def format_usage(self):
return ' | '.join(self.option_strings)


def main():
"""
Main entry point.
Expand Down Expand Up @@ -88,8 +40,10 @@ def main():
help='Port on which the exporter is listening (9221)')
parser.add_argument('address', nargs='?', default='',
help='Address to which the exporter will bind')
parser.add_argument('--server.keyfile', dest='server_keyfile', help='SSL key for server')
parser.add_argument('--server.certfile', dest='server_certfile', help='SSL certificate for server')
parser.add_argument('--server.keyfile', dest='server_keyfile',
help='SSL key for server')
parser.add_argument('--server.certfile', dest='server_certfile',
help='SSL certificate for server')

params = parser.parse_args()

Expand All @@ -106,7 +60,7 @@ def main():
if 'PVE_USER' in os.environ:
config = config_from_env(os.environ)
else:
with open(params.config) as handle:
with open(params.config, encoding='utf-8') as handle:
config = config_from_yaml(yaml.safe_load(handle))

gunicorn_options = {
Expand Down