• R/O
  • SSH
  • HTTPS

pyhabu: Commit


Commit MetaInfo

Revision90 (tree)
Time2008-01-09 10:29:19
Authorliris

Log Message

skelton creator

Change Summary

Incremental Difference

--- trunk/tools/create_habuplugin.py (nonexistent)
+++ trunk/tools/create_habuplugin.py (revision 90)
@@ -0,0 +1,132 @@
1+#!/usr/bin/env python
2+
3+"""
4+Create skelton of pyhabu plugin.
5+"""
6+
7+from getopt import gnu_getopt
8+import os
9+import os.path
10+
11+_TEMPLATE = """
12+import habu.log as log
13+
14+'''
15+ %(plugin-name)s - %(desc)s
16+ @copyright: %(author)s <%(email)s>
17+ @license: %(license)s
18+'''
19+
20+class PyHabuPlugin(object):
21+ def __init__(self, config, environ):
22+ self.config = config
23+ self.environ = environ
24+
25+ def execute(self, context):
26+ log.debug(u"Using skeleton.")
27+ return context
28+
29+
30+def create(*argv, **kwargv):
31+ return PyHabuPlugin(*argv, **kwargv)
32+
33+"""
34+
35+
36+
37+def parseArg(argv):
38+ parsedArg = {}
39+ try:
40+ options, arguments = gnu_getopt(argv[1:], "hp:d:l:a:m:",
41+ ["help",
42+ "plugin-name=", "description=",
43+ "license=",
44+ "author=", "mail="])
45+ except:
46+ printUsage(argv[0])
47+
48+ for opt, arg in options:
49+ if opt in ("-h", "---help"):
50+ printUsage(argv[0])
51+ elif opt in ("-p", "--plugin-name"):
52+ parsedArg["plugin-name"] = arg
53+ elif opt in ("-d", "--description"):
54+ parsedArg["desc"] = arg
55+ elif opt in ("-l", "--license"):
56+ parsedArg["license"] = arg
57+ elif opt in ("-a", "--author"):
58+ parsedArg["author"] = arg
59+ elif opt in ("-m", "--mail"):
60+ parsedArg["email"] = arg
61+
62+ return parsedArg
63+
64+# key, default, "description"
65+
66+_CONVERSATION_LIST = (
67+ ("plugin-name", "Plugin Name", None),
68+ ("desc", "Short Description", ""),
69+ ("license", "License", "GPL"),
70+ ("author", "Plugin Author Name", None),
71+ ("email", "Author Email Address", None),
72+ )
73+
74+def talk(conversation, parsedArg):
75+ if conversation[2]:
76+ prompt = "Please Input %s [%s]: " % conversation[1:]
77+ else:
78+ prompt = "Please Input %s: " % conversation[1]
79+
80+ result = raw_input(prompt)
81+ if not result:
82+ if conversation[2]:
83+ parsedArg[conversation[0]] = conversation[2]
84+ else:
85+ talk(conversation, parsedArg)
86+ else:
87+ parsedArg[conversation[0]] = result
88+
89+
90+def runInteractive(parsedArg):
91+ for conversation in _CONVERSATION_LIST:
92+ if not parsedArg.has_key(conversation[0]):
93+ talk(conversation, parsedArg)
94+
95+
96+def ask(fpath):
97+ result = raw_input("File(%s) exists. Do you want to overwrite it?[y/N]")
98+ result = result.lower().strip()
99+
100+ if not result or result in ("n", "no"):
101+ return False
102+ elif result in ("y", "yes"):
103+ return True
104+ else:
105+ return ask(fpath)
106+
107+def createSkelton(parsedArg):
108+ fpath = parsedArg["plugin-name"].replace(".", "/") + ".py"
109+ basedir = os.path.dirname(fpath)
110+ if not os.path.exists(basedir):
111+ os.makedirs(basedir)
112+
113+ if os.path.exists(fpath):
114+ if not ask(fpath):
115+ print "abort"
116+ return
117+
118+ fp = open(fpath, "w")
119+ fp.write(_TEMPLATE % parsedArg)
120+ fp.close()
121+
122+
123+
124+def main(argv):
125+ parsedArg = parseArg(argv)
126+ runInteractive(parsedArg)
127+ createSkelton(parsedArg)
128+
129+
130+if __name__ == "__main__":
131+ import sys
132+ main(sys.argv)
Show on old repository browser