* [gentoo-commits] proj/gentoopm:master commit in: gentoopm/, gentoopm/paludispm/, gentoopm/basepm/
@ 2011-07-10 8:03 Michał Górny
0 siblings, 0 replies; only message in thread
From: Michał Górny @ 2011-07-10 8:03 UTC (permalink / raw
To: gentoo-commits
commit: b2514c4e3a7ef40ec636eb04307247356bf3d87b
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sun Jul 10 07:54:39 2011 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Sun Jul 10 07:54:39 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/gentoopm.git;a=commit;h=b2514c4e
Start introducing custom exceptions.
---
gentoopm/basepm/pkg.py | 17 +++++++++--------
gentoopm/exceptions.py | 23 +++++++++++++++++++++++
gentoopm/paludispm/pkg.py | 5 +++--
3 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/gentoopm/basepm/pkg.py b/gentoopm/basepm/pkg.py
index 5efa45e..b1f8e7d 100644
--- a/gentoopm/basepm/pkg.py
+++ b/gentoopm/basepm/pkg.py
@@ -6,6 +6,7 @@
from abc import abstractmethod, abstractproperty
from gentoopm.basepm.atom import PMAtom
+from gentoopm.exceptions import EmptyPackageSetError, AmbiguousPackageSetError
from gentoopm.util import ABCObject
class PMPackageSet(ABCObject):
@@ -47,11 +48,11 @@ class PMPackageSet(ABCObject):
try:
best = l[0]
except IndexError:
- raise TypeError('.best called on an empty set')
+ raise EmptyPackageSetError('.best called on an empty set')
for p in l:
if p.key != best.key:
- raise KeyError('.best called on a set of differently-named packages')
+ raise AmbiguousPackageSetError('.best called on a set of differently-named packages')
return best
def select(self, *args, **kwargs):
@@ -62,10 +63,10 @@ class PMPackageSet(ABCObject):
"""
try:
return self.filter(*args, **kwargs).best
- except TypeError:
- raise KeyError('No packages match the filters.')
- except KeyError:
- raise ValueError('Ambiguous filter (matches more than a single package name).')
+ except EmptyPackageSetError:
+ raise EmptyPackageSetError('No packages match the filters.')
+ except AmbiguousPackageSetError:
+ raise AmbiguousPackageSetError('Ambiguous filter (matches more than a single package name).')
def __getitem__(self, filt):
"""
@@ -82,13 +83,13 @@ class PMPackageSet(ABCObject):
try:
ret = next(it)
except StopIteration:
- raise KeyError('No packages match the filter.')
+ raise EmptyPackageSetError('No packages match the filter.')
try:
next(it)
except StopIteration:
pass
else:
- raise ValueError('Filter matches more than one package.')
+ raise AmbiguousPackageSetError('Filter matches more than one package.')
return ret
diff --git a/gentoopm/exceptions.py b/gentoopm/exceptions.py
new file mode 100644
index 0000000..7aa237f
--- /dev/null
+++ b/gentoopm/exceptions.py
@@ -0,0 +1,23 @@
+#!/usr/bin/python
+# vim:fileencoding=utf-8
+# (c) 2011 Michał Górny <mgorny@gentoo.org>
+# Released under the terms of the 2-clause BSD license.
+
+class PMException(Exception):
+ """
+ A base class for exceptions.
+ """
+ pass
+
+class EmptyPackageSetError(PMException):
+ """
+ Empty set passed to a function requiring a non-empty one.
+ """
+ pass
+
+class AmbiguousPackageSetError(PMException):
+ """
+ Ambiguous package set passed, e.g. containing multiple package names when
+ consistenly-named set was expected.
+ """
+ pass
diff --git a/gentoopm/paludispm/pkg.py b/gentoopm/paludispm/pkg.py
index 5f40984..8907c15 100644
--- a/gentoopm/paludispm/pkg.py
+++ b/gentoopm/paludispm/pkg.py
@@ -8,6 +8,7 @@ import paludis
from gentoopm.basepm.metadata import PMPackageMetadata
from gentoopm.basepm.pkg import PMPackageSet, PMPackage, \
PMFilteredPackageSet
+from gentoopm.exceptions import EmptyPackageSetError, AmbiguousPackageSetError
class PaludisPackageSet(PMPackageSet):
_sorted = False
@@ -23,10 +24,10 @@ class PaludisPackageSet(PMPackageSet):
try:
f = next(it)
except StopIteration:
- raise TypeError('.best called on an empty set')
+ raise EmptyPackageSetError('.best called on an empty set')
for p in it:
if p.key != f.key:
- raise KeyError('.best called on a set of differently-named packages')
+ raise AmbiguousPackageSetError('.best called on a set of differently-named packages')
try:
return p
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2011-07-10 8:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-10 8:03 [gentoo-commits] proj/gentoopm:master commit in: gentoopm/, gentoopm/paludispm/, gentoopm/basepm/ Michał Górny
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox