source_code.py
Go to the documentation of this file.
1 import os.path
2 
3 from .source_code_file import SourceCodeFile
4 
5 
6 class SourceCode:
7  def __init__(self, filenames, pkg_name):
8  self.pkg_name = pkg_name
9  self.sources = {}
10  for rel_fn, file_path in filenames.items():
11  self.sources[rel_fn] = SourceCodeFile(rel_fn, file_path)
12 
13  def has_header_files(self):
14  goal_folder = os.path.join('include', self.pkg_name)
15  for source_fn in self.sources:
16  if goal_folder in source_fn:
17  return True
18  return False
19 
20  def get_source_by_language(self, language):
21  return [source for source in self.sources.values() if source.language == language]
22 
23  def setup_tags(self, cmake):
24  for tag, files in [('library', cmake.get_library_source()),
25  ('executable', cmake.get_executable_source()),
26  ('test', cmake.get_test_source())]:
27  for fn in files:
28  if fn and fn[0] == '$':
29  continue
30  if fn in self.sources:
31  self.sources[fn].tags.add(tag)
32  else:
33  print(' File %s found in CMake not found in folder!' % fn)
34 
36  packages = set()
37  for source in self.sources.values():
38  if 'test' in source.tags:
39  continue
40  packages.update(source.get_dependencies())
41  if self.pkg_name in packages:
42  packages.remove(self.pkg_name)
43  return packages
44 
46  packages = set()
47  for source in self.sources.values():
48  if 'test' in source.tags:
49  continue
50  packages.update(source.get_external_python_dependencies())
51  return packages
52 
54  packages = set()
55  for source in self.sources.values():
56  if 'test' not in source.tags:
57  continue
58  packages.update(source.get_dependencies())
59  if self.pkg_name in packages:
60  packages.remove(self.pkg_name)
61  return packages
62 
63  def search_for_patterns(self, patterns, per_line=True):
64  files = {}
65  for source in self.sources.values():
66  if per_line:
67  matches = source.search_lines_for_patterns(patterns)
68  else:
69  matches = source.search_for_patterns(patterns)
70  if len(matches) != 0:
71  files[source.rel_fn] = matches
72  return files
73 
74  def search_for_pattern(self, pattern, per_line=True):
75  return self.search_for_patterns([pattern], per_line)
76 
77  def modify_with_patterns(self, patterns, language='c++', verbose=True):
78  """
79  Given a map of patterns, replace all instances in the package source code with the given language.
80 
81  The key in the map is a regular expression string literal.
82  If there are no groups, then the matching string is replaced with the map value.
83  If there are groups, then the literals of the form $0, $1, etc in the map value are replaced with the groups
84  """
85  for source in self.get_source_by_language(language):
86  source.modify_with_patterns(patterns, verbose)
87 
88  def __repr__(self):
89  return '\n'.join(map(str, sorted(self.sources.values())))
def search_for_patterns(self, patterns, per_line=True)
Definition: source_code.py:63
def search_for_pattern(self, pattern, per_line=True)
Definition: source_code.py:74
def get_source_by_language(self, language)
Definition: source_code.py:20
def __init__(self, filenames, pkg_name)
Definition: source_code.py:7
def modify_with_patterns(self, patterns, language='c++', verbose=True)
Definition: source_code.py:77


ros_introspection
Author(s):
autogenerated on Wed Mar 3 2021 03:56:00