-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatom.py
155 lines (114 loc) · 4.46 KB
/
gatom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import argparse
from jinja2 import PackageLoader , Environment
from PIL import Image
import sys,os
import imghdr
import shutil
import SimpleHTTPServer ,SocketServer
sys.path.append(os.path.join(os.path.dirname(__file__),os.path.pardir))
env = Environment(loader = PackageLoader('gatom','templates'))
class album:
name = 'gallery'
url = '/gallery'
carousel_url = ''
cover = ''
totalPhoto = 0
totalSubAlbum = 0
class photo:
name = 'photo'
url = '/'
def runHttpServer(documentRoot,port):
os.chdir(documentRoot)
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("",port),handler)
print "server on port 8012..."
httpd.serve_forever()
def prepare_image(src,dst):
if not os.path.exists(dst) :
im = Image.open(src)
im.save(dst)
def generator_html(path,template_name ,args):
template = env.get_template(template_name)
template_content = template.render(**args)
# save static file
new_file = file(path, 'w')
new_file.write(template_content)
new_file.close()
def createAlbums(srcPath,dstPath,urlPath):
images_list = list()
albums_list = list()
album_item = album()
album_item.name =os.path.basename(dstPath)
album_item.url = urlPath
album_item.carousel_url = urlPath + '/album.html'
print "Processing Album " + album_item.name + " in " +dstPath + " ..."
for obj in os.listdir(srcPath):
src_obj_path = os.path.join(srcPath,obj)
dst_obj_path = os.path.join(dstPath,obj)
if os.path.isfile(src_obj_path) and ( imghdr.what(src_obj_path) is not None ):
image = photo()
album_item.totalPhoto +=1
prepare_image(src_obj_path,dst_obj_path)
image.name = obj
image.url = os.path.join(urlPath ,obj)
if album_item.cover is '':
album_item.cover = image.url
images_list.append(image)
if os.path.isdir(src_obj_path):
if not os.path.exists(dst_obj_path):
os.mkdir(dst_obj_path)
album_item.totalSubAlbum += 1
albums_list.append(createAlbums(src_obj_path,os.path.join(dstPath,obj),os.path.join(urlPath,obj)) )
"""if albums_list or image_list is not none"""
args = dict()
args['title'] = 'Gatom'
if images_list:
args['images'] = images_list
if albums_list:
args['albums'] = albums_list
if album_item:
args['currentAlbum'] = album_item
generator_html(os.path.join(dstPath ,'index.html'),'gallery.jinja2', args)
generator_html(os.path.join(dstPath ,'album.html'),'album.jinja2', args)
return album_item
def createStatic(dstPath):
if not os.path.exists(os.path.join(dstPath,'css')):
shutil.copytree('css',os.path.join(dstPath,'css'))
if not os.path.exists(os.path.join(dstPath,'js')):
shutil.copytree('js',os.path.join(dstPath,'js'))
if not os.path.exists(os.path.join(dstPath,'fonts')):
shutil.copytree('fonts',os.path.join(dstPath,'fonts'))
if not os.path.exists(os.path.join(dstPath,'images')):
shutil.copytree('images',os.path.join(dstPath,'images'))
def buildGallery(srcPath,dstPath):
if not os.path.exists(dstPath):
os.mkdir(dstPath)
urlPath = '/'
if not os.path.exists(dstPath):
os.mkdir(dstPath)
createStatic(dstPath)
createAlbums(srcPath,dstPath,os.path.join(urlPath))
def process(args):
"Initialize arguments"
src_gallery_path = args.src
dst_gallery_path = args.dst or "gallery"
if args.port is not None:
serverPort = int(args.port)
else:
serverPort = 8012
"""TODO create menu architecture """
gallery = buildGallery(src_gallery_path,dst_gallery_path)
"""TODO prepare photoes """
"""TODO create gallery"""
if args.server:
runHttpServer(dst_gallery_path,serverPort)
def main():
parser = argparse.ArgumentParser(description='Gatom static gallery auto generator.')
parser.add_argument('--src',required=True ,help='Source directory for the gallery')
parser.add_argument('--dst', help='Destination where the gallery site will be generated')
parser.add_argument('--port', help='Web server listening port. Default value is 8012')
parser.add_argument('--server',action='store_true',help='Whether or not run web server for newly generated gallery ')
args = parser.parse_args()
process(args)
if __name__ == '__main__' :
main()