Robot Framework Integrated Development Environment (RIDE)
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 robotide.lib.robot.utils import MultiMatcher, console_encode
19 from robotide.lib.robot.errors import DataError
20 
21 
22 class ConsoleViewer():
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  named_args = 'supported' if lib.named_args else 'not supported'
64  self._data_data([('Version', lib.version), ('Scope', lib.scope),
65  ('Named arguments', named_args)])
66  self._doc_doc(lib.doc)
67 
68  def _show_inits(self, lib):
69  self._header_header('Importing', underline='-')
70  for init in lib.inits:
71  self._show_keyword_show_keyword(init, show_name=False)
72 
73  def _show_keyword(self, kw, show_name=True):
74  if show_name:
75  self._header_header(kw.name, underline='-')
76  self._data_data([('Arguments', '[%s]' % ', '.join(kw.args))])
77  self._doc_doc(kw.doc)
78 
79  def _header(self, name, underline):
80  self._console_console('%s\n%s' % (name, underline * len(name)))
81 
82  def _data(self, items):
83  ljust = max(len(name) for name, _ in items) + 3
84  for name, value in items:
85  if value:
86  text = '%s%s' % ((name+':').ljust(ljust), value)
87  self._console_console(self._wrap_wrap(text, subsequent_indent=' '*ljust))
88 
89  def _doc(self, doc):
90  self._console_console('')
91  for line in doc.splitlines():
92  self._console_console(self._wrap_wrap(line))
93  if doc:
94  self._console_console('')
95 
96  def _wrap(self, text, width=78, **config):
97  return '\n'.join(textwrap.wrap(text, width=width, **config))
98 
99 
101 
102  def __init__(self, libdoc):
103  self._keywords_keywords = libdoc.keywords
104 
105  def search(self, patterns):
106  matcher = MultiMatcher(patterns, match_if_no_patterns=True)
107  for kw in self._keywords_keywords:
108  if matcher.match(kw.name):
109  yield kw
Used when variable does not exist.
Definition: errors.py:67
def console_encode(string, errors='replace', stream=sys.__stdout__)
Encodes Unicode to bytes in console or system encoding.
Definition: encoding.py:62