dl.Util module

dl.Util.add_doc(value)[source]

Decorator to set the ‘Call docstring’ ipython field.

dl.Util.auth_token_to_dict(token)[source]

Given an auth token splits it in its components and returns a dictionary. :param token: A string auth token

E.g. “testuser.3666.3666.$1$PKCFmMzy$OPpZg/ThBmZe/V8LVPvpi/”

Returns:

  • return

    { ‘username’: “username value”,

    ’uid’: “numeric user id”, ‘gid’: “numeric group id”, ‘hash’: hash

    }

  • E.g. { ‘username’ (“testuser”,) – ‘uid’: “3666”,

    ’gid’: “3666”, ‘hash’: “$1$PKCFmMzy$OPpZg/ThBmZe/V8LVPvpi/”

    } or None if token not valid

dl.Util.def_token(tok)[source]

Get a default token. If no token is provided, check for an existing $HOME/.datalab/id_token.<user> file and return that if it exists, otherwise default to the ANON_TOKEN.

If a token string is provided, return it directly. The value may also simply be a username, in which case the same check for a token ID file is done.

dl.Util.encode_multipart(fields, files, boundary=None)[source]

Encode dict of form fields and dict of files as multipart/form-data. Return tuple of (body_string, headers_dict). Each value in files is a dict with required keys ‘filename’ and ‘content’, and optional ‘mimetype’ (if not specified, tries to guess mime type or uses ‘application/octet-stream’).

..code-block:: python

>>> body, headers = encode_multipart({'FIELD': 'VALUE'},
...                                  {'FILE': {'filename': 'F.TXT', 'content': 'CONTENT'}},
...                                  boundary='BOUNDARY')
>>> print('\n'.join(repr(l) for l in body.split('\r\n')))
'--BOUNDARY'
'Content-Disposition: form-data; name="FIELD"'
''
'VALUE'
'--BOUNDARY'
'Content-Disposition: form-data; name="FILE"; filename="F.TXT"'
'Content-Type: text/plain'
''
'CONTENT'
'--BOUNDARY--'
''
>>> print(sorted(headers.items()))
[('Content-Length', '193'), ('Content-Type', 'multipart/form-data; boundary=BOUNDARY')]
>>> len(body)
193
dl.Util.is_auth_token(token)[source]

Check if passed in string is an auth token Usage:

is_auth_token(token)

Parameters:

token (str) – A string auth token E.g. “testuser.3666.3666.$1$PKCFmMzy$OPpZg/ThBmZe/V8LVPvpi/”

Returns:

return – True if string is a auth token

Return type:

boolean

dl.Util.multimethod(module, nargs, cm)[source]

Wrapper function to implement multimethod for functions. The identifying signature in this case is the number of required method parameters. When methods are called, all original arguments and keywords are passed.

dl.Util.parse_auth_token(token)[source]

Parses string argument token Usage:

parse_auth_token(token)

Parameters:

token (str) – A string auth token E.g. “testuser.3666.3666.$1$PKCFmMzy$OPpZg/ThBmZe/V8LVPvpi/”

Returns:

return

Return type:

a regex Match object or None

dl.Util.readTokenFile(tok_file)[source]
dl.Util.split_auth_token(token)[source]

Given an auth token split it in its components Usage:

split_auth_token(token)

Parameters:

token (str) – A string auth token E.g. “testuser.3666.3666.$1$PKCFmMzy$OPpZg/ThBmZe/V8LVPvpi/”

Returns:

return – or None if not a token E.g. [“testuser”, “3666”, “3666” , “$1$PKCFmMzy$OPpZg/ThBmZe/V8LVPvpi/”]

Return type:

[username, user_id, group_id, hash]

dl.Util.svcOverride(what, default)[source]

Check for an override of a (usually, service) URL as deined by either and environment variable, or a /tmp file given by the ‘what’ string. If neither is found, returns the default value.

dl.Util.validTableName(tbl)[source]

Return True if named table contains only valid lower-case chars or underscores. A ‘.’ in the string assumes the presence of a schema in the name, the schema and table name will be validated separately however both must be valid.