예제 #1
0
def call_run(run_fn):
    import numpy as np

    params = NS.from_dict(json.loads(sys.stdin.read()))

    def load_data(name, path, **ignored):
        if isinstance(path, str) and data_keys.match(name):
            return name, np.load(path, allow_pickle=True)
        return name, path

    print(params.dataset)
    ds = NS.walk(params.dataset, load_data)

    config = params.config
    config.framework_params = NS.dict(config.framework_params)

    try:
        result = run_fn(ds, config)
        res = dict(result)
        for name in ['predictions', 'truth', 'probabilities']:
            arr = result[name]
            if arr is not None:
                res[name] = os.path.join(config.result_dir,
                                         '.'.join([name, 'npy']))
                np.save(res[name], arr, allow_pickle=True)
    except Exception as e:
        log.exception(e)
        res = dict(error_message=str(e), models_count=0)

    print(config.result_token)
    print(json.dumps(res, separators=(',', ':')))
예제 #2
0
    def parse_id(self):
        value = True
        if self.cur == '!':
            value = False
            self.i += 1
            self.skip_spaces()

        # TODO(maximsmol): support other "truthy" and "falsy" values?
        if self.cur == '0':
            self.i += 1
            return Namespace(type='const', val=(not value))
        if self.cur == '1':
            self.i += 1
            return Namespace(type='const', val=value)

        if self.cur != '$':
            putter.die(f'$ does not preceed macro name in {cf.bold(self.x)}.')
        self.i += 1

        res = ''
        while self.i < self.l and self.cur not in ' \t)&|':
            if self.cur == '(':
                putter.die(f'Unexpected ( in macro name in {cf.bold(self.x)}.')
            res += self.cur
            self.i += 1

        self.skip_spaces()
        return Namespace(type='macro', name=res, setto=value)
예제 #3
0
    def parse_folder_command(self, res, parts):
        assert len(parts) >= 2
        name = strip_quotes(parts[1])

        if name not in res.folders:
            res.folders[name] = Namespace(files=[],
                                          libs=[],
                                          folders=Namespace())
        # TODO(maximsmol): we lose definition order information here so something might break
        folder = res.folders[name]

        if len(parts) == 3:
            folder.cond = parse_cond(parts[2])
        else:
            assert len(parts) == 2

        for cmd, body_parts in self.parse_body_commands():
            if cmd == 'folder':
                self.parse_folder_command(folder, body_parts)
            elif cmd in ['file', '-file', 'dynamicfile', '-dynamicfile']:
                self.parse_file_command(folder.files, parts)
            elif cmd in ['lib', 'libexternal']:
                lib = Namespace(type=cmd, paths=string_like(body_parts[1]))
                folder.libs.append(lib)

                if len(body_parts) == 3:
                    lib.cond = parse_cond(body_parts[2])
                else:
                    assert len(body_parts) == 2
            else:
                self.die(f'Unknown folder command {cf.bold(cmd)}.')
예제 #4
0
 def _parse(self):
     res = Namespace(projects=Namespace(),
                     groups=Namespace(),
                     games=[],
                     includes=[])
     while self.token is not None:
         self.parse_command(res)
     return res
예제 #5
0
def _mk_cmd_list_schema(pretty_prefix, *args, declarative=False):
    res = Namespace(type='cmd_list',
                    pretty_prefix=pretty_prefix,
                    cmds=Namespace(),
                    declarative=declarative)
    for cmd in args:
        assert cmd.name not in res.cmds
        res.cmds[cmd.name] = cmd
    return res
예제 #6
0
                def print_generic_dict(x_name):
                    x_key = vpc.configuration_commands[x_name].pythonized
                    if x_key not in inst:
                        return

                    # TODO(maximsmol): print the merging process
                    merged = Namespace()
                    for x in inst[x_key]:
                        if 'cond' not in x or eval_cond(x.cond):
                            merge_command_data(merged, x)

                    with putter.group(x_name):
                        for k, v in merged.items():
                            print(f'{cf.bold(k)} = {cf.bold(v)}')
예제 #7
0
        def finish_cur_res():
          nonlocal cur_res
          assert not in_cond
          assert not in_str

          if part != '':
            cur_res.parts.append(Namespace(type='raw', x=part))

          # do not push empty tokens
          if len(cur_res.parts) == 0 and 'comment' not in cur_res:
            return

          push_token(cur_res)
          cur_res = Namespace(type='parts', parts=[])
