<s> import os <EOL> from . import app <EOL> from flask import json <EOL> from repocheep import Repository <EOL> def cache ( settings ) : <EOL> return PackageCache ( settings . cachepath ) <EOL> class PackageCache ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path ) : <EOL> self . cachedir = path <EOL> def cache_path ( self , repository , codename , component , arch , package , version ) : <EOL> return os . path . join ( self . cachedir , repository . name , codename , component , arch , package , '<STR_LIT>' % version ) <EOL> def write ( self , repository , codename , component , arch , package , version , data ) : <EOL> cache_file_path = self . cache_path ( repository , codename , component , arch , package , version ) <EOL> try : <EOL> if not os . path . exists ( os . path . dirname ( cache_file_path ) ) : <EOL> os . makedirs ( os . path . dirname ( cache_file_path ) ) <EOL> json . dump ( data , open ( cache_file_path , '<STR_LIT:w>' ) ) <EOL> except Exception as e : <EOL> app . logger . warn ( '<STR_LIT>' % cache_file_path ) <EOL> app . logger . warn ( e ) <EOL> raise <EOL> def read ( self , repository , codename , component , arch , package , version ) : <EOL> path = self . cache_path ( repository , codename , component , arch , package , version ) <EOL> try : <EOL> return json . load ( open ( path , '<STR_LIT:r>' ) ) <EOL> except : <EOL> raise <EOL> class Settings ( object ) : <EOL> """<STR_LIT>""" <EOL> loaded = False <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . load ( app . config [ '<STR_LIT>' ] ) <EOL> except Exception as e : <EOL> app . logger . debug ( e ) <EOL> self . loaded = False <EOL> def load ( self , config_file ) : <EOL> """<STR_LIT>""" <EOL> data = json . load ( open ( config_file , '<STR_LIT:r>' ) ) <EOL> self . __dict__ . update ( data ) <EOL> if self . settingsfile and self . basedir : <EOL> self . loaded = True <EOL> def reload ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . load ( self . settingsfile ) <EOL> except Exception as e : <EOL> app . logger . debug ( e ) <EOL> self . loaded = False <EOL> def save ( self , form ) : <EOL> """<STR_LIT>""" <EOL> app . logger . debug ( form ) <EOL> try : <EOL> settings_dir = os . path . dirname ( form [ '<STR_LIT>' ] ) <EOL> if not os . path . exists ( settings_dir ) : <EOL> os . makedirs ( settings_dir ) <EOL> json . dump ( form , open ( form [ '<STR_LIT>' ] , '<STR_LIT:w>' ) ) <EOL> except Exception as e : <EOL> app . logger . debug ( e ) <EOL> return { '<STR_LIT:data>' : { } , '<STR_LIT:status>' : '<STR_LIT>' } <EOL> try : <EOL> self . load ( form [ '<STR_LIT>' ] ) <EOL> except : <EOL> return { '<STR_LIT:data>' : { } , '<STR_LIT:status>' : '<STR_LIT>' } <EOL> return { '<STR_LIT:data>' : form , '<STR_LIT:status>' : '<STR_LIT:OK>' } </s>
