Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit c78b30f

Browse files
author
raylu
committed
tornado template
0 parents  commit c78b30f

File tree

11 files changed

+138
-0
lines changed

11 files changed

+138
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__
2+
config.yaml

Diff for: .gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "cleancss.git"]
2+
path = cleancss.git
3+
url = git://github.com/raylu/py-cleancss.git

Diff for: LICENSE

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1-clause JSON License
2+
3+
This software shall be used for good, not evil.

Diff for: cleancss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cleancss.git/cleancss

Diff for: cleancss.git

Submodule cleancss.git added at ddc06e4

Diff for: config.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import yaml
2+
3+
class Config:
4+
def __init__(self, cdict):
5+
attrs = set(self.attrs) # copy and "unfreeze"
6+
for k, v in cdict.items():
7+
attrs.remove(k) # check if the key is allowed, mark it as present
8+
setattr(self, k, v)
9+
if len(attrs) != 0:
10+
raise KeyError('missing required bot config keys: %s' % attrs)
11+
12+
class WebConfig(Config):
13+
attrs = frozenset([
14+
'port',
15+
'host',
16+
'cookie_secret',
17+
'debug',
18+
])
19+
20+
__doc = yaml.load(open('config.yaml', 'r'))
21+
web = WebConfig(__doc['web'])

Diff for: config.yaml.example

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
web:
2+
port: 8888
3+
host: 'localhost:8888'
4+
cookie_secret: 'dis is super sekrit'
5+
debug: true
6+
7+
# vim: set et ft=yaml:

Diff for: server.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
5+
import cleancss
6+
import tornado.gen
7+
import tornado.httpclient
8+
import tornado.ioloop
9+
import tornado.web
10+
11+
from config import web as config
12+
13+
class BaseHandler(tornado.web.RequestHandler):
14+
def render(self, *args, **kwargs):
15+
kwargs['host'] = config.host
16+
return super(BaseHandler, self).render(*args, **kwargs)
17+
18+
def render_string(self, *args, **kwargs):
19+
s = super(BaseHandler, self).render_string(*args, **kwargs)
20+
return s.replace(b'\n', b'') # this is like Django's {% spaceless %}
21+
22+
class MainHandler(BaseHandler):
23+
def get(self):
24+
self.render('home.html')
25+
26+
class CSSHandler(tornado.web.RequestHandler):
27+
def get(self, css_path):
28+
css_path = os.path.join(os.path.dirname(__file__), 'static', css_path) + '.ccss'
29+
with open(css_path, 'r') as f:
30+
self.set_header('Content-Type', 'text/css')
31+
self.write(cleancss.convert(f))
32+
33+
if __name__ == '__main__':
34+
tornado.web.Application(
35+
handlers=[
36+
(r'/', MainHandler),
37+
(r'/(css/.+)\.css', CSSHandler),
38+
],
39+
template_path=os.path.join(os.path.dirname(__file__), 'templates'),
40+
static_path=os.path.join(os.path.dirname(__file__), 'static'),
41+
cookie_secret=config.cookie_secret,
42+
debug=config.debug,
43+
).listen(config.port)
44+
print('listening on :%d' % config.port)
45+
tornado.ioloop.IOLoop.instance().start()

Diff for: static/css/base.ccss

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
*:
2+
box-sizing: border-box;
3+
-moz-box-sizing: border-box;
4+
-webkit-box-sizing: border-box;
5+
6+
body:
7+
margin: 0;
8+
padding: 0;
9+
height: 100%;
10+
background: #000;
11+
color: #ddd;
12+
font-family: sans-serif;
13+
14+
a:
15+
text-decoration: none;
16+
color: #dde;
17+
18+
form:
19+
input:
20+
background: #fff;
21+
border: 1px solid #cdd;
22+
padding: 5px 10px;
23+
24+
.clear:
25+
clear: both;
26+
27+
#wrapper:
28+
width: 900px;
29+
margin: 50px auto;
30+
background: #111;

Diff for: templates/base.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" type="text/css" href="/css/base.css" />
5+
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools.js"></script>
6+
</head>
7+
<body>
8+
<div id="wrapper">
9+
<div id="topbar">
10+
<div id="title"></div>
11+
<div id="nav">
12+
</div>
13+
</div>
14+
15+
{% block main %}{% end %}
16+
</div>
17+
</body>
18+
</html>

Diff for: templates/home.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "base.html" %}
2+
3+
{% block main %}
4+
5+
hi!
6+
7+
{% end %}

0 commit comments

Comments
 (0)