예제 #8
0
def eval_cond(x):
    if x.type == 'or':
        for c in x.conds:
            if not eval_cond(c):
                continue
            return True
        return False
    elif x.type == 'and':
        for c in x.conds:
            if eval_cond(c):
                continue
            return False
        return True
    elif x.type == 'const':
        return x.val
    elif x.type == 'macro':
        if x.name not in macro_state:
            # putter.die(f'Macro {x.name} is not defined.')
            return False == x.setto

        state = macro_state[x.name]
        if isinstance(state, bool):
            return state == x.setto
        if not isinstance(state, str):
            self.die(f'Cond {state} has invalid type.')

        # print(f'{state}, {x.name}')
        # assert False

        expr = expand_macros_in_string(state)
        return eval_cond(parse_cond(Namespace(type='cond', x=expr))) == x.setto
    else:
        putter.die(f'Unknown condition type: {cf.bold(x.type)}')
 def __init__(self, model_save_path):
     
     self.modelconfig = json.load(open(os.path.join(model_save_path, 'layoutLM_config.json'), 'r', encoding='utf-8'))
     self.config = Namespace(**self.modelconfig)
     # super(TablePredictHandler, self).__init__(self.config)
     self.model_save_path = model_save_path
     
     self.pretrainmodelpath = self.config.pretrainmodelpath
     self.loadmodel(model_save_path)
예제 #10
0
    def parse_command(self, res):
        parts = self.parse_command_parts()
        cmd = self.need_command(parts)
        if cmd == 'include':
            assert len(parts) == 2
            res.includes.append(strip_quotes(parts[1]))
        elif cmd == 'games':
            assert len(parts) == 1
            for body_parts in self.parse_body():
                assert len(body_parts) == 1
                res.games.append(strip_quotes(body_parts[0]).lower())
        elif cmd == 'group':
            assert len(parts) >= 1
            projects = []
            for body_parts in self.parse_body():
                assert len(body_parts) == 1
                projects.append(strip_quotes(body_parts[0]))

            for g_name in parts[1:]:
                g_name = strip_quotes(g_name)
                group = res.groups.setdefault(g_name, Namespace())
                group.setdefault('projects', []).extend(projects)
        elif cmd == 'project':
            assert len(parts) == 2

            name = strip_quotes(parts[1])
            if name in res.projects:
                self.die(f'Duplicate project {cf.bold(name)}.')

            proj = res.projects.setdefault(name, Namespace())
            paths = proj.setdefault('paths', [])

            for body_parts in self.parse_body():
                path = strip_quotes(body_parts[0])
                if len(body_parts) == 2:
                    cond = parse_cond(body_parts[1])

                    paths.append(Namespace(path=path, cond=cond))
                else:
                    assert len(body_parts) == 1
                    paths.append(Namespace(path=path))
        else:
            print(parts)
            self.die(f'Unknown command {cf.bold(cmd)}.')
예제 #11
0
    def parse_command_parts(self, merge_strings=False):
        tok = self.consume_token()
        if tok is None:
            self.die('Unexpected EOF: missing command parts.')
        assert tok.type == 'parts'

        l = len(tok.parts)
        res = Namespace(parts=[])

        strs_to_merge = []

        def push_merged_str():
            if len(strs_to_merge) == 0:
                return
            res.parts.append(
                Namespace(type='str', x=''.join([x.x for x in strs_to_merge])))

        for i, x in enumerate(tok.parts):
            if x.type == 'str':
                if not merge_strings:
                    res.parts.append(x)
                    continue
                strs_to_merge.append(x)
                continue

            push_merged_str()

            if x.type == 'raw':
                res.parts.append(x)
                continue
            if x.type == 'cond':
                assert i == l - 1
                res.parts.append(x)
                continue
            self.die(f'Unknown token "{cf.bold(x.type)}".')

        if 'comment' in tok:
            res.comment = tok.comment

        # TODO(maximsmol): handle comments properly
        if 'comment' in res:
            pass
        return res.parts
예제 #12
0
    def parse_cmd_schema(self, res, schema, parts):
        assert schema.type == 'cmd'
        part_cmd = self.need_command(parts, lowercase=False)
        assert part_cmd == schema.name

        i = 1
        l = len(parts)

        def cast_arg(arg_type, x):
            if arg_type == 'str':
                return strip_quotes(x)
            if arg_type == 'sym':
                return raw_string(x)
            if arg_type == 'any_str':
                return string_like(x)
            if arg_type == 'merged_str':
                nonlocal i
                res = [strip_quotes(x)]
                while True:
                    if i >= l:
                        break
                    cur = parts[i]
                    if cur.type != 'str':
                        break
                    res.append(strip_quotes(x))
                    i += 1
                return ''.join(res)
            self.die(f'Unknown argument type {cf.bold(arg_type)}.')

        # args begin at idx 1
        optional_args_start = 1 + len(schema.args)
        while i < l:
            cur = parts[i]
            if cur.type == 'cond':
                # cond is only allowed at the very end
                assert i == l - 1
                res.cond = parse_cond(cur)
                break

            if i < optional_args_start:
                arg_type = schema.args[i - 1]
            elif i - optional_args_start < len(schema.optional_args):
                arg_type = schema.optional_args[i - optional_args_start]
            else:
                self.die(f'Too many arguments for {cf.bold(schema.name)}')

            # must increment here because cast_arg will need lookahead to merge strings
            i += 1
            res.setdefault('args', []).append(cast_arg(arg_type, cur))

        if schema.body_schema is not None:
            res.body = Namespace()
            for cmd, body_parts in self.parse_body_commands(lowercase=False):
                self.parse_cmd_list_schema(res.body, schema.body_schema,
                                           body_parts)
예제 #13
0
    def parse_file_command(self, res, parts):
        cmd = self.need_command(parts, lowercase=False)
        file = Namespace(
            type=cmd,  # TODO(maximsmol): strip -
            paths=[],
            instructions=[])
        res.append(file)

        if parts[-1].type == 'cond':
            file.cond = parse_cond(parts[-1])
            parts = parts[:-1]

        for p in parts[1:]:
            file.paths.append(strip_quotes(p))

        if self.token.type == '{':
            for cmd, parts in self.parse_body_commands():
                if cmd == 'configuration':
                    self.parse_configuration_comamnd(file, parts)
                else:
                    self.die(f'Unknown file command {cf.bold(cmd)}.')
예제 #14
0
def _mk_cmd_schema(name,
                   pythonized_name=None,
                   args=[],
                   optional_args=[],
                   body_schema=None,
                   allow_cond=True):
    return Namespace(name=name,
                     type='cmd',
                     args=args,
                     optional_args=optional_args,
                     pythonized_name=pythonized_name,
                     body_schema=body_schema,
                     allow_cond=True)
예제 #15
0
    def parse_configuration_comamnd(self, res, parts):
        inst = Namespace(type='configuration')
        res.instructions.append(inst)

        if len(parts) == 2:
            inst.name = strip_quotes(parts[1])
        else:
            assert len(parts) == 1

        def generic_prop_group(group_data, body_parts):

            group = Namespace()
            inst.setdefault(group_data.pythonized, []).append(group)

            if group_data.nargs > 0:
                group.args = body_parts[1:group_data.nargs + 1]

            if len(body_parts) == group_data.nargs + 2:
                group.cond = parse_cond(body_parts[-1])
            else:
                assert len(body_parts) == group_data.nargs + 1

            if group_data.allow_body:
                for cmd, body_parts in self.parse_body_commands(
                        lowercase=False):
                    group.setdefault(cmd, []).append(body_parts[1:])
            elif self.token.type == '{':
                # TODO(maximsmol): use the real name here
                self.die(
                    f'Configuration command {group_data.pythonized} should not have a body.'
                )

        for cmd, body_parts in self.parse_body_commands(lowercase=False):
            if cmd not in configuration_commands:
                self.die(f'Unknown configuration command {cf.bold(cmd)}.')

            generic_prop_group(configuration_commands[cmd], body_parts)
예제 #16
0
    def parse_or(self):
        res = []
        while self.i < self.l:
            res.append(self.parse_and())
            if self.i >= self.l:
                break
            if self.cur == '|' and self.cur == '|':
                self.i += 2
            else:
                break
            self.skip_spaces()

        if len(res) == 1:
            return res[0]
        return Namespace(type='or', conds=res)
예제 #17
0
    def parse_cmd_list_schema(self, res, schema, parts):
        assert schema.type == 'cmd_list'

        cmd = self.need_command(parts, lowercase=False)
        if cmd not in schema.cmds:
            self.die(f'Unknown {schema.pretty_prefix} command')

        cmd_data = Namespace(cmd=cmd)
        cmd_schema = schema.cmds[cmd]
        self.parse_cmd_schema(cmd_data, cmd_schema, parts)
        if schema.declarative:
            if cmd in res:
                self.die(f'Duplicate {cmd}.')
            res[cmd] = cmd_data
            return
        res.setdefault('instructions', []).append(cmd_data)
def test(model_path, vocab_path, data_path, solver_log, batch_size, Config):
    graph = ontology_declaration()

    viol_logger = logging.getLogger('regr.graph.allennlp.utils.violation')
    viol_logger.propagate = False
    fh = logging.FileHandler('violations.log', 'w')
    formatter = logging.Formatter('%(message)s')
    fh.setFormatter(formatter)
    viol_logger.addHandler(fh)
    viol_logger.setLevel(logging.DEBUG)

    Config = Namespace(Config)
    Config.Model.graph.inference_testing_set = False
    Config.Model.graph.inference_loss = False
    Config.Model.graph.inference_interval = 100

    lbp = model_declaration(graph, Config.Model)

    seed()
    lbp.load(model_path, 'last', vocab_path)
    metrics = lbp.test(data_path, solver_log, batch_size, log_violation=True)
    print(metrics)
예제 #19
0
        def generic_prop_group(group_data, body_parts):

            group = Namespace()
            inst.setdefault(group_data.pythonized, []).append(group)

            if group_data.nargs > 0:
                group.args = body_parts[1:group_data.nargs + 1]

            if len(body_parts) == group_data.nargs + 2:
                group.cond = parse_cond(body_parts[-1])
            else:
                assert len(body_parts) == group_data.nargs + 1

            if group_data.allow_body:
                for cmd, body_parts in self.parse_body_commands(
                        lowercase=False):
                    group.setdefault(cmd, []).append(body_parts[1:])
            elif self.token.type == '{':
                # TODO(maximsmol): use the real name here
                self.die(
                    f'Configuration command {group_data.pythonized} should not have a body.'
                )
예제 #20
0
파일: console.py 프로젝트: p-murli/pitivi
 def __init__(self, app):
     Namespace.__init__(self)
     self._app = app
