/
create_layout_definition.py
executable file
·69 lines (48 loc) · 1.52 KB
/
create_layout_definition.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
#!/usr/bin/python
'''
Reads all subdirectories of the argv directory and
creates portfolio sets. The names of the subdirectories
will be used as the set titles and the first instance of a
.txt files content will be used for the set description.
'''
import sys
from os import walk, sep, path
from string import find, rsplit
from json import JSONEncoder
dirfiles = 2
topdir = 0
def main(argv=None):
if argv is None:
argv = sys.argv
scanPortfolio(argv[1])
def scanPortfolio(dir):
output = list()
listing = walk(dir)
for step in listing:
title = rsplit(step[topdir], sep, 1)[1]
if title != "":
print "creating portfolio set named '" + title + "'"
images = list()
images = filter(lambda f : find(f.lower(), ".jpg") > 0, step[dirfiles])
images = map(lambda i : path.join(step[topdir], i), images)
portfolioSet = dict()
portfolioSet['title'] = title
portfolioSet['images'] = images
output.append(portfolioSet)
output = sortPortfolios(output)
layoutFile = open(path.join(dir, "layout.json"), 'w')
layoutFile.write(JSONEncoder().encode(output))
def sortPortfolios(outputList):
titleMap = dict()
for index, portfolioSet in enumerate(outputList):
titleMap[portfolioSet['title']] = index
titles = titleMap.keys()
titles = sorted(titles)
sortedOutputList = list()
print "new portfolio ordering:"
for title in titles:
sortedOutputList.append(outputList[titleMap[title]])
print "- " + title
return sortedOutputList
if __name__ == "__main__":
sys.exit(main())