Skip to content

Commit 404e6cd

Browse files
committed
Support adding components after creating library instance.
Main use case is easing creating libraries that extend other libs that use the library core.
1 parent 2a4ecf3 commit 404e6cd

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

atest/ExtendExistingLibrary.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from HybridLibrary import HybridLibrary, keyword
2+
3+
4+
class ExtendExistingLibrary(HybridLibrary):
5+
6+
def __init__(self):
7+
HybridLibrary.__init__(self)
8+
self.add_library_components([ExtendingComponent()])
9+
10+
11+
class ExtendingComponent(object):
12+
13+
@keyword
14+
def keyword_in_extending_library(self):
15+
pass

atest/run.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@
88
from robotstatuschecker import process_output
99

1010

11+
library_variants = ['Hybrid', 'Dynamic', 'Static', 'ExtendExisting']
1112
curdir = dirname(abspath(__file__))
1213
outdir = join(curdir, 'results')
1314
tests = join(curdir, 'tests.robot')
1415
sys.path.insert(0, join(curdir, '..', 'src'))
15-
for variant in ['Hybrid', 'Dynamic', 'Static']:
16+
for variant in library_variants:
1617
output = join(outdir, variant + '.xml')
1718
rc = run(tests, name=variant, variable='LIBRARY:%sLibrary' % variant,
1819
output=output, report=None, log=None)
1920
if rc > 250:
2021
sys.exit(rc)
2122
process_output(output, verbose=False)
2223
print('\nCombining results.')
23-
rc = rebot(join(outdir, 'Hybrid.xml'),
24-
join(outdir, 'Dynamic.xml'),
25-
join(outdir, 'Static.xml'),
26-
name='Acceptance Tests', outputdir=outdir)
24+
rc = rebot(*(join(outdir, variant + '.xml') for variant in library_variants),
25+
**dict(name='Acceptance Tests', outputdir=outdir))
2726
if rc == 0:
2827
print('\nAll tests passed/failed as expected.')
2928
else:

atest/tests.robot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Keyword names
1212
Method
1313
Custom name
1414
Cust omna me
15+
Run Keyword If $LIBRARY == "ExtendExistingLibrary"
16+
... Keyword in extending library
1517

1618
Method without @keyword are not keyowrds
1719
[Documentation] FAIL No keyword with name 'Not keyword' found.

src/robotlibcore.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,34 @@ def decorator(func):
3737

3838
PY2 = sys.version_info < (3,)
3939

40-
__version__ = '1.0rc1'
40+
__version__ = '1.0rc2'
4141

4242

4343
class HybridCore(object):
4444

45-
def __init__(self, libraries):
46-
self.keywords = dict(self._find_keywords(*libraries))
47-
self.keywords.update(self._find_keywords(self))
45+
def __init__(self, library_components):
46+
self.keywords = {}
47+
self.add_library_components(library_components)
48+
self.add_library_components([self])
4849

49-
def _find_keywords(self, *libraries):
50-
for library in libraries:
51-
for name, func in self._get_members(library):
50+
def add_library_components(self, library_components):
51+
for component in library_components:
52+
for name, func in self._get_members(component):
5253
if callable(func) and hasattr(func, 'robot_name'):
5354
kw_name = func.robot_name or name
54-
yield kw_name, getattr(library, name)
55+
self.keywords[kw_name] = getattr(component, name)
5556

56-
def _get_members(self, library):
57-
if inspect.ismodule(library):
58-
return inspect.getmembers(library)
59-
if inspect.isclass(library):
57+
def _get_members(self, component):
58+
if inspect.ismodule(component):
59+
return inspect.getmembers(component)
60+
if inspect.isclass(component):
6061
raise TypeError('Libraries must be modules or instances, got '
61-
'class {!r} instead.'.format(library.__name__))
62-
if type(library) != library.__class__:
62+
'class {!r} instead.'.format(component.__name__))
63+
if type(component) != component.__class__:
6364
raise TypeError('Libraries must be modules or new-style class '
6465
'instances, got old-style class {!r} instead.'
65-
.format(library.__class__.__name__))
66-
return self._get_members_from_instannce(library)
66+
.format(component.__class__.__name__))
67+
return self._get_members_from_instannce(component)
6768

6869
def _get_members_from_instannce(self, instance):
6970
# Avoid calling properties by getting members from class, not instance.

utest/test_robotlibcore.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_keyword_names():
2929
def test_dir():
3030
expected = ['Custom name',
3131
'Embedded arguments "${here}"',
32+
'add_library_components',
3233
'all_arguments',
3334
'class_attribute',
3435
'defaults',

0 commit comments

Comments
 (0)