def save_replay(self, replay_data, replay_dir, prefix=None): """Save a replay to a directory, returning the path to the replay. Args: replay_data: The result of controller.save_replay(), ie the binary data. replay_dir: Where to save the replay. This can be absolute or relative. prefix: Optional prefix for the replay filename. Returns: The full path where the replay is saved. Raises: ValueError: If the prefix contains the path seperator. """ if not prefix: replay_filename = "" elif os.path.sep in prefix: raise ValueError( "Prefix '%s' contains '%s', use replay_dir instead." % (prefix, os.path.sep)) else: replay_filename = prefix + "_" now = datetime.datetime.utcnow().replace(microsecond=0) replay_filename += "%s.SC2Replay" % now.isoformat("-").replace( ":", "-") replay_dir = self.abs_replay_path(replay_dir) if not gfile.Exists(replay_dir): gfile.MakeDirs(replay_dir) replay_path = os.path.join(replay_dir, replay_filename) with gfile.Open(replay_path, "wb") as f: f.write(replay_data) return replay_path
def map_data(self, map_name, players=None): """Return the map data for a map by name or path.""" map_names = [map_name] if players: map_names.append( os.path.join(os.path.dirname(map_name), "(%s)%s" % (players, os.path.basename(map_name)))) for name in map_names: path = os.path.join(self.data_dir, "Maps", name) if gfile.Exists(path): with gfile.Open(path, "rb") as f: return f.read() raise ValueError("Map '%s' not found." % map_name)
def save_replay(self, replay_data, replay_dir, map_name): """Save a replay to a directory, returning the path to the replay. Args: replay_data: The result of controller.save_replay(), ie the binary data. replay_dir: Where to save the replay. This can be absolute or relative. map_name: The map name, used as a prefix for the replay name. Returns: The full path where the replay is saved. """ now = datetime.datetime.utcnow().replace(microsecond=0) replay_filename = "%s_%s.SC2Replay" % (os.path.splitext( os.path.basename(map_name))[0], now.isoformat("-").replace( ":", "-")) replay_dir = self.abs_replay_path(replay_dir) if not gfile.Exists(replay_dir): gfile.MakeDirs(replay_dir) replay_path = os.path.join(replay_dir, replay_filename) with gfile.Open(replay_path, "wb") as f: f.write(replay_data) return replay_path
def replay_data(self, replay_path): """Return the replay data given a path to the replay.""" with gfile.Open(self.abs_replay_path(replay_path), "rb") as f: return f.read()
def map_data(self, map_name): """Return the map data for a map by name or path.""" with gfile.Open(os.path.join(self.data_dir, "Maps", map_name), "rb") as f: return f.read()