-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for relative path for nested include #28
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7bd5fbc
Add support for nested includes
loicgasser a2de60a
Merge branch 'master' into cwd_override
geographika ce9ebfb
Update parser.py
geographika 70a3b66
Update utils.py
geographika 0799b46
Update test_sample_maps.py
geographika d9aea69
Update test_sample_maps.py
geographika d2f487c
Update test_sample_maps.py
geographika File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,49 +13,51 @@ | |
|
||
class Parser(object): | ||
|
||
def __init__(self, cwd="", expand_includes=True, add_linebreaks=True): | ||
self.cwd = cwd | ||
def __init__(self, expand_includes=True, add_linebreaks=True): | ||
self.expand_includes = expand_includes | ||
self.add_linebreaks = add_linebreaks | ||
self.g = self.load_grammar("mapfile.g") | ||
self._nested_include = 0 | ||
|
||
def load_grammar(self, grammar_file): | ||
|
||
gf = os.path.join(os.path.dirname(__file__), grammar_file) | ||
grammar_text = open(gf).read() | ||
return Lark(grammar_text, parser="earley", lexer="standard") | ||
|
||
return Lark(grammar_text, parser='earley', lexer='standard') | ||
|
||
def strip_quotes(self, s): | ||
def _strip_quotes(self, s): | ||
s = s[:s.index('#')] if '#' in s else s | ||
return s.strip("'").strip('"') | ||
|
||
def load_includes(self, text): | ||
|
||
def load_includes(self, text, fn=None): | ||
# Per default use working directory of the process | ||
if fn is None: | ||
fn = os.getcwd() | ||
lines = text.split('\n') | ||
includes = {} | ||
|
||
include_discovered = False | ||
for idx, l in enumerate(lines): | ||
if l.strip().lower().startswith('include'): | ||
if '#' in l: | ||
l = l[:l.index('#')] | ||
|
||
parts = [p for p in l.split()] | ||
|
||
assert (len(parts) == 2) | ||
assert (parts[0].lower() == 'include') | ||
fn = os.path.join(self.cwd, self.strip_quotes(parts[1])) | ||
if l.strip().lower().startswith("include"): | ||
if not include_discovered: | ||
include_discovered = True | ||
self._nested_include += 1 | ||
if self._nested_include > 5: | ||
raise Exception("Maximum nested include exceeded! (MaxNested=5)") | ||
|
||
inc, inc_file_path = l.split() | ||
inc_file_path = self._strip_quotes(inc_file_path) | ||
if not os.path.isabs(inc_file_path): | ||
inc_file_path = os.path.join(os.path.dirname(fn), inc_file_path) | ||
try: | ||
include_text = self.open_file(fn) | ||
include_text = self.open_file(inc_file_path) | ||
except IOError as ex: | ||
logging.warning("Include file '%s' not found", fn) | ||
logging.warning("Include file '%s' not found", inc_file_path) | ||
raise ex | ||
# recursively load any further includes | ||
includes[idx] = self.load_includes(include_text) | ||
includes[idx] = self.load_includes(include_text, fn=inc_file_path) | ||
|
||
for idx, txt in includes.items(): | ||
lines.pop(idx) # remove the original include | ||
lines.insert(idx, txt) | ||
|
||
return '\n'.join(lines) | ||
|
||
def open_file(self, fn): | ||
|
@@ -71,11 +73,9 @@ def open_file(self, fn): | |
raise | ||
|
||
def parse_file(self, fn): | ||
|
||
self.cwd = os.path.dirname(fn) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. main issue was that. always the same cwd which was overriden here |
||
|
||
self._nested_include = 0 | ||
text = self.open_file(fn) | ||
return self.parse(text) | ||
return self.parse(text, fn=fn) | ||
|
||
def _add_linebreaks(self, text): | ||
""" | ||
|
@@ -92,10 +92,10 @@ def _add_linebreaks(self, text): | |
|
||
return "\n".join(new_lines) | ||
|
||
def parse(self, text): | ||
|
||
def parse(self, text, fn=None): | ||
self._nested_include = 0 | ||
if self.expand_includes: | ||
text = self.load_includes(text) | ||
text = self.load_includes(text, fn=fn) | ||
|
||
if self.add_linebreaks: | ||
text = self._add_linebreaks(text) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
MAP | ||
NAME 'include_test' | ||
INCLUDE 'mapfile_include/include2_nested_path.map' | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NAME 'test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
LAYER | ||
NAME 'include_test' | ||
INCLUDE 'include/include3_nested_path.map' | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and as a result this would not follow relative nested paths.