Pythonでmayaaファイルを作成してみる
惜しみなく実際に仕事で使っているスクリプトを公開するこのコーナー。
密かに最近python大人気なんだよね。
一応ほんpyとか言っちゃってるのでツール的なものもpythonで。
(周りは迷惑だけど!)
HTMLのidからmayaaファイルの雛形を作成するスクリプトはこんな感じ。
from HTMLParser import HTMLParser from os import path from struct import * tag_data = {} class MayaaHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): d = {} id = None d['tag'] = tag for name, value in attrs: if name != 'name' and name != 'value': d[name] = value if name == 'id' : id = value if id : tag_data[id] = d def main(url): import urllib data = urllib.urlopen(url) mp = MayaaHTMLParser() mp.feed(data.read()) file_name = path.splitext(path.basename(url))[0].capitalize() print """ <?xml version="1.0" encoding="UTF-8"?> <m:mayaa xmlns:m="http://mayaa.seasar.org" xmlns:html="http://struts.apache.org/tags-html" xmlns:bean="http://struts.apache.org/tags-bean" xmlns:s2struts="http://www.seasar.org/tags-s2struts" m:noCache="true" """ for id in tag_data : d = tag_data[id] tag = d.pop('tag').lower() id = d.pop('id') if tag == 'input': type = d.pop('type') attr = get_attr(d) if type == 'text' : print '\t\t<html:%s m:id="%s" name="%sDto" property="%s" %s errorStyleClass="text-error"/>' % (type, id, file_name, id, attr) elif tag == 'select': print """ <html:select m:id="%s" name="%sDto" property="%s" > <html:optionsCollection property="%sList"/> </html:select> """ % (id, file_name, id, id) elif tag in ['span', 'div']: attr = get_attr(d) print '\t\t<bean:write m:id="%s" name="%sDto" property="%s" %s />' % (id, file_name, id, attr) elif tag in ['textarea']: attr = get_attr(d) print '\t\t<html:textarea m:id="%s" name="%sDto" property="%s" %s />' % (id, file_name, id, attr) elif tag in ['form']: print '\t\t<html:form m:id="%s" focus="title" action="/%sSearch"/>' % (id, id) else: print id, tag, d print """ </m:mayaa> """ def get_attr(d): str = [] for key in d: str.append('%s="%s"' % (key, d[key])) str = ' '.join(str) return str if __name__=="__main__": main('file:C:/test.html')
汚いけど動けばいいのだ。何事もまず動くものを!
つーか実際これ仕事で使ってますよ。
来週までにはcheetah使って
- Dto
- Action
とかも作れちゃうようにする予定。
えーっとteeda-extentionはどるていんにお任せできちゃうのでそっち推奨です!
機会があれば作ろ。
うくく。