Browse Source

- changed variable names for clarity

- added f;ag to remove indexes
- made '--no-filename more consistent'
- styled verbose output better.
master
rra 5 years ago
parent
commit
52830ff7c4
  1. 96
      distribusi/distribusi.py
  2. 11
      distribusi/mappings.py

96
distribusi/distribusi.py

@ -7,7 +7,7 @@ import magic
from PIL import Image from PIL import Image
from distribusi.page_template import html_footer, html_head from distribusi.page_template import html_footer, html_head
from distribusi.mappings import CODE_TYPES, FILE_TYPES from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES
MIME_TYPE = magic.Magic(mime=True) MIME_TYPE = magic.Magic(mime=True)
@ -43,86 +43,91 @@ def thumbnail(image, name, args):
"<figure><a href='{}'><img class='thumbnail' src='data:image/jpg;base64,{}'></a><figcaption>{}</figcaption></figure>" "<figure><a href='{}'><img class='thumbnail' src='data:image/jpg;base64,{}'></a><figcaption>{}</figcaption></figure>"
).format(name, data_url, cap) ).format(name, data_url, cap)
except Exception as e: except Exception as e:
print(e) print('Thumbnailer:',e)
return "<figure><a href='{}'><img class='thumbnail' src='{}'></a><figcaption>{}</figcaption></figure>".format(name, name,name) return "<figure><a href='{}'><img src='{}'></a><figcaption>{}</figcaption></figure>".format(name, name,name)
def div(args, mime, tag, *values): def div(args, type_, subtype, tag, name):
id_name = values[0].split('.')[0].replace(' ', '_') id_name = name.split('.')[0].replace(' ', '_')
if not args.no_filenames: if args.no_filenames:
filename = '<br><span class="filename">{}</span>'
else:
filename = '' filename = ''
if 'image' in mime:
html = '<div id="{}">{}</div>'
elif 'pdf' in mime:
html = '<div id="{}">{}' + filename + '</div>'
else: else:
html = '<div id="{}">{}</div>' filename = '<span class="filename">{}</span>'.format(name)
if 'image' in type_:
html = '<div id="{}" class="{}">{}</div>'
elif 'pdf' in subtype:
html = '<div id="{}" class="{}">{}' + filename + '</div>'
elif 'dir' in type_ or 'html' in subtype or 'unkown-file' in subtype:
html = '<div id="{}" class="{}">{}</div>'
else:
html = '<div id="{}" class="{}">{}' + filename +'</div>'
return html.format(id_name, tag, values[0]) return html.format(id_name, subtype, tag)
def distribusify(args, directory): # noqa def distribusify(args, directory): # noqa
for root, dirs, files in os.walk(directory): for root, dirs, files in os.walk(directory):
if not args.remove_index:
html = [] html = []
if args.verbose: if args.verbose:
print('Listing', root) print('Generating directory listing for', root)
for name in files: for name in files:
if args.verbose:
print('Adding', name)
if 'index.html' not in name: if 'index.html' not in name:
full_path = os.path.join(root, name) full_path = os.path.join(root, name)
mime = MIME_TYPE.from_file(full_path) mime = MIME_TYPE.from_file(full_path)
mime, format = mime.split('/') # example: plain text # example: MIME plain/text becomes 'type' plain 'subtype' text
type_, subtype = mime.split('/')
if args.verbose: if args.verbose:
print(mime, format) print('Found', name,'as', mime)
if mime in FILE_TYPES: if type_ in FILE_TYPES:
# expansion for different kind of textfiles # expansion for different kind of textfiles
if mime == 'text': if type_ == 'text':
if name.endswith('.html') or name.endswith('.txt'): if name.endswith('.html') or subtype == 'html':
subtype = 'html'
# what types of text files to expand # what types of text files to expand
a = open(full_path).read() a = '<section id="{}">{}</section>'.format(name, open(full_path).read())
elif format in CODE_TYPES: elif subtype in CODE_TYPES or name.endswith('.txt'):
# if the plain text is code, # if the plain text is code,
# which types do we wrap in pre-tags? # which types do we wrap in pre-tags?
a = "<pre>" + open(full_path).read() + "</pre>" a = "<pre>" + open(full_path).read() + "</pre>"
else: else:
a = FILE_TYPES[mime] subtype = subtype+' unkown-file'
a = "<a href='{}'>{}</a>"
#a = FILE_TYPES[type_]
if mime == 'image' and args.thumbnail: if type_ == 'image':
caption = name
if args.thumbnail:
a = thumbnail(full_path, name, args) a = thumbnail(full_path, name, args)
else:
if args.captions: if args.captions:
cap = caption(full_path) caption = caption(full_path)
else: a = FILE_TYPES[type_].format(name, caption)
cap = name
a = FILE_TYPES[mime].format(full_path, cap)
if format in FILE_TYPES: if subtype in SUB_TYPES:
a = FILE_TYPES[format] a = SUB_TYPES[subtype]
if mime not in FILE_TYPES and format not in FILE_TYPES: if type_ not in FILE_TYPES and subtype not in SUB_TYPES:
# catch exceptions not defined in FILE_TYPES before # catch exceptions not yet defined in FILE_TYPES or SUB_TYPES
a = "<a href='{}'>{}</a>" a = "<a href='{}'>{}</a>"
if args.verbose: if args.verbose:
message = 'mime-type not in list, adding as href: \n' message = 'not in list of file types, adding as plain href: \n'
print(message, mime, format, name) print(type_, subtype, message, name)
type_ ='unkown-file'
a = a.replace('{}', name) a = a.replace('{}', name)
html.append(div(args, mime, a, name)) html.append(div(args, type_, subtype, a, name))
if root != directory: if root != directory:
html.append('<a href="../">../</a>') html.append('<a href="../">../</a>')
for name in dirs: for name in dirs:
a = "<a href='{}' class='dir'>{}/</a>".replace('{}', name) a = "<a href='{}'>{}/</a>".replace('{}', name)
html.append(div(args, 'dir', a, 'folder')) html.append(div(args,'dir', 'dir', a, 'folder'))
with open(os.path.join(root, 'index.html'), 'w') as f: with open(os.path.join(root, 'index.html'), 'w') as f:
if not args.no_template: if not args.no_template:
@ -139,3 +144,12 @@ def distribusify(args, directory): # noqa
if not args.no_template: if not args.no_template:
f.write(html_footer) f.write(html_footer)
if args.remove_index:
index = os.path.join(root, 'index.html')
if 'index.html' in files:
if args.verbose:
print('Removing index.html from', root)
try:
os.remove(index)
except Exception as e:
print(e)

11
distribusi/mappings.py

@ -1,13 +1,14 @@
CODE_TYPES = ['x-c', 'html'] CODE_TYPES = ['x-c', 'x-shellscript', 'x-python']
FILE_TYPES = { FILE_TYPES = {
'image': '<figure><img class="image" src="{}"><figcaption>{}</figcaption></figure>', 'image': '<figure><img class="image" src="{}"><figcaption>{}</figcaption></figure>',
'pdf': (
'<object data="{}" class="pdf" type="application/pdf">'
'<embed src="{}" type="application/pdf" /></object>'
),
'text': '<a href="{}" class="text">{}</a>', 'text': '<a href="{}" class="text">{}</a>',
'video': ('<video class="video" controls>' '<source src="{}"></source></video>'), 'video': ('<video class="video" controls>' '<source src="{}"></source></video>'),
'audio': ('<audio controls class="audio">' '<source src="{}"></source></audio>'), 'audio': ('<audio controls class="audio">' '<source src="{}"></source></audio>'),
} }
SUB_TYPES ={
'pdf': (
'<object data="{}" class="pdf" type="application/pdf">'
'<embed src="{}" type="application/pdf" /></object>')
}

Loading…
Cancel
Save