forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsoleviewer.py
More file actions
executable file
·109 lines (86 loc) · 3.59 KB
/
Copy pathconsoleviewer.py
File metadata and controls
executable file
·109 lines (86 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import textwrap
from robot.utils import MultiMatcher, console_encode
from robot.errors import DataError
class ConsoleViewer(object):
def __init__(self, libdoc):
self._libdoc = libdoc
self._keywords = KeywordMatcher(libdoc)
@classmethod
def handles(cls, command):
return command.lower() in ['list', 'show', 'version']
@classmethod
def validate_command(cls, command, args):
if not cls.handles(command):
raise DataError("Unknown command '%s'." % command)
if command.lower() == 'version' and args:
raise DataError("Command 'version' does not take arguments.")
def view(self, command, *args):
self.validate_command(command, args)
getattr(self, command.lower())(*args)
def list(self, *patterns):
for kw in self._keywords.search('*%s*' % p for p in patterns):
self._console(kw.name)
def show(self, *names):
if MultiMatcher(names, match_if_no_patterns=True).match('intro'):
self._show_intro(self._libdoc)
if self._libdoc.inits:
self._show_inits(self._libdoc)
for kw in self._keywords.search(names):
self._show_keyword(kw)
def version(self):
self._console(self._libdoc.version or 'N/A')
def _console(self, msg):
print(console_encode(msg))
def _show_intro(self, lib):
self._header(lib.name, underline='=')
named_args = 'supported' if lib.named_args else 'not supported'
self._data([('Version', lib.version), ('Scope', lib.scope),
('Named arguments', named_args)])
self._doc(lib.doc)
def _show_inits(self, lib):
self._header('Importing', underline='-')
for init in lib.inits:
self._show_keyword(init, show_name=False)
def _show_keyword(self, kw, show_name=True):
if show_name:
self._header(kw.name, underline='-')
self._data([('Arguments', '[%s]' % ', '.join(kw.args))])
self._doc(kw.doc)
def _header(self, name, underline):
self._console('%s\n%s' % (name, underline * len(name)))
def _data(self, items):
ljust = max(len(name) for name, _ in items) + 3
for name, value in items:
if value:
text = '%s%s' % ((name+':').ljust(ljust), value)
self._console(self._wrap(text, subsequent_indent=' '*ljust))
def _doc(self, doc):
self._console('')
for line in doc.splitlines():
self._console(self._wrap(line))
if doc:
self._console('')
def _wrap(self, text, width=78, **config):
return '\n'.join(textwrap.wrap(text, width=width, **config))
class KeywordMatcher(object):
def __init__(self, libdoc):
self._keywords = libdoc.keywords
def search(self, patterns):
matcher = MultiMatcher(patterns, match_if_no_patterns=True)
for kw in self._keywords:
if matcher.match(kw.name):
yield kw