<s> import os <EOL> from PySide import QtCore <EOL> from hashlib import sha1 <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> CONFIG_HOME = os . path . join ( os . path . expanduser ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> settings = QtCore . QSettings ( os . path . join ( CONFIG_HOME , '<STR_LIT>' ) , <EOL> QtCore . QSettings . IniFormat ) <EOL> class AppletCapStore ( object ) : <EOL> def __init__ ( self , basedir ) : <EOL> self . _dir = basedir <EOL> def _build_fname ( self , aid , version ) : <EOL> return os . path . join ( self . _dir , aid , '<STR_LIT>' % version ) <EOL> def _validate_hash ( self , fname , cap_sha1 ) : <EOL> with open ( fname , '<STR_LIT:rb>' ) as cap : <EOL> return sha1 ( cap . read ( ) ) . hexdigest ( ) == cap_sha1 <EOL> def has_file ( self , aid , version , cap_sha1 = None ) : <EOL> fname = self . _build_fname ( aid , version ) <EOL> if os . path . isfile ( fname ) : <EOL> if cap_sha1 and not self . _validate_hash ( fname , cap_sha1 ) : <EOL> return False <EOL> return True <EOL> return False <EOL> def get_filename ( self , aid , version , cap_sha1 = None ) : <EOL> fname = self . _build_fname ( aid , version ) <EOL> if not self . has_file ( aid , version ) : <EOL> raise ValueError ( "<STR_LIT>" % fname ) <EOL> if cap_sha1 and not self . _validate_hash ( fname , cap_sha1 ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return fname <EOL> def store_data ( self , aid , version , data , cap_sha1 = None ) : <EOL> fname = self . _build_fname ( aid , version ) <EOL> QtCore . QDir . root ( ) . mkpath ( os . path . dirname ( fname ) ) <EOL> target = QtCore . QFile ( fname ) <EOL> target . open ( QtCore . QIODevice . WriteOnly ) <EOL> if target . write ( data ) == - <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> target . close ( ) <EOL> if cap_sha1 and not self . _validate_hash ( fname , cap_sha1 ) : <EOL> target . remove ( ) <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> capstore = AppletCapStore ( os . path . join ( CONFIG_HOME , '<STR_LIT>' ) ) </s>
