Commit 668b408d08ea7f9491935f46853fb83cf8cf9544
Simplified configuration file
Using a simplified version of configuration file. No need to parse config
files anymore. Just store the as strings in a python file.
| | | | 1 | | 1 | |
---|
2 | *.*~ | 2 | *.*~ |
---|
3 | *.pyc | 3 | *.pyc |
---|
| | 4 | conf.py |
---|
| | | | 1 | #!/usr/bin/python | | #!/usr/bin/python |
---|
2 | | | |
---|
3 | # Read Mouchak Configuration | | # Read Mouchak Configuration |
---|
4 | | | |
---|
5 | | | |
---|
6 | import re | | import re |
---|
7 | | | |
---|
8 | def readConfig(): | | def readConfig(): |
---|
9 | confFile = 'mouchak.conf' | | confFile = 'mouchak.conf' |
---|
10 | fh = open(confFile, 'r') | | fh = open(confFile, 'r') |
---|
11 | contents = fh.read() | | contents = fh.read() |
---|
12 | | | |
---|
13 | match = re.search('DB=(.*)', contents) | | match = re.search('DB=(.*)', contents) |
---|
14 | dbName = match.group(1) | | dbName = match.group(1) |
---|
15 | match = re.search('SITE_TITLE=(.*)', contents) | | match = re.search('SITE_TITLE=(.*)', contents) |
---|
16 | title = match.group(1) | | title = match.group(1) |
---|
17 | match = re.search('HOST=(.*)', contents) | | match = re.search('HOST=(.*)', contents) |
---|
18 | host = match.group(1) | | host = match.group(1) |
---|
19 | match = re.search('PORT=(.*)', contents) | | match = re.search('PORT=(.*)', contents) |
---|
20 | port = match.group(1) | | port = match.group(1) |
---|
21 | | | |
---|
22 | return {'db': dbName, 'site_title': title, 'host': host, 'port': int(port)} | | return {'db': dbName, 'site_title': title, 'host': host, 'port': int(port)} |
---|
| | | | | | 1 | # Rename this file to conf.py and adjust the following |
---|
| | 2 | # as required |
---|
| | 3 | DB = 'test_mouchak' |
---|
| | 4 | SITE_TITLE = 'Testing Mouchak' |
---|
| | 5 | HOST = '0.0.0.0' |
---|
| | 6 | PORT = 5000 |
---|
| | | | 6 | import flask | 6 | import flask |
---|
7 | import pymongo | 7 | import pymongo |
---|
8 | import bson | 8 | import bson |
---|
9 | import readConfig | | import readConfig |
---|
| | 9 | import conf | 10 | | 10 | |
---|
11 | app = flask.Flask(__name__) | 11 | app = flask.Flask(__name__) |
---|
12 | | 12 | |
---|
13 | | 13 | |
---|
14 | config = readConfig.readConfig() | | config = readConfig.readConfig() |
---|
15 | | 14 | |
---|
16 | dbClient = pymongo.MongoClient() | 15 | dbClient = pymongo.MongoClient() |
---|
17 | db = dbClient[config['db']] | | db = dbClient[config['db']] |
---|
| | 16 | db = dbClient[conf.DB] | 18 | siteContent = db['content'] | 17 | siteContent = db['content'] |
---|
19 | siteMenu = db['menu'] | 18 | siteMenu = db['menu'] |
---|
20 | if siteMenu.find_one() == None: | 19 | if siteMenu.find_one() == None: |
---|
… | | … | |
---|
43 | @app.route('/', methods=['GET']) | 43 | @app.route('/', methods=['GET']) |
---|
44 | def index(): | 44 | def index(): |
---|
45 | return flask.render_template('index.html', content=getContent(), | 45 | return flask.render_template('index.html', content=getContent(), |
---|
46 | title=config['site_title']) | | title=config['site_title']) |
---|
| | 46 | title=config.SITE_TITLE) | 47 | | 47 | |
---|
48 | | 48 | |
---|
49 | @app.route('/edit', methods=['GET']) | 49 | @app.route('/edit', methods=['GET']) |
---|
50 | def edit(): | 50 | def edit(): |
---|
51 | return flask.render_template('editor.html', content=getContent(), | 51 | return flask.render_template('editor.html', content=getContent(), |
---|
52 | title=config['site_title']) | | title=config['site_title']) |
---|
| | 52 | title=config) | 53 | | 53 | |
---|
54 | | 54 | |
---|
55 | @app.route('/page', methods=['POST']) | 55 | @app.route('/page', methods=['POST']) |
---|
… | | … | |
---|
118 | | 118 | |
---|
119 | if __name__ == "__main__": | 119 | if __name__ == "__main__": |
---|
120 | print config | 120 | print config |
---|
121 | app.run(debug=True, host=config['host'], port=config['port']) | | app.run(debug=True, host=config['host'], port=config['port']) |
---|
| | 121 | app.run(debug=True, host=config.HOST, port=config.PORT) |