Skip to content

Instantly share code, notes, and snippets.

@notalentgeek
Last active December 20, 2016 00:19
Show Gist options
  • Save notalentgeek/8153f2332f4c6fc0bea7e49a50716421 to your computer and use it in GitHub Desktop.
Save notalentgeek/8153f2332f4c6fc0bea7e49a50716421 to your computer and use it in GitHub Desktop.
My Python codes to automate my markdown notes.
import copy
import datetime as dt
import os
import shutil
import subprocess
import sys
import time
def main(args):
# Variable that point to a folder that is
# filled with the mark down notes we want
# to automate.
targDir = sys.argv[1]
# Variable to hold a string of GMT zone.
# Input this manually. There is no way
# Python can know the time zone of when the
# file/folder was created.
manualTimeZone = sys.argv[2]
# An array of all content in the
# target directory.
targDirLst = os.listdir(targDir)
#print(targDir)
#print(targDirLst)
# Folder name for the temporary folder.
# Normalize the path name of the
# target directory.
targDirNorm = os.path.normpath(targDir)
# The last or file in the path provided
# in the argument.
targDirBase = os.path.basename(targDirNorm)
# The temporary folder name.
targDirTempFolderName = "temp-" + targDirBase
# I am trying to found the one up parent
# directory from the targDir.
targDirArray = targDir.split("/")
targDirArray.pop()
upperDir = None
for s in targDirArray:
if upperDir == None: upperDir = s
else: upperDir = upperDir + "/" + s
upperDirNorm = os.path.normpath(upperDir)
# Absolute path to the folder base.
tempDir = os.path.join(
upperDirNorm,
targDirTempFolderName
)
#print(targDirNorm)
#print(targDirBase)
#print(targDirTempFolderName)
#print(targDirArray)
#print(upperDir)
#print(upperDirNorm)
#print(tempDir)
# Check if there is a folder with same
# name with the backup folder or not.
# If there is, then delete the folder!
if os.path.exists(tempDir): shutil.rmtree(tempDir)
# Copy the files from the targDir into the
# tempDir.
shutil.copytree(targDir, tempDir)
tempDirLst = os.listdir(tempDir)
# I need to check if an element inside the
# tempDirLst is a file. If so I need to
# put it out.
for f in tempDirLst:
# Absolute location of the file
# we want to check.
mainFile = os.path.join(targDir, f)
if os.path.isfile(mainFile):
# Remove the file from the array
# if it is actually file.
tempDirLst.remove(f)
#print("Removed from the tempDirLst " + f + ".")
#print(tempDirLst)
# Do the for loop again to check and do processing inside
# the folder.
for f in tempDirLst:
# Variable to hold the absolute path to each
# of the note folder.
tempDirListContent = os.path.join(targDir, f)
#print(tempDirListContent)
realName = ConstructRealName(f, tempDirListContent, manualTimeZone)
#print(realName)
# Rename the folder according to my
# folder naming convention.
shutil.move(
os.path.join(tempDir, f),
os.path.join(tempDir, realName))
# Re - initiate the list.
# Variable to hold the absolute path to each
# of the note folder. Now using data from the
# temporary folder and not using data from the original
# folder anymore.
tempDirListContent = os.path.join(tempDir, realName)
# Find all files in the folder.
tempDirListContentLst = os.listdir(tempDirListContent)
# Need to make sure there are no
# other directory in the note folder.
# If there is a directory/folder then
# stop the program and delete the
# temporary folder.
for g in tempDirListContentLst:
# Check the file whether it is a folder
# or a file.
fldrOrFl = os.path.join(tempDirListContent, g)
CheckIfThisIsAFolderThenStop(fldrOrFl)
# Need to identify the main markdown file.
fileName, fileExt = os.path.splitext(fldrOrFl)
#print(fileName)
#print(fileExt)
if fileExt == ".md":
# Change the name of the .md files.
# Put the realName attach the absolute path
# in the front of the realName and do not forget
# to put the .md extension back.
# Do not use "pending-" in markdown name.
realNameMarkdown = realName.replace("pending-", "")
newNameOfTheMarkdownFile = os.path.join(tempDirListContent, (realNameMarkdown + ".md"))
shutil.move(fldrOrFl, newNameOfTheMarkdownFile)
# Do text processing here. Find each lines in the
# markdown files. Process the new renamed file
# not the old one!
linesWithLineBreak = None
with open(newNameOfTheMarkdownFile, "r+") as md:
# Read each lines. Each lines
# are separated with a line break.
# Or carriage return in Window.
lines = md.readlines()
#for l in lines: print(l)
# Use indexNaming to generate the index of the
# file we are going to rename.
indexNaming = 1
newLines = []
# To detect if the line is the last line.
# Because the last line does not need line break.
lCount = 0
lMax = len(lines)
#print(lMax)
# Only check lines that start with "![" or "[".
for l in lines:
# Count!
lCount = lCount + 1
#print(str(lCount) + " " + str(lMax))
# Check the first two letter for image
# and the first letter for embedded link.
if(l[:2] == "![" or
l[:1] == "["):
if l[:1] == "[":
# If l refer to an embedded link.
# Extract the address, because I want
# to check if the address is a URL or
# a local link.
testEmdLink = l.split("[")[1].split("]")[0]
#print(testEmdLink[:2])
# If testEmdLink[:2] returns "./" then we proceed
# with the naming convention.
if testEmdLink[:2] == "./":
# Change the name in the markdown file
# and the file name itself. Find the name of
# the embedded file.
fileNameName = testEmdLink.split("./")[1]
# Do not use "pending-" in file name.
realNameFile = realName.replace("pending-", "")
# Construct new name for the edited file.
newNameName = (realNameFile + "-" + str(indexNaming) + "-" +
fileNameName)
#print(fldrOrFl)
#print(l)
#print(newNameName)
# Change the file name in the upper parent
# (up one level).
#print(tempDirListContent)
#print(fileNameName)
# Absolute path to the file name we want to change.
fileNameAbsPath = os.path.join(tempDirListContent,
fileNameName)
# Absolute path to the new name.
fileNewNameAbsPath = os.path.join(tempDirListContent,
newNameName)
#print(fileNameAbsPath)
#print(fileNewNameAbsPath)
# Change the file name.
shutil.move(fileNameAbsPath, fileNewNameAbsPath)
# Construct a new lines for the data.
# DO NOT FORGET TO PUT THE LINE BREAK \n
# for non last line.
if lCount == lMax:
newLineInMarkDown = (
"[./" + newNameName + "](./" +
newNameName + ")")
else:
newLineInMarkDown = (
"[./" + newNameName + "](./" +
newNameName + ")\n")
l = newLineInMarkDown
indexNaming = indexNaming + 1
# If l refers to an image.
elif l[:2] == "![":
# Search for the absolute directory of the
# image. Get the image file name first.
imageFileName = l.split("![./")[1].split("](")[0]
#print(imageFileName)
# Then I need to get the absolute path.
absPathToImage = os.path.join(tempDirListContent, imageFileName)
# Do not use "pending-" in image name.
realNameImage = realName.replace("pending-", "")
# The supposed new name of the image.
newNameOfImage = realNameImage + "-" + str(indexNaming) + ".png"
# Path.
newAbsPathToImage = os.path.join(tempDirListContent, newNameOfImage)
#print(imageFileName)
#print(absPathToImage)
#print(imageExt)
#print(newNameOfImage)
# Use ImageMagick!
subprocess.call(["convert \"" + absPathToImage + "[600x]\" " +
newAbsPathToImage], shell=True)
print("processing " + imageFileName + " to " + newNameOfImage)
# Remove the old image file.
os.remove(absPathToImage)
print("deleting " + absPathToImage)
# Construct new line in markdown file.
if lCount == lMax:
newLineForTheMarkdownFile = ("![./" + newNameOfImage +
"](./" + newNameOfImage +")")
else:
newLineForTheMarkdownFile = ("![./" + newNameOfImage +
"](./" + newNameOfImage +")\n")
#print(newLineForTheMarkdownFile)
l = newLineForTheMarkdownFile
indexNaming = indexNaming + 1
newLines.append(l)
with open(newNameOfTheMarkdownFile, "r+") as md:
md.writelines(newLines)
# Function to check if there is at least a folder in side
# the directory.
def CheckIfThisIsAFolderThenStop(_absPathToFileOrFolder):
# If it is a folder then halt the program
# until the folder is deleted. The variable
# printOnce is only to make sure that the
# print() is only executed once for every folder
# found.
printOnce = False
while os.path.isdir(_absPathToFileOrFolder):
# Check if the folder has content(s).
# If the folder has content(s) then let the
# user delete it manually. If the folder has
# no content (for example it just an empty
# folder) then this program will delete it
# automatically.
fldrOrFlLst = os.listdir(_absPathToFileOrFolder)
if len(fldrOrFlLst) <= 0: shutil.rmtree(_absPathToFileOrFolder)
# The else statement is when there are folder
# with content in it.
else:
if not printOnce:
printOnce = True
print(
_absPathToFileOrFolder +
" is a folder and has content(s) please check and delete it manually!")
print("this program will progress automatically")
if printOnce: print("the progress continue")
# Function to construct name for the main folder
# according to my own convention.
def ConstructRealName(
_f,
_absPathToFileOrFolder,
_manualTimeZone
):
# Get the folder properties. For naming convention.
# In this case I want to get the creation time of the
# folder. The two lines below is to get the folder
# creation times.
creationTime = os.path.getctime(_absPathToFileOrFolder)
timeConvFromCTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(creationTime))
# I put x as a prefix because time variable is already taken.
# It has no meaning :D!
dateX = str(timeConvFromCTime).split(" ")[0]
timeX = str(timeConvFromCTime).split(" ")[1]
yearX = dateX.split("-")[0]
monthX = dateX.split("-")[1]
dayX = dateX.split("-")[2]
hourX = timeX.split(":")[0]
minX = timeX.split(":")[1]
#print(creationTime)
#print(timeConvFromCTime)
#print(dateX)
#print(timeX)
#print(yearX)
#print(monthX)
#print(dayX)
#print(hourX)
#print(minX)
# Construct the prefix name.
prefixName = (yearX + monthX + dayX + "-" +
hourX + minX + "-" + _manualTimeZone + "-")
#print(prefixName)
# Specific naming convention for pending folder.
if _f.split("-")[0] == "pending":
tempRealName = _f.replace("pending-", "")
#print(tempRealName)
realName = "pending-" + prefixName + tempRealName
else:
# Change the name of each folder.
# Construct the real name.
realName = prefixName + _f
#print(realName)
return realName
if __name__ == "__main__": main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment