Skip to content

Commit

Permalink
Refs #10 - Removed session.fs in favor of session.path. Moved session…
Browse files Browse the repository at this point in the history
….fs.path one level up.
  • Loading branch information
benoitbryon committed Jul 20, 2015
1 parent e6ff3bb commit 14939bb
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 403 deletions.
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -33,7 +33,7 @@ True

In this session, we can manage files:

>>> path = local_session.fs.path('hello-xal.txt')
>>> path = local_session.path('hello-xal.txt')
>>> path.exists()
False
>>> written = path.open('w').write(u'Hello world!')
Expand All @@ -56,7 +56,7 @@ Now let's make a function that does the same. It takes the session as input
argument:

>>> def hello(session):
... path = session.fs.path('hello-xal.txt')
... path = session.path('hello-xal.txt')
... path.open('w').write(u"Hello world!")
... print path.open().read()
... path.unlink()
Expand Down
2 changes: 1 addition & 1 deletion docs/index.txt
Expand Up @@ -13,7 +13,7 @@ Contents
sessions
providers
resources/index
resources/fs
resources/path
resources/sh
about/index
presentations/index
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/index.txt
Expand Up @@ -10,7 +10,7 @@ There is a base class for all resources, then each resource has its own class.

Here are resources provided by `xal` itself at the moment:

* :doc:`/resources/fs`
* :doc:`/resources/path`
* :doc:`/resources/sh`

.. note::
Expand Down
72 changes: 34 additions & 38 deletions docs/resources/fs.txt → docs/resources/path.txt
Expand Up @@ -13,13 +13,13 @@ Paths, files and directories

import xal
session = xal.LocalSession()
here = session.fs.path.cwd()
here = session.path.cwd()
assert here.name == 'docs' # Don't mess up unsupported environments!
session.fs.path.cd(here.parent) # Move to repository root.
session.path.cd(here.parent) # Move to repository root.


*********************
Use fs.path interface
Use path interface
*********************

Let's consider a `xal` :doc:`session </sessions>`:
Expand All @@ -31,38 +31,34 @@ Let's consider a `xal` :doc:`session </sessions>`:
>>> session.client.connect()
True

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

.. doctest::

>>> session.fs.path # doctest: +ELLIPSIS
<xal.fs.local.LocalPathProvider object at 0x...>

.. note::

``fs.path`` may be renamed to ``path`` in future releases.
>>> session.path # doctest: +ELLIPSIS
<xal.path.local.LocalPathProvider object at 0x...>


****************
PathProvider API
****************

Here are details about `xal`'s ``fs.path`` interface.
Here are details about `xal`'s ``path`` interface.

Path resource factory
=====================

The ``fs.path`` interface can be used as a factory to create
:class:`~xal.fs.resource.Path` resources:
The ``path`` interface can be used as a factory to create
:class:`~xal.path.resource.Path` resources:

.. doctest::

>>> path = session.fs.path('tests/fixtures')
>>> path = session.path('tests/fixtures')
>>> path
Path('tests/fixtures')

See section about `path objects <#path-objects>`_ below for details about
:class:`~xal.fs.resource.Path` instances.
:class:`~xal.path.resource.Path` instances.

cd(path)
========
Expand All @@ -73,24 +69,24 @@ It can be used as a context manager:

.. doctest::

>>> former_path = session.fs.path.cwd()
>>> with session.fs.path.cd('tests') as new_path:
... session.fs.path.cwd() == new_path
>>> former_path = session.path.cwd()
>>> with session.path.cd('tests') as new_path:
... session.path.cwd() == new_path
True
>>> session.fs.path.cwd() == former_path
>>> session.path.cwd() == former_path
True

Or standalone:

.. doctest::

>>> former_path = session.fs.path.cwd()
>>> session.fs.path.cd('tests') # doctest: +ELLIPSIS
>>> former_path = session.path.cwd()
>>> session.path.cd('tests') # doctest: +ELLIPSIS
Path('/.../tests')

Accepts text or path objects:

>>> session.fs.path.cd(former_path) # doctest: +ELLIPSIS
>>> session.path.cd(former_path) # doctest: +ELLIPSIS
Path('/...')

.. note::
Expand All @@ -107,7 +103,7 @@ In local session, it is obviously the same value as :attr:`os.path.sep`:
.. doctest::

>>> import os
>>> session.fs.path.sep == os.path.sep
>>> session.path.sep == os.path.sep
True

