Skip to content

Commit

Permalink
Remove fallback for BooleanOptionalAction (obsolete in python>=3.9) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
znerol committed Oct 16, 2023
1 parent 49933c9 commit ed319ed
Showing 1 changed file with 6 additions and 52 deletions.
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

0 comments on commit ed319ed

Please sign in to comment.