Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
Allow customizing the dict class used
Browse files Browse the repository at this point in the history
  • Loading branch information
wichert committed Aug 2, 2009
1 parent 6d8edeb commit ef8409b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 5 additions & 5 deletions bicop/config.py
Expand Up @@ -68,7 +68,7 @@ def __str__(self):
return "%s[%d]: %s" % (self.file, self.line, self.reason)


def parse(input, filename=None):
def parse(input, filename=None, dictclass=dict):
"""Read a file in a ISC-like config style.
The input can be either a file-like object or a string. If a string
Expand All @@ -80,7 +80,7 @@ def parse(input, filename=None):

tokenizer=shlex.shlex(input, filename)
tokenizer.wordchars+="/._"
return _Parse(tokenizer)
return _Parse(tokenizer, dictclass=dictclass)


def _Decode(token):
Expand All @@ -90,10 +90,10 @@ def _Decode(token):
return int(token)


def _Parse(input):
def _Parse(input, dictclass=dict):
(type_list, type_dict)=(1, 2)
stack=[]
top={}
top=dictclass()

type=type_dict

Expand All @@ -116,7 +116,7 @@ def _Parse(input):
top[command]=[]
else:
type=type_dict;
top[command]={}
top[command]=dictclass()
input.push_token(two)
input.push_token(one)
stack.append(top)
Expand Down
7 changes: 7 additions & 0 deletions bicop/tests/testParse.py
Expand Up @@ -19,6 +19,13 @@ def testNumberList(self):
parse('key { 1; 2; };'),
dict(key=[1, 2]))

def testCustomDictClass(self):
class MyDict(dict):
pass

result=parse('key { 1; 2; };', dictclass=MyDict)
self.failUnless(isinstance(result, MyDict))

def testNestedMap(self):
self.assertEqual(
parse('parent { child { key "value"; }; };'),
Expand Down

0 comments on commit ef8409b

Please sign in to comment.