This repository was archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.py
More file actions
172 lines (141 loc) · 5.11 KB
/
code.py
File metadata and controls
172 lines (141 loc) · 5.11 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# artifact_py: the design documentation tool made for everyone.
#
# Copyright (C) 2019 Rett Berg <github.com/vitiral>
#
# The source code is Licensed under either of
#
# * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
#
# at your option.
#
# Unless you explicitly state otherwise, any contribution intentionally submitted
# for inclusion in the work by you, as defined in the Apache-2.0 license, shall
# be dual licensed as above, without any additional terms or conditions.
"""
See #SPC-design.code
"""
from __future__ import unicode_literals
import re
import os
import sys
import six
from .name import NAME_VALID_STR
from .name import SUB_PART_VALID_STR
from .name import Name
from .name import SubPart
RE_NAME_KEY = "name"
RE_SUBPART_KEY = "subpart"
NAME_FULL_STR = r"(?P<name>{})(:?\.(?P<subpart>{}))?".format(
NAME_VALID_STR, SUB_PART_VALID_STR)
NAME_FULL_RE = re.compile(NAME_FULL_STR, re.I)
NAME_TAG_STR = "#" + NAME_FULL_STR
NAME_TAG_RE = re.compile(NAME_TAG_STR, re.I)
NAME_TAG_VALID_RE = re.compile("${}^".format(NAME_TAG_STR), re.I)
class ImplCode(object):
"""Implemented in code.
primary: list of CodeLoc
secondary: dict[SubPart, list[CodeLoc]]
"""
def __init__(self, primary, secondary):
self.primary = primary
self.secondary = secondary
@classmethod
def new(cls):
return cls([], {})
def insert_primary(self, codeloc):
assert isinstance(codeloc, CodeLoc)
self.primary.append(codeloc)
def insert_secondary(self, subpart, codeloc):
assert isinstance(subpart, SubPart)
assert isinstance(codeloc, CodeLoc)
if subpart not in self.secondary:
self.secondary[subpart] = []
self.secondary[subpart].append(codeloc)
def serialize(self, settings):
return {
"primary": settings.serialize_list(self.primary),
"secondary": {
n.serialize(settings): settings.serialize_list(c)
for n, c in six.iteritems(self.secondary)
},
}
class CodeLoc:
"""Represents a code location: file and line number."""
def __init__(self, file_, line):
self.file = file_
self.line = line
def serialize(self, settings):
return {
"file": settings.relpath(self.file),
"line": self.line,
}
def to_str(self, settings):
return "{}[{}]".format(settings.relpath(self.file), self.line)
def find_impls(settings):
"""Set search settings from a settings object, and begin a recursive search for code impls."""
invalid = []
impls = {}
find_impls_recursive(
invalid=invalid,
impls=impls,
code_paths=settings.code_paths,
exclude_code_paths=settings.exclude_code_paths,
)
if invalid:
raise ValueError("Paths do not exist: {}".format(invalid))
return impls
def find_impls_recursive(invalid, impls, code_paths, exclude_code_paths):
"""Recurse through directory structure and update impls with all files within."""
for code_path in code_paths:
if is_excluded(code_path, exclude_code_paths):
continue
if not os.path.exists(code_path):
invalid.append(code_path)
elif os.path.isdir(code_path):
for entry in os.listdir(code_path):
find_impls_recursive(
invalid=invalid,
impls=impls,
code_paths=[os.path.join(code_path, entry)],
exclude_code_paths=exclude_code_paths,
)
else:
update_impls_file(impls, code_path)
def is_excluded(path, exclude_code_paths):
for exclude in exclude_code_paths:
if path.startswith(exclude):
return True
return False
def update_impls_file(impls, code_file):
"""Update impls with the code impls in the code_file"""
try:
with open(code_file) as fd:
for linenum, line in enumerate(fd):
update_impls_line(code_file, impls, linenum, line)
except (IOError, UnicodeDecodeError) as exc:
# pylint: disable=no-member
six.reraise(Exception,
Exception('{} at {}'.format(repr(exc), code_file)),
sys.exc_info()[2])
def update_impls_line(code_file, impls, linenum, line):
"""update impls with the code impls on the given line of code_file"""
for match in NAME_TAG_RE.finditer(line):
codeloc = CodeLoc(code_file, line=linenum)
name, subpart = name_from_match(match)
if name not in impls:
impls[name] = ImplCode.new()
if subpart:
impls[name].insert_secondary(subpart, codeloc)
else:
impls[name].insert_primary(codeloc)
def name_from_match(match):
"""Return the name and possibly subname from the match."""
groups = match.groupdict()
name = Name.from_str(groups[RE_NAME_KEY])
subpart = groups.get(RE_SUBPART_KEY)
if subpart:
subpart = SubPart.from_str(subpart)
return name, subpart