Skip to content
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 7 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions mappyfile/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Copy link
Contributor Author

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.

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):
Expand All @@ -71,11 +73,9 @@ def open_file(self, fn):
raise

def parse_file(self, fn):

self.cwd = os.path.dirname(fn)
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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):
"""
Expand All @@ -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)
Expand Down
15 changes: 4 additions & 11 deletions mappyfile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@
import codecs


def load(fn, cwd=None):

p = Parser(cwd=cwd)
def load(fn, expand_includes=True, add_linebreaks=True):
p = Parser(expand_includes=expand_includes, add_linebreaks=add_linebreaks)
ast = p.parse_file(fn)
m = MapfileToDict()
d = m.transform(ast)

return d


def loads(s, cwd="", expand_includes=True):
p = Parser(cwd=cwd, expand_includes=expand_includes)
def loads(s, expand_includes=True, add_linebreaks=True):
p = Parser(expand_includes=expand_includes, add_linebreaks=add_linebreaks)
ast = p.parse(s)
m = MapfileToDict()
d = m.transform(ast)

return d


def write(d, output_file, indent=4):

map_string = _pprint(d, indent)
_save(output_file, map_string)

return output_file


Expand Down Expand Up @@ -59,12 +54,10 @@ def __find__(lst, key, value):

def findall(lst, key, value):
possible_values = ("'%s'" % value, '"%s"' % value)

return (item for item in lst if item[key.lower()] in possible_values)


def _save(output_file, map_string):

with codecs.open(output_file, "w", encoding="utf-8") as f:
f.write(map_string)

Expand Down
4 changes: 4 additions & 0 deletions tests/samples/include1_nested_path.map
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NAME 'test'
4 changes: 4 additions & 0 deletions tests/samples/mapfile_include/include2_nested_path.map
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
14 changes: 12 additions & 2 deletions tests/test_sample_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ def test_includes():
ast = p.parse_file('./tests/samples/include1.map')
m = MapfileToDict()

d = (m.transform(ast)) # works
d = (m.transform(ast)) # works
print(mappyfile.dumps(d))


def test_includes_nested_path():
p = Parser()

ast = p.parse_file('./tests/samples/include1_nested_path.map')
m = MapfileToDict()

d = (m.transform(ast)) # works
print(mappyfile.dumps(d))


Expand All @@ -38,5 +48,5 @@ def run_tests():
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
test_all_maps()
# run_tests()
print("Done!")