Home | Trees | Indices | Help |
|
---|
|
1 #!/usr/bin/python2.4 2 # 3 # Copyright 2008 Google Inc. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 """ 18 Encodes the two classes storing data about keys: 19 - KeyMetadata: stores metadata 20 - KeyVersion: stores key strings and types 21 22 @author: arkajit.dey@gmail.com (Arkajit Dey) 23 """ 24 25 import simplejson 26 27 import errors 28 import keyinfo31 """Encodes metadata for a keyset with a name, purpose, type, and versions.""" 3211834 self.name = name 35 self.purpose = purpose 36 self.type = type 37 self.encrypted = encrypted 38 self.__versions = {} # dictionary from version nums to KeyVersions39 40 versions = property(lambda self: self.__versions.values()) 4143 return simplejson.dumps({"name": self.name, 44 "purpose": str(self.purpose), 45 "type": str(self.type), 46 "encrypted": self.encrypted, 47 "versions": [simplejson.loads(str(v)) 48 for v in self.versions]})4951 """ 52 Adds given version and returns True if successful. 53 54 @param version: version to add 55 @type version: L{KeyVersion} 56 57 @return: True if version was successfully added (i.e. no previous version 58 had the same version number), False otherwise. 59 @rtype: boolean 60 """ 61 num = version.version_number 62 if num not in self.__versions: 63 self.__versions[num] = version 64 return True 65 return False6668 """ 69 Removes version with given version number and returns it if it exists. 70 71 @param version_number: version number to remove 72 @type version_number: integer 73 74 @return: the removed version if it exists 75 @rtype: L{KeyVersion} 76 77 @raise KeyczarError: if the version number is non-existent 78 """ 79 try: 80 self.__versions.pop(version_number) 81 except KeyError: 82 raise errors.KeyczarError("No such version number: %d" % version_number)8385 """ 86 Return the version corresponding to the given version number. 87 88 @param version_number: integer version number of desired version 89 @type version_number: integer 90 91 @return: the corresponding version if it exists 92 @rtype: L{KeyVersion} 93 94 @raise KeyczarError: if the version number is non-existent. 95 """ 96 try: 97 return self.__versions[version_number] 98 except KeyError: 99 raise errors.KeyczarError("No such version number: %d" % version_number)100 101 @staticmethod103 """ 104 Return KeyMetadata object constructed from JSON string representation. 105 106 @param json_string: a JSON representation of a KeyMetadata object 107 @type json_string: string 108 109 @return: the constructed KeyMetadata object 110 @rtype: L{KeyMetadata} 111 """ 112 meta = simplejson.loads(json_string) 113 kmd = KeyMetadata(meta['name'], keyinfo.GetPurpose(meta['purpose']), 114 keyinfo.GetType(meta['type']), meta['encrypted']) 115 for version in meta['versions']: 116 kmd.AddVersion(KeyVersion.Read(version)) 117 return kmd124 128 129 status = property(lambda self: self.__status, __SetStatus) 130150132 return simplejson.dumps({"versionNumber": self.version_number, 133 "status": str(self.status), 134 "exportable": self.exportable})135 136 @staticmethod138 """ 139 Return KeyVersion object constructed from dictionary derived from JSON. 140 141 @param version: a dictionary obtained from a JSON string representation 142 @type version: dictionary 143 144 @return: constructed KeyVersion object 145 @rtype: L{KeyVersion} 146 """ 147 return KeyVersion(version['versionNumber'], 148 keyinfo.GetStatus(version['status']), 149 version['exportable'])
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sun Aug 10 17:05:06 2008 | http://epydoc.sourceforge.net |