def recognize_file_types(self): """ Pre-parse files to recognize their types. If the filename is `__init__.*`, the type is `INIT`. Files with .resource extension are `RESOURCE` type. If the file is imported somewhere then file type is `RESOURCE`. Otherwise file type is `GENERAL`. These types are important since they are used to define parsing class for robot API. """ files = self.config.paths file_type_checker = FileTypeChecker(self.config.exec_dir) for file in self.get_files(files, self.config.recursive): if '__init__' in file.name: file_type = FileType.INIT elif file.suffix.lower() == '.resource': file_type = FileType.RESOURCE else: file_type = FileType.GENERAL file_type_checker.source = file try: model = file_type.get_parser()(str(file)) file_type_checker.visit(model) self.files[file] = (file_type, model) except DataError: print( f"Failed to decode {file}. Default supported encoding by Robot Framework is UTF-8. Skipping file" ) for resource in file_type_checker.resource_files: if resource in self.files and self.files[resource][ 0].value != FileType.RESOURCE: self.files[resource] = (FileType.RESOURCE, get_resource_model(str(resource)))
def get_ast(self): if not self._generate_ast: raise AssertionError( "The AST can only be accessed in the RobotFrameworkServerApi, not in the RobotFrameworkLanguageServer." ) from robot.api import get_model, get_resource_model, get_init_model try: source = self.source except: log.exception("Error getting source for: %s" % (self.uri,)) source = "" t = self.get_type() if t == self.TYPE_TEST_CASE: return get_model(source) elif t == self.TYPE_RESOURCE: return get_resource_model(source) elif t == self.TYPE_INIT: return get_init_model(source) else: log.critical("Unrecognized section: %s", t) return get_model(source)
def get_ast(self): if not self._generate_ast: raise AssertionError( "The AST can only be accessed in the RobotFrameworkServerApi, not in the RobotFrameworkLanguageServer." ) from robot.api import get_model, get_resource_model, get_init_model # noqa try: source = self.source except: log.exception("Error getting source for: %s" % (self.uri,)) source = "" try: t = self.get_type() if t == self.TYPE_TEST_CASE: return get_model(source) elif t == self.TYPE_RESOURCE: return get_resource_model(source) elif t == self.TYPE_INIT: return get_init_model(source) else: log.critical("Unrecognized section: %s", t) return get_model(source) except: log.critical(f"Error parsing {self.uri}") # Note: we always want to return a valid AST here (the # AST itself should have the error). model = get_model(f"*** Unable to parse: {self.uri} ***") return model
def get_project_stats_3_2_2(source): """Project stats for RF 3.2.2 API""" proj_data = [] for subdir, dirs, files in os.walk(source): for filename in files: filepath = subdir + os.sep + filename if filepath.endswith(".resource"): resource_model = get_resource_model(filepath) kw_section = [ section for section in resource_model.sections if section.__class__.__name__ == 'KeywordSection' ] proj_data.append({ 'Source': filepath, 'File Name': filename, 'Keywords': len(kw_section[0].body) if kw_section else 0, 'Test Cases': 0 }) if filepath.endswith(".robot"): suite_model = get_model(filepath) kw_section = [ section for section in suite_model.sections if section.__class__.__name__ == 'KeywordSection' ] test_section = [ section for section in suite_model.sections if section.__class__.__name__ == 'TestCaseSection' ] proj_data.append({ 'Source': filepath, 'File Name': filename, 'Keywords': len(kw_section[0].body) if kw_section else 0, 'Test Cases': len(test_section[0].body) if test_section else 0 }) return proj_data