def recover_imgs(): print 'starting img recovery attempt' if not os.path.exists('images'): os.mkdir('images') ff = os.listdir('images') picts = pictures() print 'local imgs: {}; fully initialized instances from db: {}'.format( len(ff), len(picts)) pn = [('.'.join(f.split('.')[:-1]),f) for f in ff] # orphan img files orph = [] for n,f in pn: p = picture.get(n) if not p: orph.append((n,f)) else: if not p.path: orph.append((n,f)) #orph = [(n,f) for n,f in pn if insuff(n)] if len(orph)>0: print 'Orphan img files in images/ dir: {}'.format( len(orph)) res = [] for n,f in orph: pict = picture.openfile('images', f, name=n) if pict: res.append(pict) print 'returning {} recovered img objs.'.format( len(res)) #print 'existing instances improved by recovered data: {}'.format( #len([p for p in res if p in picts])) return res print 'no recovery needed.'
def assign_img(self, img, time=None): res = False if img: if isinstance(img, picture.Pict): pict = img else: pict = picture.get(img) if pict: if not time: time = pict.date # add image ref to blog obj if not pict in self.images: self.images.add(pict) self.images_times[pict] = time res = True # add blog ref to img obj if not self in pict.sources: pict.sources.append(self) res = True #print 'assigning {} to {}.'.format(pict.name, self.name) else: if not img in self.images: self.images.add(img) res = True else: print 'tumblr.assign_img: invalid img ref. wtf!!!' return res
def clean_img_refs(t): imgs = list(t.images) t.images = set() for i in imgs: if i: if isinstance(i, picture.Pict): p = i else: p = picture.get(i) #t.assign_img(p) t.images.add(p)