Skip to content

Instantly share code, notes, and snippets.

@KaratekHD
Created August 22, 2021 10:22
Show Gist options
  • Save KaratekHD/eb9fbf1b223efdf5393fa5905bb47d2f to your computer and use it in GitHub Desktop.
Save KaratekHD/eb9fbf1b223efdf5393fa5905bb47d2f to your computer and use it in GitHub Desktop.
Simple tool do help working with openSUSE/defrag-api
import argparse
from argparse import HelpFormatter
import subprocess
import pathlib
import os
import glob
class CustomHelpFormatter(HelpFormatter):
def _format_action_invocation(self, action):
if not action.option_strings:
# Use default methods for positional arguments
default = self._get_default_metavar_for_positional(action)
metavar, = self._metavar_formatter(action, default)(1)
return metavar
else:
parts = []
if action.nargs == 0:
# Just add options, if they expects no values (like --help)
parts.extend(action.option_strings)
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
for option_string in action.option_strings:
parts.append(option_string)
# Join the argument names (like -p --param ) and add the
# metavar at the end
return '%s %s' % (', '.join(parts), args_string)
return ', '.join(parts)
"""
With the custom formatter the metavar does not get displayed twice.
With the max_help_position you can decide how long the parameters + metavar should be before a line break gets inserted,
additionally the width parameter defines the maximum length of a line.
The difference can be seen here:
https://github.com/alex1701c/Screenshots/blob/master/PythonArgparseCLI/default_output.png
https://github.com/alex1701c/Screenshots/blob/master/PythonArgparseCLI/customized_output_format.png
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Helper script for openSUSE defrag',
formatter_class=CustomHelpFormatter)
parser.add_argument(
'-v',
'--version',
action='version',
version='defrag-src 1.0')
parser.add_argument(
'-d',
'--disable',
metavar="<disable>",
help="Disable a module.")
parser.add_argument(
'-e',
'--enable',
metavar='<enable>',
help="Enable a module.")
parser.add_argument(
'-a',
'--enableall',
action="store_true",
help="Enable all modules.")
args = parser.parse_args()
if args.enable or args.disable:
if args.disable:
disable = args.disable.split()
print(f"Modules to disable: {disable}")
count = 0
for module in disable:
if not pathlib.Path(f"defrag/modules/{module}.py").is_file():
if pathlib.Path(
f"defrag/modules/{module}.py.disabled").is_file():
print(f"Error: Module '{module}' is already disabled.")
else:
print(f"Error: Module '{module}' does not exist.")
else:
os.rename(
f"defrag/modules/{module}.py",
f"defrag/modules/{module}.py.disabled")
count = count + 1
print(f"{count} modules disabled.")
if args.enable:
enable = args.enable.split()
print(f"Modules to enable: {enable}")
count = 0
for module in enable:
if not pathlib.Path(
f"defrag/modules/{module}.py.disabled").is_file():
if pathlib.Path(f"defrag/modules/{module}.py").is_file():
print(f"Error: Module '{module}' is already enabled.")
else:
print(f"Error: Module '{module}' does not exist.")
else:
os.rename(
f"defrag/modules/{module}.py.disabled",
f"defrag/modules/{module}.py")
count = count + 1
print(f"{count} modules enabled.")
elif args.enableall:
mod_paths = glob.glob("defrag/modules/*.disabled")
to_enable = []
for i in mod_paths:
to_enable.append(
i.replace(
"defrag/modules/",
"").replace(
".py.disabled",
""))
print(f"Modules to enable: {to_enable}")
count = 0
for module in to_enable:
if not pathlib.Path(
f"defrag/modules/{module}.py.disabled").is_file():
if pathlib.Path(f"defrag/modules/{module}.py").is_file():
print(f"Error: Module '{module}' is already enabled.")
else:
print(f"Error: Module '{module}' does not exist.")
else:
os.rename(
f"defrag/modules/{module}.py.disabled",
f"defrag/modules/{module}.py")
count = count + 1
print(f"{count} modules enabled.")
else:
try:
subprocess.call("python3 -m defrag", shell=True)
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment