<s> import sys , os , yaml , json <EOL> import jasy . core . Console as Console <EOL> import jasy . core . File as File <EOL> from jasy import UserError <EOL> from jasy . core . Util import getKey <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def findConfig ( fileName ) : <EOL> """<STR_LIT>""" <EOL> fileExt = os . path . splitext ( fileName ) [ <NUM_LIT:1> ] <EOL> if not fileExt : <EOL> for tryExt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if os . path . exists ( fileName + tryExt ) : <EOL> return fileName + tryExt <EOL> return None <EOL> if os . path . exists ( fileName ) and fileExt in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return fileName <EOL> else : <EOL> return None <EOL> def loadConfig ( fileName , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> configName = findConfig ( fileName ) <EOL> if configName is None : <EOL> raise UserError ( "<STR_LIT>" % fileName ) <EOL> fileHandle = open ( configName , mode = "<STR_LIT:r>" , encoding = encoding ) <EOL> fileExt = os . path . splitext ( configName ) [ <NUM_LIT:1> ] <EOL> if fileExt == "<STR_LIT>" : <EOL> result = json . load ( fileHandle ) <EOL> elif fileExt == "<STR_LIT>" : <EOL> result = yaml . load ( fileHandle ) <EOL> fileHandle . close ( ) <EOL> return result <EOL> def writeConfig ( data , fileName , indent = <NUM_LIT:2> , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> fileHandle = open ( fileName , mode = "<STR_LIT:w>" , encoding = encoding ) <EOL> fileExt = os . path . splitext ( fileName ) [ <NUM_LIT:1> ] <EOL> if fileExt == "<STR_LIT>" : <EOL> json . dump ( data , fileHandle , indent = indent , ensure_ascii = False ) <EOL> fileHandle . close ( ) <EOL> elif fileExt == "<STR_LIT>" : <EOL> yaml . dump ( data , fileHandle , default_flow_style = False , indent = indent , allow_unicode = True ) <EOL> fileHandle . close ( ) <EOL> else : <EOL> fileHandle . close ( ) <EOL> raise UserError ( "<STR_LIT>" % fileExt ) <EOL> def matchesType ( value , expected ) : <EOL> """<STR_LIT>""" <EOL> result = type ( value ) <EOL> expected = expected . lower ( ) <EOL> if result is int : <EOL> return expected in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:int>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> elif result is float : <EOL> return expected in ( "<STR_LIT:float>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> elif result is str : <EOL> return expected in ( "<STR_LIT:string>" , "<STR_LIT:str>" , "<STR_LIT>" ) <EOL> elif result is bool : <EOL> return expected in ( "<STR_LIT>" , "<STR_LIT:bool>" , "<STR_LIT>" ) <EOL> elif result is dict : <EOL> return expected in ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> elif result is list : <EOL> return expected in ( "<STR_LIT>" , "<STR_LIT:list>" ) <EOL> return False <EOL> class Config : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = None ) : <EOL> """<STR_LIT>""" <EOL> self . __data = data or { } <EOL> def debug ( self ) : <EOL> """<STR_LIT>""" <EOL> print ( self . __data ) <EOL> def export ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> def recurse ( data , prefix ) : <EOL> for key in data : <EOL> value = data [ key ] <EOL> if type ( value ) is dict : <EOL> if prefix : <EOL> recurse ( value , prefix + key + "<STR_LIT:.>" ) <EOL> else : <EOL> recurse ( value , key + "<STR_LIT:.>" ) <EOL> else : <EOL> result [ prefix + key ] = value <EOL> recurse ( self . __data , "<STR_LIT>" ) <EOL> return result <EOL> def injectValues ( self , parse = True , ** argv ) : <EOL> """<STR_LIT>""" <EOL> for key in argv : <EOL> self . set ( key , argv [ key ] , parse = parse ) <EOL> def loadValues ( self , fileName , optional = False , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> configFile = findConfig ( fileName ) <EOL> if configFile is None : <EOL> if optional : <EOL> return False <EOL> else : <EOL> raise UserError ( "<STR_LIT>" % configFile ) <EOL> data = loadConfig ( configFile , encoding = encoding ) <EOL> for key in data : <EOL> self . set ( key , data [ key ] ) <EOL> return True <EOL> def readQuestions ( self , fileName , force = False , autoDelete = True , optional = False , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> configFile = findConfig ( fileName ) <EOL> if configFile is None : <EOL> if optional : <EOL> return False <EOL> else : <EOL> raise UserError ( "<STR_LIT>" % configFile ) <EOL> data = loadConfig ( configFile , encoding = encoding ) <EOL> for entry in data : <EOL> question = entry [ "<STR_LIT>" ] <EOL> name = entry [ "<STR_LIT:name>" ] <EOL> accept = getKey ( entry , "<STR_LIT>" , None ) <EOL> required = getKey ( entry , "<STR_LIT>" , True ) <EOL> default = getKey ( entry , "<STR_LIT:default>" , None ) <EOL> force = getKey ( entry , "<STR_LIT>" , False ) <EOL> self . ask ( question , name , accept = accept , required = required , default = default , force = force ) <EOL> if autoDelete : <EOL> File . rm ( configFile ) <EOL> return True <EOL> def executeScript ( self , fileName , autoDelete = True , optional = False , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( fileName ) : <EOL> if optional : <EOL> return False <EOL> else : <EOL> raise UserError ( "<STR_LIT>" % configFile ) <EOL> env = { <EOL> "<STR_LIT>" : self , <EOL> "<STR_LIT:file>" : File <EOL> } <EOL> code = open ( fileName , "<STR_LIT:r>" , encoding = encoding ) . read ( ) <EOL> exec ( compile ( code , os . path . abspath ( fileName ) , "<STR_LIT>" ) , globals ( ) , env ) <EOL> if autoDelete : <EOL> File . rm ( "<STR_LIT>" ) <EOL> return True <EOL> def has ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if not "<STR_LIT:.>" in name : <EOL> return name in self . __data <EOL> splits = name . split ( "<STR_LIT:.>" ) <EOL> current = self . __data <EOL> for split in splits : <EOL> if split in current : <EOL> current = current [ split ] <EOL> else : <EOL> return False <EOL> return True <EOL> def get ( self , name , default = None ) : <EOL> """<STR_LIT>""" <EOL> if not "<STR_LIT:.>" in name : <EOL> return getKey ( self . __data , name , default ) <EOL> splits = name . split ( "<STR_LIT:.>" ) <EOL> current = self . __data <EOL> for split in splits [ : - <NUM_LIT:1> ] : <EOL> if split in current : <EOL> current = current [ split ] <EOL> else : <EOL> return None <EOL> return getKey ( current , splits [ - <NUM_LIT:1> ] , default ) <EOL> def ask ( self , question , name , accept = None , required = True , default = None , force = False , parse = True ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> msg = "<STR_LIT>" % question <EOL> if accept is not None : <EOL> msg += Console . colorize ( "<STR_LIT>" % accept , "<STR_LIT>" ) <EOL> if default is None : <EOL> msg += Console . colorize ( "<STR_LIT>" % name , "<STR_LIT>" ) <EOL> else : <EOL> msg += Console . colorize ( "<STR_LIT>" % ( name , default ) , "<STR_LIT>" ) <EOL> msg += "<STR_LIT>" <EOL> sys . stdout . write ( msg ) <EOL> if not force and self . has ( name ) : <EOL> print ( "<STR_LIT>" % ( self . get ( name ) , Console . colorize ( "<STR_LIT>" , "<STR_LIT>" ) ) ) <EOL> return <EOL> value = input ( ) . strip ( ) <EOL> if not required and value == "<STR_LIT>" : <EOL> value = default <EOL> if value == "<STR_LIT>" : <EOL> continue <EOL> if self . set ( name , value , accept = accept , parse = parse ) : <EOL> break <EOL> def set ( self , name , value , accept = None , parse = False ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return False <EOL> if parse : <EOL> try : <EOL> parsedValue = eval ( value ) <EOL> except : <EOL> pass <EOL> else : <EOL> value = parsedValue <EOL> if type ( value ) in ( tuple , set ) : <EOL> value = list ( value ) <EOL> if accept is not None and not matchesType ( value , accept ) : <EOL> print ( Console . colorize ( "<STR_LIT>" % str ( value ) , "<STR_LIT>" ) ) <EOL> return False <EOL> if "<STR_LIT:.>" in name : <EOL> splits = name . split ( "<STR_LIT:.>" ) <EOL> current = self . __data <EOL> for split in splits [ : - <NUM_LIT:1> ] : <EOL> if not split in current : <EOL> current [ split ] = { } <EOL> current = current [ split ] <EOL> current [ splits [ - <NUM_LIT:1> ] ] = value <EOL> else : <EOL> self . __data [ name ] = value <EOL> return True <EOL> def write ( self , fileName , indent = <NUM_LIT:2> , encoding = "<STR_LIT:utf-8>" ) : <EOL> """<STR_LIT>""" <EOL> writeConfig ( self . __data , fileName , indent = indent , encoding = encoding ) </s>
