Make Collection more safe and sane

* Remove callbacks in Collection object
  Make Collection a "dumb" object (and also a base object,
  though it isn't very useful for now)
* Rename Path to WorkingPath, because it is more about managing state
  than being a single path.
* Rewrite almost all WorkingPath, because the code was overly
  complicated for no reason (I tried some special cases and it turned
  out that fromstring didn't handle them, and that the
  quote-escape-unquote was just unecessary). I also rewrote it to be
  more pythonic (no more lambdas and maps) and added tests.
* Require the full split path when creating a Collection. Because, come to
  think of it, an object needs an unique identifier; in the case of
  Collections, it is the full path, not only its last part.
  I might even replace the id by the full split path in the future.
* There is now only one way to get items of a Collection: calling
  iter_resources().
* Rewrite flatten_resources to iter_resources_flat(), which just calls
  iter_resources() recursively.
* Rewrite the collection part of the canalplus module. There is no more
  callback or a page calling the browser to check another page!
  The logic is only in iter_resources().
  The resulting code is not very pretty, but it should get better.
  As a bonus, avoid to reload the main XML file when we already have it
  open.
* change_path() now expects a split path and not a string.
* up/home special cases for "cd" are handled in the same place, and
  store the previous place properly (but are not yet exploitable by
  an user command).

This is a big commit but it would be hard to split it in *working*
commits.

If you read this entire commit message, I will buy you a beer.

refs #774
fixes #773
This commit is contained in:
Laurent Bachelier 2012-03-08 00:33:49 +01:00
commit b4b7182960
13 changed files with 147 additions and 148 deletions

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .base import IBaseCap
from .base import IBaseCap, CapBaseObject
__all__ = ['ICapCollection', 'Collection', 'CollectionNotFound']
@ -31,42 +31,18 @@ class CollectionNotFound(Exception):
Exception.__init__(self, msg)
class Children(object):
class Collection(CapBaseObject):
"""
Dynamic property of a Collection.
Returns a list, either by calling a function or because
it already has the list.
"""
def __get__(self, obj, type=None):
if obj._children is None:
if callable(obj._fct):
obj._children = obj._fct(obj.id)
return obj._children or []
A Collection is a "fake" object returned in results, which shows you can get
more results if you go into its path.
class Collection(object):
It is a dumb object, it must not contain callbacks to a backend.
"""
Collection of objects.
Should provide a way to be filled, either by providing the children
right away, or a function. The function will be called once with the id
as an argument if there were no children provided, but only on demand.
It can be found in a list of objects, it indicantes a "folder"
you can hop into.
id and title should be unicode.
"""
children = Children()
backend = None
def __init__(self, _id=None, title=None, children=None, fct=None):
self.id = _id
def __init__(self, split_path, backend=None, title=None):
self.split_path = split_path
self.title = title
# It does not make sense to have both at init
assert not (fct is not None and children is not None)
self._children = children
self._fct = fct
def __iter__(self):
return iter(self.children)
_id = split_path[-1] if len(split_path) else None
CapBaseObject.__init__(self, _id, backend)
def __unicode__(self):
if self.title and self.id:
@ -78,19 +54,19 @@ class Collection(object):
class ICapCollection(IBaseCap):
def _flatten_resources(self, resources, clean_only=False):
def iter_resources_flat(self, objs, split_path, clean_only=False):
"""
Expand all collections in a list
If clean_only is True, do not expand collections, only remove them.
Call iter_resources() to fetch all resources in the tree.
If clean_only is True, do not explore paths, only remove them.
split_path is used to set the starting path.
"""
lst = list()
for resource in resources:
if isinstance(resource, (list, Collection)):
for resource in self.iter_resources(objs, split_path):
if isinstance(resource, Collection):
if not clean_only:
lst.extend(self._flatten_resources(resource))
for res in self.iter_resources_flat(objs, resource.split_path):
yield res
else:
lst.append(resource)
return lst
yield resource
def iter_resources(self, objs, split_path):
"""