Example #1
0
 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
Example #2
0
 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
Example #3
0
 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
Example #4
0
 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
Example #5
0
 def __del__(self):
     if self.__need_save and self.__has_write:
         if os.path.exists(self.__cache_file):
             is_exist, url_map = load(self.__cache_file)
             assert is_exist
         else:
             url_map = {}
         key, updated_value = self.__updated_key_value
         assert key and updated_value
         url_map[key] = updated_value
         # TODO: better use file lock (fcntl) to avoid file update error in mul-instances usage
         save(self.__cache_file, url_map)
Example #6
0
 def __del__(self):
     if self.__need_save and self.__has_write:
         if os.path.exists(self.__cache_file):
             is_exist, url_map = load(self.__cache_file)
             assert is_exist
         else:
             url_map = {}
         key, updated_value = self.__updated_key_value
         assert key and updated_value
         url_map[key] = updated_value
         # TODO: better use file lock (fcntl) to avoid file update error in mul-instances usage
         save(self.__cache_file, url_map)
Example #7
0
 def fetch(self, pattern):
     self.__has_write = False
     new_objs, old_objs = self.get_updated_url(pattern)
     show(get_msg(Msg.total_data_count), len(new_objs) + len(old_objs))
     url = self.choose_url(new_objs, old_objs)
     if NA == url:
         return NA, NA
     image_objs = old_objs
     image_objs.update(new_objs)
     image_slot = image_objs[url]
     graph_file, new_encoding = self.get_graph_file(pattern, url, image_slot.encoding)
     new_slot = ImageSlot(image_slot.timestamp, new_encoding, image_slot.rank)
     image_objs[url] = new_slot
     if self.__has_write:
         save(GraphFetcher.get_cache_file(pattern), image_objs)
     return graph_file, GraphFetcher.get_graph_digest(graph_file, image_objs[url])
Example #8
0
 def fetch(self, pattern):
     self.__has_write = False
     new_objs, old_objs = self.get_updated_url(pattern)
     debug("[fetch] total data count: %s" % str(len(new_objs) + len(old_objs)))
     url = self.choose_url(new_objs, old_objs)
     if NA == url:
         return NA, NA
     image_objs = old_objs
     image_objs.update(new_objs)
     image_slot = image_objs[url]
     graph_file, new_encoding = self.get_graph_file(pattern, url, image_slot.encoding)
     new_slot = ImageSlot(image_slot.timestamp, new_encoding, image_slot.rank)
     image_objs[url] = new_slot
     if self.__has_write:
         save(GraphFetcher.get_cache_file(pattern), image_objs)
     return graph_file, GraphFetcher.get_graph_digest(graph_file, image_objs[url])
Example #9
0
def save(dest = ""):
    if event.end_game: return
    level_set = os.path.split(level_dir)[-1]
    save_path = os.path.join(savegame.save_path, current_level+".yaml")
    savegame.set_current_level(level_set, current_level)
    event_obj = event.get_yaml_object()
    decal_obj = serialize.YamlDecalInvisList(
        [d.obj_id for d in decal.decals if not d.visible]
    )
    save_list = physics.body_update_list + decal.decals
    level_obj = serialize.YamlLevelData(level_dir, current_level)
    yaml_list = [level_obj, event_obj]
    serialize.save(
        level_dir, current_level, save_list, save_path, yaml_list
    )
    if dest != "":
        savegame.save_to(dest)
Example #10
0
 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
Example #11
0
 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