python3 compatible, not python2 anymore

This commit is contained in:
Q
2020-09-22 09:10:41 +03:00
parent 38f26a3c69
commit fb31b64f45
8 changed files with 165 additions and 123 deletions

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2016 Ville Rantanen
#
@@ -18,6 +18,8 @@
import sys,os
import re
import urllib
import urllib.parse
from html.parser import HTMLParser
import shutil
import csv
import subprocess
@@ -27,7 +29,7 @@ from datetime import datetime
# (c) ville.q.rantanen@gmail.com
__version__='2.20190411a'
__version__='2.20200922'
FILECONFIG=".config"
FILEDESC="descriptions.csv"
@@ -73,7 +75,7 @@ stripquotes=re.compile('^"|"$')
def getheader(path,parent,title=""):
if title == "":
title=unicode(os.path.basename(path),encoding="utf8").encode('ascii', 'xmlcharrefreplace')
title = unescape(os.path.basename(path))
return '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
@@ -89,6 +91,7 @@ def getheader(path,parent,title=""):
</HEAD>
<BODY>
'''
def getfooter():
return '''
<div id="footer">Generated with Qalbum '''+__version__+''' ('''+datetime.today().strftime("%y-%m-%d %H:%M")+''') <a href="https://bitbucket.org/MoonQ/qalbum/wiki/Home" target="_TOP">Need help?</a></div>
@@ -177,23 +180,26 @@ def getpathlist(path,options=False):
paths.sort(key=lambda x: natural_sort_key(x))
return paths
def pathscript(path,list):
''' Returns the javascript string of pathlist and pathimage arrays '''
scrstr = '<script language="javascript">var pathlist=['
parser = HTMLParser()
elements = []
for p in list:
imglist = getimagelist(os.path.join(path,p))
pathlist = getpathlist(os.path.join(path,p))
this_str='{ name:"'+unicode(p,encoding="utf8").encode('ascii', 'xmlcharrefreplace')+'", '
this_str = '{ name:"'+ parser.unescape(p) + '", '
this_str += 'size:' + str(len(imglist) + len(pathlist)) + ', '
if len(imglist) > 0:
this_str+='image:"'+unicode(p,encoding="utf8").encode('ascii', 'xmlcharrefreplace')+'/.tn/'+unicode(imglist[0],encoding="utf8").encode('ascii', 'xmlcharrefreplace')+'.jpg"}'
this_str += 'image:"'+parser.unescape(p) + '/.tn/'+parser.unescape(imglist[0]) + '.jpg"}'
else:
this_str += 'image:"" }'
elements.append(this_str)
scrstr += ','.join(elements) + '];</script>'
return scrstr
def pathlinks(path,list):
''' Returns the HTML string of subfolders '''
if len(list) == 0:
@@ -206,20 +212,21 @@ def pathlinks(path,list):
nsum = str(len(imglist))
imgstr = ""
if len(imglist) > 0:
imgstr='<span class="pathbox" style="background-image:url(\''+urllib.quote(p)+'/.tn/'+urllib.quote(imglist[0])+'.jpg\');">'
imgstr = '<span class="pathbox" style="background-image:url(\''+urllib.parse.quote(p)+'/.tn/'+urllib.parse.quote(imglist[0])+'.jpg\');">'
else:
imgstr = '<span class="pathbox">'
pathstr += '<a title="%s" href="%s/index.html">%s<span class="pathlink"><span class="pathlinktext">%s (%s)</span></span></span></a>'%(
unicode(p,encoding="utf8").encode('ascii', 'xmlcharrefreplace'),
urllib.quote(p),
unescape(p),
urllib.parse.quote(p),
imgstr,
nice.encode('ascii', 'xmlcharrefreplace'),
unescape(nice),
nsum
)
pathstr += '</script>'
pathstr += '</div>'
return pathstr
def imagescript(path,list):
''' Returns the javascript string of imagelist and imagedesc '''
strout='<script language="javascript">var imagelist=['
@@ -230,10 +237,10 @@ def imagescript(path,list):
elements=[]
for i in list:
try:
desc=singlequotes.sub("\\'",unicode(descriptions[n],encoding="utf8").encode('ascii', 'xmlcharrefreplace'))
desc=singlequotes.sub("\\'",unescape(descriptions[n]))
except:
desc=singlequotes.sub("\\'",filter(lambda x: x in string.printable, descriptions[n]).encode('ascii', 'xmlcharrefreplace'))
this_str='\n{name:"'+unicode(i,encoding="utf8").encode('ascii', 'xmlcharrefreplace')+'", '
desc=singlequotes.sub("\\'",unescape(filter(lambda x: x in string.printable, descriptions[n])))
this_str='\n{name:"' + unescape(i) + '", '
this_str+='desc:\''+desc+'\', '
this_str+='size:\''+str(sizes[n])+'\', '
this_str+='time:'+str(times[n])+'}'
@@ -242,6 +249,7 @@ def imagescript(path,list):
strout+=','.join(elements)+'];</script>'
return strout
def imagelinks(path,list):
''' Returns the HTML string of images '''
if len(list) == 0:
@@ -253,30 +261,32 @@ def imagelinks(path,list):
for i in list:
nice=nicestring(i)
try:
desc=doublequotes.sub('',unicode(descriptions[n],encoding="utf8").encode('ascii', 'xmlcharrefreplace'))
desc=doublequotes.sub('',unescape(descriptions[n]))
except:
desc=doublequotes.sub('',filter(lambda x: x in string.printable, descriptions[n]).encode('ascii', 'xmlcharrefreplace'))
desc=doublequotes.sub('',unescape(filter(lambda x: x in string.printable, descriptions[n])))
strout += '<span class="imagebox thumbbox" id="n%d"><a href="%s"><img class="thumbimage" "title="%s" src=".tn/%s.jpg"><br/>%s</a></span>'%(
n,
urllib.quote(i),
urllib.parse.quote(i),
desc,
urllib.quote(i),
nice.encode('ascii', 'xmlcharrefreplace')
urllib.parse.quote(i),
nice
)
n += 1
strout += '</noscript></div>'
return strout
def filescript(path,list):
''' Returns the javascript string of filelist '''
strout = '<script language="javascript">var filelist=['
elements=[];
elements = []
for i in list:
elements.append('"'+unicode(i,encoding="utf8").encode('ascii', 'xmlcharrefreplace')+'"')
elements.append('"' + unescape(i) + '"')
strout += ','.join(elements)+ '];</script>'
return strout
def filelinks(path,list):
''' Returns the HTML string of non image files '''
strout = '<div id="attachmentcontainer">'
@@ -285,11 +295,12 @@ def filelinks(path,list):
n=0
for i in list:
size = sizestring(os.path.getsize(os.path.join(path,i)))
strout+='<span class="attachmentbox" id="a'+str(n)+'"><a href="'+urllib.quote(i)+'">'+unicode(i,encoding="utf8").encode('ascii', 'xmlcharrefreplace')+' ['+size+']</a></span>'
strout += '<span class="attachmentbox" id="a'+str(n)+'"><a href="'+urllib.parse.quote(i)+'">' + unescape(i) +' ['+size+']</a></span>'
n += 1
strout += '</div>'
return strout
def cleanthumbs(path):
''' clears .med and .tn for unused thumbs '''
print('clearing unused thumbs...')
@@ -299,6 +310,7 @@ def cleanthumbs(path):
clearfolder(path,os.path.join(path,'.med'),re.compile("(.*)(.jpg)"))
return
def clearfolder(path,tnpath,regex):
''' clears given folder '''
list=getimagelist(tnpath)
@@ -312,6 +324,7 @@ def clearfolder(path,tnpath,regex):
continue
return
def createthumbs(path,list,options):
''' Runs imagemagick Convert to create medium sized and thumbnail images '''
if len(list)==0:
@@ -343,6 +356,7 @@ def createthumbs(path,list,options):
print_clear('')
return
def create_medium_bitmap(infile,outfile,r,link=False,vector=False):
if link:
os.symlink('../'+os.path.basename(infile),outfile)
@@ -355,6 +369,7 @@ def create_medium_bitmap(infile,outfile,r,link=False,vector=False):
convp=subprocess.call(convargs)
return
def create_thumb_bitmap(infile,outfile,vector=False,gravity='Center'):
if vector:
convargs=['convert','-density','300x300',infile,'-background','white','-flatten','-thumbnail','90x90^','-gravity',gravity,'-crop','90x90+0+0','+repage','-quality','75',outfile]
@@ -363,13 +378,14 @@ def create_thumb_bitmap(infile,outfile,vector=False,gravity='Center'):
convp=subprocess.call(convargs)
return
def getdescriptions(path,list):
''' Read descriptions.csv file and returns a list of descriptions.
Missing descriptions are replaced with the file name. '''
if not os.path.exists(os.path.join(path,FILEDESC)):
return list
desc=[i for i in list]
reader = csv.reader(open(os.path.join(path,FILEDESC),'rb'),
reader = csv.reader(open(os.path.join(path,FILEDESC),'rt'),
delimiter='\t',
doublequote=False,
escapechar='\\',
@@ -381,6 +397,7 @@ def getdescriptions(path,list):
desc[i]=stripquotes.sub('',row[1])
return desc
def getinfo(path,options):
''' Read info.txt file and returns the content.
Missing info file returns empty string. '''
@@ -389,14 +406,14 @@ def getinfo(path,options):
reader = open(os.path.join(path,options.infofile),'r')
return unicode(reader.read(),encoding="utf8",errors="ignore").encode('ascii','xmlcharrefreplace')
def crumblinks(crumbs, title, parent):
''' Create the HTML string for crumb trails '''
strout = '<div id="crumbcontainer">'
if parent:
if not parent.startswith('http://'):
parent = "../"*(len(crumbs))+parent
strout+='<a href="'+parent+'">'+'Home'.encode('ascii', 'xmlcharrefreplace')+'</a>: '
strout += '<a href="' + parent + '">' + 'Home' + '</a>: '
i = 1
for c in crumbs:
cname = os.path.basename(c)
@@ -404,19 +421,21 @@ def crumblinks(crumbs,title,parent):
cname = title
cdepth = len(crumbs) - i
clink = "../"*cdepth
strout+='<a href="'+clink+'index.html">'+unicode(cname,encoding="utf8").encode('ascii', 'xmlcharrefreplace')+'</a>: '
strout += '<a href="'+ clink +'index.html">'+ unescape(cname) + '</a>: '
i += 1
strout += '</div>'
return strout
def print_clear(s):
sys.stdout.write("\033[1K\r")
sys.stdout.write(str(s))
sys.stdout.flush()
def nicestring(s):
''' Returns a nice version of a long string '''
s = unicode(s, encoding = "utf8")
s = unescape(s)
if len(s)<20:
return s
s=s.replace("_"," ")
@@ -425,6 +444,7 @@ def nicestring(s):
s=s[0:26]+".."+s[-3:]
return s
def sizestring(size):
''' Returns human readable file size string '''
for x in ['b','kb','Mb','Gb','Tb']:
@@ -436,6 +456,10 @@ def sizestring(size):
size /= 1024.0
def unescape(s):
return HTMLParser().unescape(s)
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
''' Natural sort / Claudiu@Stackoverflow '''
return [int(text) if text.isdigit() else text.lower()
@@ -459,6 +483,7 @@ def which(program):
return None
def traverse(path,crumbs,inputs,options):
''' The recursive main function to create the index.html and seek sub folders '''

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2018 Ville Rantanen
#
@@ -18,10 +18,11 @@
import sys,os
import re
import urllib
import urllib.parse
import csv
import string
from datetime import datetime
from Qalbum import \
from qalbum.Qalbum import \
cleanthumbs, \
createthumbs, \
crumblinks, \
@@ -34,12 +35,13 @@ from Qalbum import \
nicestring, \
readconfig, \
sizestring, \
unescape, \
which, \
writeconfig
# (c) ville.q.rantanen@gmail.com
__version__='0.20190411a'
__version__='0.20200922'
imagesearch=re.compile('.*\.jpg$|.*\.jpeg$|.*\.gif$|.*\.png$|.*\.tif$|.*\.svg$|.*\.pdf$',re.I)
vectorsearch=re.compile('.*\.svg$|.*\.pdf$',re.I)
@@ -53,9 +55,10 @@ FILECONFIG=".config"
FILEINFO="info.txt"
FILEDESC="descriptions.csv"
def getheader(path,parent,title=""):
if title=="":
title=unicode(os.path.basename(path),encoding="utf8").encode('ascii', 'xmlcharrefreplace')
title=unescape(os.path.basename(path))
return '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
@@ -125,6 +128,8 @@ body {
</head>
<body>
'''
def getfooter():
return '''
<div id="footer">Generated with Qnano2 '''+__version__+''' ('''+datetime.today().strftime("%y-%m-%d %H:%M")+''') <a href="https://bitbucket.org/MoonQ/qalbum" target="_TOP">Source</a></div>
@@ -132,6 +137,7 @@ def getfooter():
</HTML>
'''
def imagelinks(path,list):
''' Returns the HTML string of images '''
strout='''\n<div id="Qnano2-gallery" data-nanogallery2='{
@@ -160,19 +166,20 @@ def imagelinks(path,list):
for n,i in enumerate(list):
nice=nicestring(i)
try:
desc=doublequotes.sub('',unicode(descriptions[n],encoding="utf8").encode('ascii', 'xmlcharrefreplace'))
desc=doublequotes.sub('',unescape(descriptions[n]))
except:
desc=doublequotes.sub('',filter(lambda x: x in string.printable, descriptions[n]).encode('ascii', 'xmlcharrefreplace'))
desc=doublequotes.sub('',unescape(filter(lambda x: x in string.printable, descriptions[n])))
strout += '<a href=".med/%s.jpg" data-ngthumb=".tn/%s.jpg" data-ngdownloadurl="%s">%s</a><br>\n'%(
urllib.quote(i),
urllib.quote(i),
urllib.quote(i),
urllib.parse.quote(i),
urllib.parse.quote(i),
urllib.parse.quote(i),
desc
)
strout += '</div>'
return strout
def pathlinks(path, list):
''' Returns the HTML string of subfolders '''
if len(list) == 0:
@@ -188,15 +195,16 @@ def pathlinks(path, list):
#~ else:
imgstr = '<span class="pathbox">'
pathstr += '<div><li><a title="%s" href="%s/index.html">%s<span class="pathlink"><span class="pathlinktext">%s (%s)</span></span></span></a></div>'%(
unicode(p,encoding="utf8").encode('ascii', 'xmlcharrefreplace'),
urllib.quote(p),
unescape(p),
urllib.parse.quote(p),
imgstr,
nice.encode('ascii', 'xmlcharrefreplace'),
nice,
nsum
)
pathstr += '</div>'
return pathstr
def filelinks(path, list):
''' Returns the HTML string of non image files '''
if len(list) == 0:
@@ -205,10 +213,11 @@ def filelinks(path, list):
strout += '<h2>Attachments</h1>'
for i in list:
size = sizestring(os.path.getsize(os.path.join(path,i)))
strout+='<div class="attachmentbox"><li><a href="'+urllib.quote(i)+'">'+unicode(i,encoding="utf8").encode('ascii', 'xmlcharrefreplace')+' ['+size+']</a></div>'
strout += '<div class="attachmentbox"><li><a href="'+urllib.parse.quote(i)+'">' + unescape(i) +' ['+size+']</a></div>'
strout += '</div>'
return strout
def traverse(path,crumbs,inputs,options):
''' The recursive main function to create the index.html and seek sub folders '''
@@ -253,6 +262,7 @@ def traverse(path,crumbs,inputs,options):
traverse(os.path.join(path,p),nextcrumbs,inputs,options)
return
def setupoptions():
''' Setup the command line options '''
from argparse import ArgumentParser
@@ -327,6 +337,7 @@ def setupdefaultoptions(options):
options.width=850
return options
def execute_plain():
''' Main execution function '''
options=setupoptions()

View File

@@ -1 +1 @@
from Qalbum import *
from qalbum.Qalbum import *

View File

@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2016 Ville Rantanen
#
@@ -39,13 +39,14 @@ def createdesc(path,list,options):
nsum=len(list)
for i in list:
inpath=os.path.join(path,i)
desc=create_description(inpath,options.format).rstrip().replace('\n','<br/>')
desc = create_description(inpath,options.format).decode('utf-8').rstrip().replace('\n','<br/>')
outfile.write(i+"\t"+desc+'\n')
outfile.flush()
print('('+str(n)+'/'+str(nsum)+') '+i+"\t"+desc)
n+=1
return
def create_description(infile,format):
if format=='AbsolutelyEverything':
idargs=['identify','-verbose',infile+'[0]']
@@ -55,6 +56,7 @@ def create_description(infile,format):
output = idp.stdout.read()
return output
def traverse(path,options):
''' The recursive main function to create the thumbs and seek sub folders '''
print(path)
@@ -68,6 +70,7 @@ def traverse(path,options):
traverse(os.path.join(path,p),options)
return
def execute():
''' Main execution '''
parser=ArgumentParser()
@@ -93,9 +96,9 @@ def execute():
'%f<br/><i>%[EXIF:DateTimeOriginal] %[EXIF:ExposureTime]s F%[EXIF:FNumber]</i>',
'AbsolutelyEverything']
if options.preset < 1:
print "Presets:"
print("Presets:")
for row in range(len(presets)):
print row+1," ",presets[row]
print(row+1," ",presets[row])
sys.exit(0)
if options.format=="":
if options.preset>len(presets):

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2016 Ville Rantanen
#
@@ -37,6 +37,7 @@ def traverse(path,options):
traverse(os.path.join(path,p),options)
return
def setupoptions():
''' Setup options '''
usage='''Usage: %(prog)s [options] folder
@@ -62,12 +63,14 @@ folder is the root folder of the image album (defaults to current folder).'''
options=Qalbum.setupdefaultoptions(options)
return options
def execute():
''' Main execution '''
options=setupoptions()
traverse(options.startpath,options)
return
if __name__ == "__main__":
execute()

View File

@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re

View File

@@ -8,12 +8,12 @@ setup(
'scripts/Qalbum-descriptor'],
package_data={'':['lib/*']},
include_package_data=True,
version = '2.20190411a',
version = '2.20200922',
description = 'A tool to create a web gallery from a folder structure of images / other files.',
author = 'Ville Rantanen',
author_email = 'ville.q.rantanen@gmail.com',
url = 'https://bitbucket.org/MoonQ/qalbum',
download_url = 'https://bitbucket.org/MoonQ/qalbum/get/tip.tar.gz',
download_url = 'https://bitbucket.org/MoonQ/qalbum/get/master.tar.gz',
keywords = ['album', 'generator', 'javascript'],
classifiers = [],
license = 'MIT',