Skip to content

Commit

Permalink
Merge pull request #17 from OphidiaBigData/feature-read_env
Browse files Browse the repository at this point in the history
Extend connection functions to get parameters from environment variables
  • Loading branch information
eldoo authored Jun 18, 2018
2 parents c2d5382 + 6b18ed4 commit 60faf93
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
46 changes: 35 additions & 11 deletions PyOphidia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_linenumber():


class Client():
"""Client(username='', password='', server='', port='11732', token='', api_mode=True) -> obj
"""Client(username='', password='', server='', port='11732', token='', read_env=False, api_mode=True) -> obj
Attributes:
username: Ophidia username
Expand Down Expand Up @@ -73,8 +73,8 @@ class Client():
pretty_print(response, response_i) -> self : Prints the last_response JSON string attribute as a formatted response
"""

def __init__(self, username='', password='', server='', port='11732', token='', api_mode=True):
"""Client(username='', password='', server='', port='11732', token='', api_mode=True) -> obj
def __init__(self, username='', password='', server='', port='11732', token='', read_env=False, api_mode=True):
"""Client(username='', password='', server='', port='11732', token='', read_env=False, api_mode=True) -> obj
:param api_mode: If True, use the class as an API and catch also framework-level errors
:type api_mode: bool
:param username: Ophidia username
Expand All @@ -87,18 +87,42 @@ def __init__(self, username='', password='', server='', port='11732', token='',
:type port: str
:param token: Ophidia token
:type token: str
:param host_partition: Name of host partition
:type host_partition: str
:param read_env: If True read the client variables from the environment
:type read_env: bool
:returns: None
:rtype: None
:raises: RuntimeError
"""

self.api_mode = api_mode
self.username = username
self.password = password
self.server = server
self.port = port
if read_env is False:
self.username = username
self.password = password
self.server = server
self.port = port
access_token = token
else:
if username:
self.username = username
else:
self.username = os.environ.get('OPH_USER')
if password:
self.password = password
else:
self.password = os.environ.get('OPH_PASSWD')
if server:
self.server = server
else:
self.server = os.environ.get('OPH_SERVER_HOST')
if port:
self.port = port
else:
self.port = os.environ.get('OPH_SERVER_PORT')
if token:
access_token = token
else:
access_token = os.environ.get('OPH_TOKEN')

self.session = ''
self.cwd = '/'
self.cdd = '/'
Expand All @@ -114,11 +138,11 @@ def __init__(self, username='', password='', server='', port='11732', token='',
self.last_error = ''
self.last_exec_time = 0.0

if not self.username and not self.password and token:
if not self.username and not self.password and access_token:
self.password = token
self.username = "__token__"

if not self.username or not self.password or not self.server or self.port is None:
if not self.username or not self.password or not self.server or not self.port:
raise RuntimeError('one or more login parameters are None')
try:
if self.api_mode:
Expand Down
10 changes: 6 additions & 4 deletions PyOphidia/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Cube():
-> dict or None : wrapper of the operator OPH_UNPUBLISH
Class Methods:
setclient(username='', password='', server, port='11732', token='')
setclient(username='', password='', server, port='11732', token='', read_env=False)
-> None : Instantiate the Client, common for all Cube objects, for submitting requests
cancel(id=None, type='kill', objkey_filter='all', display=False)
-> dict or None : wrapper of the operator OPH_CANCEL
Expand Down Expand Up @@ -224,8 +224,8 @@ class Cube():
client = None

@classmethod
def setclient(cls, username='', password='', server='', port='11732', token=''):
"""setclient(username='', password='', server='', port='11732', token='') -> None : Instantiate the Client, common for all Cube objects, for submitting requests
def setclient(cls, username='', password='', server='', port='11732', token='', read_env=False):
"""setclient(username='', password='', server='', port='11732', token='', read_env=False) -> None : Instantiate the Client, common for all Cube objects, for submitting requests
:param username: Ophidia user
:type username: str
Expand All @@ -237,12 +237,14 @@ def setclient(cls, username='', password='', server='', port='11732', token=''):
:type port: str
:param token: Ophidia token
:type token: str
:param read_env: If true read the client variables from the environment
:type read_env: bool
:returns: None
:rtype: None
"""

try:
cls.client = _client.Client(username, password, server, port, token)
cls.client = _client.Client(username, password, server, port, token, read_env)
except Exception as e:
print(get_linenumber(), "Something went wrong in setting the client:", e)
finally:
Expand Down
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ In case of authentication token is used:
ophclient = client.Client(token="token",server="127.0.0.1",port="11732")
If *OPH_USER*, *OPH_PASSWD* (or *OPH_TOKEN*), *OPH_SERVER_HOST* and *OPH_SERVER_PORT* variables have been set in the environment (see the documentation_ for more details), a client can be also created reading directly the values from the environment without the need to specify any parameter.

.. code-block:: python
ophclient = client.Client(read_env=True)
Client attributes
^^^^^^^^^^^^^^^^^
- *username*: Ophidia username
Expand Down Expand Up @@ -120,6 +127,14 @@ Instantiate a new Client common to all Cube instances:
from PyOphidia import cube
cube.Cube.setclient(username="oph-user",password="oph-passwd",server="127.0.0.1",port="11732")
or

.. code-block:: python
from PyOphidia import cube
cube.Cube.setclient(read_env=True)
Cube attributes
^^^^^^^^^^^^^^^
Instance attributes:
Expand Down Expand Up @@ -215,3 +230,4 @@ To exports data in a python-friendly format:
.. _GPLv3: http://www.gnu.org/licenses/gpl-3.0.txt
.. _Ophidia: http://ophidia.cmcc.it
.. _documentation: http://ophidia.cmcc.it/documentation/users/terminal/term_advanced.html#oph-terminal-environment

0 comments on commit 60faf93

Please sign in to comment.