def __init__(self): self.adapters = { 'pantry': lambda t, r: self.rename_rows(t, r, {'itm': 'ingkey'}), 'categories': self.handle_categories, 'ingredients': self.adapt_ids, 'recipe': self.handle_recipe, 'shopcats': lambda t, r: self.rename_rows(t, r, { 'shopkey': 'ingkey', 'category': 'shopcategory', }), 'shopcatsorder': lambda t, r: self.rename_rows(t, r, {'category': 'shopcategory'}), } self.id_converter = {} self.top_id = 1 from importers.importer import RatingConverter self.rc = RatingConverter() import convert self.conv = convert.converter()
def __init__ (self): self.adapters = { 'pantry':lambda t,r: self.rename_rows(t,r,{'itm':'ingkey'}), 'categories':self.handle_categories, 'ingredients':self.adapt_ids, 'recipe':self.handle_recipe, 'shopcats':lambda t,r: self.rename_rows(t,r,{'shopkey':'ingkey', 'category':'shopcategory', }), 'shopcatsorder':lambda t,r: self.rename_rows(t,r,{'category':'shopcategory'}), } self.id_converter = {} self.top_id = 1 from importers.importer import RatingConverter self.rc = RatingConverter() import convert self.conv = convert.converter()
class DatabaseAdapter: """Adapt old data to new""" def __init__(self): self.adapters = { 'pantry': lambda t, r: self.rename_rows(t, r, {'itm': 'ingkey'}), 'categories': self.handle_categories, 'ingredients': self.adapt_ids, 'recipe': self.handle_recipe, 'shopcats': lambda t, r: self.rename_rows(t, r, { 'shopkey': 'ingkey', 'category': 'shopcategory', }), 'shopcatsorder': lambda t, r: self.rename_rows(t, r, {'category': 'shopcategory'}), } self.id_converter = {} self.top_id = 1 from importers.importer import RatingConverter self.rc = RatingConverter() import convert self.conv = convert.converter() def adapt(self, table_name, row): if self.adapters.has_key(table_name): return self.adapters[table_name](table_name, row) else: return table_name, row def cleanup(self, db): """Things we need to do after import is done.""" self.rc.do_conversions(db) def rename_rows(self, table_name, row, name_changes): new_row = {} for k, v in row.items(): if name_changes.has_key(k): new_row[name_changes[k]] = v else: new_row[k] = v return table_name, new_row def adapt_ids(self, table_name, row, id_cols=['id', 'refid']): for c in id_cols: if row.has_key(c) and type(row[c]) != int: if row[c]: row[c] = self.adapt_id(row[c]) else: del row[c] for c in ['unit', 'item', 'ingkey']: if row.get(c, None): row[c] = row[c].strip() return table_name, row def adapt_id(self, id_obj): if self.id_converter.has_key(id_obj): return self.id_converter[id_obj] else: self.id_converter[id_obj] = self.top_id self.top_id += 1 return self.id_converter[id_obj] def handle_categories(self, table_name, row): if row.has_key('type'): return None if row.get('category', None): row['category'] = row['category'].strip() else: return table_name, row def handle_recipe(self, table_name, row): table_name, row = self.adapt_ids(table_name, row, id_cols=['id']) retval = [] if row.has_key('category'): cats = row['category'] if cats: for c in cats.split(','): crow = { 'id': row['id'], 'category': re.sub('\s+', ' ', c).strip(), } retval.append(('categories', crow)) del row['category'] if row.has_key('rating') and row['rating'] and type( row['rating']) != int: self.rc.add(row['id'], row['rating']) del row['rating'] for c in ['preptime', 'cooktime']: if row.has_key(c) and type(row[c]) != int: secs = self.conv.timestring_to_seconds(row[c]) if secs: row[c] = secs else: if row[c]: if not row.has_key('instructions'): row['instructions'] = c + ': ' + row[c] else: add = c + ': ' + row[c] + '\n\n' row['instructions'] = add + row['instructions'] del row[c] for to_buffer in ['image', 'thumb']: if row.has_key(to_buffer) and row[to_buffer]: row[to_buffer] = buffer(row[to_buffer]) if ((row.has_key('image') and row['image']) and (not row.has_key('thumb') or not row['thumb'])): # Some old versions of Gourmet had a bug that prevented # proper thumbnail creation. If our user has images with # no thumbnails, we will kindly create the thumbnails for # them. import ImageExtras img = ImageExtras.get_image_from_string(row['image']) if not img: print 'Odd -- no image for ', row.get('title', 'Untitled?') else: thumb = ImageExtras.resize_image(img, 40, 40) print 'Adding thumbnail to', row.get('title', 'Untitled?') row['thumb'] = buffer(ImageExtras.get_string_from_image(thumb)) for c in ['title', 'cuisine', 'source']: if row.get(c, None): row[c] = row[c].strip() return retval + [(table_name, row)]
class DatabaseAdapter: """Adapt old data to new""" def __init__ (self): self.adapters = { 'pantry':lambda t,r: self.rename_rows(t,r,{'itm':'ingkey'}), 'categories':self.handle_categories, 'ingredients':self.adapt_ids, 'recipe':self.handle_recipe, 'shopcats':lambda t,r: self.rename_rows(t,r,{'shopkey':'ingkey', 'category':'shopcategory', }), 'shopcatsorder':lambda t,r: self.rename_rows(t,r,{'category':'shopcategory'}), } self.id_converter = {} self.top_id = 1 from importers.importer import RatingConverter self.rc = RatingConverter() import convert self.conv = convert.converter() def adapt (self, table_name, row): if self.adapters.has_key(table_name): return self.adapters[table_name](table_name,row) else: return table_name,row def cleanup (self, db): """Things we need to do after import is done.""" self.rc.do_conversions(db) def rename_rows (self, table_name, row, name_changes): new_row = {} for k,v in row.items(): if name_changes.has_key(k): new_row[name_changes[k]]=v else: new_row[k]=v return table_name,new_row def adapt_ids (self, table_name, row, id_cols=['id','refid']): for c in id_cols: if row.has_key(c) and type(row[c])!=int: if row[c]: row[c]=self.adapt_id(row[c]) else: del row[c] for c in ['unit','item','ingkey']: if row.get(c,None): row[c]=row[c].strip() return table_name,row def adapt_id (self, id_obj): if self.id_converter.has_key(id_obj): return self.id_converter[id_obj] else: self.id_converter[id_obj]=self.top_id self.top_id += 1 return self.id_converter[id_obj] def handle_categories (self, table_name, row): if row.has_key('type'): return None if row.get('category',None): row['category']=row['category'].strip() else: return table_name,row def handle_recipe (self, table_name, row): table_name,row = self.adapt_ids(table_name,row,id_cols=['id']) retval = [] if row.has_key('category'): cats = row['category'] if cats: for c in cats.split(','): crow = {'id':row['id'], 'category':re.sub('\s+',' ',c).strip(), } retval.append(('categories',crow)) del row['category'] if row.has_key('rating') and row['rating'] and type(row['rating'])!=int: self.rc.add(row['id'],row['rating']) del row['rating'] for c in ['preptime','cooktime']: if row.has_key(c) and type(row[c])!=int: secs = self.conv.timestring_to_seconds(row[c]) if secs: row[c] = secs else: if row[c]: if not row.has_key('instructions'): row['instructions'] = c+': '+row[c] else: add = c+': '+row[c]+'\n\n' row['instructions'] = add + row['instructions'] del row[c] for to_buffer in ['image','thumb']: if row.has_key(to_buffer) and row[to_buffer]: row[to_buffer]=buffer(row[to_buffer]) if ((row.has_key('image') and row['image']) and (not row.has_key('thumb') or not row['thumb']) ): # Some old versions of Gourmet had a bug that prevented # proper thumbnail creation. If our user has images with # no thumbnails, we will kindly create the thumbnails for # them. import ImageExtras img = ImageExtras.get_image_from_string(row['image']) if not img: print 'Odd -- no image for ',row.get('title','Untitled?') else: thumb = ImageExtras.resize_image(img,40,40) print 'Adding thumbnail to',row.get('title','Untitled?') row['thumb'] = buffer(ImageExtras.get_string_from_image(thumb)) for c in ['title','cuisine','source']: if row.get(c,None): row[c]=row[c].strip() return retval + [(table_name,row)]