def load_resource(self, resource, options=None, **new_options): options = options or utils.merged_dict(self.options, new_options) if isinstance(resource, basestring): if re.match(r'https?://',resource): self.logger.info("Fetching remote resource: "+resource) contents = urllib2.urlopen(resource).read() else: directory = options.get('directory','') location = os.path.join(directory, resource) # Extract the contents so we can close the file with open(location, 'rb') as resource_file: contents = resource_file.read() # StringIO implements a fuller file-like object resource_name = resource resource = StringIO(contents) else: # Totally not designed for large files!! # We need a multiread resource, so wrap it in StringIO if not hasattr(resource,'seek'): resource = StringIO(resource.read()) resource_name = getattr(resource, 'name', 'Unknown') return (resource, resource_name)
def load_resources(self, resources, resource_loader, options=None, **new_options): """ Loads the specified set of resources using the current factory settings with specified overrides. :param resources: Either a path/url or a mixed collection of directories, paths/urls, and open file objects. :param None options: When options are passed directly into the options parameter the current factory settings are ignored and only the specified options are used during replay load. :param new_options: Options values to override current factory settings for the collection of replays to be loaded. :rtype: generator(Resource) """ options = options or utils.merged_dict(self.options, new_options) # Path to a resource? if isinstance(resources, basestring): if re.match(r'https?://',resources): yield resource_loader(resources, options=options) else: for resource in utils.get_replay_files(resources, **options): yield resource_loader(resource, options=options) # File like object? elif hasattr(resources,'read'): yield resource_loader(resources, options=options) # Collection of the above else: for resource in resources: yield resource_loader(resource, options=options)
def load_replay(self, replay_file, options=None, **new_options): options = options or utils.merged_dict(self.options, new_options) if self.cache.has(replay_file): return self.cache.get(replay_file) else: replay = super(SC2Cache, self).load_replay(replay_file, options=options) self.cache.set(replay_file, replay) return replay
def load_map(self, map_file, options=None, **new_options): options = options or utils.merged_dict(self.options, new_options) if self.cache.has(map_file): return self.cache.get(map_file) else: map = super(SC2Cache, self).load_map(map_file, options=options) self.cache.set(map_file, map) return map
def load_replay(self, replay_file, options=None, **new_options): """ Loads the specified replay using current factory settings with the specified overrides. :param replay_file: An open file object or a path/url to a single file. :param None options: When options are passed directly into the options parameter the current factory settings are ignored and only the specified options are used during replay load. :param new_options: Options values to override current factory settings while loading this replay. :rtype: :class:`Replay` """ self.logger.info("Reading {0}".format(replay_file)) options = options or utils.merged_dict(self.options, new_options) resource, name = self.load_resource(replay_file, options=options) replay = Replay(resource, name, **options) load_events = options.get('load_events',True) load_map = options.get('load_map',False) autoplay = options.get('autoplay',True) for data_file in ('replay.initData', 'replay.details', 'replay.attributes.events', 'replay.message.events',): reader = self.get_reader(data_file, replay) replay.read_data(data_file, reader) replay.load_details() replay.load_players() if load_map: map_url = Map.get_url(replay.gateway, replay.map_hash) replay.map = self.load_map(map_url, gateway=replay.gateway, map_hash=replay.map_hash) if load_events: for data_file in ('replay.game.events',): reader = self.get_reader(data_file, replay) replay.read_data(data_file, reader) replay.load_events(self.get_datapack(replay)) if autoplay: replay.listeners = self.get_listeners(replay) replay.start() return replay
def load_map(self, map_file, options=None, **new_options): """ Loads the specified map using the current factory settings with the specified overrides. :param map_file: An open file object or path/url to a single file :param None options: When options are passed directly into the options parameter the current factory settings are ignored and only the specified options are used during replay load. :param new_options: Options values to override current factory settings while loading this map. :rtype: :class:`Replay` """ options = options or utils.merged_dict(self.options, new_options) resource, name = self.load_resource(map_file, options=options) m = Map(resource, name, **options) # Build different parts of the map! # return m