Created
April 26, 2012 05:00
-
-
Save fernferret/2496058 to your computer and use it in GitHub Desktop.
Read from a psudo json/yaml/python dictionary typed data structure via a python tokenizer
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
"Advertisements" | |
{ | |
"1" | |
{ | |
"type" "S" | |
"text" "Woo Stuff" | |
} | |
"2" | |
{ | |
"type" "S" | |
"text" "Small stuff" | |
} | |
"3" | |
{ | |
"type" "H" | |
"text" "Hint style stuff" | |
} | |
"4" | |
{ | |
"type" "S" | |
"text" "Other stuff" | |
} | |
"5" | |
{ | |
"type" "S" | |
"text" "Another small message." | |
} | |
"6" | |
{ | |
"type" "S" | |
"text" "Small Message." | |
} | |
} |
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
#!/usr/bin/env python | |
import tokenize | |
import pprint | |
class ParseError(Exception): | |
pass | |
class SMConfig(object): | |
def __init__(self, filename): | |
self.filename = filename | |
def save(self, filename=None): | |
if filename: | |
self.filename = filename | |
my_file = open(self.filename, "w+") | |
my_file.close() | |
def load(self, filename=None): | |
if filename: | |
self.filename = filename | |
my_file = open(self.filename) | |
tokens = tokenize.generate_tokens(my_file.readline) | |
final_dict = {} | |
try: | |
#while True: | |
self._parse_node(tokens, final_dict) | |
except StopIteration: | |
pass | |
my_file.close() | |
return final_dict | |
def _parse_node(self, tokens, my_dict, prev_name=None): | |
next = tokens.next() | |
if my_dict == None: | |
my_dict = {} | |
if tokenize.tok_name[next[0]] == "STRING": | |
if prev_name: | |
my_dict[prev_name] = next[1].strip('"') | |
self._parse_node(tokens, my_dict) | |
return my_dict | |
return self._parse_node(tokens, my_dict, next[1].strip('"')) | |
elif tokenize.tok_name[next[0]] == "OP": | |
if next[1] == "{": | |
if not prev_name == None: | |
my_dict[prev_name] = self._parse_node(tokens, None, None) | |
return self._parse_node(tokens, my_dict, None) | |
self._parse_node(tokens, my_dict, prev_name) | |
elif next[1] == "}": | |
return my_dict | |
else: | |
return self._parse_node(tokens, my_dict, prev_name) | |
return self._parse_node(tokens, my_dict, prev_name) | |
if __name__ == "__main__": | |
pp = pprint.PrettyPrinter(indent=4) | |
cfg = SMConfig("ads.txt") | |
pp.pprint(cfg.load()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment