How to use the sc2reader.utils function in sc2reader

To help you get started, we’ve selected a few sc2reader examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GraylinKim / sc2reader / tests / tests.py View on Github external
def test_list_constructor(self):
		lookup = sc2reader.utils.PersonDict([
			sc2reader.objects.Player(1, "player1"),
			sc2reader.objects.Player(2, "player2")
		])

		self.assertEquals(lookup[1].name, "player1")
		self.assertEquals(lookup["player2"].pid, 2)
github GraylinKim / sc2reader / tests / tests.py View on Github external
def test_color(self):
		color = sc2reader.utils.Color(r=0x00, g=0x42, b=0xFF, a=75)
		self.assertEqual(color.rgba, (0x00,0x42,0xFF,75))
		self.assertEqual(color.hex, "0042FF")
github GraylinKim / sc2reader / sc2reader / scripts / sc2attributes.py View on Github external
parser = argparse.ArgumentParser(description="Recursively parses replay files, inteded for debugging parse issues.")
    parser.add_argument('folders', metavar='folder', type=str, nargs='+', help="Path to a folder")
    args = parser.parse_args()

    scripts_dir = os.path.dirname(os.path.abspath(__file__))
    data_path = os.path.normpath(os.path.join(scripts_dir, '..', 'data', 'attributes.json'))

    attributes = dict()
    if os.path.exists(data_path):
        with open(data_path, 'r') as data_file:
            data = json.load(data_file)
            attributes = data.get('attributes', attributes)
            decisions = pickle.loads(data.get('decisions', '(dp0\n.'))

    for folder in args.folders:
        for path in sc2reader.utils.get_files(folder, extension='s2gs'):
            try:
                summary = sc2reader.load_game_summary(path)
                for prop in summary.parts[0][5]:
                    group_key = prop[0][1]
                    group_name = summary.translations['enUS'][group_key]
                    attribute_values = dict()
                    if str(group_key) in attributes:
                        attribute_name, attribute_values = attributes[str(group_key)]
                        if attribute_name != group_name:
                            group_name = get_choice(group_key, attribute_name, group_name)

                    for value in prop[1]:
                        value_key = value[0].strip("\x00 ").replace(' v ', 'v')
                        value_name = summary.lang_sheets['enUS'][value[1][0][1]][value[1][0][2]]
                        if str(value_key) in attribute_values:
                            attribute_value_name = attribute_values[str(value_key)]
github GraylinKim / sc2reader / sc2reader / sc.py View on Github external
def reset(self):
        """
        Resets the current factory to default settings and removes all
        registered readers, datapacks, and listeners.
        """
        self.options = utils.AttributeDict(self.default_options)
        self.registered_readers = defaultdict(list)
        self.registered_datapacks = list()
        self.registered_listeners = defaultdict(list)
github GraylinKim / sc2reader / sc2reader / sc.py View on Github external
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(:class:`Replay`)
        """
        options = options or utils.merged_dict(self.options, new_options)

        # Get the directory and hide it from nested calls
        directory = options.get('directory','')
        if 'directory' in options: del options['directory']

        if isinstance(collection, basestring):
            full_path = os.path.join(directory, collection)
            for replay_path in utils.get_replay_files(full_path, **options):
                with open(replay_path) as replay_file:
                    yield self.load_replay(replay_file, options=options)

        else:
            for replay_file in collection:
                if isinstance(replay_file, basestring):
                    full_path = os.path.join(directory, replay_file)
                    if os.path.isdir(full_path):
                        for replay in self.load_replays(full_path, options=options):
                            yield replay
                    else:
                        yield self.load_replay(full_path, options=options)

                else:
                    yield self.load_replay(replay_file, options=options)
github GraylinKim / sc2reader / sc2reader / resources.py View on Github external
self.map_file = details['cache_handles'][-1]

            # Expand this special case mapping
            if self.region == 'sg':
                self.region = 'sea'

            dependency_hashes = [d.hash for d in details['cache_handles']]
            if hashlib.sha256('Standard Data: Swarm.SC2Mod'.encode('utf8')).hexdigest() in dependency_hashes:
                self.expansion = 'HotS'
            elif hashlib.sha256('Standard Data: Liberty.SC2Mod'.encode('utf8')).hexdigest() in dependency_hashes:
                self.expansion = 'WoL'
            else:
                self.expansion = ''

            self.windows_timestamp = details['file_time']
            self.unix_timestamp = utils.windows_to_unix(self.windows_timestamp)
            self.end_time = datetime.utcfromtimestamp(self.unix_timestamp)

            # The utc_adjustment is either the adjusted windows timestamp OR
            # the value required to get the adjusted timestamp. We know the upper
            # limit for any adjustment number so use that to distinguish between
            # the two cases.
            if details['utc_adjustment'] < 10**7*60*60*24:
                self.time_zone = details['utc_adjustment']/(10**7*60*60)
            else:
                self.time_zone = (details['utc_adjustment']-details['file_time'])/(10**7*60*60)

            self.game_length = self.length
            self.real_length = utils.Length(seconds=int(self.length.seconds/GAME_SPEED_FACTOR[self.speed]))
            self.start_time = datetime.utcfromtimestamp(self.unix_timestamp-self.real_length.seconds)
            self.date = self.end_time  # backwards compatibility
github GraylinKim / sc2reader / sc2reader / factories / sc2factory.py View on Github external
def _load_resource(self, resource, options=None, **new_options):
        """http links, filesystem locations, and file-like objects"""
        options = options or self._get_options(Resource, **new_options)

        if isinstance(resource, utils.DepotFile):
            resource = resource.url

        if isinstance(resource, basestring):
            if re.match(r'https?://', resource):
                contents = self.load_remote_resource_contents(resource, **options)

            else:
                directory = options.get('directory', '')
                location = os.path.join(directory, resource)
                contents = self.load_local_resource_contents(location, **options)

            # BytesIO implements a fuller file-like object
            resource_name = resource
            resource = BytesIO(contents)

        else: