Skip to content

Commit

Permalink
Session initialization calls client.connect(). Closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitbryon committed Jul 22, 2015
2 parents a4171fc + e7e60c5 commit 8245467
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Expand Up @@ -11,6 +11,10 @@ Simplify first steps with `xal`.
mention ability to install with restricted set of dependencies (was
restricted install first).

- Feature #13 - Session initialization calls client.connect(). Session
constructor's signature changed, making usage of sessions more
straightforward.


0.2 (2015-07-21)
----------------
Expand Down
6 changes: 1 addition & 5 deletions README.rst
Expand Up @@ -28,8 +28,6 @@ Let's initialize a session on local system:

>>> import xal
>>> local_session = xal.LocalSession()
>>> local_session.client.connect()
True

In this session, we can manage files:

Expand Down Expand Up @@ -72,9 +70,7 @@ Goodbye!
What's nice is that we can reuse the same function in another session. Let's
create a remote SSH session using Fabric...

>>> remote_session = xal.FabricSession()
>>> remote_session.client.connect(host='localhost')
True
>>> remote_session = xal.FabricSession(host='localhost')

... then just run the same function with this remote session:

Expand Down
4 changes: 1 addition & 3 deletions docs/resources/path.txt
Expand Up @@ -28,8 +28,6 @@ Let's consider a `xal` :doc:`session </sessions>`:

>>> import xal
>>> session = xal.LocalSession()
>>> session.client.connect()
True

Path API is registered as ``path`` in `xal`'s builtin :doc:`/sessions`:

Expand Down Expand Up @@ -185,7 +183,7 @@ Concrete paths are compared with respect to session:

>>> session.path('foo') == session.path('foo')
True
>>> remote_session = xal.FabricSession()
>>> remote_session = xal.FabricSession(host='localhost')
>>> session.path('foo') == remote_session.path('foo')
False

Expand Down
2 changes: 0 additions & 2 deletions docs/resources/sh.txt
Expand Up @@ -22,8 +22,6 @@ Let's consider a `xal` :doc:`session </sessions>`:

>>> import xal
>>> session = xal.LocalSession()
>>> session.client.connect()
True

Sh API is registered as ``sh`` in `xal`'s builtin :doc:`/sessions`:

Expand Down
20 changes: 8 additions & 12 deletions docs/sessions.txt
Expand Up @@ -11,16 +11,13 @@ Quickstart

In order to use `xal` or related libraries:

1. Setup session instance
2. Log in session
1. Create session instance
3. Use session's interfaces, a.k.a. :doc:`/resources/index`

.. doctest::

>>> import xal
>>> session = xal.LocalSession()
>>> session.client.connect()
True
>>> session.sh.run('echo -n "Hello"').stdout
'Hello'

Expand All @@ -43,13 +40,12 @@ At the moment, `xal` provides two pre-configured session classes:
* :class:`~xal.session.local.LocalSession` for use on localhost. Basically uses
Python builtins.

Client's ``connect()`` method takes no arguments:
Initialization method takes no arguments:

.. doctest::

>>> local_session = xal.LocalSession()
>>> local_session.client.connect()
True
>>> xal.LocalSession() # doctest: +ELLIPSIS
<xal.session.local.LocalSession object at 0x...>

* :class:`~xal.session.local.FabricSession` for use on remote SSH sessions.
Uses Fabric and Fabtools.
Expand All @@ -59,9 +55,8 @@ At the moment, `xal` provides two pre-configured session classes:

.. doctest::

>>> fabric_session = xal.FabricSession()
>>> fabric_session.client.connect(host='localhost')
True
>>> xal.FabricSession(host='localhost')
<xal.session.fabric.FabricSession object at 0x...>


.. warning::
Expand All @@ -76,7 +71,8 @@ Client
******

Client is a special provider for `xal` sessions. Its primary purpose it to
encapsulate connection with the system, hence the ``session.client.connect()``.
encapsulate connection with the system. At initialization, session do an
implicit call to the client's ``connect()`` method.

What's make "client" interface special is that the signature of ``connect()``
method is specific to providers.
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Expand Up @@ -15,8 +15,7 @@ def session(request):
if request.param == 'local':
xal_session = xal.LocalSession()
elif request.param == 'fabric':
xal_session = xal.FabricSession()
xal_session.client.connect('localhost')
xal_session = xal.FabricSession(host='localhost')
context = xal_session.path.cd(here)
context.__enter__()
return xal_session
5 changes: 3 additions & 2 deletions xal/session/__init__.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Base stuff for XAL sessions."""
from xal.registry import Registry


class Session(object):
Expand All @@ -10,14 +11,14 @@ class Session(object):
requests to the system.
"""
def __init__(self, registry=None):
def __init__(self):
"""Constructor."""
#: Mapping between identifiers and actual provider instances.
#:
#: The registry itself if a special kind of provider.
#: Every client should have at least one provider identified by
#: "registry".
self.registry = registry
self.registry = Registry()

# Attach session to registry for reverse relationship.
self.registry.xal_session = self
Expand Down
19 changes: 9 additions & 10 deletions xal/session/fabric.py
@@ -1,4 +1,8 @@
"""SSH XAL session using Fabric."""
from xal.client.fabric import FabricClient
from xal.path.fabric import FabricPathProvider
from xal.sh.fabric import FabricShProvider
from xal.sys.fabric import FabricSysProvider
from xal.session import Session


Expand All @@ -7,22 +11,17 @@ class FabricSession(Session):
#: FabricSession targets remote machines.
is_local = False

def __init__(self, **kwargs):
def __init__(self, *args, **kwargs):
"""Fabric session factory."""
# Initialize registry.
from xal.registry import Registry
registry = kwargs.setdefault('registry', Registry())
super(FabricSession, self).__init__(registry)
super(FabricSession, self).__init__()

# Let's import providers then register them to interfaces.
from xal.client.fabric import FabricClient
from xal.path.fabric import FabricPathProvider
from xal.sh.fabric import FabricShProvider
from xal.sys.fabric import FabricSysProvider

self.registry.register(
client=FabricClient(),
path=FabricPathProvider(),
sh=FabricShProvider(),
sys=FabricSysProvider(),
)

# Connect client.
self.client.connect(*args, **kwargs)
21 changes: 10 additions & 11 deletions xal/session/local.py
@@ -1,30 +1,29 @@
"""Local XAL sessions."""
from xal.client.local import LocalClient
from xal.dir.local import LocalDirProvider
from xal.path.local import LocalPathProvider
from xal.session import Session
from xal.sh.local import LocalShProvider
from xal.sys.local import LocalSysProvider


class LocalSession(Session):
"""A session on local machine."""
#: LocalSession is related to local machine.
is_local = True

def __init__(self, **kwargs):
def __init__(self, *args, **kwargs):
"""Local session factory."""
# Initialize registry.
from xal.registry import Registry
registry = kwargs.setdefault('registry', Registry())
super(LocalSession, self).__init__(registry)
super(LocalSession, self).__init__()

# Let's import providers then register them to interfaces.
from xal.client.local import LocalClient
from xal.dir.local import LocalDirProvider
from xal.path.local import LocalPathProvider
from xal.sh.local import LocalShProvider
from xal.sys.local import LocalSysProvider

self.registry.register(
client=LocalClient(),
dir=LocalDirProvider(),
path=LocalPathProvider(),
sh=LocalShProvider(),
sys=LocalSysProvider(),
)

# Connect client.
self.client.connect(*args, **kwargs)

0 comments on commit 8245467

Please sign in to comment.