diff options
| author | Sebastian Thiel <byronimo@gmail.com> | 2011-04-07 20:15:51 +0200 |
|---|---|---|
| committer | Sebastian Thiel <byronimo@gmail.com> | 2011-04-07 20:15:51 +0200 |
| commit | dba71a0c727aba19319d3e868d0ca4b8009bcef4 (patch) | |
| tree | e735881064b37b28574192904865d519f6690bd0 /gitdb/util.py | |
| parent | 7c4d3d6b000930134019515c83c10b140330d313 (diff) | |
| download | gitdb-dba71a0c727aba19319d3e868d0ca4b8009bcef4.tar.gz | |
Initial version of objects. The submodule implementation is left in git-python for now as it requires plenty of additional features which are currently only available via the git command
Diffstat (limited to 'gitdb/util.py')
| -rw-r--r-- | gitdb/util.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/gitdb/util.py b/gitdb/util.py index 432eec4..650dc57 100644 --- a/gitdb/util.py +++ b/gitdb/util.py @@ -110,6 +110,21 @@ class _RandomAccessStringIO(object): #{ Routines +def stream_copy(source, destination, chunk_size=512*1024): + """Copy all data from the source stream into the destination stream in chunks + of size chunk_size + + :return: amount of bytes written""" + br = 0 + while True: + chunk = source.read(chunk_size) + destination.write(chunk) + br += len(chunk) + if len(chunk) < chunk_size: + break + # END reading output stream + return br + def make_sha(source=''): """A python2.4 workaround for the sha/hashlib module fiasco :note: From the dulwich project """ @@ -650,5 +665,51 @@ class Iterable(object): :return: iterator yielding Items""" raise NotImplementedError("To be implemented by Subclass") + +class IterableList(list): + """ + List of iterable objects allowing to query an object by id or by named index:: + + heads = repo.heads + heads.master + heads['master'] + heads[0] + + It requires an id_attribute name to be set which will be queried from its + contained items to have a means for comparison. + + A prefix can be specified which is to be used in case the id returned by the + items always contains a prefix that does not matter to the user, so it + can be left out.""" + __slots__ = ('_id_attr', '_prefix') + + def __new__(cls, id_attr, prefix=''): + return super(IterableList,cls).__new__(cls) + + def __init__(self, id_attr, prefix=''): + self._id_attr = id_attr + self._prefix = prefix + if not isinstance(id_attr, basestring): + raise ValueError("First parameter must be a string identifying the name-property. Extend the list after initialization") + # END help debugging ! + + def __getattr__(self, attr): + attr = self._prefix + attr + for item in self: + if getattr(item, self._id_attr) == attr: + return item + # END for each item + return list.__getattribute__(self, attr) + + def __getitem__(self, index): + if isinstance(index, int): + return list.__getitem__(self,index) + try: + return getattr(self, index) + except AttributeError: + raise IndexError( "No item found with id %r" % (self._prefix + index) ) + + + #} END utilities |
