Merge pull request #174 from yvan-sraka/autopep8

Make the Python scripts follow the PEP8 code style
This commit is contained in:
Patricio Gonzalez Vivo 2017-08-28 04:08:47 +00:00 committed by GitHub
commit 55ece29705
2 changed files with 50 additions and 34 deletions

View File

@ -1,22 +1,26 @@
#! /usr/bin/python2.7 #! /usr/bin/python2.7
import glob, os, re, subprocess import glob
import os
import re
import subprocess
d='..' d = '..'
folders = [os.path.join(d,o) for o in os.listdir(d) if os.path.isdir(os.path.join(d,o))]; folders = [os.path.join(d, o) for o in os.listdir(
d) if os.path.isdir(os.path.join(d, o))]
folders.sort() folders.sort()
for folder in folders: for folder in folders:
for filename in glob.glob(folder+'/*.frag'): for filename in glob.glob(folder + '/*.frag'):
# print filename # print filename
shaderPath = filename; shaderPath = filename
shaderName,shaderExt = os.path.splitext(filename) shaderName, shaderExt = os.path.splitext(filename)
shaderImage = shaderName+".png" shaderImage = shaderName + ".png"
shaderCommand = "glslViewer " + shaderPath + " " + \ shaderCommand = "glslViewer " + shaderPath + " " + \
" -s 0.5 -o " + shaderImage " -s 0.5 -o " + shaderImage
returnCode = subprocess.call(shaderCommand, shell=True) returnCode = subprocess.call(shaderCommand, shell=True)
for filename in glob.glob(folder + '/*.gif'): for filename in glob.glob(folder + '/*.gif'):
gifPath = filename; gifPath = filename
gifName, gifExt = os.path.splitext(filename) gifName, gifExt = os.path.splitext(filename)
pngImage = gifName + ".png" pngImage = gifName + ".png"
convertCommand = "convert " + gifPath + " " + pngImage convertCommand = "convert " + gifPath + " " + pngImage

View File

