<s> """<STR_LIT>""" <EOL> import codecs <EOL> import yaml <EOL> from datetime import datetime <EOL> from commando . util import getLoggerWithNullHandler <EOL> from fswrap import File , Folder <EOL> from hyde . _compat import iteritems , str , UserDict <EOL> logger = getLoggerWithNullHandler ( '<STR_LIT>' ) <EOL> SEQS = ( tuple , list , set , frozenset ) <EOL> def make_expando ( primitive ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( primitive , dict ) : <EOL> return Expando ( primitive ) <EOL> elif isinstance ( primitive , SEQS ) : <EOL> seq = type ( primitive ) <EOL> return seq ( make_expando ( attr ) for attr in primitive ) <EOL> else : <EOL> return primitive <EOL> class Expando ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , d ) : <EOL> super ( Expando , self ) . __init__ ( ) <EOL> self . update ( d ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return iteritems ( self . __dict__ ) <EOL> def update ( self , d ) : <EOL> """<STR_LIT>""" <EOL> d = d or { } <EOL> if isinstance ( d , dict ) : <EOL> for key , value in d . items ( ) : <EOL> self . set_expando ( key , value ) <EOL> elif isinstance ( d , Expando ) : <EOL> self . update ( d . to_dict ( ) ) <EOL> def set_expando ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> setattr ( self , str ( key ) , make_expando ( value ) ) <EOL> def __repr__ ( self ) : <EOL> return str ( self . to_dict ( ) ) <EOL> def to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> d = self . __dict__ <EOL> for k , v in d . items ( ) : <EOL> if isinstance ( v , Expando ) : <EOL> result [ k ] = v . to_dict ( ) <EOL> elif isinstance ( v , SEQS ) : <EOL> seq = type ( v ) <EOL> result [ k ] = seq ( item . to_dict ( ) <EOL> if isinstance ( item , Expando ) <EOL> else item for item in v <EOL> ) <EOL> else : <EOL> result [ k ] = v <EOL> return result <EOL> def get ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . __dict__ . get ( key , default ) <EOL> class Context ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def load ( sitepath , ctx ) : <EOL> """<STR_LIT>""" <EOL> context = { } <EOL> try : <EOL> context . update ( ctx . data . __dict__ ) <EOL> except AttributeError : <EOL> pass <EOL> providers = { } <EOL> try : <EOL> providers . update ( ctx . providers . __dict__ ) <EOL> except AttributeError : <EOL> pass <EOL> for provider_name , resource_name in providers . items ( ) : <EOL> res = File ( Folder ( sitepath ) . child ( resource_name ) ) <EOL> if res . exists : <EOL> data = make_expando ( yaml . load ( res . read_all ( ) ) ) <EOL> context [ provider_name ] = data <EOL> return context <EOL> class Dependents ( UserDict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sitepath , depends_file_name = '<STR_LIT>' ) : <EOL> self . sitepath = Folder ( sitepath ) <EOL> self . deps_file = File ( self . sitepath . child ( depends_file_name ) ) <EOL> self . data = { } <EOL> if self . deps_file . exists : <EOL> self . data = yaml . load ( self . deps_file . read_all ( ) ) <EOL> import atexit <EOL> atexit . register ( self . save ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . deps_file . parent . exists : <EOL> self . deps_file . write ( yaml . dump ( self . data ) ) <EOL> def _expand_path ( sitepath , path ) : <EOL> child = sitepath . child_folder ( path ) <EOL> return Folder ( child . fully_expanded_path ) <EOL> class Config ( Expando ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sitepath , config_file = None , config_dict = None ) : <EOL> self . default_config = dict ( <EOL> mode = '<STR_LIT>' , <EOL> simple_copy = [ ] , <EOL> content_root = '<STR_LIT:content>' , <EOL> deploy_root = '<STR_LIT>' , <EOL> media_root = '<STR_LIT>' , <EOL> layout_root = '<STR_LIT>' , <EOL> media_url = '<STR_LIT>' , <EOL> base_url = "<STR_LIT:/>" , <EOL> encode_safe = None , <EOL> not_found = '<STR_LIT>' , <EOL> plugins = [ ] , <EOL> ignore = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> meta = { <EOL> "<STR_LIT>" : '<STR_LIT>' <EOL> } <EOL> ) <EOL> self . config_file = config_file <EOL> self . config_dict = config_dict <EOL> self . load_time = datetime . min <EOL> self . config_files = [ ] <EOL> self . sitepath = Folder ( sitepath ) <EOL> super ( Config , self ) . __init__ ( self . load ( ) ) <EOL> @ property <EOL> def last_modified ( self ) : <EOL> return max ( ( conf . last_modified for conf in self . config_files ) ) <EOL> def needs_refresh ( self ) : <EOL> if not self . config_files : <EOL> return True <EOL> return any ( ( conf . has_changed_since ( self . load_time ) <EOL> for conf in self . config_files ) ) <EOL> def load ( self ) : <EOL> conf = dict ( ** self . default_config ) <EOL> conf . update ( self . read_config ( self . config_file ) ) <EOL> if self . config_dict : <EOL> conf . update ( self . config_dict ) <EOL> return conf <EOL> def reload ( self ) : <EOL> if not self . config_file : <EOL> return <EOL> self . update ( self . load ( ) ) <EOL> def read_config ( self , config_file ) : <EOL> """<STR_LIT>""" <EOL> conf_file = self . sitepath . child ( <EOL> config_file if <EOL> config_file else '<STR_LIT>' ) <EOL> conf = { } <EOL> if File ( conf_file ) . exists : <EOL> self . config_files . append ( File ( conf_file ) ) <EOL> logger . info ( "<STR_LIT>" , conf_file ) <EOL> with codecs . open ( conf_file , '<STR_LIT:r>' , '<STR_LIT:utf-8>' ) as stream : <EOL> conf = yaml . load ( stream ) <EOL> if '<STR_LIT>' in conf : <EOL> parent = self . read_config ( conf [ '<STR_LIT>' ] ) <EOL> parent . update ( conf ) <EOL> conf = parent <EOL> self . load_time = datetime . now ( ) <EOL> return conf <EOL> @ property <EOL> def deploy_root_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return _expand_path ( self . sitepath , self . deploy_root ) <EOL> @ property <EOL> def content_root_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return _expand_path ( self . sitepath , self . content_root ) <EOL> @ property <EOL> def media_root_path ( self ) : <EOL> """<STR_LIT>""" <EOL> path = Folder ( self . content_root ) . child ( self . media_root ) <EOL> return _expand_path ( self . sitepath , path ) <EOL> @ property <EOL> def layout_root_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return _expand_path ( self . sitepath , self . layout_root ) </s>
