From 5399156794d80ef2b75db0b5d7ff6e9b8a3b4bdd Mon Sep 17 00:00:00 2001 From: RRA Date: Fri, 10 Mar 2017 17:49:02 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + distribusi.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ m-m-mount.sh | 11 +++++++ page_template.py | 20 +++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 distribusi.py create mode 100644 m-m-mount.sh create mode 100644 page_template.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/distribusi.py b/distribusi.py new file mode 100644 index 0000000..bde443e --- /dev/null +++ b/distribusi.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-15 -*- + +#depends on python-magic 0.4 +import os, magic, argparse, sys +from page_template import * + +parser = argparse.ArgumentParser(""" + + distribusi is a content management system for the web producing static pages based on the filesystem. It is inspired by the automatic index functions featured in several webservers. As such it uses the file system and directory hierarchy to automatically list all the files in the + directory and providing them with html classes and tags for easy styling. + + """) +parser.add_argument('-d', '--directory', help="Select which directory to distribute") +args = parser.parse_args() + +if args.directory: + print('Generating directory listing for', args.directory) + directory = args.directory +else: + directory = '.' + +mime_type = magic.Magic(mime=True) + +file_types = {'image':'', 'pdf':'{}', +'text':'{}', 'video':'', 'audio':'','html':'{}'} + +def dict_by_value(dictionary, value): + return(list(dictionary.keys())[list(dictionary.values()).index(value)]) # returns the key for the given value +def div(mime, tag, *values): + #name, full_path + if 'image' in mime: + html ='
'.format(values[0])+tag+'
{}
'.format(values[0]) + else: + html = '
'.format(values[0])+tag+'
'.format(values[0]) + return html + +for root, dirs, files in os.walk(directory): + html = [] + for name in files: + if 'index.html' not in name: + full_path = os.path.join(root,name) + mime = mime_type.from_file(full_path) + mime, format = mime.split('/') #example: text plain + + if mime in file_types: + if name.endswith('.html'): + a = open(full_path).read() + else: + a = file_types[mime] + + if format in file_types: + a = file_types[format] + + if mime not in file_types and format not in file_types: #to catch exceptions we haven't defined in file_types before + a = "{}" + # print (mime,format, name) + + a = a.replace('{}',name) + html.append(div(mime,a,name)) + #html.append(a) + if root != directory: + html.append('../') + + for name in dirs: + i = os.path.join(root,name) + a = "{}/".replace('{}',name) + # print(a) + html.append(div('dir',a,'folder')) + + + with open(os.path.join(root,'index.html'),'w') as f: + f.write(html_head) + for line in html: + f.write(line+'\n') + f.write(html_footer) \ No newline at end of file diff --git a/m-m-mount.sh b/m-m-mount.sh new file mode 100644 index 0000000..cb64d6c --- /dev/null +++ b/m-m-mount.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +#distribusi automounter! +#requires usbmount + +if grep -qs '/media/usb' /proc/mounts; then + grep '/media/usb' | while read -r line; + do + figlet "$line" + done +fi \ No newline at end of file diff --git a/page_template.py b/page_template.py new file mode 100644 index 0000000..d7dcace --- /dev/null +++ b/page_template.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +html_head = """ + + + + + + + +""" + +html_footer=""" + + + """