Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. __author__ = 'Olivier Pieters'
  4. __author_email__ = 'me@olivierpieters.be'
  5.  
  6. import yaml
  7. from os import listdir
  8. from os.path import isfile, join
  9.  
  10. # configuration
  11. output_file = "test.yaml"
  12. input_file = output_file
  13. mypath = "."
  14. extensions= ['jpg', 'png'] # only small caps!
  15.  
  16. # extract image files
  17. files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
  18. files = [f for f in files if f[f.rfind('.')+1:].lower() in extensions ]
  19.  
  20. # helper objects to store gallery data
  21. new_gallery = {}
  22. thumbs = {}
  23.  
  24. # group gallery data
  25. for f in files:
  26. filename = f[:f.rfind('-')]
  27. filename = filename[:filename.rfind(.)]
  28. if filename == "thumb":
  29. thumbs[filename] = f
  30. else:
  31. if filename in new_gallery:
  32. new_gallery[filename].append(f)
  33. else:
  34. new_gallery[filename] = [f]
  35.  
  36. # try to load YAML data
  37. if isfile(destination_file):
  38. input_gallery = yaml.load(open(input_file, 'r'))
  39. old_gallery = input_gallery['pictures']
  40. else:
  41. # create empty dummy file
  42. input_gallery = {"pictures": {}}
  43. old_gallery = {}
  44.  
  45. # merge two data sets into one
  46. for pic in new_gallery:
  47. found = False
  48. # try to find matching filename
  49. for i in old_gallery:
  50. if pic == i["filename"]:
  51. i["sizes"] = new_gallery[pic]
  52. # include thumbnail if present
  53. if pic in thumb:
  54. i["thumb"] = thumb[pic]
  55. found = True
  56. if not found:
  57. # create new entry
  58. old_gallery.append({"filename": pic, "sizes": gallery[pic]})
  59.  
  60. # write to output file
  61. with open(output_file, 'w') as f:
  62. f.write( yaml.dump(input_gallery, default_flow_style=True) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement