Skip to content

Commit

Permalink
Merge pull request #258 from dlyssenko/provide_docstring_decorator
Browse files Browse the repository at this point in the history
fixing doc build issues, errors, and providing docstring decorator
  • Loading branch information
mharista committed May 22, 2023
2 parents fd843d4 + f77cd53 commit 7161583
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PAPER =
BUILDDIR = _build
APIDIR = api_modules
CLIENTDIR = client_modules
CWD := $(shell pwd)
CWD := '$(shell pwd)'

# User-friendly check for sphinx-build
ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, '../pyeapi')
sys.path.insert(0, os.path.abspath('..') )

# -- General configuration ------------------------------------------------

Expand Down
3 changes: 1 addition & 2 deletions docs/generate_modules.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/python
from __future__ import absolute_import, division, print_function
#!/usr/bin/python3

from os import listdir, path, makedirs
from os.path import isfile, join, exists
Expand Down
4 changes: 3 additions & 1 deletion pyeapi/api/ipinterfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import re

from pyeapi.api import EntityCollection
from pyeapi.utils import _interpolate_docstr

IP_MTU_MIN = 68
IP_MTU_MAX = 65535
Expand Down Expand Up @@ -217,6 +218,7 @@ def set_address(self, name, value=None, default=False, disable=False):
default=default, disable=disable))
return self.configure(commands)

@_interpolate_docstr( 'IP_MTU_MIN', 'IP_MTU_MAX', __name__ )
def set_mtu(self, name, value=None, default=False, disable=False):
""" Configures the interface IP MTU
Expand All @@ -225,7 +227,7 @@ def set_mtu(self, name, value=None, default=False, disable=False):
config to
value (integer): The MTU value to set the interface to. Accepted
values include IP_MTU_MIN (68) to IP_MTU_MAX (65535)
values include IP_MTU_MIN to IP_MTU_MAX
default (bool): Configures the mtu parameter to its default
value using the EOS CLI default command
Expand Down
24 changes: 14 additions & 10 deletions pyeapi/api/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,20 @@ def get(self, vrf=None):
vrf (str): VRF name to return OSPF routing config for
Returns:
dict:
keys: router_id (int): OSPF router-id
vrf (str): VRF of the OSPF process
networks (dict): All networks that
are advertised in OSPF
ospf_process_id (int): OSPF proc id
redistribution (dict): All protocols that
are configured to be
redistributed in OSPF
shutdown (bool): Gives the current shutdown
off the process
keys:
router_id (int): OSPF router-id
vrf (str): VRF of the OSPF process
networks (dict): All networks that
are advertised in OSPF
ospf_process_id (int): OSPF proc id
redistribution (dict): All protocols that
are configured to be redistributed in OSPF
shutdown (bool): Gives the current shutdown
off the process
"""
match = '^router ospf .*'
if vrf:
Expand Down
34 changes: 34 additions & 0 deletions pyeapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,37 @@ def __init__(self, *cli):
assert len( cli ) >= 2, 'must be initialized with 2 or more arguments'
self.variants = [ v if not isinstance(v,
str) and isinstance(v, Iterable) else [v] for v in cli ]



def _interpolate_docstr( *tkns ):
"""Docstring decorator.
SYNOPSIS:
MIN_MTU=68
MAX_MTU=65535
@_interpolate_docstr( 'MIN_MTU', 'MAX_MTU', __name__ )
def mtu_check( val ):
"check mtu against its min value (MIN_MTU) and max value (MAX_MTU)"
...
print( mtu_check.__doc__ )
check mtu against its min value (68) and max value (65535)
Note: `__name__` must be provided as the last argument because the decorator
could be imported, thus the current (importing) module needs to be resolved
"""
def docstr_decorator( user_fn ):
"""update user_fn_wrapper doc string with the interpolated user_fn's
"""
def user_fn_wrapper( *args, **kwargs ):
return user_fn( *args, **kwargs )
module = sys.modules[ tkns[-1] ]
docstr = user_fn.__doc__
for tkn in tkns[:-1]:
sval = str( getattr(module, tkn) )
docstr = docstr.replace( tkn, sval )
user_fn_wrapper.__doc__ = docstr
return user_fn_wrapper
return docstr_decorator

0 comments on commit 7161583

Please sign in to comment.