forked from hagihala/gily
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi.py
166 lines (126 loc) · 4.43 KB
/
wsgi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
from git import *
from gitdb import IStream
from StringIO import StringIO
class PageNotFound ( Exception ):
def __init__ ( self, name ):
self.name = name
def __str__ ( self ):
return 'Page (%s) is not found' % ( self.name )
class Wiki ( object ):
def __init__ ( self, repository, extension, homepage ):
self.homepage = homepage
self.extension = extension
if os.path.isdir( os.path.join( repository, '.git' ) ):
self.repository = Repo( repository, odbt=GitDB )
else:
if not os.path.isdir( repository ):
os.makedirs( repository )
os.chdir( repository )
self.repository = Repo.init()
def find_all ( self ):
repo = self.repository
if len(repo.refs) == 0:
return []
else:
pages = []
for entry in repo.tree().traverse():
if entry.type == 'blob':
pages.append( Page( entry, repo ) )
return pages
def find ( self, name ):
blob = self.find_blob(name)
if blob is None:
raise PageNotFound( name )
return Page(blob, self.repository)
def find_blob ( self, path ):
repo = self.repository
if len(repo.refs) == 0:
return None
else:
tree = repo.tree()
blob = None
try:
blob = tree/("%s.%s" % ( path, self.extension ))
except KeyError, e:
pass
return blob
def find_or_create ( self, name, content='' ):
try:
return self.find( name )
except PageNotFound, e:
page = Page( self.create_blob_for(name, data=content), self.repository )
page.commit('Page (%s) is created.' % ( name ))
return page
def create_blob_for ( self, path, data='' ):
repo = self.repository
istream = IStream('blob', len(data), StringIO(data))
repo.odb.store( istream )
blob = Blob( repo, istream.binsha, 0100644, "%s.%s" % ( path, self.extension ) )
return blob
class Page ( object ):
def __init__ ( self, blob, repository ):
self.blob = blob
self.repository = repository
def __str__ ( self ):
return self.blob.name
def name ( self ):
return os.path.splitext( self.blob.name )[0]
def content ( self ):
try:
return self.blob.data_stream.read()
except AttributeError, e:
return None
def update_content ( self, new ):
if self.content == new:
return None
fh = open( self.blob.abspath, 'w' )
fh.write( new )
fh.close()
return self.commit('Updated: %s' % ( self.blob.name ))
def commit ( self, message ):
index = self.repository.index
blob = self.blob
if os.path.isfile( blob.abspath ):
index.add([ blob.path ])
else:
index.add([ IndexEntry.from_blob( blob ) ])
return index.commit( message );
from flask import Flask, render_template, redirect, url_for, request
from config import REPOSITORY, FILE_EXTENSION, HOMEPAGE
wiki = Wiki( REPOSITORY, FILE_EXTENSION, HOMEPAGE )
app = Flask(__name__)
@app.route('/favicon.ico')
def favicon ():
return redirect( url_for('static', filename='favicon.ico') )
@app.route('/')
def toppage ():
try:
data = wiki.find( wiki.homepage )
return render_template('page.html', page=data)
except PageNotFound, notfound:
return redirect( url_for('edit_page', page=wiki.homepage) )
@app.route('/pages')
def index_page():
pages = wiki.find_all()
return render_template('pages.html', pages=pages)
@app.route('/<page>/edit')
def edit_page ( page ):
data = wiki.find_or_create( page )
return render_template('edit.html', page=data)
@app.route('/<page>', methods=[ 'GET' ])
def view_page ( page ):
try:
data = wiki.find( page )
return render_template('page.html', page=data)
except PageNotFound, notfound:
return redirect( url_for('edit_page', page=page) )
@app.route('/<page>', methods=[ 'POST' ])
def update_page ( page ):
data = wiki.find_or_create( page )
body = request.form.get('body', '')
data.update_content( body )
return redirect( url_for('view_page', page=page) )
application = app