Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 2 changed files Side-by-side Diff

... ... @@ -0,0 +1,45 @@
  1 +import argparse
  2 +import sys
  3 +
  4 +from volia.utils import SubCommandRunner
  5 +
  6 +# Main parser
  7 +parser = argparse.ArgumentParser(description="manage files")
  8 +subparsers = parser.add_subparsers(title="actions")
  9 +
  10 +
  11 +
  12 +# Filter ids
  13 +parser_filter = subparsers.add_parser("filter", help="filter a file")
  14 +parser_filter.add_argument("--file", type=str, help="path of the file to filter")
  15 +parser_filter.add_argument("--filter", type=str, help="filter file [id_from] [id_to]")
  16 +parser_filter.set_defaults(which="filter")
  17 +
  18 +# Convert
  19 +parser_convert = subparsers.add_parser("convert", help="convert a file")
  20 +parser_convert.add_argument("--file", type=str, help="...")
  21 +parser_convert.add_argument("--type-from", type=str, choices=["old-masseffect", "new-masseffect"], help="...")
  22 +parser_convert.add_argument("--type", choices=["old-masseffect", "new-masseffect"])
  23 +parser_convert.set_defaults(which="convert")
  24 +
  25 +
  26 +# Parse
  27 +args = parser.parse_args()
  28 +
  29 +
  30 +
  31 +def filter_file(file_path, filter_path):
  32 + pass
  33 +
  34 +
  35 +def convert(file, type_from, type):
  36 + pass
  37 +
  38 +
  39 +if __name__ == "__main__":
  40 + runner = SubCommandRunner({
  41 + "convert" : convert,
  42 + "filter": filter_file,
  43 + })
  44 +
  45 + runner.run(args.which, args.__dict__, remove="which")
... ... @@ -0,0 +1,76 @@
  1 +
  2 +class SubCommandRunner:
  3 + """This class provides a simple way to call subcommands
  4 + from argparsers ones. Soon, we will add some examples to
  5 + into the documentation.
  6 + """
  7 +
  8 + def __init__(self, choice_dict):
  9 + """Initialisation of choices
  10 +
  11 + Args:
  12 + choice_dict (dict): A dictionary where each string key is associated
  13 + to a function to call.
  14 + """
  15 + self._choice_dict = choice_dict
  16 +
  17 + def run(self, choice, args=None, remove=[]):
  18 + """Run a function as the choice key.
  19 +
  20 + Args:
  21 + choice (str): [description]
  22 + args (dict, optional): dictionnary of arguments. Defaults to None.
  23 + remove (list, optional): list of key to remove from arguments. Defaults to [].
  24 + """
  25 + if args is not None:
  26 + copy_dict = args.copy()
  27 + if type(remove) == list:
  28 + [copy_dict.pop(v) for v in remove]
  29 + else:
  30 + copy_dict.pop(remove)
  31 +
  32 + try:
  33 + if args is None:
  34 + self._choice_dict.get(choice)()
  35 + else:
  36 + self._choice_dict.get(choice)(**copy_dict)
  37 + except TypeError as e:
  38 + print("TYPE ERROR HANDLED")
  39 + print("Value of args: ", args)
  40 + raise(e)
  41 +
  42 +if __name__ == "__main__":
  43 + ''' Just a dumb function '''
  44 + def dumb1():
  45 + print("Dumb 1")
  46 +
  47 + ''' Just a dumb function with one parameter '''
  48 + def dumb2(param1):
  49 + print("Dumb 2: ", param1)
  50 +
  51 + print("Utils run mode is just used to test implementation of class and function implementation.")
  52 +
  53 + runner = SubCommandRunner({
  54 + "dumb1": dumb1,
  55 + "dumb2": dumb2
  56 + })
  57 +
  58 + dumb2_args = {
  59 + "param1": "Le paramètre 1"
  60 + }
  61 +
  62 + dumb2_args_which = {
  63 + "param1": "Le paramètre 1 une seconde fois",
  64 + "which": "which value usally defined and need to be removed"
  65 + }
  66 +
  67 + dumb2_args_which_array = {
  68 + "param1": "Le paramètre 1 une troisième fois",
  69 + "which": "which value usally defined and need to be removed",
  70 + "param2": "Imagine you want to remove multiple values"
  71 + }
  72 +
  73 + runner.run("dumb1", None)
  74 + runner.run("dumb2", dumb2_args)
  75 + runner.run("dumb2", dumb2_args_which, remove="which")
  76 + runner.run("dumb2", dumb2_args_which_array, remove=["which", "param2"])
0 77 \ No newline at end of file