@ -10,37 +10,42 @@ latexEngine = "xelatex"
# Output path # Output path
outputPath = "." outputPath = "."
if not os.path.exists(outputPath): os.makedirs(outputPath) if not os.path.exists(outputPath):
os.makedirs(outputPath)
pdfBookPath = os.path.join(outputPath, "book.pdf") pdfBookPath = os.path.join(outputPath, "book.pdf")
texBookPath = os.path.join(outputPath, "book.tex") texBookPath = os.path.join(outputPath, "book.tex")
chapters = [] chapters = []
def injectShaderBlocks( _folder, _text ):
def injectShaderBlocks(_folder, _text):
rta = "" rta = ""
lines = _text.split('\n'); lines = _text.split('\n')
for line in lines: for line in lines:
if line.find('<div class=\"codeAndCanvas\"') >= 0: if line.find('<div class=\"codeAndCanvas\"') >= 0:
shaderTextureResults = re.findall(r'<div class=\"codeAndCanvas\" data=\".*\" data-imgs=\"(.*)\"></div>', line.rstrip()) shaderTextureResults = re.findall(
shaderFile = re.sub(r'<div class=\"codeAndCanvas\" data=\"(.*?)\"(>| .+>)</div>', r'\1', line.rstrip()) r'<div class=\"codeAndCanvas\" data=\".*\" data-imgs=\"(.*)\"></div>', line.rstrip())
shaderFile = re.sub(
r'<div class=\"codeAndCanvas\" data=\"(.*?)\"(>| .+>)</div>', r'\1', line.rstrip())
shaderName,shaderExt = os.path.splitext(shaderFile) shaderName, shaderExt = os.path.splitext(shaderFile)
shaderPath = folder+"/"+shaderFile; shaderPath = folder + "/" + shaderFile
if shaderTextureResults: if shaderTextureResults:
shaderTexturePaths = map (lambda f: folder+"/"+f, shaderTextureResults[0].split(",")) shaderTexturePaths = map(
lambda f: folder + "/" + f, shaderTextureResults[0].split(","))
else: else:
shaderTexturePaths = [] shaderTexturePaths = []
shaderString = open(shaderPath, 'r').read() shaderString = open(shaderPath, 'r').read()
rta += '```glsl\n'+shaderString.rstrip('\n')+'\n```\n' rta += '```glsl\n' + shaderString.rstrip('\n') + '\n```\n'
shaderImage = folder+"/tmp-"+shaderName+".png" shaderImage = folder + "/tmp-" + shaderName + ".png"
shaderCommand = "glslViewer " + shaderPath + " " + \ shaderCommand = "glslViewer " + shaderPath + " " + \
" ".join(shaderTexturePaths) + \ " ".join(shaderTexturePaths) + \
" -s 0.5 -o " + shaderImage " -s 0.5 -o " + shaderImage
print shaderCommand print shaderCommand
returnCode = subprocess.call(shaderCommand, shell=True) returnCode = subprocess.call(shaderCommand, shell=True)
rta += "![]("+shaderImage+")\n" rta += "![](" + shaderImage + ")\n"
elif line.find('.gif') >= 0: elif line.find('.gif') >= 0:
gifPath = re.sub(r'\!\[.*\]\((.*\.gif)\)', r'\1', line.rstrip()) gifPath = re.sub(r'\!\[.*\]\((.*\.gif)\)', r'\1', line.rstrip())
gifName, gifExt = os.path.splitext(gifPath) gifName, gifExt = os.path.splitext(gifPath)
@ -48,50 +53,57 @@ def injectShaderBlocks( _folder, _text ):
convertCommand = "convert " + gifPath + " " + pngImage convertCommand = "convert " + gifPath + " " + pngImage
print convertCommand print convertCommand
returnCode = subprocess.call(convertCommand, shell=True) returnCode = subprocess.call(convertCommand, shell=True)
rta += re.sub(r'\!\[(.*)\]\((.*)\.gif\)', r'![\1](\2-0.png)', line) + '\n' rta += re.sub(r'\!\[(.*)\]\((.*)\.gif\)',
r'![\1](\2-0.png)', line) + '\n'
else: else:
rta += line+'\n' rta += line + '\n'
return rta return rta
d='.'
folders = [os.path.join(d,o) for o in os.listdir(d) if os.path.isdir(os.path.join(d,o))]; d = '.'
folders = [os.path.join(d, o) for o in os.listdir(
d) if os.path.isdir(os.path.join(d, o))]
folders.sort() folders.sort()
for folder in folders: for folder in folders:
if os.path.isfile(folder+'/README.md'): if os.path.isfile(folder + '/README.md'):
with open(folder+'/README.md', "r") as originalChapter: with open(folder + '/README.md', "r") as originalChapter:
fileString = originalChapter.read() fileString = originalChapter.read()
# Correct path for images # Correct path for images
imgPattern = r'(\!\[.*?\]\()(.*)' imgPattern = r'(\!\[.*?\]\()(.*)'
subPattern = r'\1' + folder + r'/\2' subPattern = r'\1' + folder + r'/\2'
modifiedChapterString = re.sub(imgPattern, subPattern, fileString) modifiedChapterString = re.sub(imgPattern, subPattern, fileString)
modifiedChapterString = injectShaderBlocks(folder, modifiedChapterString) modifiedChapterString = injectShaderBlocks(
modifiedChapterPath = folder+'/tmp.md' folder, modifiedChapterString)
modifiedChapterPath = folder + '/tmp.md'
with open(modifiedChapterPath, "w") as modifiedChapter: with open(modifiedChapterPath, "w") as modifiedChapter:
modifiedChapter.write(modifiedChapterString) modifiedChapter.write(modifiedChapterString)
chapters.append(modifiedChapterPath) chapters.append(modifiedChapterPath)
# # Set up the appropriate options for the pandoc command # Set up the appropriate options for the pandoc command
inputOptions = chapters inputOptions = chapters
generalOptions = ["-N", "--smart", "--no-tex-ligatures", "--toc", "--standalone", "--preserve-tabs", "-V documentclass=scrbook", "-V papersize=a4", "-V links-as-note", "-S"] # generalOptions = ["-N", "--smart", "--no-tex-ligatures", "--toc", "--standalone",
latexOptions = ["--latex-engine="+latexEngine] "--preserve-tabs", "-V documentclass=scrbook", "-V papersize=a4", "-V links-as-note", "-S"]
latexOptions = ["--latex-engine=" + latexEngine]
outputOptions = ["--output={0}".format(pdfBookPath)] outputOptions = ["--output={0}".format(pdfBookPath)]
pandocCommand = ["pandoc"] + outputOptions + inputOptions + generalOptions + latexOptions pandocCommand = ["pandoc"] + outputOptions + \
inputOptions + generalOptions + latexOptions
# Print out of the chapters being built and the flags being used # Print out of the chapters being built and the flags being used
print "Generating {0} from:".format(pdfBookPath) print "Generating {0} from:".format(pdfBookPath)
for chapter in inputOptions: for chapter in inputOptions:
print "\t{0}".format(chapter) print "\t{0}".format(chapter)
print "Using the following flags:" print "Using the following flags:"
for flag in generalOptions+latexOptions: for flag in generalOptions + latexOptions:
print "\t{0}".format(flag) print "\t{0}".format(flag)
# For debugging purposes, it's a good idea to generate the .tex. Errors # For debugging purposes, it's a good idea to generate the .tex. Errors
# printed out through pandoc aren't as useful as those printed # printed out through pandoc aren't as useful as those printed
# directly from trying to build a PDF in TeXworks. # directly from trying to build a PDF in TeXworks.
texOutputOptions = ["--output={0}".format(texBookPath)] texOutputOptions = ["--output={0}".format(texBookPath)]
texPandocCommand = ["pandoc"] + texOutputOptions + inputOptions + generalOptions + latexOptions texPandocCommand = ["pandoc"] + texOutputOptions + \
inputOptions + generalOptions + latexOptions
returnCode = subprocess.call(texPandocCommand) returnCode = subprocess.call(texPandocCommand)
if returnCode == 0: if returnCode == 0:
print "Successful building of {0}".format(texBookPath) print "Successful building of {0}".format(texBookPath)