Skip to content

Commit 0559e5c

Browse files
committed
Expose kws as attributes both using orig and custom names.
Fixes #2.
1 parent 404e6cd commit 0559e5c

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

atest/librarycomponents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def method(self):
1616
return 2
1717

1818
@keyword('Custom name')
19-
def _whatever(self):
19+
def _custom_name(self):
2020
return 3
2121

2222
def not_keyword(self):

src/robotlibcore.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,20 @@ class HybridCore(object):
4444

4545
def __init__(self, library_components):
4646
self.keywords = {}
47+
self.attributes = {}
4748
self.add_library_components(library_components)
4849
self.add_library_components([self])
4950

5051
def add_library_components(self, library_components):
5152
for component in library_components:
5253
for name, func in self._get_members(component):
5354
if callable(func) and hasattr(func, 'robot_name'):
55+
kw = getattr(component, name)
5456
kw_name = func.robot_name or name
55-
self.keywords[kw_name] = getattr(component, name)
57+
self.keywords[kw_name] = kw
58+
# Expose keywords as attributes both using original
59+
# method names as well as possible custom names.
60+
self.attributes[name] = self.attributes[kw_name] = kw
5661

5762
def _get_members(self, component):
5863
if inspect.ismodule(component):
@@ -64,18 +69,18 @@ def _get_members(self, component):
6469
raise TypeError('Libraries must be modules or new-style class '
6570
'instances, got old-style class {!r} instead.'
6671
.format(component.__class__.__name__))
67-
return self._get_members_from_instannce(component)
72+
return self._get_members_from_instance(component)
6873

69-
def _get_members_from_instannce(self, instance):
74+
def _get_members_from_instance(self, instance):
7075
# Avoid calling properties by getting members from class, not instance.
7176
cls = type(instance)
7277
for name in dir(instance):
7378
owner = cls if hasattr(cls, name) else instance
7479
yield name, getattr(owner, name)
7580

7681
def __getattr__(self, name):
77-
if name in self.keywords:
78-
return self.keywords[name]
82+
if name in self.attributes:
83+
return self.attributes[name]
7984
raise AttributeError('{!r} object has no attribute {!r}'
8085
.format(type(self).__name__, name))
8186

@@ -84,7 +89,7 @@ def __dir__(self):
8489
my_attrs = dir(type(self)) + list(self.__dict__)
8590
else:
8691
my_attrs = super().__dir__()
87-
return sorted(set(my_attrs + list(self.keywords)))
92+
return sorted(set(my_attrs) | set(self.attributes))
8893

8994
def get_keyword_names(self):
9095
return sorted(self.keywords)

utest/test_robotlibcore.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ def test_keyword_names():
2929
def test_dir():
3030
expected = ['Custom name',
3131
'Embedded arguments "${here}"',
32+
'_custom_name',
33+
'_get_arg_spec',
34+
'_get_keyword_tags_supported',
35+
'_get_members',
36+
'_get_members_from_instance',
3237
'add_library_components',
3338
'all_arguments',
39+
'attributes',
3440
'class_attribute',
3541
'defaults',
3642
'doc_and_tags',
43+
'embedded',
3744
'function',
3845
'get_keyword_arguments',
3946
'get_keyword_documentation',
@@ -50,12 +57,14 @@ def test_dir():
5057
'run_keyword',
5158
'tags',
5259
'varargs_and_kwargs']
53-
assert [a for a in dir(DynamicLibrary()) if a[0] != '_'] == expected
54-
expected = [e for e in expected if e not in ('get_keyword_arguments',
60+
assert [a for a in dir(DynamicLibrary()) if a[:2] != '__'] == expected
61+
expected = [e for e in expected if e not in ('_get_arg_spec',
62+
'_get_keyword_tags_supported',
63+
'get_keyword_arguments',
5564
'get_keyword_documentation',
5665
'get_keyword_tags',
5766
'run_keyword')]
58-
assert [a for a in dir(HybridLibrary()) if a[0] != '_'] == expected
67+
assert [a for a in dir(HybridLibrary()) if a[:2] != '__'] == expected
5968

6069

6170
def test_getattr():
@@ -64,11 +73,12 @@ def test_getattr():
6473
assert lib.instance_attribute == 'not keyword'
6574
assert lib.function() == 1
6675
assert lib.method() == 2
76+
assert lib._custom_name() == 3
6777
assert getattr(lib, 'Custom name')() == 3
6878
with pytest.raises(AttributeError) as exc_info:
69-
lib.attribute
79+
lib.non_existing
7080
assert str(exc_info.value) == \
71-
"'%s' object has no attribute 'attribute'" % type(lib).__name__
81+
"'%s' object has no attribute 'non_existing'" % type(lib).__name__
7282

7383

7484
def test_get_keyword_arguments():

0 commit comments

Comments
 (0)