weboob-devel/weboob/capabilities/collection.py
Laurent Bachelier b6021d4732 Better path changing support
* Create a get_collection method similar to get_* methods for objects.
* Fix title initialization of a collection
* Remove the hack were both id and title were allowed when CDing.
  That hack only worked with the canalplus module, and failed with
  others like redmine (but they still showed in suggestions).
  Moreover, the canalplus module now has friendlier IDs
  so this is not really needed anymore.
* Allow backends to tell if a path is valid or not.
  For instance, it now allows to cd in a Redmine project with no issues
  in it. It also won't display "404" for invalid project IDs.
  By default, we still use the unreliable method of checking there is at
  least one result in iter_resources().
* Fix cd completion to work with unicode strings (all strings after an
  unicode string were ignored!)
* Do not suggest '..' when completing cd in the root

refs #774
2012-03-13 22:08:45 +01:00

103 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Copyright(C) 2010-2012 Nicolas Duhamel, Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# 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, CapBaseObject
__all__ = ['ICapCollection', 'Collection', 'CollectionNotFound']
class CollectionNotFound(Exception):
def __init__(self, split_path=None):
if split_path is not None:
msg = 'Collection not found: %s' % '/'.join(split_path)
else:
msg = 'Collection not found'
Exception.__init__(self, msg)
class Collection(CapBaseObject):
"""
A Collection is a "fake" object returned in results, which shows you can get
more results if you go into its path.
It is a dumb object, it must not contain callbacks to a backend.
"""
def __init__(self, split_path, title=None, backend=None):
self.split_path = split_path
self.title = title
_id = split_path[-1] if len(split_path) else None
CapBaseObject.__init__(self, _id, backend)
def __unicode__(self):
if self.title and self.id:
return u'%s (%s)' % (self.id, self.title)
elif self.id:
return u'%s' % self.id
else:
return u'Unknown collection'
class ICapCollection(IBaseCap):
def iter_resources_flat(self, objs, split_path, clean_only=False):
"""
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.
"""
for resource in self.iter_resources(objs, split_path):
if isinstance(resource, Collection):
if not clean_only:
for res in self.iter_resources_flat(objs, resource.split_path):
yield res
else:
yield resource
def iter_resources(self, objs, split_path):
"""
split_path is a list, either empty (root path) or with one or many
components.
"""
raise NotImplementedError()
def get_collection(self, objs, split_path):
"""
Get a collection for a given split path.
If the path is invalid (i.e. can't be handled by this module),
it should return None.
"""
collection = Collection(split_path, None, self.name)
if self._is_collection_valid(objs, collection.split_path):
return collection
def _is_collection_valid(self, objs, split_path):
"""
Tests if a collection is valid.
For compatibility reasons, and to provide a default way, it checks if
the collection has at least one object in it. However, it is not very
efficient or exact, and you are encouraged to override this method.
"""
# Root
if len(split_path) == 0:
return True
try:
i = self.iter_resources(objs, split_path)
i.next()
return True
except (StopIteration, CollectionNotFound):
return False