Skip to content

Commit 92e4d87

Browse files
committed
Document utilites
This commit adds docstrings to the utilities module, the __init__ file, and the exceptions module. Part of #7
1 parent f593559 commit 92e4d87

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

gssapi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
from gssapi.creds import Credentials # noqa
66
from gssapi.names import Name # noqa
77
from gssapi.sec_contexts import SecurityContext # noqa
8+
9+
"""The High-Level GSSAPI Wrapper"""

gssapi/_utils.py

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88

99

1010
def import_gssapi_extension(name):
11+
"""Import a GSSAPI extension module
12+
13+
This method imports a GSSAPI extension module based
14+
on the name of the extension (not including the
15+
'ext_' prefix). If the extension is not available,
16+
the method retuns None.
17+
18+
Args:
19+
name (str): the name of the extension
20+
21+
Returns:
22+
module: Either the extension module or None
23+
"""
24+
1125
try:
1226
path = 'gssapi.raw.ext_{0}'.format(name)
1327
__import__(path)
@@ -29,7 +43,20 @@ def getter(self):
2943
return property(getter, setter)
3044

3145

32-
def inquire_property(name):
46+
def inquire_property(name, doc=None):
47+
"""Creates a property based on an inquire result
48+
49+
This method creates a property that calls the
50+
:python:`_inquire` method, and return the value of the
51+
requested information.
52+
53+
Args:
54+
name (str): the name of the 'inquire' result information
55+
56+
Returns:
57+
property: the created property
58+
"""
59+
3360
def inquire_property(self):
3461
if not self._started:
3562
msg = ("Cannot read {0} from a security context whose "
@@ -38,23 +65,41 @@ def inquire_property(self):
3865

3966
return getattr(self._inquire(**{name: True}), name)
4067

41-
return property(inquire_property)
68+
return property(inquire_property, doc=doc)
4269

4370

4471
# use UTF-8 as the default encoding, like Python 3
4572
_ENCODING = 'UTF-8'
4673

4774

4875
def _get_encoding():
76+
"""Gets the current encoding used for strings.
77+
78+
This value is used to encode and decode string
79+
values like names.
80+
81+
Returns:
82+
str: the current encoding
83+
"""
4984
return _ENCODING
5085

5186

5287
def set_encoding(enc):
88+
"""Sets the current encoding used for strings
89+
90+
This value is used to encode and decode string
91+
values like names.
92+
93+
Args:
94+
enc: the encoding to use
95+
"""
96+
5397
global _ENCODING
5498
_ENCODING = enc
5599

56100

57101
def _encode_dict(d):
102+
"""Encodes any relevant strings in a dict"""
58103
def enc(x):
59104
if isinstance(x, six.text_type):
60105
return x.encode(_ENCODING)
@@ -67,6 +112,17 @@ def enc(x):
67112
# in case of Python 3, just use exception chaining
68113
@deco.decorator
69114
def catch_and_return_token(func, self, *args, **kwargs):
115+
"""Optionally defer exceptions and return a token instead
116+
117+
When `__DEFER_STEP_ERRORS__` is set on the implementing class
118+
or instance, methods wrapped with this wrapper will
119+
catch and save their :python:`GSSError` exceptions and
120+
instead return the result token attached to the exception.
121+
122+
The exception can be later retrived through :python:`_last_err`
123+
(and :python:`_last_tb` when Python 2 is in use).
124+
"""
125+
70126
try:
71127
return func(self, *args, **kwargs)
72128
except GSSError as e:
@@ -85,6 +141,13 @@ def catch_and_return_token(func, self, *args, **kwargs):
85141

86142
@deco.decorator
87143
def check_last_err(func, self, *args, **kwargs):
144+
"""Check and raise deferred errors before running the function
145+
146+
This method checks :python:`_last_err` before running the wrapped
147+
function. If present and not None, the exception will be raised
148+
with its original traceback.
149+
"""
150+
88151
if self._last_err is not None:
89152
try:
90153
if six.PY2:
@@ -115,6 +178,14 @@ def check_last_err(func, self, *args, **kwargs):
115178

116179

117180
class CheckLastError(type):
181+
"""Check for a deferred error on all methods
182+
183+
This metaclass applies the :python:`check_last_err` decorator
184+
to all methods not prefixed by '_'.
185+
186+
Additionally, it enabled `__DEFER_STEP_ERRORS__` by default.
187+
"""
188+
118189
def __new__(cls, name, parents, attrs):
119190
attrs['__DEFER_STEP_ERRORS__'] = True
120191

gssapi/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
from gssapi.raw.exceptions import * # noqa
22
from gssapi.raw.misc import GSSError # noqa
33

4+
"""High-Level API Errors
5+
6+
This module includes several high-level exceptions,
7+
in addition to GSSError and exceptions from
8+
:mod:`gssapi.raw.exceptions`.
9+
"""
10+
411

512
# non-GSS exceptions
613
class GeneralError(Exception):
14+
"""A General High-Level API Error"""
715
MAJOR_MESSAGE = "General error"
816
FMT_STR = "{maj}: {min}."
917

@@ -14,10 +22,12 @@ def __init__(self, minor_message, **kwargs):
1422

1523

1624
class UnknownUsageError(GeneralError):
25+
"""An Error indicating an unknown usage type"""
1726
MAJOR_MESSAGE = "Unable to determine {obj} usage"
1827

1928

2029
class EncryptionNotUsed(GeneralError):
30+
"""An Error indicating that encryption was requested, but not used"""
2131
MAJOR_MESSAGE = "Confidentiality was requested, but not used"
2232

2333
def __init__(self, minor_message, unwrapped_message=None, **kwargs):

0 commit comments

Comments
 (0)