176 from types
import StringType, IntType, FloatType, DictType, ListType, ClassType
182 tempalte_text = read template text from file 183 dictionary = create dictionaly by using yaml 184 t = Template(tempalte_text) 185 generated_text = t.generate(dictionary) 189 def __init__(self, template, begin_mark="\[", end_mark="\]"):
221 re_item =
r'(?:"(?:[^\\"]|\\.)*"|[-\w.:]+)' 222 re_command =
r'%s(%s(?: +%s)*)%s' % \
223 (begin_mark, re_item, re_item, end_mark)
224 re_bracket =
r'%s%s%s' % \
225 (begin_mark, begin_mark, end_mark)
226 re_comment =
r'%s#[^%s]*%s' % \
227 (begin_mark, end_mark, end_mark)
230 (re_command, re_bracket, re_comment))
231 self.
re_args = re.compile(
r'"(?:[^\\"]|\\.)*"|[-\w.:]+')
255 gen = Generator(self.
token, dict)
257 return gen.generate()
270 tmp_cmd +=
"self.set_index(%s)\n" % (self.
index)
276 tmp_cmd += cmd +
"\n" 283 except YATException, e:
299 cmd_text =
"self.write_token(%s)" % (self.
index)
306 cmd_text =
"self.write(\"" + self.
begin_mark +
"\")" 313 args = self.
re_args.findall(cmd)
323 if args[0] ==
"endfor":
326 elif args[0] ==
"else":
329 elif args[0] ==
"last":
330 self.__last_cmd(args)
332 elif args[0] ==
"endif":
339 if args[0] ==
"if-any":
343 if args[0] ==
"for" and args[2] ==
"in":
346 elif args[0] ==
"for-inv" and args[2] ==
"in":
349 elif args[0] ==
"if" and args[2] ==
"is":
351 elif args[0] ==
"elif" and args[2] ==
"is":
353 elif args[0] ==
"if-index" and args[2] ==
"is":
355 elif args[0] ==
"elif-index" and args[2] ==
"is":
364 cmd_text =
"self.write_dict(\"%s\")" % (args[0])
375 The following [for] directive 376 [for tmp_key in directive] 377 is converted into the following python command. 378 for i in len(directive): 379 self.dicts.append({tmp_key: ditective[i]) 380 and, endfor directive terminate as the following, 388 cmd_text =
"%s_list = self.get_list(\"%s\")" % (key, directive)
390 cmd_text =
"%s_len = len(%s_list)" % (key, key)
392 cmd_text =
"for %s_index in range(len(%s_list)):" % (key, key)
395 cmd_text =
"self.push_dict({\"%s\": %s_list[%s_index]})" \
402 The following [for] directive 403 [for tmp_key in directive] 404 is converted into the following python command. 405 for i in len(directive): 406 self.dicts.append({tmp_key: ditective[i]) 407 and, endfor directive terminate as the following, 415 cmd_text =
"%s_list = self.get_list(\"%s\")" % (key, directive)
417 cmd_text =
"%s_len = len(%s_list)" % (key, key)
419 cmd_text =
"for %s_index in range(len(%s_list))[::-1]:" % (key, key)
422 cmd_text =
"self.push_dict({\"%s\": %s_list[%s_index]})" \
430 if cxt !=
"for" and cxt !=
"for-inv":
450 The following [if] directive 451 [if directive is string] 452 is converted into the following python command. 453 if self.__get_string() == "string": 457 if string[0] ==
'"' or string[-1] ==
"'" \
458 or string[0] ==
'"' or string[-1] ==
"'":
459 cmd_text =
"if self.get_text(\"%s\") == %s:" % \
462 cmd_text =
"if self.get_text(\"%s\") == self.get_text(\"%s\"):" % \
474 if string[0] ==
'"' or string[-1] ==
"'" \
475 or string[0] ==
'"' or string[-1] ==
"'":
476 cmd_text =
"elif self.get_text(\"%s\") == %s:" % \
479 cmd_text =
"elif self.get_text(\"%s\") == self.get_text(\"%s\"):" % \
490 cmdlist = {
"first":
"if %s_index == 0:",
491 "even" :
"if (%s_index %% 2) == 0:",
492 "odd" :
"if (%s_index %% 2) != 0:",
493 "last" :
"if %s_index == %s_len - 1:"}
496 if len(self.
re_number.findall(cmd)) == 1:
497 cmd_text =
"if %s_index == %s:" % (key, cmd)
498 elif cmdlist.has_key(cmd):
500 cmd_text = cmdlist[cmd] % (key,key)
502 cmd_text = cmdlist[cmd] % (key)
510 if self.
cmd_cxt[-1] !=
"if-index":
514 cmdlist = {
"first":
"elif %s_index == 0:",
515 "even" :
"elif (%s_index %% 2) == 0:",
516 "odd" :
"elif (%s_index %% 2) != 0:",
517 "last" :
"elif %s_index == %s_len - 1:"}
520 if len(self.
re_number.findall(cmd)) == 1:
521 cmd_text =
"elif %s_index == %s:" % (key, cmd)
522 elif cmdlist.has_key(cmd):
524 cmd_text = cmdlist[cmd] % (key,key)
526 cmd_text = cmdlist[cmd] % (key)
536 cmd_text =
"if self.has_key(\"%s\"):" % (directive)
543 if self.
cmd_cxt[-1] !=
"if-any":
546 cmd_text =
"if self.has_key(\"%s\"):" % (directive)
555 and self.
cmd_cxt[-1] !=
"if-any":
565 and self.
cmd_cxt[-1] !=
"if-any":
576 print "Parse Error: line", e.lineno,
"in input data" 580 print "------------------------------------------------------------" 581 for i
in range(1,10):
583 if l > 0
and l < length:
586 uline =
'~'*len(lines[l])
588 print "------------------------------------------------------------" 589 if hasattr(e,
'context'):
590 print "Current context:" 596 next = self.
index + 2
598 if self.
token[next] ==
None:
return 599 text = self.
token[next]
601 if tlen > 0
and text[0] ==
'\n':
602 self.
token[next] = text[1:]
604 elif tlen > 0
and text[0] ==
'\r':
605 self.
token[next] = text[1:]
607 elif tlen > 1
and text[0:2] ==
'\r\n':
608 self.
token[next] = text[2:]
612 for i
in range(self.
index):
613 if isinstance(self.
token[i], StringType):
614 l += self.
token[i].count(
'\n')
615 for i
in range(1, self.
index, 3):
624 class Generator(GeneratorBase): 625 def __init__(self, token, dict): 626 GeneratorBase.__init__(self, token, dict) 630 except YATException, e: 646 print "\nTemplate Generation Error: line", e.lineno,
"in input data" 649 for i, s
in enumerate(self.
token):
652 temp +=
"[" + s +
"]\n" 655 lines = temp.split(
"\n")
657 print "Template text:" 658 print "------------------------------------------------------------" 659 for i
in range(1,10):
661 if l > 0
and l < length:
664 uline =
'~'*len(lines[l])
666 if hasattr(e,
'context'):
667 print "\nCurrent context:" 668 print "------------------------------------------------------------" 678 if len(self.
dicts) < 2:
693 for i
in range(0, self.
index, 3):
694 if self.
token[i] !=
None:
695 cnt += self.
token[i].count(
'\n')
697 for i
in range(1, self.
index, 3):
698 if self.
token[i] !=
None:
704 if isinstance(val, StringType):
706 if isinstance(val, IntType)
or isinstance(val, FloatType):
709 "\" should have string, int or float value.")
713 if not isinstance(val, ListType):
715 "\"" + keytext +
"\" should have list value.")
726 keys = keytext.split(
'.')
727 for i
in range(len(self.
dicts) - 1, -1, -1):
729 if dict_value !=
None:
741 for i
in range(length):
742 if isinstance(d, DictType)
and d.has_key(keys[i]):
755 class UnknownError(YATException):
763 self.
value =
"Unmatched block error: " + msg
772 self.
value =
"Specified key is not final element: ",\
773 dictkey,
"=>", dictvalue
778 self.
value =
"Invalid directive: \"[" + directive +
"]\"" 783 self.
value =
"Unmatched data and input: ", description
786 def __init__(self, lineno, description, context = None):
788 self.
value =
"Value not found for: \"" + description +
"\"\n" 792 self.
context = yaml.dump(context, default_flow_style =
False)
801 if type(anItem)==list:
809 if __name__ ==
"__main__":
815 dict.append({
"a":
"This is a",
816 "b": {
"1":
"This is b.1",
819 template.append(
"""[a] 828 dict.append({
"list": [0, 1, 2],
830 {
"name":
"x",
"value":
"1.0"},
831 {
"name":
"y",
"value":
"0.2"},
832 {
"name":
"z",
"value":
"0.1"}]})
833 template.append(
"""[for lst in list] 836 [for lst in listed_dict] 837 [lst.name]: [lst.value] 844 dict.append({
"list": [0,1,2,3,4,5,6,7,8,9,10]})
845 template.append(
"""[for key in list] 846 [if-index key is 3] [key] is hoge!! 847 [elif-index key is 6] [key] is foo!! 848 [elif-index key is 9] [key] is bar!! 849 [elif-index key is first] [key] is first 850 [elif-index key is last] Omoro-------!!!! 851 [elif-index key is odd] [key] is odd number 852 [elif-index key is even] [key] is even number 859 dict.append({
"key1":
"a",
"key2":
"b"})
860 template.append(
"""[if key1 is a] 863 This key1 is not "a". 869 dict.append({
"key1":
"a",
"key2":
"b"})
870 template.append(
"""[if-any key1] 875 key3 does not exists. 885 if len(dict) == len(template):
886 for i
in range(len(dict)-1,len(dict)):
894 print "Dictionary:\n" 895 print yaml.dump(dict[i], default_flow_style=
False)
897 print "Generated Script:\n" 900 print "Generated Text:\n" 901 print t.generate(dict[i])
def del_nl_after_cmd(self)
def __elif_index_cmd(self, args)
def __elif_any_cmd(self, args)
def __print_error(self, e)
def __endif_cmd(self, args)
def __for_inv_cmd(self, args)
def write_dict(self, keytext)
def push_dict(self, dict)
def __init__(self, token, dict)
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
def get_value(self, keytext)
def get_dict_value(self, keys, dict)
def __elif_cmd(self, args)
def __write_cmd(self, cmd)
def __init__(self, lineno, description, context=None)
def __init__(self, lineno, directive)
def __init__(self, lineno, msg)
def __init__(self, lineno, msg)
void append(SDOPackage::NVList &dest, const SDOPackage::NVList &src)
Append an element to NVList.
def __else_cmd(self, args)
def __init__(self, dictkey, dictvalue)
def __init__(self, template, begin_mark="\, end_mark="\")
def __if_any_cmd(self, args)
def __init__(self, lineno, description)
def __for_cmd(self, args)
def __write_cmd_noindex(self, cmd)
def __if_index_cmd(self, args)
def set_index(self, index)
def get_list(self, keytext)
def __init__(self, lineno)
def get_text(self, keytext)
def has_key(self, keytext)
def __endfor_cmd(self, args)
def write_token(self, index)
def __parse_template(self, dict)