<s> import sys <EOL> import os <EOL> import time <EOL> import ConfigParser <EOL> import tempfile <EOL> import codecs <EOL> from StringIO import StringIO <EOL> from openflow . optin_manager . sfa . util . xml import XML <EOL> default_config = """<STR_LIT:U+0020>""" <EOL> def isbool ( v ) : <EOL> return v . lower ( ) in ( "<STR_LIT:true>" , "<STR_LIT:false>" ) <EOL> def str2bool ( v ) : <EOL> return v . lower ( ) in ( "<STR_LIT:true>" , "<STR_LIT:1>" ) <EOL> class Config : <EOL> def __init__ ( self , config_file = '<STR_LIT>' ) : <EOL> self . _files = [ ] <EOL> self . config_path = os . path . dirname ( config_file ) <EOL> self . config = ConfigParser . ConfigParser ( ) <EOL> self . filename = config_file <EOL> if not os . path . isfile ( self . filename ) : <EOL> self . create ( self . filename ) <EOL> self . load ( self . filename ) <EOL> def _header ( self ) : <EOL> header = """<STR_LIT>""" % ( time . asctime ( ) , os . linesep . join ( self . _files ) ) <EOL> return header . strip ( ) . split ( os . linesep ) <EOL> def create ( self , filename ) : <EOL> if not os . path . exists ( os . path . dirname ( filename ) ) : <EOL> os . makedirs ( os . path . dirname ( filename ) ) <EOL> configfile = open ( filename , '<STR_LIT:w>' ) <EOL> configfile . write ( default_config ) <EOL> configfile . close ( ) <EOL> def load ( self , filename ) : <EOL> if filename : <EOL> try : <EOL> self . config . read ( filename ) <EOL> except ConfigParser . MissingSectionHeaderError : <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> self . load_xml ( filename ) <EOL> else : <EOL> self . load_shell ( filename ) <EOL> self . _files . append ( filename ) <EOL> self . set_attributes ( ) <EOL> def load_xml ( self , filename ) : <EOL> xml = XML ( filename ) <EOL> categories = xml . xpath ( '<STR_LIT>' ) <EOL> for category in categories : <EOL> section_name = category . get ( '<STR_LIT:id>' ) <EOL> if not self . config . has_section ( section_name ) : <EOL> self . config . add_section ( section_name ) <EOL> options = category . xpath ( '<STR_LIT>' ) <EOL> for option in options : <EOL> option_name = option . get ( '<STR_LIT:id>' ) <EOL> value = option . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . text <EOL> if not value : <EOL> value = "<STR_LIT>" <EOL> self . config . set ( section_name , option_name , value ) <EOL> def load_shell ( self , filename ) : <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> for line in f : <EOL> try : <EOL> if line . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> parts = line . strip ( ) . split ( "<STR_LIT:=>" ) <EOL> if len ( parts ) < <NUM_LIT:2> : <EOL> continue <EOL> option = parts [ <NUM_LIT:0> ] <EOL> value = parts [ <NUM_LIT:1> ] . replace ( '<STR_LIT:">' , '<STR_LIT>' ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> section , var = self . locate_varname ( option , strict = False ) <EOL> if section and var : <EOL> self . set ( section , var , value ) <EOL> except : <EOL> pass <EOL> f . close ( ) <EOL> def locate_varname ( self , varname , strict = True ) : <EOL> varname = varname . lower ( ) <EOL> sections = self . config . sections ( ) <EOL> section_name = "<STR_LIT>" <EOL> var_name = "<STR_LIT>" <EOL> for section in sections : <EOL> if varname . startswith ( section . lower ( ) ) and len ( section ) > len ( section_name ) : <EOL> section_name = section . lower ( ) <EOL> var_name = varname . replace ( section_name , "<STR_LIT>" ) [ <NUM_LIT:1> : ] <EOL> if strict and not self . config . has_option ( section_name , var_name ) : <EOL> raise ConfigParser . NoOptionError ( var_name , section_name ) <EOL> return ( section_name , var_name ) <EOL> def set_attributes ( self ) : <EOL> sections = self . config . sections ( ) <EOL> for section in sections : <EOL> for item in self . config . items ( section ) : <EOL> name = "<STR_LIT>" % ( section , item [ <NUM_LIT:0> ] ) <EOL> value = item [ <NUM_LIT:1> ] <EOL> if isbool ( value ) : <EOL> value = str2bool ( value ) <EOL> elif value . isdigit ( ) : <EOL> value = int ( value ) <EOL> setattr ( self , name , value ) <EOL> setattr ( self , name . upper ( ) , value ) <EOL> def variables ( self ) : <EOL> """<STR_LIT>""" <EOL> variables = { } <EOL> for section in self . config . sections ( ) : <EOL> category = { <EOL> '<STR_LIT:id>' : section , <EOL> '<STR_LIT:name>' : section , <EOL> '<STR_LIT:description>' : section , <EOL> } <EOL> variable_list = { } <EOL> for item in self . config . items ( section ) : <EOL> var_name = item [ <NUM_LIT:0> ] <EOL> name = "<STR_LIT>" % ( section , var_name ) <EOL> value = item [ <NUM_LIT:1> ] <EOL> if isbool ( value ) : <EOL> value_type = bool <EOL> elif value . isdigit ( ) : <EOL> value_type = int <EOL> else : <EOL> value_type = str <EOL> variable = { <EOL> '<STR_LIT:id>' : var_name , <EOL> '<STR_LIT:type>' : value_type , <EOL> '<STR_LIT:value>' : value , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:description>' : name , <EOL> } <EOL> variable_list [ name ] = variable <EOL> variables [ section ] = ( category , variable_list ) <EOL> return variables <EOL> def verify ( self , config1 , config2 , validate_method ) : <EOL> return True <EOL> def validate_type ( self , var_type , value ) : <EOL> return True <EOL> @ staticmethod <EOL> def is_xml ( config_file ) : <EOL> try : <EOL> x = Xml ( config_file ) <EOL> return True <EOL> except : <EOL> return False <EOL> @ staticmethod <EOL> def is_ini ( config_file ) : <EOL> try : <EOL> c = ConfigParser . ConfigParser ( ) <EOL> c . read ( config_file ) <EOL> return True <EOL> except ConfigParser . MissingSectionHeaderError : <EOL> return False <EOL> def dump ( self , sections = [ ] ) : <EOL> sys . stdout . write ( output_python ( ) ) <EOL> def output_python ( self , encoding = "<STR_LIT:utf-8>" ) : <EOL> buf = codecs . lookup ( encoding ) [ <NUM_LIT:3> ] ( StringIO ( ) ) <EOL> buf . writelines ( [ "<STR_LIT>" + line + os . linesep for line in self . _header ( ) ] ) <EOL> for section in self . sections ( ) : <EOL> buf . write ( "<STR_LIT>" % ( section , os . linesep ) ) <EOL> for ( name , value ) in self . items ( section ) : <EOL> buf . write ( "<STR_LIT>" % ( name , value , os . linesep ) ) <EOL> buf . write ( os . linesep ) <EOL> return buf . getvalue ( ) <EOL> def output_shell ( self , show_comments = True , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> buf = codecs . lookup ( encoding ) [ <NUM_LIT:3> ] ( StringIO ( ) ) <EOL> buf . writelines ( [ "<STR_LIT>" + line + os . linesep for line in self . _header ( ) ] ) <EOL> for section in self . sections ( ) : <EOL> for ( name , value ) in self . items ( section ) : <EOL> if value : <EOL> option = "<STR_LIT>" % ( section . upper ( ) , name . upper ( ) ) <EOL> if isbool ( value ) : <EOL> value = str ( str2bool ( value ) ) <EOL> elif not value . isdigit ( ) : <EOL> value = '<STR_LIT>' % value <EOL> buf . write ( option + "<STR_LIT:=>" + value + os . linesep ) <EOL> return buf . getvalue ( ) <EOL> def output_php ( selfi , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> buf = codecs . lookup ( encoding ) [ <NUM_LIT:3> ] ( StringIO ( ) ) <EOL> buf . write ( "<STR_LIT>" + os . linesep ) <EOL> buf . writelines ( [ "<STR_LIT>" + line + os . linesep for line in self . _header ( ) ] ) <EOL> for section in self . sections ( ) : <EOL> for ( name , value ) in self . items ( section ) : <EOL> option = "<STR_LIT>" % ( section , name ) <EOL> buf . write ( os . linesep ) <EOL> buf . write ( "<STR_LIT>" + option + os . linesep ) <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> buf . write ( "<STR_LIT>" % ( option , value ) + os . linesep ) <EOL> buf . write ( "<STR_LIT>" + os . linesep ) <EOL> return buf . getvalue ( ) <EOL> def output_xml ( self , encoding = "<STR_LIT:utf-8>" ) : <EOL> pass <EOL> def output_variables ( self , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> buf = codecs . lookup ( encoding ) [ <NUM_LIT:3> ] ( StringIO ( ) ) <EOL> for section in self . sections ( ) : <EOL> for ( name , value ) in self . items ( section ) : <EOL> option = "<STR_LIT>" % ( section , name ) <EOL> buf . write ( option + os . linesep ) <EOL> return buf . getvalue ( ) <EOL> pass <EOL> def write ( self , filename = None ) : <EOL> if not filename : <EOL> filename = self . filename <EOL> configfile = open ( filename , '<STR_LIT:w>' ) <EOL> self . config . write ( configfile ) <EOL> def save ( self , filename = None ) : <EOL> self . write ( filename ) <EOL> def get_trustedroots_dir ( self ) : <EOL> return self . config_path + os . sep + '<STR_LIT>' <EOL> def get_openflow_aggrMgr_info ( self ) : <EOL> aggr_mgr_ip = '<STR_LIT:localhost>' <EOL> if ( hasattr ( self , '<STR_LIT>' ) ) : <EOL> aggr_mgr_ip = self . OPENFLOW_AGGREGATE_MANAGER_IP <EOL> aggr_mgr_port = <NUM_LIT> <EOL> if ( hasattr ( self , '<STR_LIT>' ) ) : <EOL> aggr_mgr_port = self . OPENFLOW_AGGREGATE_MANAGER_PORT <EOL> return ( aggr_mgr_ip , aggr_mgr_port ) <EOL> def get_interface_hrn ( self ) : <EOL> if ( hasattr ( self , '<STR_LIT>' ) ) : <EOL> return self . SFA_INTERFACE_HRN <EOL> else : <EOL> return "<STR_LIT>" <EOL> def __getattr__ ( self , attr ) : <EOL> print self . config . __dict__ . keys ( ) <EOL> return getattr ( self . config , attr ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> filename = None <EOL> print '<STR_LIT>' <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> print '<STR_LIT>' <EOL> filename = sys . argv [ <NUM_LIT:1> ] <EOL> config = Config ( filename ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> config = Config ( ) <EOL> config . dump ( ) </s>
