def handle_image(graph_file, action): assert action in [DELETE, DISCARD, INC_RANK, DEC_RANK] key_str = get_delim() + "image_" end_pos = graph_file.find(key_str) assert -1 != end_pos begin_pos = graph_file[:end_pos].rfind(get_delim()) assert -1 != begin_pos pattern = graph_file[begin_pos + 1:end_pos] has_cache, cached_objs = load(GraphFetcher.get_cache_file(pattern)) assert has_cache file_encoding = graph_file[graph_file.find(key_str) + len(key_str):graph_file.find(".jpg")] for url in cached_objs: image_slot = cached_objs[url] if image_slot.encoding == file_encoding: new_encoding = NA if DELETE == action else \ None if DISCARD == action else \ image_slot.encoding # no change new_rank = image_slot.rank + 1 if INC_RANK == action else \ image_slot.rank - 1 if DEC_RANK == action and image_slot.rank is not 1 else \ image_slot.rank # no change updated_slot = ImageSlot(timestamp=image_slot.timestamp, encoding=new_encoding, rank=new_rank) cached_objs[url] = updated_slot save(GraphFetcher.get_cache_file(pattern), cached_objs) if action in [DELETE, DISCARD]: os.remove(graph_file) msg = "" if action in [DELETE, DISCARD] else \ get_msg(Msg.change_rank_to) + str(new_rank) + "!" if new_rank is not image_slot.rank else \ get_msg(Msg.cannot_lower_down_rank_as_it_is_already_the_lowest) if image_slot.rank is 1 else \ None assert msg is not None return msg assert False
def handle_image(location, graph_file, action): assert action in [INC_RANK, DEC_RANK] handler = GraphDirHandler(location) assert handler.__valid base_file = graph_file.replace(location + get_delim(), "") for image in handler.__status_cache: if image == base_file: has_change = True status = handler.__status_cache[image] if INC_RANK == action: status.rank += 1 msg = get_msg(Msg.change_rank_to) + str(status.rank) else: if 1 == status.rank: msg = get_msg(Msg.cannot_lower_down_rank_as_it_is_already_the_lowest) has_change = False else: status.rank -= 1 msg = get_msg(Msg.change_rank_to) + str(status.rank) if has_change: handler.__status_cache[image] = status cache_file = location + get_delim() + GraphDirHandler.CACHE_FILE # TODO: it shows that the timestamp not change... timestamp = time.ctime(os.path.getmtime(location)) save(cache_file, [timestamp, handler.__status_cache]) return msg assert False
def handle_image(location, graph_file, action): assert action in [INC_RANK, DEC_RANK] handler = GraphDirHandler(location) assert handler.__valid base_file = graph_file.replace(location + get_delim(), "") for image in handler.__status_cache: if image == base_file: has_change = True status = handler.__status_cache[image] if INC_RANK == action: status.rank += 1 msg = "change rank to " + str(status.rank) else: if 1 == status.rank: msg = "cannot lower down rank as it is already the lowest!" has_change = False else: status.rank -= 1 msg = "change rank to " + str(status.rank) if has_change: handler.__status_cache[image] = status cache_file = location + get_delim( ) + GraphDirHandler.CACHE_FILE # TODO: it shows that the timestamp not change... timestamp = time.ctime(os.path.getmtime(location)) save(cache_file, [timestamp, handler.__status_cache]) return msg assert False
def handle_image(graph_file, action): assert action in [DELETE, DISCARD, INC_RANK, DEC_RANK] key_str = get_delim() + "image_" end_pos = graph_file.find(key_str) assert -1 != end_pos begin_pos = graph_file[:end_pos].rfind(get_delim()) assert -1 != begin_pos pattern = graph_file[begin_pos + 1:end_pos] has_cache, cached_objs = load(GraphFetcher.get_cache_file(pattern)) assert has_cache file_encoding = graph_file[graph_file.find(key_str) + len(key_str):graph_file.find(".jpg")] for url in cached_objs: image_slot = cached_objs[url] if image_slot.encoding == file_encoding: new_encoding = NA if DELETE == action else \ None if DISCARD == action else \ image_slot.encoding # no change new_rank = image_slot.rank + 1 if INC_RANK == action else \ image_slot.rank - 1 if DEC_RANK == action and image_slot.rank is not 1 else \ image_slot.rank # no change updated_slot = ImageSlot(timestamp=image_slot.timestamp, encoding=new_encoding, rank=new_rank) cached_objs[url] = updated_slot save(GraphFetcher.get_cache_file(pattern), cached_objs) if action in [DELETE, DISCARD]: os.remove(graph_file) msg = "" if action in [DELETE, DISCARD] else \ "change rank to " + str(new_rank) + "!" if new_rank is not image_slot.rank else \ "cannot lower down rank as it is already the lowest!" if image_slot.rank is 1 else \ None assert msg is not None return msg assert False
def get_graph_digest(self, graph_file): if NA is graph_file: return "NA" full_graph_file = self.__location + get_delim() + graph_file timestamp = time.ctime(os.path.getmtime(full_graph_file)) return "location:%s\ntimestamp:%s\nrank:%s" % ( full_graph_file, timestamp, self.__status_cache[graph_file].rank)
def __load_or_create_status(self): status_cache = {} # key: image_file, value: status cache_file = self.__location + get_delim() + GraphDirHandler.CACHE_FILE cache_existed = os.path.exists(cache_file) if cache_existed: success, cache_data = load(cache_file) assert success [timestamp, status_cache] = cache_data if not self.dir_changed(timestamp): return status_cache else: info("directory %s has changed, update cache file" % self.__location) else: info("create a new cache file for directory: %s" % self.__location) image_files = [] for root, _, files in os.walk(self.__location): assert len(root) >= 1 if root[-1] != get_delim(): root += get_delim() for base_file in files: basename, ext = os.path.splitext(base_file) if ext.replace(".", "") in GraphDirHandler.RECOGNIZED_IMAGE_EXT: image_files.append((root + base_file).replace( self.__location + get_delim(), "")) if not image_files: if cache_existed: os.remove(cache_file) self.__valid = False return None existed_image = {} for image in image_files: existed_image[image] = 1 # 1 is just a dummy value if image not in status_cache: status_cache[image] = Status() to_be_deleted = [] for image in status_cache: # this check works when some image is deleted if image not in existed_image: to_be_deleted.append(image) for image in to_be_deleted: status_cache.pop(image) # TODO: this makes an 'always' has-changed 2nd time image timestamp = time.ctime(os.path.getmtime(self.__location)) save(cache_file, [timestamp, status_cache]) return status_cache
def get_graph_digest(self, graph_file): if NA is graph_file: return "NA" full_graph_file = self.__location + get_delim() + graph_file timestamp = time.ctime(os.path.getmtime(full_graph_file)) return "%s:%s\n%s:%s\n%s:%s" % ( get_msg(Msg.location), full_graph_file, get_msg(Msg.timestamp), timestamp, get_msg(Msg.rank), self.__status_cache[graph_file].rank)
def __load_or_create_status(self): status_cache = {} # key: image_file, value: status cache_file = self.__location + get_delim() + GraphDirHandler.CACHE_FILE cache_existed = os.path.exists(cache_file) if cache_existed: success, cache_data = load(cache_file) assert success [timestamp, status_cache] = cache_data if not self.dir_changed(timestamp): return status_cache else: info(get_msg(Msg.directory), self.__location, get_msg(Msg.has_changed_update_cache_file)) else: info("%s%s" % (get_msg(Msg.create_new_cache_file_for_directory), self.__location)) image_files = [] for root, _, files in os.walk(self.__location): assert len(root) >= 1 if root[-1] != get_delim(): root += get_delim() for base_file in files: basename, ext = os.path.splitext(base_file) if ext.replace(".", "") in GraphDirHandler.RECOGNIZED_IMAGE_EXT: image_files.append((root + base_file).replace(self.__location, "")) if not image_files: if cache_existed: os.remove(cache_file) self.__valid = False return None existed_image = {} for image in image_files: existed_image[image] = 1 # 1 is just a dummy value if image not in status_cache: status_cache[image] = Status() to_be_deleted = [] for image in status_cache: # this check works when some image is deleted if image not in existed_image: to_be_deleted.append(image) for image in to_be_deleted: status_cache.pop(image) # TODO: this makes an 'always' has-changed 2nd time image timestamp = time.ctime(os.path.getmtime(self.__location)) save(cache_file, [timestamp, status_cache]) return status_cache
def get_graph(self): if not self.__valid: return NA, NA graph_file = get_weighted_random_dict_key(self.__status_cache) full_graph_file = self.__location + get_delim() + graph_file return full_graph_file, self.get_graph_digest(graph_file)
def pickle_home(): return get_data_home() + "pickle" + get_delim()
def pic_home(): return get_data_home() + "picture" + get_delim()
def get_graph_dir(pattern): return pic_home() + str(pattern) + get_delim()