def load( self , fname=None ): """ Loads an object from an encrypted file. This method will raise an IOError if the file format is invalid or the decryption failed. The loaded object will to saved to the interal 'dict' fname -- [Optional] Specifies what file to load. Defualts to the last registered file path ( if any, else an IOError will be raised ). """ global userPath; if fname: self._path = fname; elif self._path: fpath = self._path; else: raise IOError( "No path to load from!" ); fpath = os.path.join( userPath , self._path ); if not os.path.exists( fpath ): raise IOError( "File: '%s', does not exists!" % ( fpath ) ); with open( fpath , "rb" ) as f: fdata = f.read(); cipher , _ = newCipher( fdata[:AES.block_size] ); fdata = cipher.decrypt( fdata[AES.block_size:] ); if fdata[:9] != "@NETBASM\n": raise IOError( "Invalid decryption key for file '%s'!" % ( fpath ) ); fdata = fdata[9:]; # fdata is now an array of key/values self._internal = pyzerial.load( fdata ); self._unsaved = False;
def loadObject( this , fname ): """ Loads an encrypted user file. if the file format or the key is invalid 'None' will be returned. If the file is decryped corretly a new UserObject will be returned. fname -- The name of the file to load. """ global userPath; fpath = os.path.join( userPath , fname ); if not os.path.exists( fpath ): return None; with open( fpath , "rb" ) as f: fdata = f.read(); cipher , _ = newCipher( fdata[:AES.block_size] ); fdata = cipher.decrypt( fdata[AES.block_size:] ); if fdata[:9] != "@NETBASM\n": return None; fdata = fdata[9:]; # fdata is now an array of key/values robj = this( **pyzerial.load( fdata ) ); # Make a new UserObject robj._unsaved = False; robj._path = fname; return robj;