autoparse.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. # Copyright 2014-2015 Nathan West
  2. #
  3. # This file is part of autocommand.
  4. #
  5. # autocommand is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # autocommand is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with autocommand. If not, see <http://www.gnu.org/licenses/>.
  17. import sys
  18. from re import compile as compile_regex
  19. from inspect import signature, getdoc, Parameter
  20. from argparse import ArgumentParser
  21. from contextlib import contextmanager
  22. from functools import wraps
  23. from io import IOBase
  24. from autocommand.errors import AutocommandError
  25. _empty = Parameter.empty
  26. class AnnotationError(AutocommandError):
  27. '''Annotation error: annotation must be a string, type, or tuple of both'''
  28. class PositionalArgError(AutocommandError):
  29. '''
  30. Postional Arg Error: autocommand can't handle postional-only parameters
  31. '''
  32. class KWArgError(AutocommandError):
  33. '''kwarg Error: autocommand can't handle a **kwargs parameter'''
  34. class DocstringError(AutocommandError):
  35. '''Docstring error'''
  36. class TooManySplitsError(DocstringError):
  37. '''
  38. The docstring had too many ---- section splits. Currently we only support
  39. using up to a single split, to split the docstring into description and
  40. epilog parts.
  41. '''
  42. def _get_type_description(annotation):
  43. '''
  44. Given an annotation, return the (type, description) for the parameter.
  45. If you provide an annotation that is somehow both a string and a callable,
  46. the behavior is undefined.
  47. '''
  48. if annotation is _empty:
  49. return None, None
  50. elif callable(annotation):
  51. return annotation, None
  52. elif isinstance(annotation, str):
  53. return None, annotation
  54. elif isinstance(annotation, tuple):
  55. try:
  56. arg1, arg2 = annotation
  57. except ValueError as e:
  58. raise AnnotationError(annotation) from e
  59. else:
  60. if callable(arg1) and isinstance(arg2, str):
  61. return arg1, arg2
  62. elif isinstance(arg1, str) and callable(arg2):
  63. return arg2, arg1
  64. raise AnnotationError(annotation)
  65. def _add_arguments(param, parser, used_char_args, add_nos):
  66. '''
  67. Add the argument(s) to an ArgumentParser (using add_argument) for a given
  68. parameter. used_char_args is the set of -short options currently already in
  69. use, and is updated (if necessary) by this function. If add_nos is True,
  70. this will also add an inverse switch for all boolean options. For
  71. instance, for the boolean parameter "verbose", this will create --verbose
  72. and --no-verbose.
  73. '''
  74. # Impl note: This function is kept separate from make_parser because it's
  75. # already very long and I wanted to separate out as much as possible into
  76. # its own call scope, to prevent even the possibility of suble mutation
  77. # bugs.
  78. if param.kind is param.POSITIONAL_ONLY:
  79. raise PositionalArgError(param)
  80. elif param.kind is param.VAR_KEYWORD:
  81. raise KWArgError(param)
  82. # These are the kwargs for the add_argument function.
  83. arg_spec = {}
  84. is_option = False
  85. # Get the type and default from the annotation.
  86. arg_type, description = _get_type_description(param.annotation)
  87. # Get the default value
  88. default = param.default
  89. # If there is no explicit type, and the default is present and not None,
  90. # infer the type from the default.
  91. if arg_type is None and default not in {_empty, None}:
  92. arg_type = type(default)
  93. # Add default. The presence of a default means this is an option, not an
  94. # argument.
  95. if default is not _empty:
  96. arg_spec['default'] = default
  97. is_option = True
  98. # Add the type
  99. if arg_type is not None:
  100. # Special case for bool: make it just a --switch
  101. if arg_type is bool:
  102. if not default or default is _empty:
  103. arg_spec['action'] = 'store_true'
  104. else:
  105. arg_spec['action'] = 'store_false'
  106. # Switches are always options
  107. is_option = True
  108. # Special case for file types: make it a string type, for filename
  109. elif isinstance(default, IOBase):
  110. arg_spec['type'] = str
  111. # TODO: special case for list type.
  112. # - How to specificy type of list members?
  113. # - param: [int]
  114. # - param: int =[]
  115. # - action='append' vs nargs='*'
  116. else:
  117. arg_spec['type'] = arg_type
  118. # nargs: if the signature includes *args, collect them as trailing CLI
  119. # arguments in a list. *args can't have a default value, so it can never be
  120. # an option.
  121. if param.kind is param.VAR_POSITIONAL:
  122. # TODO: consider depluralizing metavar/name here.
  123. arg_spec['nargs'] = '*'
  124. # Add description.
  125. if description is not None:
  126. arg_spec['help'] = description
  127. # Get the --flags
  128. flags = []
  129. name = param.name
  130. if is_option:
  131. # Add the first letter as a -short option.
  132. for letter in name[0], name[0].swapcase():
  133. if letter not in used_char_args:
  134. used_char_args.add(letter)
  135. flags.append('-{}'.format(letter))
  136. break
  137. # If the parameter is a --long option, or is a -short option that
  138. # somehow failed to get a flag, add it.
  139. if len(name) > 1 or not flags:
  140. flags.append('--{}'.format(name))
  141. arg_spec['dest'] = name
  142. else:
  143. flags.append(name)
  144. parser.add_argument(*flags, **arg_spec)
  145. # Create the --no- version for boolean switches
  146. if add_nos and arg_type is bool:
  147. parser.add_argument(
  148. '--no-{}'.format(name),
  149. action='store_const',
  150. dest=name,
  151. const=default if default is not _empty else False)
  152. def make_parser(func_sig, description, epilog, add_nos):
  153. '''
  154. Given the signature of a function, create an ArgumentParser
  155. '''
  156. parser = ArgumentParser(description=description, epilog=epilog)
  157. used_char_args = {'h'}
  158. # Arange the params so that single-character arguments are first. This
  159. # esnures they don't have to get --long versions. sorted is stable, so the
  160. # parameters will otherwise still be in relative order.
  161. params = sorted(
  162. func_sig.parameters.values(),
  163. key=lambda param: len(param.name) > 1)
  164. for param in params:
  165. _add_arguments(param, parser, used_char_args, add_nos)
  166. return parser
  167. _DOCSTRING_SPLIT = compile_regex(r'\n\s*-{4,}\s*\n')
  168. def parse_docstring(docstring):
  169. '''
  170. Given a docstring, parse it into a description and epilog part
  171. '''
  172. if docstring is None:
  173. return '', ''
  174. parts = _DOCSTRING_SPLIT.split(docstring)
  175. if len(parts) == 1:
  176. return docstring, ''
  177. elif len(parts) == 2:
  178. return parts[0], parts[1]
  179. else:
  180. raise TooManySplitsError()
  181. def autoparse(
  182. func=None, *,
  183. description=None,
  184. epilog=None,
  185. add_nos=False,
  186. parser=None):
  187. '''
  188. This decorator converts a function that takes normal arguments into a
  189. function which takes a single optional argument, argv, parses it using an
  190. argparse.ArgumentParser, and calls the underlying function with the parsed
  191. arguments. If it is not given, sys.argv[1:] is used. This is so that the
  192. function can be used as a setuptools entry point, as well as a normal main
  193. function. sys.argv[1:] is not evaluated until the function is called, to
  194. allow injecting different arguments for testing.
  195. It uses the argument signature of the function to create an
  196. ArgumentParser. Parameters without defaults become positional parameters,
  197. while parameters *with* defaults become --options. Use annotations to set
  198. the type of the parameter.
  199. The `desctiption` and `epilog` parameters corrospond to the same respective
  200. argparse parameters. If no description is given, it defaults to the
  201. decorated functions's docstring, if present.
  202. If add_nos is True, every boolean option (that is, every parameter with a
  203. default of True/False or a type of bool) will have a --no- version created
  204. as well, which inverts the option. For instance, the --verbose option will
  205. have a --no-verbose counterpart. These are not mutually exclusive-
  206. whichever one appears last in the argument list will have precedence.
  207. If a parser is given, it is used instead of one generated from the function
  208. signature. In this case, no parser is created; instead, the given parser is
  209. used to parse the argv argument. The parser's results' argument names must
  210. match up with the parameter names of the decorated function.
  211. The decorated function is attached to the result as the `func` attribute,
  212. and the parser is attached as the `parser` attribute.
  213. '''
  214. # If @autoparse(...) is used instead of @autoparse
  215. if func is None:
  216. return lambda f: autoparse(
  217. f, description=description,
  218. epilog=epilog,
  219. add_nos=add_nos,
  220. parser=parser)
  221. func_sig = signature(func)
  222. docstr_description, docstr_epilog = parse_docstring(getdoc(func))
  223. if parser is None:
  224. parser = make_parser(
  225. func_sig,
  226. description or docstr_description,
  227. epilog or docstr_epilog,
  228. add_nos)
  229. @wraps(func)
  230. def autoparse_wrapper(argv=None):
  231. if argv is None:
  232. argv = sys.argv[1:]
  233. # Get empty argument binding, to fill with parsed arguments. This
  234. # object does all the heavy lifting of turning named arguments into
  235. # into correctly bound *args and **kwargs.
  236. parsed_args = func_sig.bind_partial()
  237. parsed_args.arguments.update(vars(parser.parse_args(argv)))
  238. return func(*parsed_args.args, **parsed_args.kwargs)
  239. # TODO: attach an updated __signature__ to autoparse_wrapper, just in case.
  240. # Attach the wrapped function and parser, and return the wrapper.
  241. autoparse_wrapper.func = func
  242. autoparse_wrapper.parser = parser
  243. return autoparse_wrapper
  244. @contextmanager
  245. def smart_open(filename_or_file, *args, **kwargs):
  246. '''
  247. This context manager allows you to open a filename, if you want to default
  248. some already-existing file object, like sys.stdout, which shouldn't be
  249. closed at the end of the context. If the filename argument is a str, bytes,
  250. or int, the file object is created via a call to open with the given *args
  251. and **kwargs, sent to the context, and closed at the end of the context,
  252. just like "with open(filename) as f:". If it isn't one of the openable
  253. types, the object simply sent to the context unchanged, and left unclosed
  254. at the end of the context. Example:
  255. def work_with_file(name=sys.stdout):
  256. with smart_open(name) as f:
  257. # Works correctly if name is a str filename or sys.stdout
  258. print("Some stuff", file=f)
  259. # If it was a filename, f is closed at the end here.
  260. '''
  261. if isinstance(filename_or_file, (str, bytes, int)):
  262. with open(filename_or_file, *args, **kwargs) as file:
  263. yield file
  264. else:
  265. yield filename_or_file