def __init__(self, table_dir, index_dir, open_tab, rf_extension): self.table_dir = table_dir self.index_dir = index_dir self.open_tab = open_tab self.rf_extension = rf_extension self.get_doc = GetKeywordDocumentation(table_dir=table_dir, index_dir=index_dir, open_tab=open_tab)
def test_get_table_name_from_index_with_embedding_arg_kw(self): _get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file) kw_details = _get_doc.get_table_name_from_index( '', 'Embedding arg To Keyword Name') self.assertEqual(kw_details.table_name, self.resource_b_table_name) self.assertEqual(kw_details.kw, 'Embedding ${arg} To Keyword Name') self.assertEqual(kw_details.kw_object_name, 'resource_b')
def test_embedding_arg_kw_doc(self): _get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file) doc = _get_doc.return_documentation('', 'Embedding arg To Keyword Name') self.assertEqual(doc, 'Keyword with embedding arg to keyword name') kw = ('Keyword Which Also Has Really Long Name But Not As' ' Long The Class Name By 1234 In Keyword') doc = _get_doc.return_documentation('OtherNameLib', kw) self.assertEqual(doc, 'Documentation is here')
def test_get_table_name_from_index_with_embedding_arg_kw(self): _get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file ) kw_details = _get_doc.get_table_name_from_index( '', 'Embedding arg To Keyword Name' ) self.assertEqual(kw_details.table_name, self.resource_b_table_name) self.assertEqual(kw_details.kw, 'Embedding ${arg} To Keyword Name') self.assertEqual(kw_details.kw_object_name, 'resource_b')
def test_get_table_name_from_index_with_test_b(self): _get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file) cell = ('Keyword Which Also Has Really Long Name But ' 'Not As Long The Class Name By 1234 In Keyword') object_name = 'OtherNameLib' kw_details = _get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(kw_details.table_name, self.othernamelib_table_name) self.assertEqual(kw_details.kw, cell.replace('1234', '${argument}')) self.assertEqual( kw_details.kw_object_name, ('LibraryNameWhichIsLongerThan100CharactersButItSeemsThatIt' 'RequiresQuiteAlotLettersInTheFileName' 'AndIsNotGoodRealLifeExample'))
def setUp(self): self.get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_a_file, rf_extension=self.rf_extension )
def __init__(self, table_dir, index_dir, open_tab, rf_extension): self.table_dir = table_dir self.index_dir = index_dir self.open_tab = open_tab self.rf_extension = rf_extension self.get_doc = GetKeywordDocumentation( table_dir=table_dir, index_dir=index_dir, open_tab=open_tab )
def test_embedding_arg_kw_doc(self): _get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file ) doc = _get_doc.return_documentation( '', 'Embedding arg To Keyword Name' ) self.assertEqual(doc, 'Keyword with embedding arg to keyword name') kw = ( 'Keyword Which Also Has Really Long Name But Not As' ' Long The Class Name By 1234 In Keyword' ) doc = _get_doc.return_documentation( 'OtherNameLib', kw ) self.assertEqual(doc, 'Documentation is here')
def test_get_table_name_from_index_with_test_b(self): _get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file ) cell = ( 'Keyword Which Also Has Really Long Name But ' 'Not As Long The Class Name By 1234 In Keyword' ) object_name = 'OtherNameLib' kw_details = _get_doc.get_table_name_from_index( object_name, cell ) self.assertEqual(kw_details.table_name, self.othernamelib_table_name) self.assertEqual(kw_details.kw, cell.replace('1234', '${argument}')) self.assertEqual( kw_details.kw_object_name, ( 'LibraryNameWhichIsLongerThan100CharactersButItSeemsThatIt' 'RequiresQuiteAlotLettersInTheFileName' 'AndIsNotGoodRealLifeExample' ) )
class GetKeyword(object): """Returns details to locate the file and keyword from the file Returns the real path to the file where the keyword is located. Also returns the regex patters which can be used to search the file from the documentation. """ emebeded_re = '\\$\\{.+\\}' def __init__(self, table_dir, index_dir, open_tab, rf_extension): self.table_dir = table_dir self.index_dir = index_dir self.open_tab = open_tab self.rf_extension = rf_extension self.get_doc = GetKeywordDocumentation(table_dir=table_dir, index_dir=index_dir, open_tab=open_tab) def return_file_and_patter(self, object_name, keyword): """Returns regex and filename patter to find keyword from the source ``keyword`` -- Keyword documentation to search from database. ``object_name`` -- Library or resource object name. Searches the file path where the keyword is located and forms the regex patter which can be used to search the keyword from the file. Can be used to get the keyword from the robot data or from the Python libraries. """ regex = None file_path = None kw_details = self.get_doc.get_table_name_from_index( object_name, keyword) if not kw_details.table_name: return regex, file_path table_path = path.join(self.table_dir, kw_details.table_name) data = get_data_from_json(table_path) if DBJsonSetting.file_path in data: file_path_table = data[DBJsonSetting.file_path] else: file_path_table = None if self.rf_data(file_path_table): regex = self.get_regex_resource(kw_details.kw) file_path = file_path_table return regex, file_path else: return self.get_lib_keyword(table_path, kw_details.kw_object_name, kw_details.kw) def get_lib_keyword(self, table_path, object_name, keyword): regex = self.get_regex_library(keyword) file_path = self.get_lib_keyword_file(table_path, object_name, keyword) return regex, file_path def get_lib_keyword_file(self, table_path, object_name, keyword): """Returns file path from db where library keyword is defined""" data = get_data_from_json(table_path) table_keywords = data[DBJsonSetting.keywords] table_kw_object = data[DBJsonSetting.library_module] for table_kw_data in table_keywords: if kw_equals_kw_candite(keyword, table_kw_data): if not object_name or object_name == table_kw_object: return table_keywords[table_kw_data][ DBJsonSetting.keyword_file] def get_regex_library(self, keyword): """Returns the regex patters for library keywords""" if re.search(self.emebeded_re, keyword): return self._get_regex_lib_embedded(keyword) else: return self._get_regex_lib_no_embedded(keyword) def _get_regex_lib_embedded(self, keyword): """returns regex for lib keyword with embedded args""" regex_from_deco = r'(?i)(\@keyword.+name=[\'"]' words = self.split_kw_to_words(keyword) for word in words: if re.search(self.emebeded_re, word): regex_from_deco = '{0}{1}[_ ]?'.format(regex_from_deco, self.emebeded_re) else: regex_from_deco = '{0}{1}[_ ]?'.format(regex_from_deco, word.lower()) return '{0})'.format(regex_from_deco.rstrip('[_ ]?')) def _get_regex_lib_no_embedded(self, keyword): """returns regex for lib keyword with no embedded args""" words = self.split_kw_to_words(keyword) regex_from_func = '(?im)(def ' regex_from_deco = r'(\@keyword.+name=[\'"]' for word in words: regex_from_func = '{0}{1}_?'.format(regex_from_func, word.lower()) regex_from_deco = '{0}{1}[_ ]'.format(regex_from_deco, word.lower()) regex_from_func = regex_from_func.rstrip('_?') regex_from_deco = regex_from_deco.rstrip('[_ ]') return r'{0}\()|{1})'.format(regex_from_func, regex_from_deco) def get_regex_resource(self, keyword): """Returns the regex for user defined keywords""" words = self.split_kw_to_words(keyword) regex_patter = '(?im)^' for word in words: if re.search(self.emebeded_re, word): regex_patter = '{0}{1}[_ ]?'.format(regex_patter, self.emebeded_re) else: regex_patter = '{0}{1}[_ ]?'.format(regex_patter, word.lower()) regex_patter = regex_patter.rstrip('[_ ]?') regex_patter = '{0}$'.format(regex_patter) return regex_patter def split_kw_to_words(self, keyword): words = [] # Like: LOG if keyword.isupper() and '_' not in keyword and ' ' not in keyword: words.append(keyword) # Like: RunKeyword elif '_' not in keyword and ' ' not in keyword: words = re.findall('[a-zA-Z0-9][^A-Z0-9]*', keyword) # The rest, like: Run Keyword or run keyword else: [words.extend(word.split('_')) for word in keyword.split(' ')] return words def rf_data(self, file_path): """Returns True if open tab is Robot Framework resource or suite""" if self.is_string(file_path): return file_path.endswith(self.rf_extension) else: return None def is_string(self, str_): if version_info.major > 2: status = isinstance(str_, str) else: status = isinstance(str_, basestring) return status
def setUp(self): self.get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_a_file)
class GetDocumentation(unittest.TestCase): @classmethod def setUpClass(cls): cls.rf_extension = 'robot' db_base = path.join(env.RESULTS_DIR, 'database_in_package_dir') cls.db_dir = path.join(db_base, 'db_dir') cls.index_dir = path.join( db_base, 'index_dir', ) cls.suite_dir = path.join(env.TEST_DATA_DIR, 'suite_tree') if path.exists(db_base): shutil.rmtree(db_base) makedirs(cls.db_dir) makedirs(cls.index_dir) scanner = Scanner() scanner.scan(cls.suite_dir, cls.rf_extension, cls.db_dir) index_all(cls.db_dir, cls.index_dir) def setUp(self): self.get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_a_file) def test_kw_only(self): cell = 'No Operation' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Does absolutely nothing.') cell = 'Log' doc = self.get_doc.return_documentation(object_name, cell) expected = 'Logs the given message with' self.assertTrue(doc.startswith(expected)) def test_user_kw_with_module(self): object_name = 'test_a' cell = 'Test A Keyword' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Some Doc Here') def test_error_conditions(self): cell = '${TEST_A}' object_name = None doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = '' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'Not Here' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'No Operation' object_name = 'NotBuiltInModule' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_module_name_not_correct(self): cell = 'No Operation' object_name = '_BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'builtin' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'BUILTIN' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_get_table_name_from_index(self): cell = 'No Operation' object_name = 'BuiltIn' kw_details = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(kw_details.table_name, self.builtin_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, object_name) cell = 'Test A Keyword' object_name = None kw_details = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(kw_details.table_name, self.test_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, 'test_a') object_name = '' kw_details = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(kw_details.table_name, self.test_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, 'test_a') object_name = 'test_a' kw_details = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(kw_details.table_name, self.test_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, object_name) cell = 'Resource A Keyword 1' object_name = None kw_details = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(kw_details.table_name, self.resource_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, 'resource_a') def test_get_table_name_from_index_with_test_b(self): _get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file) cell = ('Keyword Which Also Has Really Long Name But ' 'Not As Long The Class Name By 1234 In Keyword') object_name = 'OtherNameLib' kw_details = _get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(kw_details.table_name, self.othernamelib_table_name) self.assertEqual(kw_details.kw, cell.replace('1234', '${argument}')) self.assertEqual( kw_details.kw_object_name, ('LibraryNameWhichIsLongerThan100CharactersButItSeemsThatIt' 'RequiresQuiteAlotLettersInTheFileName' 'AndIsNotGoodRealLifeExample')) def test_get_keyword_documentation(self): cell = 'No Operation' object_name = 'BuiltIn' expected_doc = 'Does absolutely nothing.' doc = self.get_doc.get_keyword_documentation(self.builtin_table_path, object_name, cell) self.assertEqual(doc, expected_doc) cell = 'Log' expected_doc = 'Logs the given message with' doc = self.get_doc.get_keyword_documentation(self.builtin_table_path, object_name, cell) self.assertTrue(doc.startswith(expected_doc)) def test_embedding_arg_kw_doc(self): _get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file) doc = _get_doc.return_documentation('', 'Embedding arg To Keyword Name') self.assertEqual(doc, 'Keyword with embedding arg to keyword name') kw = ('Keyword Which Also Has Really Long Name But Not As' ' Long The Class Name By 1234 In Keyword') doc = _get_doc.return_documentation('OtherNameLib', kw) self.assertEqual(doc, 'Documentation is here') def test_get_table_name_from_index_with_embedding_arg_kw(self): _get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file) kw_details = _get_doc.get_table_name_from_index( '', 'Embedding arg To Keyword Name') self.assertEqual(kw_details.table_name, self.resource_b_table_name) self.assertEqual(kw_details.kw, 'Embedding ${arg} To Keyword Name') self.assertEqual(kw_details.kw_object_name, 'resource_b') @property def test_a_file(self): return path.normpath(path.join(self.suite_dir, 'test_a.robot')) @property def test_b_file(self): return path.normpath(path.join(self.suite_dir, 'test_b.robot')) @property def resource_a_table_file(self): return path.normpath(path.join(self.suite_dir, 'resource_a.robot')) @property def resource_b_table_file(self): return path.normpath(path.join(self.suite_dir, 'resource_b.robot')) @property def test_a_table_name(self): return rf_table_name(self.test_a_file) @property def resource_a_table_name(self): return rf_table_name(self.resource_a_table_file) @property def resource_b_table_name(self): return rf_table_name(self.resource_b_table_file) @property def builtin_table_name(self): return lib_table_name('BuiltIn') @property def test_a_index_name(self): return 'index-{0}'.format(self.test_a_table_name) @property def builtin_table_path(self): return path.join(self.db_dir, self.builtin_table_name) @property def othernamelib_table_name(self): return lib_table_name( 'LibraryNameWhichIsLongerThan100CharactersButItSeemsThatItRequires' 'QuiteAlotLettersInTheFileNameAndIsNotGoodRealLifeExample')
class GetDocumentation(unittest.TestCase): @classmethod def setUpClass(cls): cls.rf_extension = 'robot' db_base = path.join( env.RESULTS_DIR, 'database_in_package_dir') cls.db_dir = path.join( db_base, 'db_dir' ) cls.index_dir = path.join( db_base, 'index_dir', ) cls.suite_dir = path.join( env.TEST_DATA_DIR, 'suite_tree' ) if path.exists(db_base): shutil.rmtree(db_base) makedirs(cls.db_dir) makedirs(cls.index_dir) scanner = Scanner() scanner.scan( cls.suite_dir, cls.rf_extension, cls.db_dir) index_all(cls.db_dir, cls.index_dir) def setUp(self): self.get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_a_file ) def test_kw_only(self): cell = 'No Operation' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Does absolutely nothing.') cell = 'Log' doc = self.get_doc.return_documentation(object_name, cell) expected = 'Logs the given message with' self.assertTrue(doc.startswith(expected)) def test_user_kw_with_module(self): object_name = 'test_a' cell = 'Test A Keyword' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Some Doc Here') def test_error_conditions(self): cell = '${TEST_A}' object_name = None doc = self.get_doc.return_documentation( object_name, cell) self.assertEqual(doc, None) object_name = '' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'Not Here' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'No Operation' object_name = 'NotBuiltInModule' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_module_name_not_correct(self): cell = 'No Operation' object_name = '_BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'builtin' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'BUILTIN' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_get_table_name_from_index(self): cell = 'No Operation' object_name = 'BuiltIn' kw_details = self.get_doc.get_table_name_from_index( object_name, cell) self.assertEqual(kw_details.table_name, self.builtin_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, object_name) cell = 'Test A Keyword' object_name = None kw_details = self.get_doc.get_table_name_from_index( object_name, cell) self.assertEqual(kw_details.table_name, self.test_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, 'test_a') object_name = '' kw_details = self.get_doc.get_table_name_from_index( object_name, cell ) self.assertEqual(kw_details.table_name, self.test_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, 'test_a') object_name = 'test_a' kw_details = self.get_doc.get_table_name_from_index( object_name, cell) self.assertEqual(kw_details.table_name, self.test_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, object_name) cell = 'Resource A Keyword 1' object_name = None kw_details = self.get_doc.get_table_name_from_index( object_name, cell) self.assertEqual(kw_details.table_name, self.resource_a_table_name) self.assertEqual(kw_details.kw, cell) self.assertEqual(kw_details.kw_object_name, 'resource_a') def test_get_table_name_from_index_with_test_b(self): _get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file ) cell = ( 'Keyword Which Also Has Really Long Name But ' 'Not As Long The Class Name By 1234 In Keyword' ) object_name = 'OtherNameLib' kw_details = _get_doc.get_table_name_from_index( object_name, cell ) self.assertEqual(kw_details.table_name, self.othernamelib_table_name) self.assertEqual(kw_details.kw, cell.replace('1234', '${argument}')) self.assertEqual( kw_details.kw_object_name, ( 'LibraryNameWhichIsLongerThan100CharactersButItSeemsThatIt' 'RequiresQuiteAlotLettersInTheFileName' 'AndIsNotGoodRealLifeExample' ) ) def test_get_keyword_documentation(self): cell = 'No Operation' object_name = 'BuiltIn' expected_doc = 'Does absolutely nothing.' doc = self.get_doc.get_keyword_documentation( self.builtin_table_path, object_name, cell) self.assertEqual(doc, expected_doc) cell = 'Log' expected_doc = 'Logs the given message with' doc = self.get_doc.get_keyword_documentation( self.builtin_table_path, object_name, cell) self.assertTrue(doc.startswith(expected_doc)) def test_embedding_arg_kw_doc(self): _get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file ) doc = _get_doc.return_documentation( '', 'Embedding arg To Keyword Name' ) self.assertEqual(doc, 'Keyword with embedding arg to keyword name') kw = ( 'Keyword Which Also Has Really Long Name But Not As' ' Long The Class Name By 1234 In Keyword' ) doc = _get_doc.return_documentation( 'OtherNameLib', kw ) self.assertEqual(doc, 'Documentation is here') def test_get_table_name_from_index_with_embedding_arg_kw(self): _get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_b_file ) kw_details = _get_doc.get_table_name_from_index( '', 'Embedding arg To Keyword Name' ) self.assertEqual(kw_details.table_name, self.resource_b_table_name) self.assertEqual(kw_details.kw, 'Embedding ${arg} To Keyword Name') self.assertEqual(kw_details.kw_object_name, 'resource_b') @property def test_a_file(self): return path.normcase(path.join(self.suite_dir, 'test_a.robot')) @property def test_b_file(self): return path.normcase(path.join(self.suite_dir, 'test_b.robot')) @property def resource_a_table_file(self): return path.normcase(path.join(self.suite_dir, 'resource_a.robot')) @property def resource_b_table_file(self): return path.normcase(path.join(self.suite_dir, 'resource_b.robot')) @property def test_a_table_name(self): return rf_table_name(self.test_a_file) @property def resource_a_table_name(self): return rf_table_name(self.resource_a_table_file) @property def resource_b_table_name(self): return rf_table_name(self.resource_b_table_file) @property def builtin_table_name(self): return lib_table_name('BuiltIn') @property def test_a_index_name(self): return 'index-{0}'.format(self.test_a_table_name) @property def builtin_table_path(self): return path.join(self.db_dir, self.builtin_table_name) @property def othernamelib_table_name(self): return lib_table_name( 'LibraryNameWhichIsLongerThan100CharactersButItSeemsThatItRequires' 'QuiteAlotLettersInTheFileNameAndIsNotGoodRealLifeExample' )
class GetKeyword(object): """Returns details to locate the file and keyword from the file Returns the real path to the file where the keyword is located. Also returns the regex patters which can be used to search the file from the documentation. """ def __init__(self, table_dir, index_dir, open_tab, rf_extension): self.table_dir = table_dir self.index_dir = index_dir self.open_tab = open_tab self.rf_extension = rf_extension self.get_doc = GetKeywordDocumentation( table_dir=table_dir, index_dir=index_dir, open_tab=open_tab ) def return_file_and_patter(self, object_name, keyword): """Returns regex and filename patter to find keyword from the source ``keyword`` -- Keyword documentation to search from database. ``object_name`` -- Library or resource object name. Searches the file path where the keyword is located and forms the regex patter which can be used to search the keyword from the file. Can be used to get the keyword from the robot data or from the Python libraries. """ regex = None file_path = None table_name = self.get_doc.get_table_name_from_index(object_name, keyword) if not table_name: return regex, file_path table_path = path.join(self.table_dir, table_name) data = get_data_from_json(table_path) if DBJsonSetting.file_path in data: file_path_table = data[DBJsonSetting.file_path] else: file_path_table = None if self.rf_data(file_path_table): regex = self.get_regex_resource(keyword) file_path = file_path_table return regex, file_path else: return self.get_lib_keyword( table_path, object_name, keyword ) def get_lib_keyword(self, table_path, object_name, keyword): regex = self.get_regex_library(keyword) file_path = self.get_lib_keyword_file( table_path, object_name, keyword ) return regex, file_path def get_lib_keyword_file(self, table_path, object_name, keyword): """Returns file path from db where library keyword is defined""" data = get_data_from_json(table_path) table_keywords = data[DBJsonSetting.keywords] table_kw_object = data[DBJsonSetting.library_module] for table_kw_data in table_keywords: if kw_equals_kw_candite(keyword, table_kw_data): if not object_name or object_name == table_kw_object: return table_keywords[table_kw_data][DBJsonSetting.keyword_file] def get_regex_library(self, keyword): """Returns the regex patters for library keywords""" words = self.split_kw_to_words(keyword) regex_from_func = '(?im)(def ' regex_from_deco = r'(\@keyword.+name=[\'"]' for word in words: regex_from_func = '{0}{1}_?'.format(regex_from_func, word.lower()) regex_from_deco = '{0}{1}[_ ]'.format( regex_from_deco, word.lower()) regex_from_func = regex_from_func.rstrip('_?') regex_from_deco = regex_from_deco.rstrip('[_ ]') return r'{0}\()|{1})'.format(regex_from_func, regex_from_deco) def get_regex_resource(self, keyword): """Returns the regex patters for user defined keywords""" words = self.split_kw_to_words(keyword) regex_patter = '(?im)^' for word in words: regex_patter = '{0}{1}[_ ]?'.format(regex_patter, word.lower()) regex_patter = regex_patter.rstrip('[_ ]?') return '{0}$'.format(regex_patter) def split_kw_to_words(self, keyword): words = [] # Like: LOG if keyword.isupper() and '_' not in keyword and ' ' not in keyword: words.append(keyword) # Like: RunKeyword elif '_' not in keyword and ' ' not in keyword: words = re.findall('[a-zA-Z0-9][^A-Z0-9]*', keyword) # The rest, like: Run Keyword or run keyword else: [words.extend(word.split('_')) for word in keyword.split(' ')] return words def rf_data(self, file_path): """Returns True if open tab is Robot Framework resource or suite""" if self.is_string(file_path): return file_path.endswith(self.rf_extension) else: return None def is_string(self, str_): if version_info.major > 2: status = isinstance(str_, str) else: status = isinstance(str_, basestring) return status
class GetDocumentation(unittest.TestCase): @classmethod def setUpClass(cls): cls.rf_extension = 'robot' db_base = path.join(env.RESULTS_DIR, 'database_in_package_dir') cls.db_dir = path.join(db_base, 'db_dir') cls.index_dir = path.join( db_base, 'index_dir', ) cls.suite_dir = path.join(env.TEST_DATA_DIR, 'suite_tree') if path.exists(db_base): shutil.rmtree(db_base) mkdir(db_base) mkdir(cls.db_dir) mkdir(cls.index_dir) scanner = Scanner() scanner.scan(cls.suite_dir, cls.rf_extension, cls.db_dir) index_all(cls.db_dir, cls.index_dir) def setUp(self): self.get_doc = GetKeywordDocumentation(table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_a_file) def test_kw_only(self): cell = 'No Operation' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Does absolutely nothing.') cell = 'Log' doc = self.get_doc.return_documentation(object_name, cell) expected = 'Logs the given message with' self.assertTrue(doc.startswith(expected)) def test_user_kw_with_module(self): object_name = 'test_a' cell = 'Test A Keyword' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Some Doc Here') def test_error_conditions(self): cell = '${TEST_A}' object_name = None doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = '' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'Not Here' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'No Operation' object_name = 'NotBuiltInModule' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_module_name_not_correct(self): cell = 'No Operation' object_name = '_BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'builtin' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'BUILTIN' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_get_table_name_from_index(self): cell = 'No Operation' object_name = 'BuiltIn' table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.builtin_table_name) cell = 'Test A Keyword' object_name = None table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.test_a_table_name) object_name = '' table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.test_a_table_name) object_name = 'test_a' table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.test_a_table_name) cell = 'Resource A Keyword 1' object_name = None table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.resource_a_table_name) def test_get_keyword_documentation(self): cell = 'No Operation' object_name = 'BuiltIn' expected_doc = 'Does absolutely nothing.' doc = self.get_doc.get_keyword_documentation(self.builtin_table_path, object_name, cell) self.assertEqual(doc, expected_doc) cell = 'Log' expected_doc = 'Logs the given message with' doc = self.get_doc.get_keyword_documentation(self.builtin_table_path, object_name, cell) self.assertTrue(doc.startswith(expected_doc)) @property def test_a_file(self): return path.normcase(path.join(self.suite_dir, 'test_a.robot')) @property def resource_a_table_file(self): return path.normcase(path.join(self.suite_dir, 'resource_a.robot')) @property def test_a_table_name(self): return rf_table_name(self.test_a_file) @property def resource_a_table_name(self): return rf_table_name(self.resource_a_table_file) @property def builtin_table_name(self): return lib_table_name('BuiltIn') @property def test_a_index_name(self): return 'index-{0}'.format(self.test_a_table_name) @property def builtin_table_path(self): return path.join(self.db_dir, self.builtin_table_name)
class GetDocumentation(unittest.TestCase): @classmethod def setUpClass(cls): cls.rf_extension = 'robot' db_base = path.join( env.RESULTS_DIR, 'database_in_package_dir') cls.db_dir = path.join( db_base, 'db_dir' ) cls.index_dir = path.join( db_base, 'index_dir', ) cls.suite_dir = path.join( env.TEST_DATA_DIR, 'suite_tree' ) if path.exists(db_base): shutil.rmtree(db_base) mkdir(db_base) mkdir(cls.db_dir) mkdir(cls.index_dir) scanner = Scanner() scanner.scan( cls.suite_dir, cls.rf_extension, cls.db_dir) index_all(cls.db_dir, cls.index_dir) def setUp(self): self.get_doc = GetKeywordDocumentation( table_dir=self.db_dir, index_dir=self.index_dir, open_tab=self.test_a_file, rf_extension=self.rf_extension ) def test_kw_only(self): cell = 'No Operation' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Does absolutely nothing.') cell = 'Log' doc = self.get_doc.return_documentation(object_name, cell) expected = 'Logs the given message with' self.assertTrue(doc.startswith(expected)) def test_user_kw_with_module(self): object_name = 'test_a' cell = 'Test A Keyword' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, 'Some Doc Here') def test_error_conditions(self): cell = '${TEST_A}' object_name = None doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = '' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'Not Here' object_name = 'BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) cell = 'No Operation' object_name = 'NotBuiltInModule' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_module_name_not_correct(self): cell = 'No Operation' object_name = '_BuiltIn' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'builtin' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) object_name = 'BUILTIN' doc = self.get_doc.return_documentation(object_name, cell) self.assertEqual(doc, None) def test_get_table_name_from_index(self): cell = 'No Operation' object_name = 'BuiltIn' table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.builtin_table_name) cell = 'Test A Keyword' object_name = None table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.test_a_table_name) object_name = '' table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.test_a_table_name) object_name = 'test_a' table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.test_a_table_name) cell = 'Resource A Keyword 1' object_name = None table_name = self.get_doc.get_table_name_from_index(object_name, cell) self.assertEqual(table_name, self.resource_a_table_name) def test_get_keyword_documentation(self): cell = 'No Operation' object_name = 'BuiltIn' expected_doc = 'Does absolutely nothing.' doc = self.get_doc.get_keyword_documentation( self.builtin_table_path, object_name, cell) self.assertEqual(doc, expected_doc) cell = 'Log' expected_doc = 'Logs the given message with' doc = self.get_doc.get_keyword_documentation( self.builtin_table_path, object_name, cell) self.assertTrue(doc.startswith(expected_doc)) @property def test_a_file(self): return path.normcase(path.join(self.suite_dir, 'test_a.robot')) @property def resource_a_table_file(self): return path.normcase(path.join(self.suite_dir, 'resource_a.robot')) @property def test_a_table_name(self): return rf_table_name(self.test_a_file) @property def resource_a_table_name(self): return rf_table_name(self.resource_a_table_file) @property def builtin_table_name(self): return lib_table_name('BuiltIn') @property def test_a_index_name(self): return 'index-{0}'.format(self.test_a_table_name) @property def builtin_table_path(self): return path.join(self.db_dir, self.builtin_table_name)