-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
145 lines (130 loc) · 4.38 KB
/
utils.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
from ConfigParser import ConfigParser
import fnmatch
import os
from mako.template import Template
from functools import partial
from types import MethodType
import json
from pyexiv2 import Image
import re
# read in the config
config = ConfigParser()
config.read('development.ini')
_config = {}
# i prefer using a normal dict for lookups
for section in config.sections():
_config[section] = {}
for k,v in config.items(section):
_config[section][k] = v
_config.update(config.defaults())
config = _config
# ty: http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python
def recursive_find(root,patterns):
if type(patterns) not in (tuple,list):
patterns = [patterns]
patterns = [re.compile(pattern) for pattern in patterns]
matches = []
for root, dirnames, filenames in os.walk(root):
file_matches = _recursive_find(filenames,patterns)
matches += [os.path.join(root,m) for m in file_matches]
return matches
def _recursive_find(filenames,patterns):
m = []
for p in patterns:
m += [x for x in filenames if p.match(x)]
return m
def read_map(map_path):
# we are going to read in the map as json
try:
lookup = json.load(open(map_path,'r'))
except IOError: # no existing file
lookup = {}
except ValueError: # can't deserialize
lookup = {}
return lookup
def get_map():
""" return default map as read from config """
map_path = config.get('map_path')
return read_map(map_path)
def get_renderer(name):
root = config.get('template_root')
path = os.path.join(root,'%s.mako' % name)
template = Template(filename=path)
# we are going to decorate the templates
# render method to make the config available
_r = partial(template.render,
config=config,
get_media_page_url=get_media_page_url,
get_media_url=get_media_url)
return _r
IMAGE_COMMENT_TAG = 'Exif.Image.ImageDescription'
COMMENT_DELIMINATOR = '\n\n'
def get_image_comments(path):
image = Image(path)
image.readMetadata()
comments = image.getComment()
if not comments:
return []
pieces = comments.split(COMMENT_DELIMINATOR)
comments = []
print 'getting:',path
for piece in pieces:
data = {}
sub_pieces = [x.strip() for x in piece.split(':') if x.strip()]
# the first piece (if there is more than one)
# is the label. if there is only one it's the body
if len(sub_pieces) == 0:
continue
elif len(sub_pieces) == 1:
body = data.get('body','')
body += ('\n' if body else '') + sub_pieces[0]
data['body'] = body
else:
# first is going to be the label w/
# possible sub labels
if '[' in sub_pieces[0]:
label = sub_pieces[0].split('[')[0]
data['label'] = label
sub_labels = sub_pieces[0][len(label):]
sub_labels = sub_labels.replace('[')
# the last one will be blank
sub_labels = sub_labels.split(']')[:-1]
data['sub_labels'] = sub_labels
else:
data['label'] = sub_pieces[0]
print 'adding:',data
comments.append(data)
return comments
TEMPLATES = {
'rating': "rating[0-100]:%s",
'body': "%s"
}
def set_image_comment(path,*args,**kwargs):
"""
takes any kwarg, to use as a labeled part of the comment.
can also take single arg to use as body of comment
"""
if args and not kwargs:
kwargs['body'] = '\n'.join(args)
image = Image(path)
image.readMetadata()
# append is a possible kwarg
append = True
existing = image.getComment() or "" if append else ""
comment_parts = [COMMENT_DELIMINATOR] if existing else []
for k,v in kwargs.iteritems():
if v is None:
continue
if k == 'append':
append = v
else:
template = TEMPLATES.get(k,"%s:%s" % (k,v))
comment_parts.append(template % v)
image.setComment(existing + "\n".join(comment_parts))
image.writeMetadata()
def get_media_page_url(mid):
""" return url for media's page """
return '%s%s' % (config.get('media_pages_root'),mid)
def get_media_url(mid):
""" returns the url for media's data """
return '%s%s' % (config.get('media_files_root'),mid)