<s> import sys <EOL> import logging <EOL> import xml . etree . ElementTree as etree <EOL> from . import __version__ , DEBUG1 , DEBUG2 , DEBUG3 <EOL> _encoding = '<STR_LIT:utf-8>' <EOL> _tags_forcelist = set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class PanConfigError ( Exception ) : <EOL> pass <EOL> class PanConfig : <EOL> def __init__ ( self , <EOL> config = None , <EOL> tags_forcelist = _tags_forcelist ) : <EOL> self . _log = logging . getLogger ( __name__ ) . log <EOL> self . _config_version = <NUM_LIT:0> <EOL> self . _config_panorama = None <EOL> self . _config_multi_vsys = None <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , sys . version ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , etree . VERSION ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , __version__ ) <EOL> if config is None : <EOL> raise PanConfigError ( '<STR_LIT>' ) <EOL> self . _log ( DEBUG2 , '<STR_LIT:%s>' , type ( config ) ) <EOL> if hasattr ( config , '<STR_LIT>' ) : <EOL> self . config_root = config <EOL> else : <EOL> try : <EOL> self . config_root = etree . fromstring ( config ) <EOL> except etree . ParseError as msg : <EOL> raise PanConfigError ( '<STR_LIT>' <EOL> % msg ) <EOL> self . _log ( DEBUG1 , '<STR_LIT>' , self . config_root ) <EOL> def __find_xpath ( self , xpath = None ) : <EOL> self . _log ( DEBUG1 , '<STR_LIT>' , xpath ) <EOL> if xpath : <EOL> try : <EOL> nodes = self . config_root . findall ( xpath ) <EOL> except SyntaxError as msg : <EOL> raise PanConfigError ( '<STR_LIT>' % msg ) <EOL> else : <EOL> nodes = [ self . config_root ] <EOL> self . _log ( DEBUG1 , '<STR_LIT>' , nodes ) <EOL> return nodes <EOL> def config_version ( self ) : <EOL> if self . _config_version != <NUM_LIT:0> : <EOL> return self . _config_version <EOL> self . _config_version = None <EOL> if self . config_root . tag == '<STR_LIT>' : <EOL> self . _config_version = self . config_root . get ( '<STR_LIT:version>' , default = None ) <EOL> return self . _config_version <EOL> def config_panorama ( self ) : <EOL> if self . _config_panorama is not None : <EOL> return self . _config_panorama <EOL> xpaths = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> if self . config_root . tag == '<STR_LIT>' : <EOL> for xpath in xpaths : <EOL> elem = self . config_root . find ( xpath ) <EOL> if elem is not None : <EOL> self . _config_panorama = True <EOL> break <EOL> else : <EOL> self . _config_panorama = False <EOL> return self . _config_panorama <EOL> def config_multi_vsys ( self ) : <EOL> if self . _config_multi_vsys is not None : <EOL> return self . _config_multi_vsys <EOL> path = "<STR_LIT>" <EOL> if self . config_root . tag == '<STR_LIT>' : <EOL> nodes = self . config_root . findall ( path ) <EOL> if len ( nodes ) > <NUM_LIT:1> : <EOL> self . _config_multi_vsys = True <EOL> else : <EOL> self . _config_multi_vsys = False <EOL> return self . _config_multi_vsys <EOL> def xml ( self , xpath = None ) : <EOL> nodes = self . __find_xpath ( xpath ) <EOL> if not nodes : <EOL> return None <EOL> s = '<STR_LIT>' . encode ( ) <EOL> for elem in nodes : <EOL> s += etree . tostring ( elem , encoding = _encoding ) <EOL> if not s : <EOL> return None <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , type ( s ) ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , type ( s . decode ( _encoding ) ) ) <EOL> return s . decode ( _encoding ) <EOL> def python ( self , xpath = None ) : <EOL> nodes = self . __find_xpath ( xpath ) <EOL> if not nodes : <EOL> return None <EOL> d = { } <EOL> if len ( nodes ) > <NUM_LIT:1> : <EOL> for elem in nodes : <EOL> self . __serialize_py ( elem , d ) <EOL> else : <EOL> self . __serialize_py ( nodes [ <NUM_LIT:0> ] , d ) <EOL> return d <EOL> def __serialize_py ( self , elem , obj , forcelist = False ) : <EOL> tag = elem . tag <EOL> text = elem . text <EOL> tail = elem . tail <EOL> text_strip = None <EOL> if text : <EOL> text_strip = text . strip ( ) <EOL> attrs = elem . items ( ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , forcelist , tag ) <EOL> if forcelist : <EOL> if tag not in obj : <EOL> obj [ tag ] = [ ] <EOL> if not len ( elem ) and not text_strip and not attrs : <EOL> obj [ tag ] . append ( None ) <EOL> return <EOL> if not len ( elem ) and text_strip and not attrs : <EOL> obj [ tag ] . append ( text ) <EOL> return <EOL> obj [ tag ] . append ( { } ) <EOL> o = obj [ tag ] [ - <NUM_LIT:1> ] <EOL> else : <EOL> if not len ( elem ) and not text_strip and not attrs : <EOL> obj [ tag ] = None <EOL> return <EOL> if not len ( elem ) and text_strip and not attrs : <EOL> if text_strip == '<STR_LIT:yes>' : <EOL> obj [ tag ] = True <EOL> elif text_strip == '<STR_LIT>' : <EOL> obj [ tag ] = False <EOL> else : <EOL> obj [ tag ] = text <EOL> return <EOL> obj [ tag ] = { } <EOL> o = obj [ tag ] <EOL> for k , v in attrs : <EOL> o [ k ] = v <EOL> if text_strip : <EOL> o [ tag ] = text <EOL> if len ( elem ) : <EOL> tags = { } <EOL> for e in elem : <EOL> if e . tag in tags : <EOL> tags [ e . tag ] += <NUM_LIT:1> <EOL> else : <EOL> tags [ e . tag ] = <NUM_LIT:1> <EOL> for e in elem : <EOL> forcelist = False <EOL> if e . tag in _tags_forcelist or tags [ e . tag ] > <NUM_LIT:1> : <EOL> forcelist = True <EOL> self . __serialize_py ( e , o , forcelist ) <EOL> def flat ( self , path , xpath = None ) : <EOL> nodes = self . __find_xpath ( xpath ) <EOL> if not nodes : <EOL> return None <EOL> obj = [ ] <EOL> for elem in nodes : <EOL> self . __serialize_flat ( elem , path + elem . tag , obj ) <EOL> return obj <EOL> def __serialize_flat ( self , elem , path , obj ) : <EOL> tag = elem . tag <EOL> text = elem . text <EOL> tail = elem . tail <EOL> text_strip = None <EOL> if text : <EOL> text_strip = text . strip ( ) <EOL> attrs = elem . items ( ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , len ( elem ) , tag ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , text_strip ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , attrs ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , path ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , obj ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' ) <EOL> if not text_strip : <EOL> obj . append ( path ) <EOL> elif text_strip : <EOL> lines = text . splitlines ( ) <EOL> if len ( lines ) > <NUM_LIT:1> : <EOL> n = <NUM_LIT:1> <EOL> for line in lines : <EOL> s = path + '<STR_LIT>' % ( n , line ) <EOL> obj . append ( s ) <EOL> n += <NUM_LIT:1> <EOL> else : <EOL> s = path + '<STR_LIT>' % text <EOL> obj . append ( s ) <EOL> for k , v in attrs : <EOL> path += "<STR_LIT>" % ( k , v ) <EOL> obj . append ( path ) <EOL> for e in elem : <EOL> self . __serialize_flat ( e , path + '<STR_LIT:/>' + e . tag , obj ) <EOL> def __quote_space ( self , s ) : <EOL> if '<STR_LIT:U+0020>' in s : <EOL> return '<STR_LIT>' % s <EOL> return s <EOL> def set_cli ( self , path , xpath = None , member_list = False ) : <EOL> nodes = self . __find_xpath ( xpath ) <EOL> if not nodes : <EOL> return None <EOL> obj = [ ] <EOL> for elem in nodes : <EOL> self . __serialize_set_cli ( elem , path + elem . tag , obj , <EOL> member_list ) <EOL> return obj <EOL> def __serialize_set_cli ( self , elem , path , obj , member_list = False ) : <EOL> tag = elem . tag <EOL> text = elem . text <EOL> tail = elem . tail <EOL> text_strip = None <EOL> if text : <EOL> text_strip = text . strip ( ) <EOL> attrs = elem . items ( ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , <EOL> len ( elem ) , member_list , tag ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , text_strip ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , attrs ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , path ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , obj ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' ) <EOL> for k , v in attrs : <EOL> if k == '<STR_LIT:name>' : <EOL> path += '<STR_LIT:U+0020>' + self . __quote_space ( v ) <EOL> if member_list : <EOL> nodes = elem . findall ( '<STR_LIT>' ) <EOL> self . _log ( DEBUG3 , '<STR_LIT>' , len ( nodes ) , tag ) <EOL> if len ( nodes ) > <NUM_LIT:1> : <EOL> members = [ ] <EOL> for e in nodes : <EOL> members . append ( self . __quote_space ( e . text ) ) <EOL> path += '<STR_LIT>' + '<STR_LIT:U+0020>' . join ( members ) + '<STR_LIT>' <EOL> obj . append ( path ) <EOL> return <EOL> if not len ( elem ) : <EOL> if text_strip : <EOL> path += '<STR_LIT:U+0020>' + self . __quote_space ( text ) <EOL> obj . append ( path ) <EOL> for e in elem : <EOL> tpath = path <EOL> if e . tag not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> tpath += '<STR_LIT:U+0020>' + e . tag <EOL> self . __serialize_set_cli ( e , tpath , obj , member_list ) <EOL> def config_xpaths ( self ) : <EOL> xpaths_panos_4_1 = '''<STR_LIT>''' <EOL> xpaths_panos_5_0 = '''<STR_LIT>''' <EOL> xpaths_panos_6_0 = '''<STR_LIT>''' <EOL> xpaths_panos_multi_vsys_4_1 = '''<STR_LIT>''' <EOL> xpaths_panorama_4_1 = '''<STR_LIT>''' <EOL> xpaths_panorama_5_0 = '''<STR_LIT>''' <EOL> xpaths_panorama_6_0 = '''<STR_LIT>''' <EOL> xpaths_panorama_6_1 = '''<STR_LIT>''' <EOL> xpaths_panos = xpaths_panos_4_1 <EOL> xpaths_panos_multi_vsys = xpaths_panos_multi_vsys_4_1 <EOL> xpaths_panorama = xpaths_panorama_4_1 <EOL> if self . config_version ( ) is not None : <EOL> if self . config_version ( ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> xpaths_panos = xpaths_panos_5_0 <EOL> xpaths_panorama = xpaths_panorama_5_0 <EOL> elif self . config_version ( ) in [ '<STR_LIT>' ] : <EOL> xpaths_panos = xpaths_panos_6_0 <EOL> xpaths_panorama = xpaths_panorama_6_0 <EOL> elif self . config_version ( ) in [ '<STR_LIT>' ] : <EOL> xpaths_panos = xpaths_panos_6_0 <EOL> xpaths_panorama = xpaths_panorama_6_1 <EOL> if self . config_multi_vsys ( ) : <EOL> xpaths = xpaths_panos_multi_vsys <EOL> elif self . config_panorama ( ) : <EOL> xpaths = xpaths_panorama <EOL> else : <EOL> xpaths = xpaths_panos <EOL> xpaths = xpaths . split ( '<STR_LIT:\n>' ) <EOL> xpaths = [ s for s in xpaths if s ] <EOL> return xpaths </s>
