Skip to content

Commit 6fdbbc8

Browse files
committed
Add caching of file with new dictionary
Add __files_text to the scanner object. It is a dictionary of file declaration to the content of each file in a list of strings. The first time a comment is a part of a file, read it in and store it in the dictionary. Each time afterwards, use the stored value to capture the lines. Use the (begin/end) columns to more precisely capture the text of each comment. Update the test to remove extra characters.
1 parent da5b7f0 commit 6fdbbc8

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

pygccxml/parser/scanner.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def __init__(self, xml_file, decl_factory, config, *args):
158158
self.__files = {}
159159
# mapping between decl id -> access
160160
self.__access = {}
161+
# mapping between file decl_id -> text
162+
self.__files_text = {}
161163
# current object under construction
162164
self.__inst = None
163165
# mapping from id to members
@@ -200,14 +202,26 @@ def read(self):
200202
def _handle_comment(self, declaration):
201203
comm_decl = self.__declarations.get(declaration.comment)
202204
if comm_decl:
203-
with open(self.__files.get(comm_decl.location.file_name, "r")) as file:
204-
line_list = file.readlines()
205-
comment_text = []
206-
for indx in range(comm_decl.start_line - 1,comm_decl.end_line):
207-
comm_line = line_list[indx]
208-
comm_line = comm_line.strip("\n")
209-
comment_text.append(comm_line)
210-
comm_decl.text = comment_text
205+
# First acquire file_name placeholder
206+
file_decl = comm_decl.location.file_name
207+
# If first encounter:
208+
# Find real name and read it into memory
209+
if file_decl not in self.__files_text:
210+
src_name = self.__files.get(file_decl)
211+
with open(src_name, "r") as file:
212+
line_list = file.readlines()
213+
self.__files_text[file_decl] = line_list
214+
comment_text = []
215+
# Capture file text for parsing
216+
file_text = self.__files_text.get(file_decl)
217+
# Use lines and columns to capture only comment text
218+
for indx in range(comm_decl.begin_line - 1, comm_decl.end_line):
219+
comm_line = file_text[indx]
220+
comm_line = comm_line[comm_decl.begin_column - 1:comm_decl.end_column]
221+
# Remove newlines from end of string
222+
comm_line = comm_line.rstrip("\n")
223+
comment_text.append(comm_line)
224+
comm_decl.text = comment_text
211225
return comm_decl
212226

213227
def endDocument(self):

unittests/test_comments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ def test(self):
4444
tmethod = tclass.member_functions()[0]
4545

4646
self.assertIn("comment", dir(tmethod))
47-
self.assertEqual([" /// cxx comment", " /// with multiple lines"], tmethod.comment.text)
47+
self.assertEqual(["/// cxx comment", "/// with multiple lines"], tmethod.comment.text)
4848

4949
tconstructor = tclass.constructors()[0]
5050

5151
self.assertIn("comment", dir(tconstructor))
52-
self.assertEqual([" /** doc comment */"], tconstructor.comment.text)
52+
self.assertEqual(["/** doc comment */"], tconstructor.comment.text)
5353

5454
def create_suite():
5555
suite = unittest.TestSuite()

0 commit comments

Comments
 (0)