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.
  • Diff rendering mode:
  • inline
  • side by side

.gitignore

11
2*.*~2*.*~
3*.pyc3*.pyc
4conf.py

readConfig.py

1#!/usr/bin/python
2
3# Read Mouchak Configuration
4
5
6import re
7
8def readConfig():
9 confFile = 'mouchak.conf'
10 fh = open(confFile, 'r')
11 contents = fh.read()
12
13 match = re.search('DB=(.*)', contents)
14 dbName = match.group(1)
15 match = re.search('SITE_TITLE=(.*)', contents)
16 title = match.group(1)
17 match = re.search('HOST=(.*)', contents)
18 host = match.group(1)
19 match = re.search('PORT=(.*)', contents)
20 port = match.group(1)
21
22 return {'db': dbName, 'site_title': title, 'host': host, 'port': int(port)}

sampleConf.py

1# Rename this file to conf.py and adjust the following
2# as required
3DB = 'test_mouchak'
4SITE_TITLE = 'Testing Mouchak'
5HOST = '0.0.0.0'
6PORT = 5000

server.py

6import flask6import flask
7import pymongo7import pymongo
8import bson8import bson
9import readConfig
9import conf
1010
11app = flask.Flask(__name__)11app = flask.Flask(__name__)
1212
1313
14config = readConfig.readConfig()
1514
16dbClient = pymongo.MongoClient()15dbClient = pymongo.MongoClient()
17db = dbClient[config['db']]
16db = dbClient[conf.DB]
18siteContent = db['content']17siteContent = db['content']
19siteMenu = db['menu']18siteMenu = db['menu']
20if siteMenu.find_one() == None:19if siteMenu.find_one() == None:
43@app.route('/', methods=['GET'])43@app.route('/', methods=['GET'])
44def index():44def index():
45 return flask.render_template('index.html', content=getContent(),45 return flask.render_template('index.html', content=getContent(),
46 title=config['site_title'])
46 title=config.SITE_TITLE)
4747
4848
49@app.route('/edit', methods=['GET'])49@app.route('/edit', methods=['GET'])
50def edit():50def edit():
51 return flask.render_template('editor.html', content=getContent(),51 return flask.render_template('editor.html', content=getContent(),
52 title=config['site_title'])
52 title=config)
5353
5454
55@app.route('/page', methods=['POST'])55@app.route('/page', methods=['POST'])
118118
119if __name__ == "__main__":119if __name__ == "__main__":
120 print config120 print config
121 app.run(debug=True, host=config['host'], port=config['port'])
121 app.run(debug=True, host=config.HOST, port=config.PORT)