예제 #21
0
def build_project(name):
    project_data = Namespace(configuration=Namespace(compiler=Namespace()))

    root_path = None

    def build_data(path):
        data = read_project(path)
        for inst in data.instructions:
            if inst.type == 'conditional':
                val = expand_macros_in_string(inst.value)
                if 'cond' not in inst or eval_cond(inst.cond):
                    if val == '1':
                        val = True
                    if val == '0':
                        val = False

                    if val in no_strings or val in yes_strings:
                        putter.warning(
                            f'Found yes/no string but refusing to convert it: {render_macro_value(val)}'
                        )

                    print(
                        f'{cf.magenta("Set conditional")} {cf.bold("$"+inst.name)} = {render_macro_value(val)}{cond_suffix(inst)}'
                    )
                    if inst.name in macro_state and val != macro_state[
                            inst.name]:
                        with putter.indent():
                            putter.warning(
                                f'Overriding old value of {render_macro_value(macro_state[inst.name])}'
                            )
                    macro_state[inst.name] = val
                else:
                    print(
                        f'Not setting conditional {cf.bold("$"+inst.name)} = {render_macro_value(val)}{cond_suffix(inst)} due to the false condition'
                    )
            elif inst.type == 'macro':
                val = expand_macros_in_string(inst.value)
                if 'cond' not in inst or eval_cond(inst.cond):
                    if val == '1':
                        val = True
                    if val == '0':
                        val = False

                    if val in no_strings or val in yes_strings:
                        putter.warning(
                            f'Found yes/no stringbut refusing to convert it: {render_macro_value(val)}'
                        )

                    print(
                        f'{cf.magenta("Set")} {cf.bold("$"+inst.name)} = {render_macro_value(val)}{cond_suffix(inst)}'
                    )
                    if inst.name in macro_state and val != macro_state[
                            inst.name]:
                        with putter.indent():
                            putter.warning(
                                f'Overriding old value of {render_macro_value(macro_state[inst.name])}'
                            )
                    macro_state[inst.name] = val
                else:
                    print(
                        f'Not setting {cf.bold("$"+inst.name)} = {render_macro_value(val)}{cond_suffix(inst)} due to the false condition'
                    )
            elif inst.type == 'include':
                if 'cond' not in inst or eval_cond(inst.cond):
                    print(
                        f'{cf.magenta("Including")} {cf.italic(inst.path)}{cond_suffix(inst)}'
                    )
                    with putter.indent():
                        include_path = expand_macros_in_string(inst.path)
                        resolved_path = Path(
                            ntpath.normpath(
                                str(root_path.parent / include_path)))
                        print(
                            f'{cf.magenta("Resolved to")} {cf.italic(resolved_path)}'
                        )
                        build_data(resolved_path)
                else:
                    print(
                        f'Not including {cf.italic(Path(inst.path).name)}{cond_suffix(inst)} due to the false condition'
                    )
            elif inst.type == 'configuration':

                def print_generic_dict(x_name):
                    x_key = vpc.configuration_commands[x_name].pythonized
                    if x_key not in inst:
                        return

                    # TODO(maximsmol): print the merging process
                    merged = Namespace()
                    for x in inst[x_key]:
                        if 'cond' not in x or eval_cond(x.cond):
                            merge_command_data(merged, x)

                    with putter.group(x_name):
                        for k, v in merged.items():
                            print(f'{cf.bold(k)} = {cf.bold(v)}')

                suffix = ''
                if 'name' in inst:
                    suffix = f' "{inst.name}"'
                with putter.group(f'Configuration{suffix}'):
                    for generic_group in vpc.configuration_commands:
                        print_generic_dict(generic_group)
            elif inst.type == 'macro_required':
                invalid_value = macro_state.get(
                    inst.name) == '' and not inst.allow_empty
                if inst.name not in macro_state or invalid_value:
                    if 'default' not in inst:
                        if inst.name not in macro_state:
                            die(f'Macro {cf.bold("$"+inst.name)} is required, but not defined.'
                                )
                        if not inst.allow_empty:
                            die(f'Macro {cf.bold("$"+inst.name)} is required, but is set to an empty string.'
                                )
                    with putter.indent():
                        print(
                            f'{cf.magenta("Defaulted")} {cf.bold("$"+inst.name)} = {render_macro_value(inst.default)}'
                        )
                        macro_state[inst.name] = inst.default
                else:
                    print(
                        f'{cf.magenta("Found required")} {cf.bold("$"+inst.name)}'
                    )
            else:
                putter.warning(f'Skipped instruction {cf.bold(inst.type)}.')

    root_path = resolve_project_path(name)
    print(f'Resolved project to {cf.italic(root_path)}')
    print()
    try:
        build_data(root_path)
    except (ValueError, AssertionError):
        putter.newline()
        putter.error(f'Root path is: {cf.italic(root_path)}')
        die('Could not parse file.', linefeed_before=False)
        raise
    except FileNotFoundError as e:
        putter.newline()
        putter.error(f'Root path is: {cf.italic(root_path)}')
        die(f'File not found: {cf.italic(e.filename)}.', linefeed_before=False)
        raise
예제 #22
0
파일: console.py 프로젝트: pitivi/pitivi
 def __init__(self, app):
     Namespace.__init__(self)
     self._app = app
예제 #23
0
mainlist = read_manifest(Path('vpc_scripts') / 'default.vgc')
to_include = deque(mainlist.includes)
while len(to_include) > 0:
    incl_path = to_include.popleft()
    included = read_manifest(Path(incl_path))

    if parsed_args.log_includes:
        print(f'Included "{cf.bold(incl_path)}".')

    merge_command_data(mainlist.projects, included.projects)
    merge_command_data(mainlist.games, included.games)
    merge_command_data(mainlist.groups, included.groups, allow_duplicates=True)

    to_include.extend(included.includes)

macro_state = Namespace()


def cond_suffix(x):
    if 'cond' not in x:
        return ''
    return f' {render_cond(x.cond, macro_state)}'