pure_path(path)
Expand All @@ -119,22 +115,22 @@ situations where you need to compare paths on `pure path

.. doctest::

>>> session.fs.path('foo').xal_session is session
>>> session.path('foo').xal_session is session
True
>>> session.fs.path.pure_path('foo').xal_session is None
>>> session.path.pure_path('foo').xal_session is None
True


************
Path objects
************

The ``fs.path`` interface can be used as a factory to create
:class:`~xal.fs.resource.Path` resources:
The ``path`` interface can be used as a factory to create
:class:`~xal.path.resource.Path` resources:

.. doctest::

>>> path = session.fs.path('tests/fixtures')
>>> path = session.path('tests/fixtures')
>>> path
Path('tests/fixtures')
>>> print path
Expand All @@ -145,7 +141,7 @@ path objects themselves:

.. doctest::

>>> session.fs.path(session.fs.path('foo'))
>>> session.path(session.path('foo'))
Path('foo')

Pure paths VS concrete paths
Expand All @@ -158,15 +154,15 @@ paths":

.. doctest::

>>> path = session.fs.path.pure_path('foo')
>>> path = session.path.pure_path('foo')
>>> path.xal_session is None
True

* "concrete paths" are the ones attached to a session.

.. doctest::

>>> path = session.fs.path('foo')
>>> path = session.path('foo')
>>> path.xal_session is session
True

Expand All @@ -178,19 +174,19 @@ concrete paths:

.. doctest::

>>> session.fs.path.pure_path('foo') == session.fs.path.pure_path('foo')
>>> session.path.pure_path('foo') == session.path.pure_path('foo')
True
>>> session.fs.path.pure_path('foo') == session.fs.path('foo')
>>> session.path.pure_path('foo') == session.path('foo')
True

Concrete paths are compared with respect to session:

.. doctest::

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

Concatenation
Expand All @@ -200,7 +196,7 @@ Use slash (division) operator ``/`` to concatenate paths:

.. doctest::

>>> session.fs.path('foo') / session.fs.path('bar')
>>> session.path('foo') / session.path('bar')
Path('foo/bar')

Properties
Expand All @@ -210,7 +206,7 @@ Properties

.. doctest::

>>> path = session.fs.path('/home/user/hello.txt.cpold')
>>> path = session.path('/home/user/hello.txt.cpold')
>>> path.drive
''
>>> path.root
Expand Down Expand Up @@ -288,7 +284,7 @@ cwd()

:mod:`pathlib` implements ``cwd()`` as a class-level method of ``Path``. In
`xal`, it is a method of the instance of provider API, generally
``session.fs.cwd()``.
``session.path.cwd()``.

resolve() works with non-existent files
---------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions docs/sessions.txt
Expand Up @@ -101,7 +101,7 @@ instance acts as a router to adequate item in registry.
>>> session.registry # doctest: +ELLIPSIS
<xal.registry.Registry object at 0x...>
>>> sorted(session.registry.items.keys())
['client', 'dir', 'fs', 'sh', 'sys']
['client', 'dir', 'path', 'sh', 'sys']
>>> session.registry('sh') # doctest: +ELLIPSIS
<xal.sh.local.LocalShProvider object at 0x...>
>>> session.sh is session.registry('sh')
Expand All @@ -125,17 +125,17 @@ But, if you are curious, here are some hints:
As an example, Fabric-based implementation of path API uses session's sh
API via ``self.xal_session.sh``:

.. literalinclude:: /../xal/fs/fabric.py
.. literalinclude:: /../xal/path/fabric.py
:language: python
:pyobject: FabricFileSystemProvider.cwd
:pyobject: FabricPathProvider.cwd

* when providers instanciate a ressource, they assign a reference to the
session as ``xal_session`` attribute. It makes it possible to have generic
resource classes that take advantage of providers' specific implementation.

As an example, Path resource is generic, but most methods just redirect to
``self.xal_session.fs.path`` with ``self`` as first argument:
``self.xal_session.path`` with ``self`` as first argument:

.. literalinclude:: /../xal/fs/resource.py
.. literalinclude:: /../xal/path/resource.py
:language: python
:pyobject: Path.chmod
2 changes: 1 addition & 1 deletion tests/conftest.py
Expand Up @@ -17,6 +17,6 @@ def session(request):
elif request.param == 'fabric':
xal_session = xal.FabricSession()
xal_session.client.connect('localhost')
context = xal_session.fs.path.cd(here)
context = xal_session.path.cd(here)
context.__enter__()
return xal_session

0 comments on commit 14939bb

Please sign in to comment.