summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@macaw.me>2020-06-14 21:48:20 +0800
committerFrederick Yin <fkfd@macaw.me>2020-06-14 21:48:20 +0800
commit4110a4df25170289dce8690a572d26c58063adb9 (patch)
tree176f04ccb064b7c797e4f1072dfb4d7fd4d0f505
parentd34ba645c25f13ed53c64025d7a08ebda192af8a (diff)
Humanized file sizes
Includes hurry.filesize dep and its license ZPL-2.0
-rw-r--r--README.md7
-rw-r--r--ZPL-2.059
-rw-r--r--git-gmi/git.py11
3 files changed, 73 insertions, 4 deletions
diff --git a/README.md b/README.md
index 34523b6..fdc7f36 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ Dependencies:
- relatively new version of Python (3.8.3 personally)
- pygit2 (`pip install pygit2`)
+- hurry.filesize (`pip install hurry.filesize`)
- a gemini server capable of serving CGI
You need to edit the shebang of `git-gmi/cgi`:
@@ -51,4 +52,8 @@ Then, if you use Jetforce, run:
$ jetforce --dir /path/to/gemini --cgi-dir git [other arguments]
```
-Access your instance at `gemini://your-domain-or-ip/git/cgi/`. Don't forget the trailing slash. \ No newline at end of file
+Access your instance at `gemini://your-domain-or-ip/git/cgi/`. Don't forget the trailing slash, because jetforce is not capable of telling git.gmi its existence, potentially messing up relative links.
+
+## Licensing's a Bitch
+
+`hurry.filesize` is licensed under ZPL-2.0. I'm obligated, on ZPL-2.0's terms, to include it. I am once again ~~asking~~ begging you to use permissive licenses like a normal human being. \ No newline at end of file
diff --git a/ZPL-2.0 b/ZPL-2.0
new file mode 100644
index 0000000..44e0648
--- /dev/null
+++ b/ZPL-2.0
@@ -0,0 +1,59 @@
+Zope Public License (ZPL) Version 2.0
+-----------------------------------------------
+
+This software is Copyright (c) Zope Corporation (tm) and
+Contributors. All rights reserved.
+
+This license has been certified as open source. It has also
+been designated as GPL compatible by the Free Software
+Foundation (FSF).
+
+Redistribution and use in source and binary forms, with or
+without modification, are permitted provided that the
+following conditions are met:
+
+1. Redistributions in source code must retain the above
+ copyright notice, this list of conditions, and the following
+ disclaimer.
+
+2. Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions, and the following
+ disclaimer in the documentation and/or other materials
+ provided with the distribution.
+
+3. The name Zope Corporation (tm) must not be used to
+ endorse or promote products derived from this software
+ without prior written permission from Zope Corporation.
+
+4. The right to distribute this software or to use it for
+ any purpose does not give you the right to use Servicemarks
+ (sm) or Trademarks (tm) of Zope Corporation. Use of them is
+ covered in a separate agreement (see
+ http://www.zope.com/Marks).
+
+5. If any files are modified, you must cause the modified
+ files to carry prominent notices stating that you changed
+ the files and the date of any change.
+
+Disclaimer
+
+ THIS SOFTWARE IS PROVIDED BY ZOPE CORPORATION ``AS IS''
+ AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
+ NO EVENT SHALL ZOPE CORPORATION OR ITS CONTRIBUTORS BE
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ DAMAGE.
+
+
+This software consists of contributions made by Zope
+Corporation and many individuals on behalf of Zope
+Corporation. Specific attributions are listed in the
+accompanying credits file.
diff --git a/git-gmi/git.py b/git-gmi/git.py
index 990ca0c..c8fa5c6 100644
--- a/git-gmi/git.py
+++ b/git-gmi/git.py
@@ -1,4 +1,5 @@
from pygit2 import *
+from hurry.filesize import size, alternative
import mimetypes
from const import *
@@ -6,6 +7,10 @@ mimetypes.add_type("text/gemini", ".gmi")
mimetypes.add_type("text/gemini", ".gemini")
+def convert_filesize(bytes: int) -> str:
+ return size(bytes, system=alternative)
+
+
class GitGmiRepo:
def __init__(self, name: str, path: str):
self.name = name
@@ -38,7 +43,7 @@ class GitGmiRepo:
):
found_readme = True
response += (
- f"## {item['name']} | {item['size']} bytes\n"
+ f"## {item['name']} | {convert_filesize(item['size'])}\n"
f"{item['blob'].data.decode('utf-8')}"
)
if not found_readme:
@@ -147,7 +152,7 @@ class GitGmiRepo:
f"=> {item['name']}/ {item['name']}/ | {item['size']} items\n"
)
elif item["type"] == "file":
- response += f"=> {item['name']} {item['name']} | {item['size']} bytes\n"
+ response += f"=> {item['name']} {item['name']} | {convert_filesize(item['size'])}\n"
return response
def get_blob(self, commit_str: str, location=[]) -> Blob:
@@ -169,7 +174,7 @@ class GitGmiRepo:
response = (
f"{STATUS_SUCCESS} {META_GEMINI}\n"
+ self.generate_header()
- + f"## {self.name}/{'/'.join(location)} | {blob.size} bytes\n\n"
+ + f"## {self.name}/{'/'.join(location)} | {convert_filesize(blob.size)}\n\n"
f"=> {blob.name}?raw view raw\n\n"
f"```\n"
)