def render_macro_value(x):
    if not isinstance(x, str):
        return str(x)

    stripped = x.strip()
    if stripped != x or stripped == '':
        return cf.bold(f'"{x}"')
예제 #24
0
def _mk_configuration_command(pythonized, nargs=0, allow_body=False):
    return Namespace(pythonized=pythonized, nargs=nargs, allow_body=allow_body)


configuration_commands = Namespace(
    Compiler=_mk_configuration_command('compiler', allow_body=True),
    General=_mk_configuration_command('general', allow_body=True),
    Linker=_mk_configuration_command('linker', allow_body=True),
    Debugging=_mk_configuration_command('debugging', allow_body=True),
    ManifestTool=_mk_configuration_command('manifest_tool', allow_body=True),
    XMLDocumentGenerator=_mk_configuration_command('xml_document_generator',
                                                   allow_body=True),
    BrowseInformation=_mk_configuration_command('browse_information',
                                                allow_body=True),
    Resources=_mk_configuration_command('resources', allow_body=True),
    ExcludedFromBuild=_mk_configuration_command('excluded_from_build',
                                                nargs=1),
    CustomBuildStep=_mk_configuration_command('custom_build_step',
                                              allow_body=True),
    PreBuildEvent=_mk_configuration_command('pre_build_event',
                                            allow_body=True),
    PreLinkEvent=_mk_configuration_command('pre_link_event', allow_body=True),
    PostBuildEvent=_mk_configuration_command('post_build_event',
                                             allow_body=True),
    PostLinkEvent=_mk_configuration_command('post_link_event',
                                            allow_body=True))


# TODO(maximsmol): we are keeping track of "type" here just for asserts, it's
#                  not really necessary
# TODO(maximsmol): set allow cond only for specific commands
예제 #25
0
        'pretrained_files': {
            'word': 'data/glove.6B/glove.6B.300d.txt'
        },
        'trainer': {
            'num_epochs':
            100,
            'patience':
            None,
            'serialization_dir':
            'log.{}'.format(time.strftime("%Y%m%d-%H%M%S", time.gmtime())),
        },
        'optimizer': {
            'type': 'adam',
            'lr': 1e-4,
            'weight_decay': 1e-5
        },
        'scheduler': {
            'type': 'reduce_on_plateau',
            'patience': 10
        },
        'iterator': {
            'batch_size': 8,
        }
    },
    'Source': {
        'emr': caller_source()
    }
}

Config = Namespace(config)
예제 #26
0
 def push_merged_str():
     if len(strs_to_merge) == 0:
         return
     res.parts.append(
         Namespace(type='str', x=''.join([x.x for x in strs_to_merge])))
예제 #27
0
    eval_manager_target.fit()
    eval_manager_sens.fit()

    with torch.no_grad():
        # If we have wandb logger, or we return results,
        # we want to have the report as a dict.
        return_results = True
        output_dict = logger is not None or return_results
        _, report_target, acc_target = eval_manager_target.evaluate(
            output_dict)
        _, report_sens, acc_sens = eval_manager_sens.evaluate(output_dict)
        print(report_target)
        print(report_sens)
        if logger is not None:
            logger.log_metrics({
                "target_classification_report": report_target,
                "sens_classification_report": report_sens,
            })
        print("~ evaluation results ~~~~~~~~~~~~~")
        print("best target acc:", round(acc_target, 2))
        print("best sens acc:  ", round(acc_sens, 2))
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        return acc_target, acc_sens


if __name__ == "__main__":
    torch.manual_seed(0)
    args = Namespace(dataset="adult")
    evaluate_embeddings(args)
예제 #28
0
파일: server.py 프로젝트: jn0/yim
 def __init__(self, **kw):
     self.env = Namespace(kw)
     self.log = logging.getLogger(self.__class__.__name__)
     self.log.setLevel(logging.root.level)
     self.clients = dict()
예제 #29
0
def _mk_configuration_command(pythonized, nargs=0, allow_body=False):
    return Namespace(pythonized=pythonized, nargs=nargs, allow_body=allow_body)
