Distribusi is a content management system for the web that produces static pages based on the file system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.4 KiB

#!/usr/bin/env python3
8 years ago
# -*- coding: iso-8859-15 -*-
#depends on python-magic 0.4
import os, magic, argparse, sys
from page_template import *
parser = argparse.ArgumentParser("""
distbusi is a content management system for the web that produces static index pages based on folders in the filesystem. It is inspired by the automatic index functions featured in several web servers. It works by traversing 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.
""")
8 years ago
parser.add_argument('-d', '--directory', help="Select which directory to distribute")
parser.add_argument('-v', '--verbose', help="Print verbose debug output", action="store_true")
8 years ago
args = parser.parse_args()
if args.directory:
if args.verbose:
print('Generating directory listing for', args.directory)
8 years ago
directory = args.directory
else:
directory = '.'
mime_type = magic.Magic(mime=True)
file_types = {'image':'<img class="image" src="{}">', 'pdf':'<object data="{}" class="pdf" type="application/pdf"><embed src="{}" type="application/pdf" /></object>',
7 years ago
'text':'<a href="{}" class="text">{}</a>', 'video':'<video class="video" controls><source src="{}"></source></video>', 'audio':'<audio controls class="audio"> <source src="{}"></source></audio>'}
8 years ago
code_types = ['x-c', 'html']
8 years ago
def dict_by_value(dictionary, value):
return(list(dictionary.keys())[list(dictionary.values()).index(value)]) # returns the key for the given value
7 years ago
8 years ago
def div(mime, tag, *values):
#name, full_path
class_name = values[0].split('.')[0].replace(' ', '_')
8 years ago
if 'image' in mime:
html ='<div class="{}">'.format(class_name)+tag+'<br><span class="filename">{}</span></div>'.format(values[0])
elif 'pdf' in format:
html ='<div class="{}">'.format(class_name)+tag+'<br><class="filename">{}</span></div>'.format(values[0])
8 years ago
else:
html = '<div class="{}">'.format(class_name)+tag+'</div>'.format(values[0])
8 years ago
return html
for root, dirs, files in os.walk(directory):
html = []
if args.verbose:
print ('Listing', root)
8 years ago
for name in files:
if args.verbose:
print ('Adding', name)
8 years ago
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 args.verbose:
print(mime, format)
if mime in file_types: #expansion for different kind of textfiles
if mime == 'text':
if name.endswith('.html') or name.endswith('.txt'): #what types of text files to expand
a = open(full_path).read()
elif format in code_types: #if the plain text is code, which types do we wrap in pre-tags?
a = "<pre>"+open(full_path).read()+"</pre>"
else:
a = file_types[mime]
8 years ago
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 = "<a href='{}'>{}</a>"
if args.verbose:
print ('mime-type not in list, adding as href: \n', mime,format, name)
8 years ago
a = a.replace('{}',name)
html.append(div(mime,a,name))
#html.append(a)
if root != directory:
html.append('<a href="../">../</a>')
for name in dirs:
i = os.path.join(root,name)
a = "<a href='{}' class='dir'>{}/</a>".replace('{}',name)
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')
7 years ago
f.write(html_footer)