From e5177b9150290004f472d08c13dfe78075f029e8 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 8 Dec 2009 03:51:27 -0500 Subject: [PATCH] Extend libbe.util.id to handle id (path) creation. --- libbe/util/{beuuid.py => id.py} | 49 +++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) rename libbe/util/{beuuid.py => id.py} (67%) diff --git a/libbe/util/beuuid.py b/libbe/util/id.py similarity index 67% rename from libbe/util/beuuid.py rename to libbe/util/id.py index a3a3b6c..0f1576c 100644 --- a/libbe/util/beuuid.py +++ b/libbe/util/id.py @@ -16,15 +16,14 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ -Backwards compatibility support for Python 2.4. Once people give up -on 2.4 ;), the uuid call should be merged into bugdir.py +Handle ID creation and parsing. """ import libbe + if libbe.TESTING == True: import unittest - try: from uuid import uuid4 # Python >= 2.5 def uuid_gen(): @@ -58,6 +57,50 @@ except ImportError: raise Exception, strerror return output.rstrip('\n') + +def _assemble(*args): + for i,arg in enumerate(args): + if arg == None: + args[i] = '' + return '/'.join(args) + +def _split(id): + args = id.split('/') + for i,arg in enumerate(args): + if arg == '': + args[i] = None + return args + + +def bugdir_id(bugdir, *args): + return _assemble(bugdir.uuid, args) + +def bug_id(bug, *args): + if bug.bug == None: + bugdir_id = None + else: + bugdir_id = bugdir_id(bug.bugdir) + return _assemble(bugdir_id, bug.uuid, args) + +def comment_id(comment, *args): + if comment.bug == None: + bug_id = None + else: + bug_id = bug_id(comment.bug) + return _assemble(bug_id, comment.uuid, args) + +def parse_id(id): + args = _split(id) + ret = {'bugdir':args.pop(0)} + type = 'bugdir' + for child_name in ['bug', 'comment']: + if len(args) > 0 and is_a_uuid(args[0]): + ret[child_name] = args.pop(0) + type = child_name + ret['type'] = type + ret['remaining'] = os.path.join(args) + return ret + if libbe.TESTING == True: class UUIDtestCase(unittest.TestCase): def testUUID_gen(self): -- 2.26.2