예제 #30
0
    def parse_command(self, res):
        parts = self.parse_command_parts()
        cmd = self.need_command(parts, lowercase=False)
        if cmd == 'Macro':
            name = string_like(parts[1])
            # TODO(maximsmol): weird unquoted macro values are allowed
            #                  and we skip the spaces in them + this parsing
            #                  is all around weird
            value = string_like(parts[2])

            cond = None
            if len(parts) > 3:
                # TODO(maximsmol): check that weird unquoted macros are unquoted
                value_parts = [value]
                i = 3
                for p in parts[3:]:
                    if p.type == 'cond':
                        cond = parse_cond(parts[3])
                        assert i == len(parts) - 1
                        break

                    value_parts.append(raw_string(p))
                    i += 1
                # TODO(maximsmol): this actually loses some whitespaces
                value = ' '.join(value_parts)
            else:
                assert len(parts) == 3

            inst = Namespace(type='macro')
            inst.name = name
            inst.value = value
            if cond is not None:
                inst.cond = cond
            res.instructions.append(inst)
        elif cmd == 'Conditional':
            name = raw_string(parts[1])

            inst = Namespace(type='conditional')
            res.instructions.append(inst)

            inst.name = name
            inst.value = strip_quotes(parts[2])

            cond = None
            if len(parts) == 4:
                inst.cond = parse_cond(parts[3])
            else:
                assert len(parts) == 3
        elif cmd == 'MacroRequired':
            inst = Namespace(type='macro_required')

            if len(parts) == 3:
                inst.default = strip_quotes(parts[2])
            else:
                assert len(parts) == 2

            inst.name = string_like(parts[1])
            inst.allow_empty = False
        elif cmd == 'MacroRequiredAllowEmpty':
            inst = Namespace(type='macro_required')

            if len(parts) == 3:
                inst.default = strip_quotes(parts[2])
            else:
                assert len(parts) == 2

            inst.name = string_like(parts[1])
            inst.allow_empty = True
        elif cmd == 'LoadAddressMacro':
            inst = Namespace(type='address_macro')
            res.instructions.append(inst)

            assert len(parts) == 2

            inst.name = raw_string(parts[1])

            for body_parts in self.parse_body():
                # TODO(maximsmol): parse this
                # this is only used on XBOX afaik
                pass
        elif cmd == 'LoadAddressMacroAuto':
            inst = Namespace(type='address_macro_auto')
            res.instructions.append(inst)

            assert len(parts) == 4

            inst.name = raw_string(parts[1])

            inst.addr = int(raw_string(parts[2]), 16)
            inst.cond = parse_cond(parts[3])

            for body_parts in self.parse_body():
                # TODO(maximsmol): parse this
                # this is only used on XBOX afaik
                pass
        elif cmd == 'IgnoreRedundancyWarning':
            inst = Namespace(type='ignore_redundancy_warning')
            assert len(parts) == 2

            inst.value = strip_quotes(parts[1]).lower()
        elif cmd in ['Include',
                     'include']:  # Valve, why do you insist on mixing case
            path = strip_quotes(parts[1])
            cond = None
            if len(parts) == 3:
                cond = parse_cond(parts[2])
            else:
                assert len(parts) == 2

            inst = Namespace(type='include')
            inst.path = path
            if cond is not None:
                inst.cond = cond
            res.instructions.append(inst)
        elif cmd == 'Configuration':
            self.parse_configuration_comamnd(res, parts)
        elif cmd == 'Project':
            proj = Namespace(type='project', folders=Namespace(), files=[])
            res.instructions.append(proj)

            if len(parts) == 2:
                name = strip_quotes(parts[1])
            else:
                assert len(parts) == 1

            for cmd, body_parts in self.parse_body_commands():
                if cmd == 'folder':
                    self.parse_folder_command(proj, body_parts)
                elif cmd in ['file']:
                    self.parse_file_command(proj.files, parts)
                else:
                    self.die(f'Unknown project command {cf.bold(cmd)}.')
        elif cmd == 'CustomBuildStep':
            self.parse_cmd_list_schema(res, project_schema, parts)
            # TODO(maximsmol): type will be renamed to name
            res.instructions[-1].type = cmd
        else:
            self.die(f'Unknown command {cf.bold(cmd)}.')
예제 #31
0
 def _parse(self):
     res = Namespace(instructions=[])
     while self.token is not None:
         self.parse_command(res)
     return res