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