You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. """
  2. Created on Jan 24, 2014 <-- so long ago!
  3. """
  4. import sys
  5. from argparse import ArgumentParser
  6. from functools import wraps
  7. from gooey.python_bindings.control import choose_hander
  8. from gooey.python_bindings.parameters import gooey_params
  9. from gooey.python_bindings.types import GooeyParams
  10. IGNORE_COMMAND = '--ignore-gooey'
  11. def Gooey(f=None, **gkwargs):
  12. """
  13. Decoration entry point for the Gooey process.
  14. See types.GooeyParams for kwargs options
  15. """
  16. params: GooeyParams = gooey_params(**gkwargs)
  17. @wraps(f)
  18. def inner(*args, **kwargs):
  19. parser_handler = choose_hander(params, gkwargs.get('cli', sys.argv))
  20. # monkey patch parser
  21. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  22. ArgumentParser.parse_args = parser_handler
  23. # return the wrapped, now monkey-patched, user function
  24. # to be later invoked
  25. return f(*args, **kwargs)
  26. def thunk(func):
  27. """
  28. This just handles the case where the decorator is called
  29. with arguments (i.e. @Gooey(foo=bar) rather than @Gooey).
  30. Cause python is weird, when a decorator is called (e.g. @decorator())
  31. rather than just declared (e.g. @decorator), in complete and utter
  32. defiance of what your lying eyes see, it changes from a higher order
  33. function, to a function that takes an arbitrary argument *and then*
  34. returns a higher order function. i.e.
  35. decorate :: (a -> b) -> (a -> b)
  36. decorate() :: c -> (a -> b) -> (a -> b)
  37. wat.
  38. """
  39. return Gooey(func, **params)
  40. return inner if callable(f) else thunk