realms-wiki/reimagine/wiki.py

83 lines
2.4 KiB
Python
Raw Normal View History

from gittle import Gittle
2013-10-01 07:10:10 +03:00
from util import to_canonical
from lxml.html.clean import clean_html
2013-09-29 00:33:00 +03:00
2013-10-02 04:50:48 +03:00
class MyGittle(Gittle):
def file_history(self, path):
"""Returns all commits where given file was modified
"""
versions = []
commits_info = self.commit_info()
seen_shas = set()
for commit in commits_info:
try:
files = self.get_commit_files(commit['sha'], paths=[path])
file_path, file_data = files.items()[0]
except IndexError:
continue
file_sha = file_data['sha']
if file_sha in seen_shas:
continue
else:
seen_shas.add(file_sha)
versions.append(dict(author=commit['author']['name'],
time=commit['time'],
file_sha=file_sha,
sha=commit['sha'],
message=commit['message']))
return versions
class Wiki():
path = None
base_path = '/'
default_ref = 'master'
default_committer_name = 'Anon'
default_committer_email = 'anon@anon.anon'
2013-10-02 04:50:48 +03:00
index_page = 'home'
2013-09-29 00:33:00 +03:00
repo = None
2013-10-01 07:10:10 +03:00
def __init__(self, path):
try:
2013-10-02 04:50:48 +03:00
self.repo = MyGittle.init(path)
2013-10-01 07:10:10 +03:00
except OSError:
# Repo already exists
2013-10-02 04:50:48 +03:00
self.repo = MyGittle(path)
2013-10-01 07:10:10 +03:00
self.path = path
2013-10-01 07:10:10 +03:00
def write_page(self, name, content, message=None, create=False):
name = to_canonical(name)
#content = clean_html(content)
filename = name.lower() + ".md"
f = open(self.path + "/" + filename, 'w')
f.write(content)
f.close()
if create:
self.repo.add(filename)
return self.repo.commit(name=self.default_committer_name,
email=self.default_committer_email,
message=message,
files=[filename])
2013-10-01 07:10:10 +03:00
def rename_page(self, old_name, new_name):
self.repo.mv([old_name, new_name])
2013-10-02 04:50:48 +03:00
def get_page(self, name, sha='HEAD'):
2013-09-29 00:33:00 +03:00
name = name.lower() + ".md"
2013-10-01 07:10:10 +03:00
try:
2013-10-02 04:50:48 +03:00
return self.repo.get_commit_files(sha, paths=[name]).get(name)
2013-10-01 07:10:10 +03:00
except KeyError:
# HEAD doesn't exist yet
2013-10-02 04:50:48 +03:00
return None
def get_history(self, name):
name = name.lower() + ".md"
return self.repo.file_history(name)