def test_parse_with_sections_missing_bracket(): with tempfile.NamedTemporaryFile() as tf: fpath = tf.name template_vars = {'name': 'Cylc'} tf.write("#!jinja2\n[[section1]\na={{ name }}\n# comment!".encode()) tf.flush() with pytest.raises(FileParseError) as cm: parse(fpath=fpath, output_fname="", template_vars=template_vars) exc = cm.value assert exc.reason == 'bracket mismatch' assert exc.line == '[[section1]'
def test_parse_invalid_line(): with tempfile.NamedTemporaryFile() as of: with tempfile.NamedTemporaryFile() as tf: fpath = tf.name template_vars = {'name': 'Cylc'} tf.write("#!jinja2\n{{ name }}\n".encode()) tf.flush() with pytest.raises(FileParseError) as cm: parse(fpath=fpath, output_fname=of.name, template_vars=template_vars) exc = cm.value assert exc.reason == 'Invalid line' assert exc.line_num == 1 assert exc.line == 'Cylc'
def test_parse_with_sections(): with tempfile.NamedTemporaryFile() as of: with tempfile.NamedTemporaryFile() as tf: fpath = tf.name template_vars = {'name': 'Cylc'} tf.write(("#!jinja2\n[section1]\n" "a={{ name }}\n# comment!\n" "[[subsection1]]\n" "[[subsection2]]\n" "[section2]").encode()) tf.flush() r = parse(fpath=fpath, output_fname=of.name, template_vars=template_vars) expected = OrderedDictWithDefaults() expected['section1'] = OrderedDictWithDefaults() expected['section1']['a'] = 'Cylc' expected['section1']['subsection1'] = OrderedDictWithDefaults() expected['section1']['subsection2'] = OrderedDictWithDefaults() expected['section2'] = OrderedDictWithDefaults() assert r == expected of.flush() output_file_contents = of.read().decode() assert output_file_contents == ('[section1]\na=Cylc\n# comment!\n' '[[subsection1]]\n' '[[subsection2]]\n' '[section2]\n')
def test_parse_with_sections_error_wrong_level(): with tempfile.NamedTemporaryFile() as of: with tempfile.NamedTemporaryFile() as tf: fpath = tf.name template_vars = {'name': 'Cylc'} tf.write(("#!jinja2\n[section1]\n" "a={{ name }}\n# comment!\n" "[[[subsection1]]]\n") # expected [[]] instead! .encode()) tf.flush() with pytest.raises(FileParseError) as cm: parse(fpath=fpath, output_fname=of.name, template_vars=template_vars) exc = cm.value assert exc.line_num == 4 assert exc.line == '[[[subsection1]]]'
def test_parse_keys_only_multiline(): with tempfile.NamedTemporaryFile() as of: with tempfile.NamedTemporaryFile() as tf: fpath = tf.name template_vars = {'name': 'Cylc'} tf.write("#!jinja2\na='''value is \\\n{{ name }}'''\n".encode()) tf.flush() r = parse(fpath=fpath, output_fname=of.name, template_vars=template_vars) expected = OrderedDictWithDefaults() expected['a'] = "'''value is Cylc'''" assert r == expected
def test_unclosed_multiline(): with tempfile.NamedTemporaryFile() as tf: fpath = tf.name template_vars = {'name': 'Cylc'} tf.write((''' [scheduling] [[graph]] R1 = """ foo [runtime] [[foo]] script = """ echo hello world """ ''').encode()) tf.flush() with pytest.raises(FileParseError) as cm: parse(fpath=fpath, output_fname="", template_vars=template_vars) exc = cm.value assert exc.reason == 'Invalid line' assert 'echo hello world' in exc.line assert 'Did you forget to close [scheduling][graph]R1?' in str(exc)
def test_parse_comments(): with tempfile.NamedTemporaryFile() as of: with tempfile.NamedTemporaryFile() as tf: fpath = tf.name template_vars = {'name': 'Cylc'} tf.write("#!jinja2\na={{ name }}\n# comment!".encode()) tf.flush() r = parse(fpath=fpath, output_fname=of.name, template_vars=template_vars) expected = OrderedDictWithDefaults() expected['a'] = 'Cylc' assert r == expected of.flush() output_file_contents = of.read().decode() assert output_file_contents == 'a=Cylc\n# comment!\n'
def loadcfg(self, rcfile, title=""): """Parse a config file, upgrade or deprecate items if necessary, validate it against the spec, and if this is not the first load, combine/override with the existing loaded config.""" sparse = parse(rcfile, self.output_fname, self.tvars) if self.upgrader is not None: self.upgrader(sparse, title) self.validate(sparse) if not self.sparse: self.sparse = sparse else: # Already loaded, override with new items. replicate(self.sparse, sparse)