diff options
author | Jean Abou Samra <jean@abou-samra.fr> | 2023-03-30 22:25:29 +0200 |
---|---|---|
committer | Jean Abou Samra <jean@abou-samra.fr> | 2023-03-30 22:25:29 +0200 |
commit | 242e56e9c0ab98f5e51a1eb765a79b84e44125ce (patch) | |
tree | 9eed8175cff87f617bf9f48e6e56a76a8587b505 | |
parent | 243dd815b90bbc25f038149e9b99b70f0bd1a146 (diff) | |
download | pygments-git-242e56e9c0ab98f5e51a1eb765a79b84e44125ce.tar.gz |
Use autodoc for pygments.util
-rw-r--r-- | doc/docs/api.rst | 40 | ||||
-rw-r--r-- | pygments/util.py | 28 |
2 files changed, 32 insertions, 36 deletions
diff --git a/doc/docs/api.rst b/doc/docs/api.rst index 6260fde7..a1dd71e7 100644 --- a/doc/docs/api.rst +++ b/doc/docs/api.rst @@ -180,6 +180,8 @@ A formatter is derived from this class: .. autoclass:: Formatter :members: __init__, get_style_defs, format + + .. module:: pygments.util Option processing @@ -191,36 +193,8 @@ dictionary of options. If the value is already in the type expected by the option, it is returned as-is. Otherwise, if the value is a string, it is first converted to the expected type if possible. -.. exception:: OptionError - - This exception will be raised by all option processing functions if - the type or value of the argument is not correct. - -.. function:: get_bool_opt(options, optname, default=None) - - Intuitively, this is `options.get(optname, default)`, but restricted to - Boolean value. The Booleans can be represented as string, in order to accept - Boolean value from the command line arguments. If the key `optname` is - present in the dictionary `options` and is not associated with a Boolean, - raise an `OptionError`. If it is absent, `default` is returned instead. - - The valid string values for ``True`` are ``1``, ``yes``, ``true`` and - ``on``, the ones for ``False`` are ``0``, ``no``, ``false`` and ``off`` - (matched case-insensitively). - -.. function:: get_int_opt(options, optname, default=None) - - As :func:`get_bool_opt`, but interpret the value as an integer. - -.. function:: get_list_opt(options, optname, default=None) - - If the key `optname` from the dictionary `options` is a string, - split it at whitespace and return it. If it is already a list - or a tuple, it is returned as a list. - -.. function:: get_choice_opt(options, optname, allowed, default=None) - - If the key `optname` from the dictionary is not in the sequence - `allowed`, raise an error, otherwise return it. - - .. versionadded:: 0.8 +.. autoexception:: OptionError +.. autofunction:: get_bool_opt +.. autofunction:: get_int_opt +.. autofunction:: get_list_opt +.. autofunction:: get_choice_opt diff --git a/pygments/util.py b/pygments/util.py index d7ec9c11..941fdb9e 100644 --- a/pygments/util.py +++ b/pygments/util.py @@ -32,10 +32,16 @@ class ClassNotFound(ValueError): class OptionError(Exception): - pass - + """ + This exception will be raised by all option processing functions if + the type or value of the argument is not correct. + """ def get_choice_opt(options, optname, allowed, default=None, normcase=False): + """ + If the key `optname` from the dictionary is not in the sequence + `allowed`, raise an error, otherwise return it. + """ string = options.get(optname, default) if normcase: string = string.lower() @@ -46,6 +52,17 @@ def get_choice_opt(options, optname, allowed, default=None, normcase=False): def get_bool_opt(options, optname, default=None): + """ + Intuitively, this is `options.get(optname, default)`, but restricted to + Boolean value. The Booleans can be represented as string, in order to accept + Boolean value from the command line arguments. If the key `optname` is + present in the dictionary `options` and is not associated with a Boolean, + raise an `OptionError`. If it is absent, `default` is returned instead. + + The valid string values for ``True`` are ``1``, ``yes``, ``true`` and + ``on``, the ones for ``False`` are ``0``, ``no``, ``false`` and ``off`` + (matched case-insensitively). + """ string = options.get(optname, default) if isinstance(string, bool): return string @@ -66,6 +83,7 @@ def get_bool_opt(options, optname, default=None): def get_int_opt(options, optname, default=None): + """As :func:`get_bool_opt`, but interpret the value as an integer.""" string = options.get(optname, default) try: return int(string) @@ -78,8 +96,12 @@ def get_int_opt(options, optname, default=None): 'must give an integer value' % ( string, optname)) - def get_list_opt(options, optname, default=None): + """ + If the key `optname` from the dictionary `options` is a string, + split it at whitespace and return it. If it is already a list + or a tuple, it is returned as a list. + """ val = options.get(optname, default) if isinstance(val, str): return val.split() |