<s> import os <EOL> import re <EOL> from tproxy import config <EOL> from tproxy . util import parse_address <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from . util import base_uri , get_host <EOL> ROUTE_RE = re . compile ( "<STR_LIT>" ) <EOL> def validate_path ( value ) : <EOL> if value is not None and not os . path . exists ( value ) : <EOL> raise IOError ( "<STR_LIT>" % value ) <EOL> return value <EOL> class SpoolDir ( config . Setting ) : <EOL> name = "<STR_LIT>" <EOL> section = "<STR_LIT>" <EOL> cli = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> meta = "<STR_LIT>" <EOL> validator = validate_path <EOL> default = None <EOL> desc = """<STR_LIT>""" <EOL> class Hostname ( config . Setting ) : <EOL> name = "<STR_LIT:host>" <EOL> section = "<STR_LIT>" <EOL> cli = [ "<STR_LIT>" ] <EOL> meta = "<STR_LIT>" <EOL> validator = config . validate_string <EOL> default = None <EOL> desc = """<STR_LIT>""" <EOL> class RouteConfig ( config . Config ) : <EOL> def __init__ ( self , usage = None ) : <EOL> super ( RouteConfig , self ) . __init__ ( usage = usage ) <EOL> self . _spooldir = None <EOL> self . routes = { } <EOL> self . hosts = [ ] <EOL> self . rmtime = None <EOL> def is_listen_ssl ( self ) : <EOL> return self . ssl_keyfile is not None <EOL> def get_spooldir ( self ) : <EOL> if self . _spooldir : <EOL> return self . _spooldir <EOL> spooldir = self . spooldir <EOL> if not spooldir : <EOL> if not os . path . exists ( '<STR_LIT>' ) : <EOL> raise IOError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> spooldir = '<STR_LIT>' <EOL> self . _spooldir = spooldir <EOL> return self . _spooldir <EOL> def load_routes ( self ) : <EOL> """<STR_LIT>""" <EOL> fname = os . path . join ( self . get_spooldir ( ) , '<STR_LIT>' ) <EOL> if not os . path . exists ( fname ) : <EOL> return <EOL> mtime = os . stat ( fname ) . st_mtime <EOL> if self . rmtime == mtime : <EOL> return <EOL> self . rmtime = mtime <EOL> with open ( fname , '<STR_LIT:r>' ) as f : <EOL> routes_conf = json . load ( f ) <EOL> for name , conf in routes_conf . items ( ) : <EOL> host = conf . get ( '<STR_LIT:host>' , '<STR_LIT>' ) <EOL> routes = conf . get ( '<STR_LIT>' , { } ) <EOL> self . hosts . append ( ( re . compile ( host ) , name ) ) <EOL> _routes = [ ] <EOL> for ( route , route_conf ) in routes . items ( ) : <EOL> if '<STR_LIT>' in route_conf : <EOL> is_ssl = '<STR_LIT>' in route_conf <EOL> remote = parse_address ( route_conf . get ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> host = get_host ( remote , is_ssl = is_ssl ) <EOL> route_conf . update ( dict ( <EOL> host = host , <EOL> base_uri = base_uri ( host , is_ssl = is_ssl ) , <EOL> remote = remote , <EOL> listen = self . address , <EOL> listen_ssl = self . is_listen_ssl ( ) ) ) <EOL> if ROUTE_RE . match ( route ) : <EOL> spec = re . compile ( route ) <EOL> else : <EOL> spec = re . compile ( "<STR_LIT>" % route ) <EOL> _routes . append ( ( route , spec , route_conf ) ) <EOL> _routes . sort ( ) <EOL> _routes . reverse ( ) <EOL> self . routes [ name ] = _routes <EOL> self . hosts . sort ( ) <EOL> self . hosts . reverse ( ) </s>
