def save_new(self, arg, profile): 'Prepare to write a new profile.' # Create a pathname. name = ''.join(random.sample(self.STRING, len(self.STRING))) pathname = os.path.join(self.savepath, name) while os.path.exists(pathname): name = ''.join(random.sample(self.STRING, len(self.STRING))) pathname = os.path.join(self.savepath, name) # Create destination and keys. destination = open(pathname, 'wb') random.seed(int(os.path.getctime(pathname))) major = spice.major() minor = spice.minor() random.seed() # Restore randomness. # Create a new profile entry. self.profiles[arg] = [profile, pathname, major, minor] self.aliases.append(arg) return destination, major, minor
def autoread(self, pathname): 'Read in profiles from their pathnames.' # Create the keys. random.seed(int(os.path.getctime(pathname))) major = spice.major() minor = spice.minor() random.seed() # Restore randomness. # Decode the file. string = cStringIO.StringIO() spice.decode(file(pathname, 'rb'), string, major, minor) string = string.getvalue() # Extract the data. namesize = ord(string[0]) + 2 name = string[1:namesize] profile = string[namesize:] # Archive the data. assert profile, '%r has no profile data.' % pathname self.profiles[name] = [profile, pathname, major, minor]
def export_(self, arg, key): 'Encode all profiles and export them.' try: destination = open(arg, 'wb') except: raise Exception('%r CANNOT BE CREATED' % arg) random.seed(key) major = spice.major() minor = spice.minor() random.seed() # Restore randomness. for name in self.aliases: profile = self.profiles[name][0] assert len(profile) <= 16777216, '%r IS TOO LARGE' % name len_name = chr(len(name) - 1) len_profile = self.str_(len(profile) - 1) source = cStringIO.StringIO(len_name + len_profile + name + profile) spice.encode(source, destination, major, minor) destination.close()
def import_(self, arg, key): 'Import all profiles and decode them.' # Decode the data being imported. try: source = open(arg, 'rb') except: raise Exception('%r CANNOT BE OPENED' % arg) random.seed(key) major = spice.major() minor = spice.minor() random.seed() # Restore randomness. destination = cStringIO.StringIO() spice.decode(source, destination, major, minor) source.close() destination.seek(0) # Import the decoded profiles. len_name = destination.read(1) while len_name: len_profile = destination.read(3) assert len(len_profile) == 3, '%r IS CORRUPT' % arg len_name = ord(len_name) + 1 name = destination.read(len_name) assert len(name) == len_name, '%r IS CORRUPT' % arg len_profile = self.int_(len_profile) + 1 profile = destination.read(len_profile) assert len(profile) == len_profile, '%r IS CORRUPT' % arg # Check for duplicate names. if name in self.aliases: name = name[:250] code = ''.join(random.sample(self.STRING, 3)) temp = '%s [%s]' % (name, code) while temp in self.aliases: code = ''.join(random.sample(self.STRING, 3)) temp = '%s [%s]' % (name, code) name = temp # Save the new profile to disk. self.save_act(name, profile, *self.save_new(name, profile)) len_name = destination.read(1)