<s> """<STR_LIT>""" <EOL> import os , sys <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> try : <EOL> import cStringIO as StringIO <EOL> except ImportError : <EOL> import StringIO <EOL> from hashlib import md5 <EOL> from twisted . python import log , runtime <EOL> from twisted . persisted import styles <EOL> from zope . interface import implements , Interface <EOL> def _encrypt ( passphrase , data ) : <EOL> from Crypto . Cipher import AES as cipher <EOL> leftover = len ( data ) % cipher . block_size <EOL> if leftover : <EOL> data += '<STR_LIT:U+0020>' * ( cipher . block_size - leftover ) <EOL> return cipher . new ( md5 ( passphrase ) . digest ( ) [ : <NUM_LIT:16> ] ) . encrypt ( data ) <EOL> def _decrypt ( passphrase , data ) : <EOL> from Crypto . Cipher import AES <EOL> return AES . new ( md5 ( passphrase ) . digest ( ) [ : <NUM_LIT:16> ] ) . decrypt ( data ) <EOL> class IPersistable ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def setStyle ( style ) : <EOL> """<STR_LIT>""" <EOL> def save ( tag = None , filename = None , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> class Persistent : <EOL> implements ( IPersistable ) <EOL> style = "<STR_LIT>" <EOL> def __init__ ( self , original , name ) : <EOL> self . original = original <EOL> self . name = name <EOL> def setStyle ( self , style ) : <EOL> """<STR_LIT>""" <EOL> self . style = style <EOL> def _getFilename ( self , filename , ext , tag ) : <EOL> if filename : <EOL> finalname = filename <EOL> filename = finalname + "<STR_LIT>" <EOL> elif tag : <EOL> filename = "<STR_LIT>" % ( self . name , tag , ext ) <EOL> finalname = "<STR_LIT>" % ( self . name , tag , ext ) <EOL> else : <EOL> filename = "<STR_LIT>" % ( self . name , ext ) <EOL> finalname = "<STR_LIT>" % ( self . name , ext ) <EOL> return finalname , filename <EOL> def _saveTemp ( self , filename , passphrase , dumpFunc ) : <EOL> f = open ( filename , '<STR_LIT:wb>' ) <EOL> if passphrase is None : <EOL> dumpFunc ( self . original , f ) <EOL> else : <EOL> s = StringIO . StringIO ( ) <EOL> dumpFunc ( self . original , s ) <EOL> f . write ( _encrypt ( passphrase , s . getvalue ( ) ) ) <EOL> f . close ( ) <EOL> def _getStyle ( self ) : <EOL> if self . style == "<STR_LIT:source>" : <EOL> from twisted . persisted . aot import jellyToSource as dumpFunc <EOL> ext = "<STR_LIT>" <EOL> else : <EOL> def dumpFunc ( obj , file ) : <EOL> pickle . dump ( obj , file , <NUM_LIT:2> ) <EOL> ext = "<STR_LIT>" <EOL> return ext , dumpFunc <EOL> def save ( self , tag = None , filename = None , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> ext , dumpFunc = self . _getStyle ( ) <EOL> if passphrase : <EOL> ext = '<STR_LIT:e>' + ext <EOL> finalname , filename = self . _getFilename ( filename , ext , tag ) <EOL> log . msg ( "<STR_LIT>" + self . name + "<STR_LIT>" + finalname + "<STR_LIT>" ) <EOL> self . _saveTemp ( filename , passphrase , dumpFunc ) <EOL> if runtime . platformType == "<STR_LIT:win32>" and os . path . isfile ( finalname ) : <EOL> os . remove ( finalname ) <EOL> os . rename ( filename , finalname ) <EOL> log . msg ( "<STR_LIT>" ) <EOL> Persistant = Persistent <EOL> class _EverythingEphemeral ( styles . Ephemeral ) : <EOL> initRun = <NUM_LIT:0> <EOL> def __init__ ( self , mainMod ) : <EOL> """<STR_LIT>""" <EOL> self . mainMod = mainMod <EOL> def __getattr__ ( self , key ) : <EOL> try : <EOL> return getattr ( self . mainMod , key ) <EOL> except AttributeError : <EOL> if self . initRun : <EOL> raise <EOL> else : <EOL> log . msg ( "<STR_LIT>" % key ) <EOL> return styles . Ephemeral ( ) <EOL> def load ( filename , style , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> mode = '<STR_LIT:r>' <EOL> if style == '<STR_LIT:source>' : <EOL> from twisted . persisted . aot import unjellyFromSource as _load <EOL> else : <EOL> _load , mode = pickle . load , '<STR_LIT:rb>' <EOL> if passphrase : <EOL> fp = StringIO . StringIO ( _decrypt ( passphrase , <EOL> open ( filename , '<STR_LIT:rb>' ) . read ( ) ) ) <EOL> else : <EOL> fp = open ( filename , mode ) <EOL> ee = _EverythingEphemeral ( sys . modules [ '<STR_LIT:__main__>' ] ) <EOL> sys . modules [ '<STR_LIT:__main__>' ] = ee <EOL> ee . initRun = <NUM_LIT:1> <EOL> try : <EOL> value = _load ( fp ) <EOL> finally : <EOL> sys . modules [ '<STR_LIT:__main__>' ] = ee . mainMod <EOL> styles . doUpgrade ( ) <EOL> ee . initRun = <NUM_LIT:0> <EOL> persistable = IPersistable ( value , None ) <EOL> if persistable is not None : <EOL> persistable . setStyle ( style ) <EOL> return value <EOL> def loadValueFromFile ( filename , variable , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> if passphrase : <EOL> mode = '<STR_LIT:rb>' <EOL> else : <EOL> mode = '<STR_LIT:r>' <EOL> fileObj = open ( filename , mode ) <EOL> d = { '<STR_LIT>' : filename } <EOL> if passphrase : <EOL> data = fileObj . read ( ) <EOL> data = _decrypt ( passphrase , data ) <EOL> exec data in d , d <EOL> else : <EOL> exec fileObj in d , d <EOL> value = d [ variable ] <EOL> return value <EOL> def guessType ( filename ) : <EOL> ext = os . path . splitext ( filename ) [ <NUM_LIT:1> ] <EOL> return { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' , <EOL> } [ ext ] <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] </s>
