Robot Framework Integrated Development Environment (RIDE)
iteminfo.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 os
17 from functools import total_ordering
18 
19 from .. import utils
20 from ..lib.robot.libdocpkg.htmlwriter import DocToHtml
21 
22 
23 
24 class ItemInfo():
25 
26 
38  def __init__(self, name, source, details):
39  self.namename = name
40  self.sourcesource = source
41  if details is not None:
42  self.detailsdetails = details
43  self._priority_priority = PRIORITIES.get(self.__class__, PRIORITIES[ItemInfo])
44 
45  @property
46  longname = property
47 
48  def longname(self):
49  return '%s.%s' % (self.sourcesource, self.namename)
50 
51  def name_begins_with(self, prefix):
52  return utils.normalize(self.namename).startswith(prefix)
53 
54  def longname_begins_with(self, prefix):
55  return utils.normalize(self.longnamelongnamelongname).startswith(prefix)
56 
57  def is_library_keyword(self):
58  return False
59 
60  def is_user_keyword(self):
61  return not self.is_library_keywordis_library_keyword()
62 
63  @staticmethod
64  def m_cmp(a, b):
65  return (a > b) - (a < b)
66 
67  def __cmp__(self, other):
68  if self._priority_priority == other._priority:
69  name_cmp = self.m_cmpm_cmp(self.namename.upper(),other.name.upper())
70  return name_cmp if name_cmp else self.m_cmpm_cmp(self.sourcesource, other.source)
71  return self.m_cmpm_cmp(self._priority_priority, other._priority)
72 
73  def __eq__(self, other):
74  return not self.__cmp____cmp__(other) if isinstance(other, ItemInfo) else False
75 
76  def __hash__(self):
77  return hash((self.namename, self.sourcesource))
78 
79 
80 @total_ordering
82 
83  def __init__(self, name, value, source):
84  ItemInfo.__init__(self, name, self._source_name_source_name(source), None)
85  self._original_source_original_source = source
86  self._value_value = value
87 
88  def _source_name(self, source):
89  return os.path.basename(source) if source else ''
90 
91  def name_matches(self, pattern):
92  normalized = utils.normalize(self._undecorate_undecorate(pattern))
93  return utils.normalize(self.namename[2:-1]).startswith(normalized)
94 
95  def _undecorate(self, pattern):
96  def get_prefix_length():
97  if pattern[0] not in ['$', '@', '&']:
98  return 0
99  elif len(pattern) > 1 and pattern[1] == '{':
100  return 2
101  else:
102  return 1
103  if not pattern:
104  return pattern
105  without_prefix = pattern[get_prefix_length():]
106  if pattern[-1] == '}':
107  return without_prefix[:-1]
108  else:
109  return without_prefix
110 
111  @property
112  details = property
113 
114  def details(self):
115  value = self._value_value
116  if self.namename.startswith('@'):
117  if value is None:
118  value = '[ ]'
119  return ('<table>'
120  '<tr><td><i>Name:</i></td><td>%s</td></tr>'
121  '<tr><td><i>Source:</i></td><td>%s</td></tr>'
122  '<tr><td valign=top><i>Value:</i></td><td>%s</td></tr>'
123  '</table>') % (self.namename, self._original_source_original_source, str(value))
124 
125  def __eq__(self, other):
126  return self.namename.lower() == other.name.lower()
127 
128  def __hash__(self):
129  return hash(repr(self))
130 
131  def __lt__(self, other):
132  return self.namename.lower() < other.name.lower()
133 
134 
135 @total_ordering
137 
138  SOURCE = 'Argument'
139 
140  def __init__(self, name, value):
141  VariableInfo.__init__(self, name, value, self.SOURCESOURCE)
142 
143  def __eq__(self, other):
144  return self.longnamelongnamelongname.lower() == other.longname.lower()
145 
146  def __hash__(self):
147  return hash(repr(self))
148 
149  def __lt__(self, other):
150  return self.longnamelongnamelongname.lower() < other.longname.lower()
151 
152 
154 
155  SOURCE = 'Local variable'
156 
157  def __init__(self, name):
158  VariableInfo.__init__(self, name, '', self.SOURCESOURCE)
159 
160 
161 @total_ordering
163 
164  def __init__(self, item):
165  self.docdoc = self._doc(item).strip()
166  self.doc_formatdoc_format = "ROBOT"
167  ItemInfo.__init__(self, self._name_name(item), self._source(item),
168  None)
169  self.shortdocshortdoc = self.docdoc.splitlines()[0] if self.docdoc else ''
170  self.itemitem = item
171 
172  @property
173  arguments = property
174 
175  def arguments(self):
176  return self._parse_args(self.itemitem)
177 
178  @property
179  details = property
180 
181  def details(self):
182  formatter = DocToHtml(self.doc_formatdoc_format)
183  return ('<table>'
184  '<tr><td><i>Name:</i></td><td>%s</td></tr>'
185  '<tr><td><i>Source:</i></td><td>%s &lt;%s&gt;</td></tr>'
186  '<tr><td><i>Arguments:</i></td><td>%s</td></tr>'
187  '</table>'
188  '<table>'
189  '<tr><td>%s</td></tr>'
190  '</table>') % \
191  (self._name_name(self.itemitem), self._source(self.itemitem), self._type,
192  self._format_args_format_args(self.argumentsargumentsarguments),
193  formatter(self.docdoc))
194 
195  def _format_args(self, args):
196  return '[ %s ]' % ' | '.join(args)
197 
198  def __str__(self):
199  return 'KeywordInfo[name: %s, source: %s, doc: %s]' %(self.namename,
200  self.sourcesource,
201  self.docdoc)
202 
203  def _name(self, item):
204  return item.name
205 
206  def __eq__(self, other):
207  return self.namename.lower() == other.name.lower()
208 
209  def __hash__(self):
210  return hash(repr(self))
211 
212  def __lt__(self, other):
213  return self.namename.lower() < other.name.lower()
214 
215 
217 
218  def __init__(self, item, source, source_type, doc_format):
219  self._type_type = source_type
220  self._source_source = lambda x: source
221  _KeywordInfo.__init__(self, item)
222  self.argsargs = self._format_args_format_args(self._parse_args_parse_args(item))
223  if doc_format in ("TEXT", "ROBOT", "REST", "HTML"):
224  self.doc_formatdoc_formatdoc_format = doc_format
225  else:
226  self.doc_formatdoc_formatdoc_format = "ROBOT"
227 
228  def with_alias(self, alias):
229  if alias:
230  self.sourcesourcesource = alias
231  return self
232 
233  def _name(self, node):
234  return node.get('name')
235 
236  def _doc(self, node):
237  return node.find('doc').text or ''
238 
239  def _parse_args(self, node):
240  args_node = node.find('arguments')
241  return [arg_node.text for arg_node in args_node.findall('arg')]
242 
244  return True
245 
246 
247 @total_ordering
249 
252  _type = 'test library'
253 
256  _library_alias = None
257  item = None
258 
259  def __init__(self, name, doc, doc_format, library_name, args):
260  self._item_name_item_name = name
261  self.docdocdoc = doc.strip()
262  self._item_library_name_item_library_name = library_name
263  self._args_args = args
264  ItemInfo.__init__(self, self._item_name_item_name, library_name, None)
265  self.shortdocshortdocshortdoc = self.docdocdoc.splitlines()[0] if self.docdocdoc else ''
266 
267  if doc_format in ("TEXT", "ROBOT", "REST", "HTML"):
268  self.doc_formatdoc_formatdoc_format = doc_format
269  else:
270  self.doc_formatdoc_formatdoc_format = "ROBOT"
271 
272  def with_alias(self, alias):
273  self._library_alias_library_alias = alias
274  self.sourcesourcesource = self._source_source(self.itemitemitem)
275  return self
276 
277  def _source(self, item):
278  if self._library_alias_library_alias:
279  return self._library_alias_library_alias
280  return self._item_library_name_item_library_name
281 
282  @property
283  arguments = property
284 
285  def arguments(self):
286  return self._args_args
287 
289  return True
290 
291  def __eq__(self, other):
292  if isinstance(other, str): # DEBUG
293  return self.namename.lower() == other.name.lower() # and self.__hash__ == other.__hash__
294 
295  def __hash__(self):
296  return hash(repr(self)) # self.name) #
297 
298  def __lt__(self, other):
299  return self.namename.lower() < other.name.lower()
300 
301  def _name(self, item):
302  return self._item_name_item_name
303 
304 
305 @total_ordering
306 
310 
313  _type = 'test library'
314 
317  _library_alias = None
318  item = None
319 
320  def __init__(self, name, doc, doc_format='ROBOT', library_name='BuiltIn',
321  *args):
322  self._item_name_item_name = name
323  self.docdocdoc = doc.strip()
324  self._item_library_name_item_library_name = library_name
325  self.doc_formatdoc_formatdoc_format = doc_format
326  self._args_args = args
327  ItemInfo.__init__(self, self._item_name_item_name, library_name, None)
328  self.shortdocshortdocshortdoc = self.docdocdoc.splitlines()[0] if self.docdocdoc else ''
329 
330  def with_alias(self, alias):
331  self._library_alias_library_alias = alias
332  self.sourcesourcesource = self._source_source(self.itemitemitem)
333  return self
334 
335  def _source(self, item):
336  if self._library_alias_library_alias:
337  return self._library_alias_library_alias
338  return self._item_library_name_item_library_name
339 
340  @property
341  arguments = property
342 
343  def arguments(self):
344  return self._args_args
345 
347  return True
348 
349  def __eq__(self, other):
350  if isinstance(other, str): # DEBUG
351  return self.namenamename == other.name # must match Capital case
352 
353  def __hash__(self):
354  return hash(repr(self)) # self.name) #
355 
356  def __lt__(self, other):
357  return self.namenamename < other.name
358 
359  def _name(self, item):
360  return self._item_name_item_name
361 
362 
364 
365  def _source(self, item):
366  return os.path.basename(item.source) if item.source else ''
367 
368  def _doc(self, item):
369  return utils.unescape(item.doc.value)
370 
371  def _parse_args(self, uk):
372  parsed = []
373  for arg in uk.args.value:
374  if self._is_scalar_is_scalar(arg):
375  parsed.append(self._parse_name_and_default_parse_name_and_default(arg))
376  elif self._is_list_is_list(arg):
377  parsed.append(self._parse_vararg_parse_vararg(arg))
378  elif self._is_dict_is_dict(arg):
379  parsed.append(self._parse_kwarg_parse_kwarg(arg))
380  return parsed
381 
382  def _is_scalar(self, arg):
383  return arg.startswith('$')
384 
385  def _parse_name_and_default(self, arg):
386  parts = arg.split('=', 1)
387  name = self._strip_var_syntax_chars_strip_var_syntax_chars(parts[0])
388  if len(parts) == 1:
389  return name
390  return name + '=' + parts[1]
391 
392  def _strip_var_syntax_chars(self, string):
393  return string[2:-1]
394 
395  def _is_list(self, arg):
396  return arg.startswith('@')
397 
398  def _is_dict(self, arg):
399  return arg.startswith('&')
400 
401  def _parse_vararg(self, arg):
402  return '*' + self._strip_var_syntax_chars_strip_var_syntax_chars(arg)
403 
404  def _parse_kwarg(self, arg):
405  return '**' + self._strip_var_syntax_chars_strip_var_syntax_chars(arg)
406 
407 
408 @total_ordering
410 
413  _type = 'test case file'
414 
415  @property
416  longname = property
417 
418  def longname(self):
419  return self.namename
420 
421  def __eq__(self, other):
422  if isinstance(other, str): # DEBUG
423  return self.namename.lower() == other.name.lower() # and self.__hash__ == other.__hash__
424 
425  def __hash__(self):
426  return hash(self.longnamelongnamelongnamelongnamelongname) # repr(self))
427 
428  def __lt__(self, other):
429  return self.namename.lower() < other.name.lower()
430 
431 
432 @total_ordering
434 
437  _type = 'resource file'
438 
439  @property
440  longname = property
441 
442  def longname(self):
443  return self.itemitem.parent.parent.rawname + '.' + self.namename
444 
445  def __eq__(self, other):
446  if isinstance(other, self.__class__):
447  return self.namename.lower() == other.name.lower()
448  else:
449  return False
450 
451  def __hash__(self):
452  return hash(self.longnamelongnamelongnamelongnamelongname) # repr(self))
453 
454  def __lt__(self, other):
455  return self.namename.lower() < other.name.lower()
456 
457 
458 PRIORITIES = {ItemInfo: 50,
459  LibraryKeywordInfo: 40,
460  BlockKeywordInfo: 35,
461  ResourceUserKeywordInfo: 30,
462  TestCaseUserKeywordInfo: 20,
463  VariableInfo: 10,
464  ArgumentInfo: 5,
465  LocalVariableInfo: 5}
def __init__(self, name, value)
Definition: iteminfo.py:140
Special Info for FOR and END, documentation and since 5.0, IF, ELSE, ELSEIF, WHILE,...
Definition: iteminfo.py:309
def __init__(self, name, doc, doc_format='ROBOT', library_name='BuiltIn', *args)
Definition: iteminfo.py:321
Represents an object that can be displayed by content assistant.
Definition: iteminfo.py:24
def longname_begins_with(self, prefix)
Definition: iteminfo.py:54
def __eq__(self, other)
Definition: iteminfo.py:73
def __init__(self, name, source, details)
Creates an item info.
Definition: iteminfo.py:38
def __cmp__(self, other)
Definition: iteminfo.py:67
def name_begins_with(self, prefix)
Definition: iteminfo.py:51
def __init__(self, name, doc, doc_format, library_name, args)
Definition: iteminfo.py:259
def _source_name(self, source)
Definition: iteminfo.py:88
def name_matches(self, pattern)
Definition: iteminfo.py:91
def __init__(self, name, value, source)
Creates an item info.
Definition: iteminfo.py:83
def _undecorate(self, pattern)
Definition: iteminfo.py:95
def _strip_var_syntax_chars(self, string)
Definition: iteminfo.py:392
def __init__(self, item, source, source_type, doc_format)
Definition: iteminfo.py:218