|
@ -1,4 +1,3 @@ |
|
|
import base64 |
|
|
|
|
|
import os |
|
|
import os |
|
|
|
|
|
|
|
|
import magic |
|
|
import magic |
|
@ -12,16 +11,35 @@ MIME_TYPE = magic.Magic(mime=True) |
|
|
def add_alttext(full_path_image): |
|
|
def add_alttext(full_path_image): |
|
|
try: |
|
|
try: |
|
|
image_filename_no_ext = os.path.splitext(full_path_image)[0] |
|
|
image_filename_no_ext = os.path.splitext(full_path_image)[0] |
|
|
alttext_filename = f'{image_filename_no_ext}_alttext.txt' |
|
|
alttext_filename = f"{image_filename_no_ext}_alttext.txt" |
|
|
if os.path.isfile(alttext_filename): |
|
|
return _read_matching_text_file( |
|
|
print(f"{image_filename_no_ext} has {alttext_filename}") |
|
|
image_filename_no_ext, alttext_filename |
|
|
with open(alttext_filename, 'r') as alttext_file: |
|
|
) |
|
|
return alttext_file.read() |
|
|
|
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
print(f"exception {e} raised while making alttext") |
|
|
print(f"exception {e} raised while making alttext") |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_description(full_path_image): |
|
|
|
|
|
try: |
|
|
|
|
|
image_filename_no_ext = os.path.splitext(full_path_image)[0] |
|
|
|
|
|
description_filename = f"{image_filename_no_ext}_dv_description.txt" |
|
|
|
|
|
return _read_matching_text_file( |
|
|
|
|
|
image_filename_no_ext, description_filename |
|
|
|
|
|
) |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
print(f"exception {e} raised while adding description") |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _read_matching_text_file(image_filename_no_ext, filename): |
|
|
|
|
|
if not os.path.isfile(filename): |
|
|
|
|
|
return |
|
|
|
|
|
print(f"{image_filename_no_ext} has {filename}") |
|
|
|
|
|
with open(filename, "r") as text_file: |
|
|
|
|
|
return text_file.read() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def thumbnail(full_path_image, name, args): |
|
|
def thumbnail(full_path_image, name, args): |
|
|
if full_path_image.endswith("_thumbnail.jpg"): |
|
|
if full_path_image.endswith("_thumbnail.jpg"): |
|
|
return |
|
|
return |
|
@ -94,7 +112,7 @@ def write_index(args, index, html, html_head, html_footer): |
|
|
index_file.write(html_footer) |
|
|
index_file.write(html_footer) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handle_text_files(name, full_path): |
|
|
def handle_text_files(name, full_path, subtype): |
|
|
if name.endswith(".html") or subtype == "html": |
|
|
if name.endswith(".html") or subtype == "html": |
|
|
subtype = "html" |
|
|
subtype = "html" |
|
|
# what types of text files to expand |
|
|
# what types of text files to expand |
|
@ -119,9 +137,15 @@ def handle_image_files(name, full_path, args): |
|
|
return |
|
|
return |
|
|
if args.alttexts: |
|
|
if args.alttexts: |
|
|
image_alttext = add_alttext(full_path) |
|
|
image_alttext = add_alttext(full_path) |
|
|
|
|
|
if args.alttexts: |
|
|
|
|
|
image_description = add_description(full_path) |
|
|
if image_alttext is None: |
|
|
if image_alttext is None: |
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{name}"></a>' |
|
|
if image_description is None: |
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a>' |
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{name}"></a>' |
|
|
|
|
|
return f'<figure><a href="{name}"><img class="thumbnail" src="{thumbnail_filename}"></a><figcaption>{image_description}</figcaption></figure>' |
|
|
|
|
|
if image_description is None: |
|
|
|
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a>' |
|
|
|
|
|
return f'<figure><a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a><figcaption>{image_description}</figcaption></figure>' |
|
|
return FILE_TYPES[type_].format(name, image_alttext) |
|
|
return FILE_TYPES[type_].format(name, image_alttext) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -150,7 +174,9 @@ def distribusify(args, directory): # noqa |
|
|
|
|
|
|
|
|
if args.exclude_directory: |
|
|
if args.exclude_directory: |
|
|
if args.verbose: |
|
|
if args.verbose: |
|
|
print("Excluding directory:", ", ".join(args.exclude_directory)) |
|
|
print( |
|
|
|
|
|
"Excluding directory:", ", ".join(args.exclude_directory) |
|
|
|
|
|
) |
|
|
dirs[:] = [d for d in dirs if d not in args.exclude_directory] |
|
|
dirs[:] = [d for d in dirs if d not in args.exclude_directory] |
|
|
|
|
|
|
|
|
if args.no_hidden: |
|
|
if args.no_hidden: |
|
@ -172,6 +198,9 @@ def distribusify(args, directory): # noqa |
|
|
if name.endswith("_alttext.txt"): |
|
|
if name.endswith("_alttext.txt"): |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
if name.endswith("_dv_description.txt"): |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
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) |
|
|
type_, subtype = mime.split("/") |
|
|
type_, subtype = mime.split("/") |
|
@ -182,7 +211,7 @@ def distribusify(args, directory): # noqa |
|
|
if type_ in FILE_TYPES: |
|
|
if type_ in FILE_TYPES: |
|
|
match type_: |
|
|
match type_: |
|
|
case "text": |
|
|
case "text": |
|
|
subtype, tag = handle_text_files(name, full_path) |
|
|
subtype, tag = handle_text_files(name, full_path, subtype) |
|
|
case "image": |
|
|
case "image": |
|
|
tag = handle_image_files(name, full_path, args) |
|
|
tag = handle_image_files(name, full_path, args) |
|
|
if tag is None: |
|
|
if tag is None: |
|
@ -197,7 +226,9 @@ def distribusify(args, directory): # noqa |
|
|
# catch exceptions not yet defined in FILE_TYPES or SUB_TYPES |
|
|
# catch exceptions not yet defined in FILE_TYPES or SUB_TYPES |
|
|
tag = "<a href='{}'>{}</a>" |
|
|
tag = "<a href='{}'>{}</a>" |
|
|
if args.verbose: |
|
|
if args.verbose: |
|
|
message = "not in list of file types, adding as plain href: \n" |
|
|
message = ( |
|
|
|
|
|
"not in list of file types, adding as plain href: \n" |
|
|
|
|
|
) |
|
|
print(type_, subtype, message, name) |
|
|
print(type_, subtype, message, name) |
|
|
subtype = subtype + " unkown-file" |
|
|
subtype = subtype + " unkown-file" |
|
|
|
|
|
|
|
|