From 59c2e948797d088e795cc2290fa8c3004ddb71c1 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sun, 14 Jun 2020 18:03:01 +0800 Subject: Massive improvements - Python CGI server script renamed to gateway.py to avoid confusion - Repo not found error handling - Page header & navs - Fix faulty 20 response header - View (non-)raw blob --- git-gmi/git.py | 55 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 14 deletions(-) (limited to 'git-gmi/git.py') diff --git a/git-gmi/git.py b/git-gmi/git.py index 9a0ab0d..990ca0c 100644 --- a/git-gmi/git.py +++ b/git-gmi/git.py @@ -2,6 +2,9 @@ from pygit2 import * import mimetypes from const import * +mimetypes.add_type("text/gemini", ".gmi") +mimetypes.add_type("text/gemini", ".gemini") + class GitGmiRepo: def __init__(self, name: str, path: str): @@ -9,25 +12,37 @@ class GitGmiRepo: self.path = path try: self.repo = Repository(path) - except FileNotFoundError: - print(f"Error: repository {path} not found") + except GitError: + raise FileNotFoundError(f"Error: no such repo: {name}") + + def generate_header(self): + header = ( + f"# {self.name}\n" + f"=> {CGI_PATH} {GIT_GMI_SITE_TITLE}\n" + f"=> {CGI_PATH}{self.name}/summary summary\n" + f"=> {CGI_PATH}{self.name}/tree/master/ tree\n" + f"=> {CGI_PATH}{self.name}/log log\n\n" + ) + return header def view_summary(self) -> str: + response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header() tree = self.get_tree("master") trls = self.list_tree(tree) + found_readme = False for item in trls: - if item["type"] == "file" and item["name"].lower().split(".")[0] == ( - "readme" + if ( + item["type"] == "file" + and item["name"].lower().split(".")[0] == ("readme") + and not found_readme ): - # mimetypes.guess_type() returns tuple (type, encoding) - # only the first one of which we care about - response = ( - f"{STATUS_SUCCESS} {mimetypes.guess_type(item['name'])[0]}\n" - "=> tree/master/ tree\n" - "=> log/ log\n\n" + found_readme = True + response += ( + f"## {item['name']} | {item['size']} bytes\n" + f"{item['blob'].data.decode('utf-8')}" ) - response += item["blob"].data.decode("utf-8") - break + if not found_readme: + response += "## No readme found." return response def get_commit_log(self) -> list: @@ -48,7 +63,7 @@ class GitGmiRepo: return log # reverse chronical order def view_log(self) -> str: - response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header() log = self.get_commit_log() for cmt in log: response += f"## {cmt['short_id']} - {cmt['author']}\n{cmt['msg']}\n\n" @@ -123,7 +138,7 @@ class GitGmiRepo: def view_tree(self, branch: str, location=[]) -> str: # actual Gemini response # consists of a header and a body - response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header() tree = self.get_tree(branch) contents = self.list_tree(tree, location) for item in contents: @@ -150,6 +165,18 @@ class GitGmiRepo: raise FileNotFoundError(f"Error: No such tree: {'/'.join(location[:-1])}") def view_blob(self, branch: str, location=[]) -> str: + blob = self.get_blob(branch, location) + response = ( + f"{STATUS_SUCCESS} {META_GEMINI}\n" + + self.generate_header() + + f"## {self.name}/{'/'.join(location)} | {blob.size} bytes\n\n" + f"=> {blob.name}?raw view raw\n\n" + f"```\n" + ) + response += blob.data.decode("utf-8") + "\n```" + return response + + def view_raw_blob(self, branch: str, location=[]) -> str: blob = self.get_blob(branch, location) guessed_mimetype = mimetypes.guess_type(blob.name)[0] or "text/plain" response = f"{STATUS_SUCCESS} {guessed_mimetype}\n" -- cgit v1.2.3