Skip to content

Commit

Permalink
Merge pull request #70 from chimpler/pycparsing-update
Browse files Browse the repository at this point in the history
Pycparsing update
  • Loading branch information
darthbear committed Feb 27, 2016
2 parents 4920714 + 5fca417 commit 2ee0033
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
10 changes: 5 additions & 5 deletions pyhocon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .config_parser import ConfigParser, ConfigFactory, ConfigSubstitutionException # noqa
from .config_tree import ConfigTree, ConfigList, UndefinedKey # noqa
from .config_tree import ConfigInclude, ConfigSubstitution, ConfigUnquotedString, ConfigValues # noqa
from .config_tree import ConfigMissingException, ConfigException, ConfigWrongTypeException # noqa
from .tool import HOCONConverter # noqa
from pyhocon.config_parser import ConfigParser, ConfigFactory, ConfigSubstitutionException # noqa
from pyhocon.config_tree import ConfigTree, ConfigList, UndefinedKey # noqa
from pyhocon.config_tree import ConfigInclude, ConfigSubstitution, ConfigUnquotedString, ConfigValues # noqa
from pyhocon.config_tree import ConfigMissingException, ConfigException, ConfigWrongTypeException # noqa
from pyhocon.tool import HOCONConverter # noqa
27 changes: 14 additions & 13 deletions pyhocon/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
from pyparsing import Forward, Keyword, QuotedString, Word, Literal, Suppress, Regex, Optional, SkipTo, ZeroOrMore, \
Group, lineno, col, TokenConverter, replaceWith, alphanums
from pyparsing import ParserElement
from .config_tree import ConfigTree, ConfigSubstitution, ConfigList, ConfigValues, ConfigUnquotedString, \
ConfigInclude
from .exceptions import ConfigSubstitutionException, ConfigMissingException
from pyhocon.config_tree import ConfigTree, ConfigSubstitution, ConfigList, ConfigValues, ConfigUnquotedString, \
ConfigInclude, NoneValue
from pyhocon.exceptions import ConfigSubstitutionException, ConfigMissingException
import logging


use_urllib2 = False
try:
# For Python 3.0 and later
Expand All @@ -25,7 +24,6 @@
except NameError:
basestring = str


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -186,7 +184,7 @@ def include_config(token):
assign_expr = Forward()
true_expr = Keyword("true", caseless=True).setParseAction(replaceWith(True))
false_expr = Keyword("false", caseless=True).setParseAction(replaceWith(False))
null_expr = Keyword("null", caseless=True).setParseAction(replaceWith(None))
null_expr = Keyword("null", caseless=True).setParseAction(replaceWith(NoneValue()))
key = QuotedString('"', escChar='\\', unquoteResults=False) | Word(alphanums + '._- ')

eol = Word('\n\r').suppress()
Expand Down Expand Up @@ -215,7 +213,8 @@ def include_config(token):
value_expr = number_expr | true_expr | false_expr | null_expr | string_expr

include_expr = (Keyword("include", caseless=True).suppress() - (
quoted_string | ((Keyword('url') | Keyword('file')) - Literal('(').suppress() - quoted_string - Literal(')').suppress()))) \
quoted_string | (
(Keyword('url') | Keyword('file')) - Literal('(').suppress() - quoted_string - Literal(')').suppress()))) \
.setParseAction(include_config)

dict_expr = Forward()
Expand All @@ -233,11 +232,13 @@ def include_config(token):
assign_expr << Group(
key -
ZeroOrMore(comment_no_comma_eol) -
(dict_expr | Suppress(Literal('=') | Literal(':')) - ZeroOrMore(comment_no_comma_eol) - ConcatenatedValueParser(multi_value_expr))
(dict_expr | Suppress(Literal('=') | Literal(':')) - ZeroOrMore(
comment_no_comma_eol) - ConcatenatedValueParser(multi_value_expr))
)

# the file can be { ... } where {} can be omitted or []
config_expr = ZeroOrMore(comment_eol | eol) + (list_expr | dict_expr | inside_dict_expr) + ZeroOrMore(comment_eol | eol_comma)
config_expr = ZeroOrMore(comment_eol | eol) + (list_expr | dict_expr | inside_dict_expr) + ZeroOrMore(
comment_eol | eol_comma)
config = config_expr.parseString(content, parseAll=True)[0]
if resolve:
ConfigParser.resolve_substitutions(config)
Expand Down Expand Up @@ -290,8 +291,8 @@ def find_substitutions(item):

substitutions = []
if isinstance(item, ConfigTree):
for key, child in item.items():
substitutions += find_substitutions(child)
for key, child in item.items():
substitutions += find_substitutions(child)
elif isinstance(item, list):
for child in item:
substitutions += find_substitutions(child)
Expand Down Expand Up @@ -327,9 +328,9 @@ def final_fixup(item):
resolved_value = None

if isinstance(resolved_value, ConfigValues):
resolved_value = resolved_value.transform()
resolved_value = resolved_value.transform()
if isinstance(resolved_value, ConfigValues):
unresolved = True
unresolved = True
else:
# replace token by substitution
config_values = substitution.parent
Expand Down
11 changes: 9 additions & 2 deletions pyhocon/config_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
basestring = str

import re
from .exceptions import ConfigException, ConfigWrongTypeException, ConfigMissingException
from pyhocon.exceptions import ConfigException, ConfigWrongTypeException, ConfigMissingException


class UndefinedKey(object):
pass


class NoneValue(object):
pass


class ConfigTree(OrderedDict):
KEY_SEP = '.'

Expand Down Expand Up @@ -110,7 +114,10 @@ def _get(self, key_path, key_index=0, default=UndefinedKey):
return default

if key_index == len(key_path) - 1:
return elt
if isinstance(elt, NoneValue):
return None
else:
return elt
elif isinstance(elt, ConfigTree):
return elt._get(key_path, key_index + 1, default)
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup
from setuptools.command.test import test as TestCommand

required_packages = ['pyparsing==2.0.6']
required_packages = ['pyparsing==2.1.0']
if sys.version_info[:2] == (2, 6):
required_packages.append('argparse')
required_packages.append('ordereddict')
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = pep8, py26, py27, py33, py34
envlist = flake8, py26, py27, py33, py34

[testenv]
passenv = TRAVIS TRAVIS_JOB_ID TRAVIS_BRANCH
Expand All @@ -14,4 +14,4 @@ commands =
[testenv:flake8]
basepython = python
deps = flake8
commands = flake8 pyhocon tests
commands = flake8 pyhocon tests setup.py

0 comments on commit 2ee0033

Please sign in to comment.