171 from types
import StringType, IntType, FloatType, DictType, ListType, ClassType
177 tempalte_text = read template text from file 178 dictionary = create dictionaly by using yaml 179 t = Template(tempalte_text) 180 generated_text = t.generate(dictionary) 184 def __init__(self, template, begin_mark="\[", end_mark="\]"):
216 re_item =
r'(?:"(?:[^\\"]|\\.)*"|[-\w.:]+)' 217 re_command =
r'%s(%s(?: +%s)*)%s' % \
218 (begin_mark, re_item, re_item, end_mark)
219 re_bracket =
r'%s%s%s' % \
220 (begin_mark, begin_mark, end_mark)
221 re_comment =
r'%s#[^%s]*%s' % \
222 (begin_mark, end_mark, end_mark)
225 (re_command, re_bracket, re_comment))
226 self.
re_args = re.compile(
r'"(?:[^\\"]|\\.)*"|[-\w.:]+')
250 gen = Generator(self.
token, dict)
252 return gen.generate()
265 tmp_cmd +=
"self.set_index(%s)\n" % (self.
index)
271 tmp_cmd += cmd +
"\n" 278 except YATException, e:
294 cmd_text =
"self.write_token(%s)" % (self.
index)
301 cmd_text =
"self.write(\"" + self.
begin_mark +
"\")" 308 args = self.re_args.findall(cmd)
318 if args[0] ==
"endfor":
321 elif args[0] ==
"else":
324 elif args[0] ==
"last":
325 self.__last_cmd(args)
327 elif args[0] ==
"endif":
334 if args[0] ==
"if-any":
338 if args[0] ==
"for" and args[2] ==
"in":
341 elif args[0] ==
"if" and args[2] ==
"is":
343 elif args[0] ==
"elif" and args[2] ==
"is":
345 elif args[0] ==
"if-index" and args[2] ==
"is":
347 elif args[0] ==
"elif-index" and args[2] ==
"is":
356 cmd_text =
"self.write_dict(\"%s\")" % (args[0])
367 The following [for] directive 368 [for tmp_key in directive] 369 is converted into the following python command. 370 for i in len(directive): 371 self.dicts.append({tmp_key: ditective[i]) 372 and, endfor directive terminate as the following, 380 cmd_text =
"%s_list = self.get_list(\"%s\")" % (key, directive)
382 cmd_text =
"%s_len = len(%s_list)" % (key, key)
384 cmd_text =
"for %s_index in range(len(%s_list)):" % (key, key)
387 cmd_text =
"self.push_dict({\"%s\": %s_list[%s_index]})" \
390 self.cmd_cxt.append(
"for")
394 cxt = self.cmd_cxt.pop()
415 The following [if] directive 416 [if directive is string] 417 is converted into the following python command. 418 if self.__get_string() == "string": 422 cmd_text =
"if self.get_text(\"%s\") == \"%s\":" % \
426 self.cmd_cxt.append(
"if")
434 cmd_text =
"elif self.get_text(\"%s\") == \"%s\":" % \
445 cmdlist = {
"first":
"if %s_index == 0:",
446 "even" :
"if (%s_index %% 2) == 0:",
447 "odd" :
"if (%s_index %% 2) != 0:",
448 "last" :
"if %s_index == %s_len - 1:"}
451 if len(self.re_number.findall(cmd)) == 1:
452 cmd_text =
"if %s_index == %s:" % (key, cmd)
453 elif cmdlist.has_key(cmd):
455 cmd_text = cmdlist[cmd] % (key,key)
457 cmd_text = cmdlist[cmd] % (key)
462 self.cmd_cxt.append(
"if-index")
465 if self.
cmd_cxt[-1] !=
"if-index":
469 cmdlist = {
"first":
"elif %s_index == 0:",
470 "even" :
"elif (%s_index %% 2) == 0:",
471 "odd" :
"elif (%s_index %% 2) != 0:",
472 "last" :
"elif %s_index == %s_len - 1:"}
475 if len(self.re_number.findall(cmd)) == 1:
476 cmd_text =
"elif %s_index == %s:" % (key, cmd)
477 elif cmdlist.has_key(cmd):
479 cmd_text = cmdlist[cmd] % (key,key)
481 cmd_text = cmdlist[cmd] % (key)
491 cmd_text =
"if self.has_key(\"%s\"):" % (directive)
494 self.cmd_cxt.append(
"if-any")
498 if self.
cmd_cxt[-1] !=
"if-any":
501 cmd_text =
"if self.has_key(\"%s\"):" % (directive)
510 and self.
cmd_cxt[-1] !=
"if-any":
519 and self.
cmd_cxt[-1] !=
"if-any":
528 print "Parse Error: line", e.lineno,
"in input data" 530 lines = self.template.split(
"\n")
532 print "------------------------------------------------------------" 533 for i
in range(1,10):
535 if l > 0
and l < length:
538 uline =
'~'*len(lines[l])
540 print "------------------------------------------------------------" 544 next = self.
index + 2
546 if self.
token[next] ==
None:
return 547 text = self.
token[next]
549 if tlen > 0
and text[0] ==
'\n':
550 self.
token[next] = text[1:]
552 elif tlen > 0
and text[0] ==
'\r':
553 self.
token[next] = text[1:]
555 elif tlen > 1
and text[0:2] ==
'\r\n':
556 self.
token[next] = text[2:]
560 for i
in range(self.
index):
561 if isinstance(self.
token[i], StringType):
562 l += self.
token[i].count(
'\n')
563 for i
in range(1, self.
index, 3):
572 class Generator(GeneratorBase): 573 def __init__(self, token, dict): 574 GeneratorBase.__init__(self, token, dict) 578 except YATException, e: 594 print "\nTemplate Generation Error: line", e.lineno,
"in input data" 597 for i, s
in enumerate(self.
token):
600 temp +=
"[" + s +
"]\n" 603 lines = temp.split(
"\n")
605 print "------------------------------------------------------------" 606 for i
in range(1,10):
608 if l > 0
and l < length:
611 uline =
'~'*len(lines[l])
613 print "------------------------------------------------------------" 619 self.dicts.append(dict)
622 if len(self.
dicts) < 2:
637 for i
in range(0, self.
index, 3):
638 if self.
token[i] !=
None:
639 cnt += self.
token[i].count(
'\n')
641 for i
in range(1, self.
index, 3):
642 if self.
token[i] !=
None:
648 if isinstance(val, StringType):
650 if isinstance(val, IntType)
or isinstance(val, FloatType):
653 "\" should have string, int or float value.")
657 if not isinstance(val, ListType):
659 "\"" + keytext +
"\" should have list value.")
670 keys = keytext.split(
'.')
671 for i
in range(len(self.
dicts) - 1, -1, -1):
673 if dict_value !=
None:
680 for i
in range(length):
681 if isinstance(d, DictType)
and d.has_key(keys[i]):
694 class UnknownError(YATException):
702 self.
value =
"Unmatched block error: " + msg
711 self.
value =
"Specified key is not final element: ",\
712 dictkey,
"=>", dictvalue
717 self.
value =
"Invalid directive: \"[" + directive +
"]\"" 722 self.
value =
"Unmatched data and input: ", description
727 self.
value =
"Value not found for: \"" + description +
"\"" 734 if type(anItem)==list:
742 if __name__ ==
"__main__":
748 dict.append({
"a":
"This is a",
749 "b": {
"1":
"This is b.1",
752 template.append(
"""[a] 761 dict.append({
"list": [0, 1, 2],
763 {
"name":
"x",
"value":
"1.0"},
764 {
"name":
"y",
"value":
"0.2"},
765 {
"name":
"z",
"value":
"0.1"}]})
766 template.append(
"""[for lst in list] 769 [for lst in listed_dict] 770 [lst.name]: [lst.value] 777 dict.append({
"list": [0,1,2,3,4,5,6,7,8,9,10]})
778 template.append(
"""[for key in list] 779 [if-index key is 3] [key] is hoge!! 780 [elif-index key is 6] [key] is foo!! 781 [elif-index key is 9] [key] is bar!! 782 [elif-index key is first] [key] is first 783 [elif-index key is last] Omoro-------!!!! 784 [elif-index key is odd] [key] is odd number 785 [elif-index key is even] [key] is even number 792 dict.append({
"key1":
"a",
"key2":
"b"})
793 template.append(
"""[if key1 is a] 796 This key1 is not "a". 802 dict.append({
"key1":
"a",
"key2":
"b"})
803 template.append(
"""[if-any key1] 808 key3 does not exists. 818 if len(dict) == len(template):
819 for i
in range(len(dict)-1,len(dict)):
827 print "Dictionary:\n" 828 print yaml.dump(dict[i], default_flow_style=
False)
830 print "Generated Script:\n" 833 print "Generated Text:\n" 834 print t.generate(dict[i])
def write_token(self, index)
def __write_cmd(self, cmd)
def __if_index_cmd(self, args)
def get_dict_value(self, keys, dict)
def __print_error(self, e)
def has_key(self, keytext)
def get_list(self, keytext)
def __init__(self, lineno, description)
def del_nl_after_cmd(self)
def push_dict(self, dict)
def __init__(self, lineno)
def __init__(self, lineno, msg)
def __elif_index_cmd(self, args)
def __init__(self, template, begin_mark="\[", end_mark="\]")
def __init__(self, lineno, directive)
def set_index(self, index)
def __parse_template(self, dict)
def __endfor_cmd(self, args)
def get_text(self, keytext)
def __elif_any_cmd(self, args)
def get_value(self, keytext)
def write_dict(self, keytext)
def __elif_cmd(self, args)
def __endif_cmd(self, args)
def __init__(self, lineno, description)
def __init__(self, lineno, msg)
def __write_cmd_noindex(self, cmd)
def __init__(self, dictkey, dictvalue)
def __else_cmd(self, args)
def __if_any_cmd(self, args)
def __init__(self, token, dict)
def __for_cmd(self, args)