From b86459a1114684f4e65a5639d46975670304f65b Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 25 Jan 2014 17:02:10 +0100 Subject: apt/package.py: Pass an apt_pkg.Dependency to BaseDependency Instead of passing the properties to __init__(), let's just pass the apt_pkg.Dependency. Store that in a _dep attribute, and provide properties to provide the API. The other classes do it this way as well. Reported-by: Michael Schaller --- apt/package.py | 60 ++++++++++++++++++++++++++------------ doc/source/library/apt.package.rst | 23 ++------------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/apt/package.py b/apt/package.py index d1a72744..d75a5185 100644 --- a/apt/package.py +++ b/apt/package.py @@ -61,15 +61,7 @@ class FetchError(Exception): class BaseDependency(object): - """A single dependency. - - Attributes defined here: - name - The name of the dependency - relation - The relation (<,<=,=,!=,>=,>,) - version - The version depended on - rawtype - The type of the dependendy (e.g. 'Recommends') - pre_depend - Boolean value whether this is a pre-dependency. - """ + """A single dependency.""" class __dstr(str): """Compare helper for compatibility with old third-party code. @@ -95,12 +87,45 @@ class BaseDependency(object): def __ne__(self, other): return not self.__eq__(other) - def __init__(self, name, rel, ver, pre, rawtype=None): - self.name = name - self.relation = self.__dstr(rel) - self.version = ver - self.pre_depend = pre - self.rawtype = rawtype + def __init__(self, dep): + self._dep = dep # apt_pkg.Dependency + + @property + def name(self): + """The name of the target package.""" + return self._dep.target_pkg.name + + @property + def relation(self): + """The relation (<, <=, !=, =, >=, >, ). + + Note that the empty string is a valid string as well, if no version + is specified. + """ + return self.__dstr(self._dep.comp_type) + + @property + def version(self): + """The target version or None. + + It is None if and only if relation is the empty string.""" + return self._dep.target_ver + + @property + def rawtype(self): + """Type of the dependency. + + This should be one of 'Breaks', 'Conflicts', 'Depends', 'Enhances', + 'PreDepends', 'Recommends', 'Replaces', 'Suggests'. + + Additional types might be added in the future. + """ + return self._dep.dep_type_untranslated + + @property + def pre_depend(self): + """Whether this is a PreDepends.""" + return self._dep.dep_type_untranslated == 'PreDepends' def __repr__(self): return ('' @@ -433,10 +458,7 @@ class Version(object): for dep_ver_list in depends[type_]: base_deps = [] for dep_or in dep_ver_list: - base_deps.append(BaseDependency(dep_or.target_pkg.name, - dep_or.comp_type, dep_or.target_ver, - (type_ == "PreDepends"), - rawtype=type_)) + base_deps.append(BaseDependency(dep_or)) depends_list.append(Dependency(base_deps)) except KeyError: pass diff --git a/doc/source/library/apt.package.rst b/doc/source/library/apt.package.rst index 825bf3df..1a69c1e2 100644 --- a/doc/source/library/apt.package.rst +++ b/doc/source/library/apt.package.rst @@ -23,27 +23,8 @@ The Version class Dependency Information ---------------------- -.. class:: BaseDependency - - The :class:`BaseDependency` class defines various attributes for accessing - the parts of a dependency. The attributes are as follows: - - .. attribute:: name - - The name of the dependency - - .. attribute:: relation - - The relation (<,<=,=,!=,>=,>,), as string. Note that the empty string - is a valid string as well, if no version is specified. - - .. attribute:: version - - The version or None. - - .. attribute:: pre_depend - - Boolean value whether this is a pre-dependency. +.. autoclass:: BaseDependency + :members: .. class:: Dependency -- cgit v1.2.3