chiark / gitweb /
plugins: new m.filesize plugin.
authorVladimír Vondruš <mosra@centrum.cz>
Wed, 11 Oct 2017 19:50:00 +0000 (21:50 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Wed, 11 Oct 2017 19:54:40 +0000 (21:54 +0200)
pelican-plugins/m/filesize.py [new file with mode: 0644]

diff --git a/pelican-plugins/m/filesize.py b/pelican-plugins/m/filesize.py
new file mode 100644 (file)
index 0000000..3155be1
--- /dev/null
@@ -0,0 +1,42 @@
+import os
+import gzip
+from docutils import nodes
+from docutils.parsers import rst
+from pelican import signals
+
+settings = {}
+
+def init(pelicanobj):
+    settings['path'] = pelicanobj.settings.get('PATH', 'content')
+    pass
+
+def filesize(name, rawtext, text, lineno, inliner, options={}, content=[]):
+    size = os.path.getsize(text.format(filename=os.path.join(os.getcwd(), settings['path'])))
+
+    for unit in ['','k','M','G','T']:
+        if abs(size) < 1024.0:
+            size_string = "%3.1f %sB" % (size, unit)
+            break
+        size /= 1024.0
+    else: size_string = "%.1f PB" % size
+
+    return [nodes.inline(size_string, size_string)], []
+
+def filesize_gz(name, rawtext, text, lineno, inliner, options={}, content=[]):
+    with open(text.format(filename=os.path.join(os.getcwd(), settings['path'])), mode='rb') as f:
+        size = len(gzip.compress(f.read()))
+
+    for unit in ['','k','M','G','T']:
+        if abs(size) < 1024.0:
+            size_string = "%3.1f %sB" % (size, unit)
+            break
+        size /= 1024.0
+    else: size_string = "%.1f PB" % size
+
+    return [nodes.inline(size_string, size_string)], []
+
+def register():
+    signals.initialized.connect(init)
+
+    rst.roles.register_local_role('filesize', filesize)
+    rst.roles.register_local_role('filesize-gz', filesize_gz)