Created
August 8, 2019 16:35
-
-
Save thespacedoctor/f179996777d68dcb81f8e0e6e1afc9b2 to your computer and use it in GitHub Desktop.
[Generate a Multimarkdown Links to Images in an Assets Folder] #images #markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# encoding: utf-8 | |
""" | |
*Given a folder of images, generate an markdown displaying the images and a pre-generated MMD link to that image for use with nvAlt.* | |
:Author: | |
David Young | |
:Date Created: | |
December 10, 2013 | |
.. todo:: | |
@review: when complete pull all general functions and classes into dryxPython | |
Usage: | |
generate_mmd_page_of_mmd_image_links --pathToImageFolder=<pathToImageFolder> --pathToMMDFile=<pathToMMDFile> | |
-h, --help show this help message | |
-v, --version show version | |
""" | |
################# GLOBAL IMPORTS #################### | |
import sys | |
import os | |
from docopt import docopt | |
from dryxPython import logs as dl | |
from dryxPython import commonutils as dcu | |
def main(arguments=None): | |
""" | |
*The main function used when ``generate_mmd_page_of_mmd_image_links.py`` is run as a single script from the cl, or when installed as a cl command* | |
""" | |
########## IMPORTS ########## | |
## STANDARD LIB ## | |
## THIRD PARTY ## | |
## LOCAL APPLICATION ## | |
## ACTIONS BASED ON WHICH ARGUMENTS ARE RECIEVED ## | |
# PRINT COMMAND-LINE USAGE IF NO ARGUMENTS PASSED | |
if arguments == None: | |
arguments = docopt(__doc__) | |
# SETUP LOGGER -- DEFAULT TO CONSOLE LOGGER IF NONE PROVIDED IN SETTINGS | |
if 'settings' in locals() and "logging settings" in settings: | |
log = dl.setup_dryx_logging( | |
yaml_file=arguments["--settingsFile"] | |
) | |
elif "--logger" not in arguments or arguments["--logger"] is None: | |
log = dl.console_logger( | |
level="DEBUG" | |
) | |
log.debug('logger setup') | |
# unpack remaining cl arguments using `exec` to setup the variable names | |
# automatically | |
for arg, val in arguments.iteritems(): | |
varname = arg.replace("--", "") | |
if isinstance(val, str) or isinstance(val, unicode): | |
exec(varname + " = '%s'" % (val,)) | |
else: | |
exec(varname + " = %s" % (val,)) | |
if arg == "--dbConn": | |
dbConn = val | |
log.debug('%s = %s' % (varname, val,)) | |
## START LOGGING ## | |
startTime = dcu.get_now_sql_datetime() | |
log.info( | |
'--- STARTING TO RUN THE generate_mmd_page_of_mmd_image_links.py AT %s' % | |
(startTime,)) | |
# call the worker function | |
generate_mmd_page_of_mmd_image_links( | |
log=log, | |
pathToImageFolder=pathToImageFolder, | |
pathToMMDFile=pathToMMDFile | |
) | |
if "dbConn" in locals() and dbConn: | |
dbConn.commit() | |
dbConn.close() | |
## FINISH LOGGING ## | |
endTime = dcu.get_now_sql_datetime() | |
runningTime = dcu.calculate_time_difference(startTime, endTime) | |
log.info( | |
'-- FINISHED ATTEMPT TO RUN THE generate_mmd_page_of_mmd_image_links.py AT %s (RUNTIME: %s) --' % | |
(endTime, runningTime, )) | |
return | |
################################################################### | |
# CLASSES # | |
################################################################### | |
################################################################### | |
# PUBLIC FUNCTIONS # | |
################################################################### | |
# LAST MODIFIED : ddate | |
# CREATED : ddate | |
# AUTHOR : DRYX | |
# copy usage method(s) into function below and select the following snippet from the command palette: | |
# x-setup-worker-function-parameters-from-usage-method | |
def generate_mmd_page_of_mmd_image_links( | |
log, | |
pathToImageFolder, | |
pathToMMDFile, | |
): | |
""" | |
*generate_mmd_page_of_mmd_image_links* | |
**Key Arguments:** | |
- ``log`` -- the logger | |
- ``pathToImageFolder`` -- pathToImageFolder | |
- ``pathToMMDFile`` -- pathToMMDFile | |
**Return:** | |
- None | |
.. todo:: | |
@review: when complete, clean worker function and add comments | |
@review: when complete add logging | |
""" | |
################ > IMPORTS ################ | |
## STANDARD LIB ## | |
import collections | |
## THIRD PARTY ## | |
## LOCAL APPLICATION ## | |
import dryxPython.commonutils as dcu | |
basePath = pathToImageFolder | |
imageDictionary = {} | |
for d in os.listdir(basePath): | |
if os.path.isfile(os.path.join(basePath, d)) and d[0:1] != ".": | |
dateCreated, dateModified = dcu.get_file_creation_modification_dates.get_file_creation_modification_dates( | |
log=log, | |
pathToFile=os.path.join(basePath, d) | |
) | |
basename = os.path.basename(d) | |
imageDictionary[str(dateModified) + basename] = basename | |
oimageDictionary = collections.OrderedDict( | |
reversed(sorted(imageDictionary.items()))) | |
pathToWriteFile = pathToMMDFile | |
try: | |
log.debug("attempting to open the file %s" % (pathToWriteFile,)) | |
writeFile = open(pathToWriteFile, 'w') | |
except IOError, e: | |
message = 'could not open the file %s' % (pathToWriteFile,) | |
log.critical(message) | |
raise IOError(message) | |
basename = pathToImageFolder | |
if basename[-1] == "/": | |
basename = basename[0:-1] | |
basename = os.path.basename(basename) | |
count = 1 | |
for k, v in oimageDictionary.iteritems(): | |
image = """![image title][%s]""" % (count,) | |
footRef = """[%s]: %s/%s "image title" width=200px """ % (count, | |
basename, v) | |
footRef2 = """[%s]: /assets/images/%s "image title" width=800px """ % ( | |
count, v) | |
log.debug('image: %s' % (image,)) | |
writeFile.write("\n\n%s\n\n%s\n\n\t%s\n\n\t%s\n\n" % | |
(image, footRef, image, footRef2)) | |
count += 1 | |
writeFile.close() | |
return None | |
# use the tab-trigger below for new function | |
# x-def-with-logger | |
################################################################### | |
# PRIVATE (HELPER) FUNCTIONS # | |
################################################################### | |
############################################ | |
# CODE TO BE DEPECIATED # | |
############################################ | |
if __name__ == '__main__': | |
main() | |
################################################################### | |
# TEMPLATE FUNCTIONS # | |
################################################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment