1 |
commit: f478491cb7bada1a009c63b02f40103149fc0abd |
2 |
Author: André Erdmann <dywi <AT> mailerd <DOT> de> |
3 |
AuthorDate: Tue Jul 30 16:01:52 2013 +0000 |
4 |
Commit: André Erdmann <dywi <AT> mailerd <DOT> de> |
5 |
CommitDate: Tue Jul 30 16:01:52 2013 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=f478491c |
7 |
|
8 |
roverlay/util: objects.py |
9 |
|
10 |
--- |
11 |
roverlay/util/objects.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ |
12 |
1 file changed, 69 insertions(+) |
13 |
|
14 |
diff --git a/roverlay/util/objects.py b/roverlay/util/objects.py |
15 |
new file mode 100644 |
16 |
index 0000000..dd19046 |
17 |
--- /dev/null |
18 |
+++ b/roverlay/util/objects.py |
19 |
@@ -0,0 +1,69 @@ |
20 |
+# R overlay -- |
21 |
+# -*- coding: utf-8 -*- |
22 |
+# Copyright (C) 2013 André Erdmann <dywi@×××××××.de> |
23 |
+# Distributed under the terms of the GNU General Public License; |
24 |
+# either version 2 of the License, or (at your option) any later version. |
25 |
+ |
26 |
+class MethodNotImplementedError ( NotImplementedError ): |
27 |
+ def __init__ ( self, obj, method, msg=None ): |
28 |
+ if isinstance ( obj, str ): |
29 |
+ obj_name = obj |
30 |
+ elif hasattr ( obj, '__class__' ): |
31 |
+ obj_name = obj.__class__.__name__ |
32 |
+ elif hasattr ( obj, '__name__' ): |
33 |
+ obj_name = obj.__name__ |
34 |
+ else: |
35 |
+ obj_name = repr ( obj ) |
36 |
+ |
37 |
+ if isinstance ( method, str ): |
38 |
+ method_name = method |
39 |
+ elif hasattr ( method, '__name__' ): |
40 |
+ method_name = method.__name__ |
41 |
+ else: |
42 |
+ method_name = repr ( method ) |
43 |
+ |
44 |
+ method_str = "{}.{}()".format ( obj_name, method_name ) |
45 |
+ |
46 |
+ if msg: |
47 |
+ super ( MethodNotImplementedError, self ).__init__ ( |
48 |
+ method_str + ': ' + str ( msg ) |
49 |
+ ) |
50 |
+ else: |
51 |
+ super ( MethodNotImplementedError, self ).__init__ ( method_str ) |
52 |
+ # --- end of __init__ (...) --- |
53 |
+ |
54 |
+# --- end of MethodNotImplementedError --- |
55 |
+ |
56 |
+class MethodNotImplemented ( MethodNotImplementedError ): |
57 |
+ # compat class |
58 |
+ pass |
59 |
+# --- end of MethodNotImplemented --- |
60 |
+ |
61 |
+class AbstractMethodError ( MethodNotImplementedError ): |
62 |
+ def __init__ ( self, obj, method ): |
63 |
+ super ( AbstractMethodError, self ).__init__ ( |
64 |
+ obj, method, "has to be implemented by derived classes" |
65 |
+ ) |
66 |
+ |
67 |
+# --- end of AbstractMethodError --- |
68 |
+ |
69 |
+def _get_exception_wrapper ( err_cls, func ): |
70 |
+ def wrapped ( obj, *args, **kwargs ): |
71 |
+ raise err_cls ( obj, func ) |
72 |
+ # --- end of wrapped (...) --- |
73 |
+ |
74 |
+ if func is not None: |
75 |
+ wrapped.__name__ = func.__name__ |
76 |
+ wrapped.__doc__ = func.__doc__ |
77 |
+ wrapped.__dict__.update ( func.__dict__ ) |
78 |
+ return wrapped |
79 |
+ |
80 |
+# --- end of _get_exception_wrapper (...) --- |
81 |
+ |
82 |
+def abstractmethod ( func=None ): |
83 |
+ return _get_exception_wrapper ( AbstractMethodError, func ) |
84 |
+# --- end of abstractmethod (...) --- |
85 |
+ |
86 |
+def not_implemented ( func=None ): |
87 |
+ return _get_exception_wrapper ( MethodNotImplementedError, func ) |
88 |
+# --- end of not_implemented (...) --- |