Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- __author__ = 'Olivier Pieters'
- __author_email__ = 'me@olivierpieters.be'
- import yaml
- from os import listdir
- from os.path import isfile, join
- # configuration
- output_file = "test.yaml"
- input_file = output_file
- mypath = "."
- extensions= ['jpg', 'png'] # only small caps!
- # extract image files
- files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
- files = [f for f in files if f[f.rfind('.')+1:].lower() in extensions ]
- # helper objects to store gallery data
- new_gallery = {}
- thumbs = {}
- # group gallery data
- for f in files:
- filename = f[:f.rfind('-')]
- filename = filename[:filename.rfind(.)]
- if filename == "thumb":
- thumbs[filename] = f
- else:
- if filename in new_gallery:
- new_gallery[filename].append(f)
- else:
- new_gallery[filename] = [f]
- # try to load YAML data
- if isfile(destination_file):
- input_gallery = yaml.load(open(input_file, 'r'))
- old_gallery = input_gallery['pictures']
- else:
- # create empty dummy file
- input_gallery = {"pictures": {}}
- old_gallery = {}
- # merge two data sets into one
- for pic in new_gallery:
- found = False
- # try to find matching filename
- for i in old_gallery:
- if pic == i["filename"]:
- i["sizes"] = new_gallery[pic]
- # include thumbnail if present
- if pic in thumb:
- i["thumb"] = thumb[pic]
- found = True
- if not found:
- # create new entry
- old_gallery.append({"filename": pic, "sizes": gallery[pic]})
- # write to output file
- with open(output_file, 'w') as f:
- f.write( yaml.dump(input_gallery, default_flow_style=True) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement