diff options
Diffstat (limited to 'quoins/command.py')
-rw-r--r-- | quoins/command.py | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/quoins/command.py b/quoins/command.py new file mode 100644 index 0000000..2e6411a --- /dev/null +++ b/quoins/command.py | |||
@@ -0,0 +1,103 @@ | |||
1 | import os | ||
2 | from paste.script import command | ||
3 | from paste.deploy import appconfig | ||
4 | |||
5 | def get_config(self, config_spec): | ||
6 | section = self.options.section_name | ||
7 | if section is None: | ||
8 | if '#' in config_spec: | ||
9 | config_spec, section = config_spec.split('#', 1) | ||
10 | else: | ||
11 | section = 'main' | ||
12 | if not ':' in section: | ||
13 | plain_section = section | ||
14 | section = 'app:'+section | ||
15 | else: | ||
16 | plain_section = section.split(':', 1)[0] | ||
17 | if not config_spec.startswith('config:'): | ||
18 | config_spec = 'config:' + config_spec | ||
19 | if plain_section != 'main': | ||
20 | config_spec += '#' + plain_section | ||
21 | config_file = config_spec[len('config:'):].split('#', 1)[0] | ||
22 | config_file = os.path.join(os.getcwd(), config_file) | ||
23 | self.logging_file_config(config_file) | ||
24 | conf = appconfig(config_spec, relative_to=os.getcwd()) | ||
25 | return conf | ||
26 | |||
27 | class OpenIDCommand(command.Command): | ||
28 | max_args = 1 | ||
29 | min_args = 1 | ||
30 | |||
31 | usage = "CONFIG_FILE" | ||
32 | summary = "Setup Quoins OpenID tables" | ||
33 | group_name = "Quoins" | ||
34 | |||
35 | parser = command.Command.standard_parser(verbose=True) | ||
36 | parser.add_option('--name', | ||
37 | action='store', | ||
38 | dest='section_name', | ||
39 | default=None, | ||
40 | help='The name of the section to set up (default: app:main)') | ||
41 | |||
42 | def command(self): | ||
43 | config_file = self.args[0] | ||
44 | if self.verbose: | ||
45 | print "Using config file: %s" % config_file | ||
46 | |||
47 | conf = get_config(self, config_file) | ||
48 | |||
49 | from quoins.controllers import get_oid_connection | ||
50 | from openid.store.sqlstore import MySQLStore | ||
51 | import MySQLdb | ||
52 | |||
53 | conn = get_oid_connection(conf) | ||
54 | store = MySQLStore(conn) | ||
55 | try: | ||
56 | store.createTables() | ||
57 | except MySQLdb.OperationalError, message: | ||
58 | errorcode = message[0] | ||
59 | if errorcode == 1050: | ||
60 | print 'ok' | ||
61 | else: | ||
62 | raise | ||
63 | |||
64 | class BlogCommand(command.Command): | ||
65 | max_args = 1 | ||
66 | min_args = 1 | ||
67 | |||
68 | usage = "CONFIG_FILE" | ||
69 | summary = "Create a new Quoins Blog" | ||
70 | group_name = "Quoins" | ||
71 | |||
72 | parser = command.Command.standard_parser(verbose=True) | ||
73 | parser.add_option('--name', | ||
74 | action='store', | ||
75 | dest='section_name', | ||
76 | default=None, | ||
77 | help='The name of the section to set up (default: app:main)') | ||
78 | |||
79 | def command(self): | ||
80 | config_file = self.args[0] | ||
81 | if self.verbose: | ||
82 | print "Using config file: %s" % config_file | ||
83 | |||
84 | conf = get_config(self, config_file) | ||
85 | |||
86 | from model import Blog, DBSession, init_model | ||
87 | from sqlalchemy import create_engine | ||
88 | init_model(create_engine(conf.get('sqlalchemy.url'))) | ||
89 | |||
90 | title = raw_input("Blog title: ") | ||
91 | subtitle = raw_input("Blog subtitle: ") | ||
92 | comments = raw_input("Allow comments by default? (y/n) ") | ||
93 | if comments.strip().lower()=='y': | ||
94 | comments = True | ||
95 | else: | ||
96 | comments = False | ||
97 | b = Blog() | ||
98 | b.title = title.strip() | ||
99 | if subtitle: | ||
100 | b.subtitle = subtitle.strip() | ||
101 | b.allow_comments = comments | ||
102 | DBSession.add(b) | ||
103 | DBSession.flush() | ||