summaryrefslogtreecommitdiffstats
path: root/utils.py
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2013-11-18 11:14:43 +0100
committerJan Pokorný <jpokorny@redhat.com>2013-11-18 11:14:43 +0100
commitb182bb2e0a9266b313112d507e9803e5ef2de387 (patch)
treef348cb4d1b254d2e7fa8cc61a1f82d3b4a2e9e99 /utils.py
downloadclufter-b182bb2e0a9266b313112d507e9803e5ef2de387.tar.gz
clufter-b182bb2e0a9266b313112d507e9803e5ef2de387.tar.xz
clufter-b182bb2e0a9266b313112d507e9803e5ef2de387.zip
Initial commit
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/utils.py b/utils.py
new file mode 100644
index 0000000..57f4d89
--- /dev/null
+++ b/utils.py
@@ -0,0 +1,65 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2012 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2 (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+
+import os
+
+
+# name the exitcodes
+ecodes = 'SUCCESS', 'FAILURE'
+EC = type('EC', (), {n: v for v, n in enumerate('EXIT_' + i for i in ecodes)})
+
+
+head_tail = lambda x=None, *y: (x, x if x is None else y)
+filtervars = lambda src,which: {x: src[x] for x in which if x in src}
+filtervarsdef = lambda src,which: {x: src[x] for x in which if src.get(x, None)}
+filtervarspop = lambda src,which: {x: src.pop(x) for x in which if x in src}
+
+
+def which(name, *where):
+ where = tuple(os.path.abspath(i) for i in where)
+ if 'PATH' in os.environ:
+ path = tuple(i for i in os.environ['PATH'].split(os.pathsep)
+ if len(i.strip()))
+ else:
+ path = ()
+ for p in where + path:
+ check = os.path.join(p, name)
+ if os.path.exists(check):
+ return check
+ else:
+ return None
+
+
+class ClufterError(Exception):
+ def __init__(self, ctx_self, msg, *args):
+ self.ctx_self = ctx_self
+ self.msg = msg
+
+ def __str__(self):
+ ret = getattr(self.ctx_self, '__name__',
+ self.ctx_self.__class__.__name__)
+ return ret + ': ' + self.msg.format(*self.args)
+
+
+class ClufterPlainError(ClufterError):
+ def __init__(self, msg, *args):
+ super(ClufterPlainError, self).__init__(self, None, msg, *args)
+
+
+# Inspired by http://stackoverflow.com/a/1383402
+class classproperty(property):
+ def __init__(self, fnc):
+ property.__init__(self, classmethod(fnc))
+
+ def __get__(self, this, owner):
+ return self.fget.__get__(None, owner)()
+
+
+class hybridproperty(property):
+ def __init__(self, fnc):
+ property.__init__(self, classmethod(fnc))
+
+ def __get__(self, this, owner):
+ return self.fget.__get__(None, this if this else owner)()