Skip to content

Commit 751e2f8

Browse files
committed
Merge pull request #8 from olemb/refactor
Refactoring of Python code
2 parents 038d3e5 + 146760d commit 751e2f8

27 files changed

+106
-96
lines changed

cicero.py

+3-28
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
import os
2-
import argparse
1+
import sys
2+
from cicero.main import main
33

4-
5-
def parse_args():
6-
parser = argparse.ArgumentParser()
7-
arg = parser.add_argument
8-
9-
arg('--file', '-f', dest='filename', help='Serve local file')
10-
arg('--debug', dest='debug', action='store_true', default=False)
11-
arg('--host', dest='host', default=os.environ.get('HOST', '0.0.0.0'))
12-
arg('--port', dest='port', type=int, default=int(os.environ.get('PORT', 5000)))
13-
14-
return parser.parse_args()
15-
16-
17-
if __name__ == '__main__':
18-
args = parse_args()
19-
20-
if args.filename:
21-
from preview_app import app
22-
app.config['filename'] = args.filename
23-
app.config['image_dir'] = os.path.dirname(args.filename)
24-
else:
25-
from github_app import app
26-
27-
app.debug = args.debug
28-
29-
app.run(host=args.host, port=args.port)
4+
sys.exit(main())

cicero/__init__.py

Whitespace-only changes.

cicero/app.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
import flask
3+
4+
def _get_subdir(dirname):
5+
return os.path.join(os.path.dirname(__file__), dirname)
6+
7+
8+
app = flask.Flask('Cicero',
9+
template_folder=_get_subdir('templates'),
10+
static_folder=_get_subdir('static'))
11+
12+
13+
@app.errorhandler(404)
14+
def page_not_found(e):
15+
return flask.render_template('404.html'), 404

cicero/git.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import flask
2+
import urllib
3+
from .images import fix_images
4+
5+
blueprint = flask.Blueprint('git', __name__)
6+
7+
@blueprint.route('/')
8+
def home():
9+
return flask.render_template('index.html')
10+
11+
12+
@blueprint.route('/v1/github/<user_name>/<repo_name>/<branch_name>/<file_name>/remark/')
13+
def talk(user_name, repo_name, branch_name, file_name):
14+
try:
15+
url = 'https://raw.githubusercontent.com/{}/{}/{}/{}'.format(
16+
user_name, repo_name, branch_name, file_name)
17+
18+
response = urllib.urlopen(url)
19+
20+
markdown = response.readlines()
21+
if markdown == 'Not Found':
22+
return flask.render_template('404.html')
23+
24+
# we do not use https://raw.githubusercontent.com because it does not handle svg files
25+
prefix = 'https://cdn.rawgit.com/{}/{}/{}/'.format(user_name, repo_name, branch_name)
26+
return flask.render_template('slides.html', markdown=''.join(fix_images(markdown, prefix)))
27+
except IOError:
28+
return flask.render_template('404.html')

images.py cicero/images.py

File renamed without changes.

cicero/main.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import argparse
3+
from .app import app
4+
from . import git
5+
from . import preview
6+
7+
8+
def parse_args():
9+
parser = argparse.ArgumentParser()
10+
arg = parser.add_argument
11+
12+
arg('--file', '-f', dest='filename', help='Serve local file')
13+
arg('--debug', dest='debug', action='store_true', default=False)
14+
arg('--host', dest='host', default=os.environ.get('HOST', '0.0.0.0'))
15+
arg('--port', dest='port', type=int, default=int(os.environ.get('PORT', 5000)))
16+
17+
return parser.parse_args()
18+
19+
20+
def main():
21+
args = parse_args()
22+
23+
if args.filename:
24+
app.config['filename'] = args.filename
25+
app.config['imagedir'] = os.path.dirname(args.filename)
26+
app.register_blueprint(preview.blueprint)
27+
else:
28+
app.register_blueprint(git.blueprint)
29+
30+
app.debug = args.debug
31+
32+
app.run(host=args.host, port=args.port)

cicero/preview.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import io
2+
import flask
3+
from .images import fix_images
4+
5+
blueprint = flask.Blueprint('preview', __name__)
6+
7+
8+
@blueprint.route('/')
9+
def home():
10+
config = flask.current_app.config
11+
12+
with io.open(config['filename'], 'r', encoding='utf-8') as mkdfile:
13+
markdown = mkdfile.readlines()
14+
15+
markdown = ''.join(fix_images(markdown, 'images/'))
16+
17+
return flask.render_template('slides.html', markdown=markdown)
18+
19+
20+
@blueprint.route('/images/<path:path>')
21+
def serve_image(path):
22+
config = flask.current_app.config
23+
return flask.send_from_directory(config['imagedir'], path)
24+
25+
26+
@blueprint.errorhandler(404)
27+
def page_not_found(e):
28+
return flask.render_template('404.html'), 404
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

github_app.py

-33
This file was deleted.

paths.py

-6
This file was deleted.

preview_app.py

-29
This file was deleted.

0 commit comments

Comments
 (0)