<s> from robot import parsing <EOL> from robot . variables . filesetter import VariableFileSetter <EOL> from robot . variables . store import VariableStore <EOL> from robot . variables . variables import Variables <EOL> from robot . libdocpkg . robotbuilder import LibraryDocBuilder <EOL> from robot . utils . importer import Importer <EOL> from robot . libraries import STDLIBS <EOL> from robot . output import LOGGER as ROBOT_LOGGER <EOL> from robot . errors import DataError <EOL> from os import path <EOL> import xml . etree . ElementTree as ET <EOL> from tempfile import mkdtemp <EOL> import logging <EOL> import inspect <EOL> from parser_utils . util import normalise_path <EOL> from db_json_settings import DBJsonSetting <EOL> logging . basicConfig ( <EOL> format = '<STR_LIT>' , <EOL> level = logging . DEBUG ) <EOL> def strip_and_lower ( text ) : <EOL> return text . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> class DataParser ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . file_path = None <EOL> self . rf_variables = Variables ( ) <EOL> self . rf_var_storage = VariableStore ( self . rf_variables ) <EOL> self . libdoc = LibraryDocBuilder ( ) <EOL> def parse_resource ( self , file_path ) : <EOL> self . file_path = file_path <EOL> if path . exists ( file_path ) : <EOL> if '<STR_LIT>' in file_path : <EOL> folder = path . dirname ( file_path ) <EOL> model = parsing . TestDataDirectory ( source = folder ) . populate ( ) <EOL> else : <EOL> model = parsing . ResourceFile ( file_path ) . populate ( ) <EOL> return self . _parse_robot_data ( file_path , model ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' , file_path ) <EOL> raise ValueError ( <EOL> '<STR_LIT>' . format ( file_path ) ) <EOL> def parse_suite ( self , file_path ) : <EOL> self . file_path = file_path <EOL> if path . exists ( file_path ) : <EOL> model = parsing . TestCaseFile ( source = file_path ) . populate ( ) <EOL> return self . _parse_robot_data ( file_path , model ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' , file_path ) <EOL> raise ValueError ( <EOL> '<STR_LIT>' . format ( file_path ) ) <EOL> def parse_variable_file ( self , file_path , args = None ) : <EOL> if not args : <EOL> args = [ ] <EOL> data = { } <EOL> data [ DBJsonSetting . file_name ] = path . basename ( file_path ) <EOL> data [ DBJsonSetting . file_path ] = normalise_path ( file_path ) <EOL> self . file_path = file_path <EOL> setter = VariableFileSetter ( self . rf_var_storage ) <EOL> var_list = [ ] <EOL> try : <EOL> variables = setter . set ( file_path , args ) <EOL> except DataError : <EOL> variables = [ ] <EOL> for variable in variables : <EOL> var_list . append ( variable [ <NUM_LIT:0> ] ) <EOL> data [ DBJsonSetting . variables ] = sorted ( var_list ) <EOL> return data <EOL> def parse_library ( self , library , args = None ) : <EOL> """<STR_LIT>""" <EOL> data = { } <EOL> if not args : <EOL> data [ DBJsonSetting . arguments ] = [ ] <EOL> else : <EOL> arg_list = [ ] <EOL> for arg in args : <EOL> arg_list . append ( arg ) <EOL> data [ DBJsonSetting . arguments ] = arg_list <EOL> if path . isfile ( library ) : <EOL> data [ DBJsonSetting . file_path ] = normalise_path ( library ) <EOL> if library . endswith ( '<STR_LIT>' ) : <EOL> library_module , keywords = self . _parse_xml_doc ( library ) <EOL> data [ DBJsonSetting . keywords ] = keywords <EOL> data [ DBJsonSetting . library_module ] = library_module <EOL> elif library . endswith ( '<STR_LIT>' ) : <EOL> data [ DBJsonSetting . file_name ] = path . basename ( library ) <EOL> data [ DBJsonSetting . library_module ] = path . splitext ( <EOL> data [ DBJsonSetting . file_name ] ) [ <NUM_LIT:0> ] <EOL> data [ DBJsonSetting . keywords ] = self . _parse_python_lib ( <EOL> library , data [ DBJsonSetting . arguments ] ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> data [ DBJsonSetting . library_module ] = library <EOL> data [ DBJsonSetting . keywords ] = self . _parse_python_lib ( <EOL> library , data [ DBJsonSetting . arguments ] ) <EOL> if data [ DBJsonSetting . keywords ] is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> return data <EOL> def register_console_logger ( self ) : <EOL> ROBOT_LOGGER . register_console_logger ( ) <EOL> def unregister_console_logger ( self ) : <EOL> ROBOT_LOGGER . unregister_console_logger ( ) <EOL> def close_logger ( self ) : <EOL> ROBOT_LOGGER . close ( ) <EOL> def _parse_python_lib ( self , library , args ) : <EOL> lib_with_args = self . _lib_arg_formatter ( library , args ) <EOL> kws = { } <EOL> try : <EOL> lib = self . libdoc . build ( lib_with_args ) <EOL> except DataError : <EOL> raise ValueError ( <EOL> '<STR_LIT>' . format ( library ) ) <EOL> if library in STDLIBS : <EOL> import_name = '<STR_LIT>' + library <EOL> else : <EOL> import_name = library <EOL> importer = Importer ( '<STR_LIT>' ) <EOL> libcode = importer . import_class_or_module ( <EOL> import_name , return_source = False ) <EOL> for keyword in lib . keywords : <EOL> kw = { } <EOL> kw [ DBJsonSetting . keyword_name ] = keyword . name <EOL> kw [ DBJsonSetting . tags ] = list ( keyword . tags . _tags ) <EOL> kw [ DBJsonSetting . keyword_arguments ] = keyword . args <EOL> kw [ DBJsonSetting . documentation ] = keyword . doc <EOL> kw [ DBJsonSetting . keyword_file ] = self . _get_library_kw_source ( <EOL> libcode , keyword . name ) <EOL> kws [ strip_and_lower ( keyword . name ) ] = kw <EOL> return kws <EOL> def _get_library_kw_source ( self , libcode , keyword ) : <EOL> kw_func = keyword . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> func = None <EOL> func_file = None <EOL> if hasattr ( libcode , kw_func ) : <EOL> func = getattr ( libcode , kw_func ) <EOL> if func : <EOL> kw_class = self . get_class_that_defined_method ( func ) <EOL> if kw_class : <EOL> func_file = self . get_function_file ( kw_class ) <EOL> else : <EOL> func_file = self . get_function_file ( func ) <EOL> return func_file <EOL> def get_class_that_defined_method ( self , meth ) : <EOL> try : <EOL> class_mro = inspect . getmro ( meth . im_class ) <EOL> except AttributeError : <EOL> return None <EOL> for cls in class_mro : <EOL> if meth . __name__ in cls . __dict__ : <EOL> return cls <EOL> return None <EOL> def get_function_file ( self , kw_class ) : <EOL> file_ = inspect . getsourcefile ( kw_class ) <EOL> if file_ and path . exists ( file_ ) : <EOL> return normalise_path ( file_ ) <EOL> else : <EOL> return None <EOL> def _lib_arg_formatter ( self , library , args ) : <EOL> args = self . _argument_path_formatter ( library , args ) <EOL> if not args : <EOL> return library <EOL> else : <EOL> for item in args : <EOL> library = '<STR_LIT>' . format ( lib = library , item = item ) <EOL> return library <EOL> def _argument_path_formatter ( self , library , args ) : <EOL> """<STR_LIT>""" <EOL> arguments = [ ] <EOL> for arg in args : <EOL> if '<STR_LIT>' in arg or '<STR_LIT>' in arg or '<STR_LIT>' in arg : <EOL> f = mkdtemp ( ) <EOL> logging . info ( <EOL> '<STR_LIT>' ) <EOL> logging . debug ( '<STR_LIT>' , library ) <EOL> logging . debug ( '<STR_LIT>' , arg , f ) <EOL> arguments . append ( f ) <EOL> else : <EOL> arguments . append ( arg ) <EOL> return arguments <EOL> def _parse_xml_doc ( self , library ) : <EOL> root = ET . parse ( library ) . getroot ( ) <EOL> if ( '<STR_LIT:type>' , DBJsonSetting . library ) in root . items ( ) : <EOL> return root . attrib [ '<STR_LIT:name>' ] , self . _parse_xml_lib ( root ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> root . attrib [ '<STR_LIT:name>' ] ) <EOL> ) <EOL> def _parse_xml_lib ( self , root ) : <EOL> kws = { } <EOL> for element in root . findall ( '<STR_LIT>' ) : <EOL> kw = { } <EOL> kw [ DBJsonSetting . keyword_file ] = None <EOL> kw [ DBJsonSetting . keyword_name ] = element . attrib [ '<STR_LIT:name>' ] <EOL> kw [ DBJsonSetting . documentation ] = element . find ( '<STR_LIT>' ) . text <EOL> tags = [ ] <EOL> [ tags . append ( tag . text ) for tag in element . findall ( '<STR_LIT>' ) ] <EOL> kw [ DBJsonSetting . tags ] = tags <EOL> arg = [ ] <EOL> [ arg . append ( tag . text ) for tag in element . findall ( '<STR_LIT>' ) ] <EOL> kw [ DBJsonSetting . keyword_arguments ] = arg <EOL> kws [ strip_and_lower ( kw [ DBJsonSetting . keyword_name ] ) ] = kw <EOL> return kws <EOL> def _parse_robot_data ( self , file_path , model ) : <EOL> data = { } <EOL> data [ DBJsonSetting . file_name ] = path . basename ( file_path ) <EOL> data [ DBJsonSetting . file_path ] = normalise_path ( file_path ) <EOL> data [ DBJsonSetting . keywords ] = self . _get_keywords ( model ) <EOL> data [ DBJsonSetting . variables ] = self . _get_global_variables ( model ) <EOL> lib , res , v_files = self . _get_imports ( <EOL> model , <EOL> path . dirname ( normalise_path ( file_path ) ) , <EOL> file_path <EOL> ) <EOL> data [ DBJsonSetting . resources ] = res <EOL> data [ DBJsonSetting . libraries ] = lib <EOL> data [ DBJsonSetting . variable_files ] = v_files <EOL> return data <EOL> def _get_keywords ( self , model ) : <EOL> kw_data = { } <EOL> for kw in model . keywords : <EOL> tmp = { } <EOL> tmp [ DBJsonSetting . keyword_arguments ] = kw . args . value <EOL> tmp [ DBJsonSetting . documentation ] = kw . doc . value <EOL> tmp [ DBJsonSetting . tags ] = kw . tags . value <EOL> tmp [ DBJsonSetting . keyword_name ] = kw . name <EOL> kw_data [ strip_and_lower ( kw . name ) ] = tmp <EOL> return kw_data <EOL> def _get_imports ( self , model , file_dir , file_path ) : <EOL> lib = [ ] <EOL> res = [ ] <EOL> var_files = [ ] <EOL> for setting in model . setting_table . imports : <EOL> if setting . type == '<STR_LIT>' : <EOL> lib . append ( self . _format_library ( setting , file_dir ) ) <EOL> elif setting . type == '<STR_LIT>' : <EOL> res . append ( self . _format_resource ( setting , file_path ) ) <EOL> elif setting . type == '<STR_LIT>' : <EOL> var_files . append ( self . _format_variable_file ( setting ) ) <EOL> return lib , res , var_files <EOL> def _format_library ( self , setting , file_dir ) : <EOL> data = { } <EOL> lib_name = setting . name <EOL> if lib_name . endswith ( '<STR_LIT>' ) and not path . isfile ( lib_name ) : <EOL> lib_path = path . abspath ( path . join ( file_dir , lib_name ) ) <EOL> lib_name = path . basename ( lib_path ) <EOL> elif lib_name . endswith ( '<STR_LIT>' ) and path . isfile ( lib_name ) : <EOL> lib_path = normalise_path ( lib_name ) <EOL> lib_name = path . basename ( lib_name ) <EOL> else : <EOL> lib_path = None <EOL> data [ DBJsonSetting . library_name ] = lib_name <EOL> data [ DBJsonSetting . library_alias ] = setting . alias <EOL> data [ DBJsonSetting . library_arguments ] = setting . args <EOL> data [ DBJsonSetting . library_path ] = lib_path <EOL> return data <EOL> def _format_resource ( self , setting , file_path ) : <EOL> if path . isabs ( setting . name ) : <EOL> return setting . name <EOL> else : <EOL> c_dir = path . dirname ( self . file_path ) <EOL> resource_path = normalise_path ( path . join ( c_dir , setting . name ) ) <EOL> if not path . isfile ( resource_path ) : <EOL> print ( '<STR_LIT>' . format ( file_path ) , <EOL> '<STR_LIT>' . format ( setting . name ) ) <EOL> return resource_path <EOL> def _format_variable_file ( self , setting ) : <EOL> data = { } <EOL> v_path = normalise_path ( path . join ( <EOL> path . dirname ( self . file_path ) , setting . name ) ) <EOL> args = { } <EOL> args [ '<STR_LIT>' ] = setting . args <EOL> data [ v_path ] = args <EOL> return data <EOL> def _get_global_variables ( self , model ) : <EOL> var_data = [ ] <EOL> for var in model . variable_table . variables : <EOL> if var : <EOL> var_data . append ( var . name ) <EOL> return var_data </s>
