Skip to content

Commit 64855f3

Browse files
committed
Support for get_keyword_source
1 parent 2769575 commit 64855f3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/robotlibcore.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"""
2121

2222
import inspect
23+
import os
2324
import sys
2425
try:
2526
import typing
@@ -174,6 +175,25 @@ def __join_defaults_with_types(self, method, types):
174175
types[name] = type(value)
175176
return types
176177

178+
def get_keyword_source(self, keyword_name):
179+
method = self.__get_keyword(keyword_name)
180+
try:
181+
path = os.path.normpath(inspect.getfile(method))
182+
except TypeError:
183+
path = ''
184+
nro = self.__get_keyword_line(method)
185+
return '%s:%s' % (path, nro)
186+
187+
def __get_keyword_line(self, method):
188+
try:
189+
source, nro = inspect.getsourcelines(method)
190+
except OSError:
191+
return ''
192+
for line in source:
193+
if 'def' in line:
194+
return nro
195+
nro += 1
196+
177197

178198
class StaticCore(HybridCore):
179199

utest/test_get_keyword_source.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from os import path
2+
3+
import pytest
4+
from DynamicLibrary import DynamicLibrary
5+
6+
7+
@pytest.fixture(scope='module')
8+
def lib():
9+
return DynamicLibrary()
10+
11+
12+
@pytest.fixture(scope='module')
13+
def lib_path():
14+
cur_dir = path.dirname(__file__)
15+
return path.normpath(path.join(cur_dir, '..', 'atest', 'DynamicLibrary.py'))
16+
17+
18+
@pytest.fixture(scope='module')
19+
def lib_path_components():
20+
cur_dir = path.dirname(__file__)
21+
return path.normpath(path.join(cur_dir, '..', 'atest', 'librarycomponents.py'))
22+
23+
24+
def test_location_in_main(lib, lib_path):
25+
source = lib.get_keyword_source('keyword_in_main')
26+
assert source == '%s:20' % lib_path
27+
28+
29+
def test_location_in_class(lib, lib_path_components):
30+
source = lib.get_keyword_source('method')
31+
assert source == '%s:15' % lib_path_components
32+
33+
34+
def test_location_in_class_custom_keyword_name(lib, lib_path_components):
35+
source = lib.get_keyword_source('Custom name')
36+
assert source == '%s:19' % lib_path_components

utest/test_robotlibcore.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_dir():
3131
'Embedded arguments "${here}"',
3232
'_DynamicCore__get_arg_spec',
3333
'_DynamicCore__get_keyword',
34+
'_DynamicCore__get_keyword_line',
3435
'_DynamicCore__get_keyword_tags_supported',
3536
'_DynamicCore__get_typing_hints',
3637
'_DynamicCore__join_defaults_with_types',
@@ -48,6 +49,7 @@ def test_dir():
4849
'get_keyword_arguments',
4950
'get_keyword_documentation',
5051
'get_keyword_names',
52+
'get_keyword_source',
5153
'get_keyword_tags',
5254
'get_keyword_types',
5355
'instance_attribute',
@@ -65,10 +67,12 @@ def test_dir():
6567
expected = [e for e in expected if e not in ('_DynamicCore__get_typing_hints',
6668
'_DynamicCore__get_arg_spec',
6769
'_DynamicCore__get_keyword',
70+
'_DynamicCore__get_keyword_line',
6871
'_DynamicCore__get_keyword_tags_supported',
6972
'_DynamicCore__join_defaults_with_types',
7073
'get_keyword_arguments',
7174
'get_keyword_documentation',
75+
'get_keyword_source',
7276
'get_keyword_tags',
7377
'run_keyword',
7478
'get_keyword_types')]

0 commit comments

Comments
 (0)