forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobotbuilder.py
More file actions
139 lines (113 loc) · 4.89 KB
/
Copy pathrobotbuilder.py
File metadata and controls
139 lines (113 loc) · 4.89 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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 os
import sys
from robot.errors import DataError
from robot.parsing import disable_curdir_processing
from robot.running import TestLibrary, UserLibrary, UserErrorHandler
from robot.utils import split_tags_from_doc, unescape
from .model import LibraryDoc, KeywordDoc
class LibraryDocBuilder(object):
_argument_separator = '::'
def build(self, library):
name, args = self._split_library_name_and_args(library)
lib = TestLibrary(name, args)
libdoc = LibraryDoc(name=lib.name,
doc=self._get_doc(lib),
version=lib.version,
scope=str(lib.scope),
doc_format=lib.doc_format)
libdoc.inits = self._get_initializers(lib)
libdoc.keywords = KeywordDocBuilder().build_keywords(lib)
return libdoc
def _split_library_name_and_args(self, library):
args = library.split(self._argument_separator)
name = args.pop(0)
return self._normalize_library_path(name), args
def _normalize_library_path(self, library):
path = library.replace('/', os.sep)
if os.path.exists(path):
return os.path.abspath(path)
return library
def _get_doc(self, lib):
return lib.doc or "Documentation for library ``%s``." % lib.name
def _get_initializers(self, lib):
if lib.init.arguments.maxargs:
return [KeywordDocBuilder().build_keyword(lib.init)]
return []
class ResourceDocBuilder(object):
def build(self, path):
res = self._import_resource(path)
libdoc = LibraryDoc(name=res.name,
doc=self._get_doc(res),
type='resource')
libdoc.keywords = KeywordDocBuilder(resource=True).build_keywords(res)
return libdoc
@disable_curdir_processing
def _import_resource(self, path):
return UserLibrary(self._find_resource_file(path))
def _find_resource_file(self, path):
if os.path.isfile(path):
return os.path.normpath(path)
for dire in [item for item in sys.path if os.path.isdir(item)]:
candidate = os.path.normpath(os.path.join(dire, path))
if os.path.isfile(candidate):
return candidate
raise DataError("Resource file '%s' does not exist." % path)
def _get_doc(self, res):
if res.doc:
return unescape(res.doc)
return "Documentation for resource file ``%s``." % res.name
class KeywordDocBuilder(object):
def __init__(self, resource=False):
self._resource = resource
def build_keywords(self, lib):
return [self.build_keyword(kw) for kw in lib.handlers]
def build_keyword(self, kw):
doc, tags = self._get_doc_and_tags(kw)
return KeywordDoc(name=kw.name,
args=self._get_args(kw.arguments),
doc=doc,
tags=tags)
def _get_doc_and_tags(self, kw):
doc = self._get_doc(kw)
doc, tags = split_tags_from_doc(doc)
return doc, kw.tags + tags
def _get_doc(self, kw):
if self._resource and not isinstance(kw, UserErrorHandler):
return unescape(kw.doc)
return kw.doc
def _get_args(self, argspec):
""":type argspec: :py:class:`robot.running.arguments.ArgumentSpec`"""
args = [self._format_arg(arg, argspec) for arg in argspec.positional]
if argspec.varargs:
args.append('*%s' % self._format_arg(argspec.varargs, argspec))
if argspec.kwonlyargs:
if not argspec.varargs:
args.append('*')
args.extend(self._format_arg(arg, argspec)
for arg in argspec.kwonlyargs)
if argspec.kwargs:
args.append('**%s' % self._format_arg(argspec.kwargs, argspec))
return args
def _format_arg(self, arg, argspec):
result = arg
if argspec.types is not None and arg in argspec.types:
result = '%s: %s' % (result, self._format_type(argspec.types[arg]))
if arg in argspec.defaults:
result = '%s=%s' % (result, argspec.defaults[arg])
return result
def _format_type(self, type_):
return type_.__name__ if isinstance(type_, type) else type_