Exemplo n.º 1
0
    def console_writer_before_each_feature(self, feature):
        """
            Writes feature header to the console

            :param Feature feature: the feature to write to the console
        """
        output = ""
        for tag in feature.tags:
            output += colorful.cyan(u"@{0}{1}\n".format(
                tag.name, "({0})".format(tag.arg) if tag.arg else ""))

        leading = "\n    " if feature.description else ""

        output += u"{0}{1}: {2}  # {3}{4}{5}".format(
            self.get_id_sentence_prefix(feature, colorful.bold_cyan),
            colorful.bold_white(feature.keyword),
            colorful.bold_white(feature.sentence),
            colorful.bold_black(feature.path), leading,
            colorful.white("\n    ".join(feature.description)))

        if feature.background:
            output += u"\n\n    {0}: {1}".format(
                colorful.bold_white(feature.background.keyword),
                colorful.bold_white(feature.background.sentence))
            for step in feature.background.all_steps:
                output += '\n' + self._get_step_before_output(
                    step, colorful.cyan)

        write(output)
Exemplo n.º 2
0
    def console_writer_before_each_feature(self, feature):
        """
            Writes feature header to the console

            :param Feature feature: the feature to write to the console
        """
        output = ""
        for tag in feature.tags:
            output += colorful.cyan(
                "@{0}{1}\n".format(tag.name, "({0})".format(tag.arg) if tag.arg else "")
            )

        leading = "\n    " if feature.description else ""

        output += "{0}{1}: {2}  # {3}{4}{5}".format(
            self.get_id_sentence_prefix(feature, colorful.bold_cyan),
            colorful.bold_white(feature.keyword),
            colorful.bold_white(feature.sentence),
            colorful.bold_black(feature.path),
            leading,
            colorful.white("\n    ".join(feature.description)),
        )

        if feature.background:
            output += "\n\n    {0}: {1}".format(
                colorful.bold_white(feature.background.keyword),
                colorful.bold_white(feature.background.sentence),
            )
            for step in feature.background.all_steps:
                output += "\n" + self._get_step_before_output(step, colorful.cyan)

        write(output)
    def _process(self):
        # Prepare message
        msg = []
        for msg_index in range(0,len(self.message)):
            if self.exit_on_failure is False:
                msg_header = '{}{}'.format(self.exception_name,
                                           colorful.bold_white(':')) if msg_index == 0 else ' '*(len(self.exception_name)+1)
                msg.append('\t\t{} {}'.format(colorful.bold_red(msg_header), colorful.red(self.message[msg_index])))
            else:
                msg.append(self.message[msg_index] if msg_index == 0
                                                   else '{}{} {} {}'.format("\t"*2,
                                                                            ' '*(len(self.exception_name)+1),
                                                                            colorful.bold_white(':'),
                                                                            self.message[msg_index]))

        if self.exit_on_failure is False:
            for message in msg:
                console_write(message)

            if self.no_failure is False:
                self._fail_step(self.step_obj.id)
            else:
                self.step_obj.state = Step.State.SKIPPED
            return

        if self.no_failure is False:
            raise self.exception('\n'.join(msg))
Exemplo n.º 4
0
    def console_writer_before_each_scenario(self, scenario):
        """
            Writes the scenario header to the console

            :param Scenario scenario: the scenario to write to the console
        """
        output = "\n"
        if isinstance(scenario.parent, ScenarioOutline):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(
                scenario, colorful.bold_yellow, len(scenario.parent.scenarios))
            colored_pipe = colorful.bold_white("|")
            output = "        {0}{1} {2} {1}".format(
                id_prefix,
                colored_pipe,
                (" {0} ").format(colored_pipe).join(
                    str(
                        colorful.bold_yellow("{1: <{0}}".format(
                            scenario.parent.get_column_width(i), x)))
                    for i, x in enumerate(scenario.example.data)),
            )
        elif isinstance(scenario.parent, ScenarioLoop):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(
                scenario, colorful.bold_yellow, len(scenario.parent.scenarios))
            colored_pipe = colorful.bold_white("|")
            output = "        {0}{1} {2: <18} {1}".format(
                id_prefix, colored_pipe,
                str(colorful.bold_yellow(scenario.iteration)))
        else:
            id_prefix = self.get_id_sentence_prefix(scenario,
                                                    colorful.bold_cyan)
            for tag in scenario.tags:
                if (
                        tag.name == "precondition" and world.config.expand
                        and world.config.show
                ):  # exceptional for show command when scenario steps expand and tag is a precondition -> comment it out
                    output += colorful.white("    # @{0}{1}\n".format(
                        tag.name, "({0})".format(tag.arg) if tag.arg else ""))
                else:
                    output += colorful.cyan("    @{0}{1}\n".format(
                        tag.name, "({0})".format(tag.arg) if tag.arg else ""))
            output += "    {0}{1}: {2}".format(
                id_prefix,
                colorful.bold_white(scenario.keyword),
                colorful.bold_white(scenario.sentence),
            )
        write(output)
Exemplo n.º 5
0
    def context_registers(self):
        """Show contents of ARM registers."""

        colorful.bold_white = colorful.bold_base2
        regs_list = []

        # Dump registers R0 to R9
        for reg in xrange(UC_ARM_REG_R0, UC_ARM_REG_R0 + 10):
            reg_string = colorful.bold_white(
                "R{:d}:".format(reg - UC_ARM_REG_R0)) + colorful.bold_base01(
                    "  0x{:08x}".format(self.emu.reg_read(reg)))
            regs_list.append(reg_string)

        # Dump registers with alias
        sl_reg = colorful.bold_white("SL:") + colorful.base01(
            "  0x{:08x}".format(self.emu.reg_read(UC_ARM_REG_SL)))
        fp_reg = colorful.bold_white("FP:") + colorful.base01(
            "  0x{:08x}".format(self.emu.reg_read(UC_ARM_REG_FP)))
        ip_reg = colorful.bold_white("IP:") + colorful.base01(
            "  0x{:08x}".format(self.emu.reg_read(UC_ARM_REG_IP)))
        sp_reg = colorful.bold_white("SP:") + colorful.base01(
            "  0x{:08x}".format(self.emu.reg_read(UC_ARM_REG_SP)))
        lr_reg = colorful.bold_white("LR:") + colorful.base01(
            "  0x{:08x}".format(self.emu.reg_read(UC_ARM_REG_LR)))
        pc_reg = colorful.bold_white("PC:") + colorful.base01(
            "  0x{:08x}".format(self.emu.reg_read(UC_ARM_REG_PC)))
        cpsr_reg = colorful.bold_white("CPSR:") + colorful.base01(
            "  0x{:08x}".format(self.emu.reg_read(UC_ARM_REG_CPSR)))

        regs_list.extend(
            [sl_reg, fp_reg, ip_reg, sp_reg, lr_reg, pc_reg, cpsr_reg])

        table_show_registers(regs_list)
Exemplo n.º 6
0
    def _get_step_before_output(self, step, color_func=None):
        if color_func is None:
            color_func = colorful.bold_yellow
        output = "\r        {0}{1}".format(
            self.get_id_sentence_prefix(step, color_func), color_func(step.sentence)
        )

        if step.text:
            id_padding = self.get_id_padding(len(step.parent.steps))
            output += colorful.bold_white('\n            {0}"""'.format(id_padding))
            output += colorful.cyan(
                "".join(
                    [
                        "\n                {0}{1}".format(id_padding, l)
                        for l in step.raw_text
                    ]
                )
            )
            output += colorful.bold_white('\n            {0}"""'.format(id_padding))

        if step.table_header:
            colored_pipe = colorful.bold_white("|")
            col_widths = self.get_table_col_widths(
                [step.table_header] + step.table_data
            )

            # output table header
            output += "\n          {0} {1} {0}".format(
                colored_pipe,
                (" {0} ")
                .format(colored_pipe)
                .join(
                    str(colorful.white("{1: <{0}}".format(col_widths[i], x)))
                    for i, x in enumerate(step.table_header)
                ),
            )

            # output table data
            for row in step.table_data:
                output += "\n          {0} {1} {0}".format(
                    colored_pipe,
                    (" {0} ")
                    .format(colored_pipe)
                    .join(
                        str(color_func("{1: <{0}}".format(col_widths[i], x)))
                        for i, x in enumerate(row)
                    ),
                )

        return output
Exemplo n.º 7
0
def match_line(input_text):
    # determine closest line to input
    if input_text in ["testing"]:
        line = cf.bold_cornflowerBlue_on_snow(input_text)
    else:
        line = cf.bold_white(input_text)
    return line
Exemplo n.º 8
0
def write_feature_header(feature):
    """Write the Feature Header to stdout

    The Feature Header will be printed in the form of:

    @tag-a
    @tag-b
    Feature: short description
        description line 1
        description line 2
    """
    # write Tags
    for tag in feature.tags:
        write_tagline(tag)

    # write Feature heading
    feature_heading = "{feature_keyword}: {short_description}".format(
        feature_keyword=cf.bold_white(feature.keyword),
        short_description=cf.white(feature.short_description),
    )
    print(feature_heading, flush=True)

    # write Feature description if available
    if feature.description:
        feature_description = "\n".join(INDENT_STEP + l
                                        for l in feature.description)
        print(feature_description + "\n", flush=True)

    # write Background if available
    if feature.background:
        background = "{background_keyword}: {short_description}".format(
            background_keyword=cf.bold_white(feature.background.keyword),
            short_description=cf.white(feature.background.short_description)
            if feature.background.short_description else "",
        )

        print(INDENT_STEP + background, flush=True)

        # TODO: write background steps
        for step in feature.background.steps:
            write_step(step,
                       cf.deepSkyBlue3,
                       indentation=INDENT_STEP + INDENT_STEP)

        print("", flush=True)
    def _process(self):
        # Prepare message
        msg = []
        for msg_index in range(0, len(self.message)):
            if self.exit_on_failure is False or self.no_failure is True:
                msg_header = '{}{}'.format(
                    self.exception_name, colorful.bold_white(':')
                ) if msg_index == 0 else ' ' * (len(self.exception_name) + 1)
                if str(world.config.formatter) in ('gherkin'):
                    # this line could be improved by letting radish handle the printing
                    msg.append('\t\t{} {}'.format(
                        colorful.bold_red(msg_header),
                        colorful.red(self.message[msg_index])))
                elif str(world.config.formatter) in ('silent_formatter'):
                    msg.append('{} '.format(colorful.bold_red(msg_header)))
                    msg.append('{}'.format(
                        colorful.red(self.message[msg_index])))

            else:
                msg.append(
                    self.message[msg_index] if msg_index == 0 else '{}{} {} {}'
                    .format("\t" * 2, ' ' * (len(self.exception_name) + 1),
                            colorful.bold_white(':'), self.message[msg_index]))

        if self.exit_on_failure is False or (self.no_failure is True and msg):

            if str(world.config.formatter) in ('gherkin'):
                for message in msg:
                    console_write(message)
            elif str(world.config.formatter) in ('silent_formatter'):
                if not hasattr(self.step_obj.context,
                               'failure_msg'):  # where to put this
                    self.step_obj.context.failure_msg = []
                self.step_obj.context.failure_msg.extend(msg)

            if self.no_failure is False:
                self._fail_step(self.step_obj.id)
            else:
                self.step_obj.state = Step.State.SKIPPED
                for step in self.step_obj.parent.all_steps:
                    step.runable = False
            return

        if self.no_failure is False:
            raise self.exception('\n'.join(msg))
Exemplo n.º 10
0
    def _get_step_before_output(self, step, color_func=None):
        if color_func is None:
            color_func = colorful.bold_yellow
        output = "\r        {0}{1}".format(
            self.get_id_sentence_prefix(step, color_func),
            color_func(step.sentence))

        if step.text:
            id_padding = self.get_id_padding(len(step.parent.steps))
            output += colorful.bold_white(
                '\n            {0}"""'.format(id_padding))
            output += colorful.cyan("".join([
                "\n                {0}{1}".format(id_padding, l)
                for l in step.raw_text
            ]))
            output += colorful.bold_white(
                '\n            {0}"""'.format(id_padding))

        if step.table_header:
            colored_pipe = colorful.bold_white("|")
            col_widths = self.get_table_col_widths([step.table_header] +
                                                   step.table_data)

            # output table header
            output += "\n          {0} {1} {0}".format(
                colored_pipe,
                (" {0} ").format(colored_pipe).join(
                    str(colorful.white("{1: <{0}}".format(col_widths[i], x)))
                    for i, x in enumerate(step.table_header)),
            )

            # output table data
            for row in step.table_data:
                output += "\n          {0} {1} {0}".format(
                    colored_pipe,
                    (" {0} ").format(colored_pipe).join(
                        str(color_func("{1: <{0}}".format(col_widths[i], x)))
                        for i, x in enumerate(row)),
                )

        return output
Exemplo n.º 11
0
def write_scenario_header(scenario):
    """Write the Scenario header"""
    indentation_level = 1 if isinstance(scenario.rule, DefaultRule) else 2
    indentation = INDENT_STEP * indentation_level

    scenario_heading = "{scenario_keyword}: {short_description}".format(
        scenario_keyword=cf.bold_white(scenario.keyword),
        short_description=cf.white(scenario.short_description),
    )

    for tag in scenario.tags:
        write_tagline(tag, indentation)
    print(indentation + scenario_heading, flush=True)
Exemplo n.º 12
0
def write_rule_header(rule):
    """Write the Rule header

    The short description is only written if it's not a DefaultRule
    """
    if isinstance(rule, DefaultRule):
        return

    rule_heading = "{rule_keyword}: {short_description}".format(
        rule_keyword=cf.bold_white(rule.keyword),
        short_description=cf.white(rule.short_description),
    )

    print(INDENT_STEP + rule_heading + "\n", flush=True)
Exemplo n.º 13
0
def read_wikipedia(input_text):
    print_output: print(cf.slateGray("Recognized: {0}".format(cf.bold_white(input_text))))
    input_text = input_text[13:].strip()
    if input_text.strip() != '':
        url = "https://en.wikipedia.org/api/rest_v1/page/summary/{}".format(input_text)
        logging.info('URL: ' + url)
        r = requests.get(url)
        page = r.json()
        if 'extract' in page:
            resp = page["extract"]
            print(resp)
            echo_line(resp, False)
        else:
            echo_line('No details found', False)
Exemplo n.º 14
0
def dump(filename=None):
    """
    dump corpus content
    """
    with tempfile.TemporaryDirectory() as temp_dir:
        env = dict(os.environ,
                   LLVM_PROFILE_FILE=os.path.join(temp_dir, 'default.profraw'))
        call = [FUZZCOVER_BINARY, '--dump', CORPUS_DIRECTORY]

        if filename:
            call.append(filename)

        subprocess.run(call, env=env)

        if filename:
            print('Saved corpus to {filename}.'.format(
                filename=cf.bold_white(filename)))
Exemplo n.º 15
0
def echo_line(input_text, print_output = True):
    filename = 'response.wav'
    if input_text.strip() != '':
        #say_text = 'I heard, ' + input_text
        if len(input_text) <= 2:
            input_text = 'error with text length'
        else:
            say_text = input_text
            url = 'http://0.0.0.0:5002/api/tts?text={}'.format(say_text)
            r = requests.get(url)
            with open(filename, 'wb') as f:
                f.write(r.content)
            logging.debug('saved wav for {}'.format(url))
            #p = pyaudio.PyAudio()
            #play_wav(filename, p)
            play_wav(filename)
        #p.terminate()
    if print_output: print(cf.slateGray("Recognized: {0}".format(cf.bold_white(input_text))))
    return
Exemplo n.º 16
0
    def console_writer_after_each_step(self, step):
        """
            Writes the step to the console after it was run

            :param Step step: the step to write to the console
        """
        if not isinstance(step.parent.parent, Feature):
            return

        color_func = self.get_color_func(step.state)
        line_jump_seq = self.get_line_jump_seq() * (
            ((len(step.raw_text) + 3) if step.text else 1)
            + (len(step.table) + 1 if step.table_header else 0)
        )
        output = "{0}        ".format(line_jump_seq)

        if isinstance(step.parent, ScenarioOutline):
            # Highlight ScenarioOutline placeholders e.g. '<method>'
            output += "".join(
                str(
                    colorful.white(item)
                    if (
                        self._placeholder_regex.search(item)
                        and item.strip("<>") in step.parent.examples_header
                    )
                    else color_func(item)
                )
                for item in self._placeholder_regex.split(step.sentence)
            )
        else:
            output += "{0}{1}".format(
                self.get_id_sentence_prefix(step, colorful.bold_cyan),
                color_func(step.sentence),
            )

        if step.text:
            id_padding = self.get_id_padding(len(step.parent.steps))
            output += colorful.bold_white('\n            {0}"""'.format(id_padding))
            output += colorful.cyan(
                "".join(
                    [
                        "\n                {0}{1}".format(id_padding, l)
                        for l in step.raw_text
                    ]
                )
            )
            output += colorful.bold_white('\n            {0}"""'.format(id_padding))

        if step.table_header:
            colored_pipe = colorful.bold_white("|")
            col_widths = self.get_table_col_widths(
                [step.table_header] + step.table_data
            )

            # output table header
            output += "\n          {0} {1} {0}".format(
                colored_pipe,
                (" {0} ")
                .format(colored_pipe)
                .join(
                    str(colorful.white("{1: <{0}}".format(col_widths[i], x)))
                    for i, x in enumerate(step.table_header)
                ),
            )

            # output table data
            for row in step.table_data:
                output += "\n          {0} {1} {0}".format(
                    colored_pipe,
                    (" {0} ")
                    .format(colored_pipe)
                    .join(
                        str(color_func("{1: <{0}}".format(col_widths[i], x)))
                        for i, x in enumerate(row)
                    ),
                )

        if step.state == step.State.FAILED:
            if world.config.with_traceback:
                output += "\n          {0}{1}".format(
                    self.get_id_padding(len(step.parent.steps) - 2),
                    "\n          ".join(
                        [
                            str(colorful.red(l))
                            for l in step.failure.traceback.split("\n")[:-2]
                        ]
                    ),
                )
            output += "\n          {0}{1}: {2}".format(
                self.get_id_padding(len(step.parent.steps) - 2),
                colorful.bold_red(step.failure.name),
                colorful.red(step.failure.reason),
            )

        write(output)
Exemplo n.º 17
0
    def console_writer_after_each_scenario(self, scenario):
        """
            If the scenario is a ExampleScenario it will write the Examples header

            :param Scenario scenario: the scenario which was ran.
        """
        output = ""
        if isinstance(scenario, ScenarioOutline):
            output += "\n    {0}:\n".format(
                colorful.bold_white(scenario.example_keyword)
            )
            output += colorful.bold_white(
                "        {0}| {1} |".format(
                    self.get_id_padding(len(scenario.scenarios), offset=2),
                    " | ".join(
                        "{1: <{0}}".format(scenario.get_column_width(i), x)
                        for i, x in enumerate(scenario.examples_header)
                    ),
                )
            )
        elif isinstance(scenario, ScenarioLoop):
            output += "\n    {0}: {1}".format(
                colorful.bold_white(scenario.iterations_keyword),
                colorful.cyan(scenario.iterations),
            )
        elif isinstance(scenario.parent, ScenarioOutline):
            colored_pipe = colorful.bold_white("|")
            color_func = self.get_color_func(scenario.state)
            output += "{0}        {1}{2} {3} {2}".format(
                self.get_line_jump_seq(),
                self.get_id_sentence_prefix(
                    scenario, colorful.bold_cyan, len(scenario.parent.scenarios)
                ),
                colored_pipe,
                (" {0} ")
                .format(colored_pipe)
                .join(
                    str(
                        color_func(
                            "{1: <{0}}".format(scenario.parent.get_column_width(i), x)
                        )
                    )
                    for i, x in enumerate(scenario.example.data)
                ),
            )

            if scenario.state == Step.State.FAILED:
                failed_step = scenario.failed_step
                if world.config.with_traceback:
                    output += "\n          {0}{1}".format(
                        self.get_id_padding(len(scenario.parent.scenarios)),
                        "\n          ".join(
                            [
                                str(colorful.red(l))
                                for l in failed_step.failure.traceback.split("\n")[:-2]
                            ]
                        ),
                    )
                output += "\n          {0}{1}: {2}".format(
                    self.get_id_padding(len(scenario.parent.scenarios)),
                    colorful.bold_red(failed_step.failure.name),
                    colorful.red(failed_step.failure.reason),
                )
        elif isinstance(scenario.parent, ScenarioLoop):
            colored_pipe = colorful.bold_white("|")
            color_func = self.get_color_func(scenario.state)
            output += "{0}        {1}{2} {3: <18} {2}".format(
                self.get_line_jump_seq(),
                self.get_id_sentence_prefix(
                    scenario, colorful.bold_cyan, len(scenario.parent.scenarios)
                ),
                colored_pipe,
                str(color_func(scenario.iteration)),
            )

            if scenario.state == Step.State.FAILED:
                failed_step = scenario.failed_step
                if world.config.with_traceback:
                    output += "\n          {0}{1}".format(
                        self.get_id_padding(len(scenario.parent.scenarios)),
                        "\n          ".join(
                            [
                                str(colorful.red(l))
                                for l in failed_step.failure.traceback.split("\n")[:-2]
                            ]
                        ),
                    )
                output += "\n          {0}{1}: {2}".format(
                    self.get_id_padding(len(scenario.parent.scenarios)),
                    colorful.bold_red(failed_step.failure.name),
                    colorful.red(failed_step.failure.reason),
                )

        if output:
            write(output)
Exemplo n.º 18
0
    def console_writer_before_each_scenario(self, scenario):
        """
            Writes the scenario header to the console

            :param Scenario scenario: the scenario to write to the console
        """
        output = "\n"
        if isinstance(scenario.parent, ScenarioOutline):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(
                scenario, colorful.bold_yellow, len(scenario.parent.scenarios)
            )
            colored_pipe = colorful.bold_white("|")
            output = "        {0}{1} {2} {1}".format(
                id_prefix,
                colored_pipe,
                (" {0} ")
                .format(colored_pipe)
                .join(
                    str(
                        colorful.bold_yellow(
                            "{1: <{0}}".format(scenario.parent.get_column_width(i), x)
                        )
                    )
                    for i, x in enumerate(scenario.example.data)
                ),
            )
        elif isinstance(scenario.parent, ScenarioLoop):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(
                scenario, colorful.bold_yellow, len(scenario.parent.scenarios)
            )
            colored_pipe = colorful.bold_white("|")
            output = "        {0}{1} {2: <18} {1}".format(
                id_prefix, colored_pipe, str(colorful.bold_yellow(scenario.iteration))
            )
        else:
            id_prefix = self.get_id_sentence_prefix(scenario, colorful.bold_cyan)
            for tag in scenario.tags:
                if (
                    tag.name == "precondition"
                    and world.config.expand
                    and world.config.show
                ):  # exceptional for show command when scenario steps expand and tag is a precondition -> comment it out
                    output += colorful.white(
                        "    # @{0}{1}\n".format(
                            tag.name, "({0})".format(tag.arg) if tag.arg else ""
                        )
                    )
                else:
                    output += colorful.cyan(
                        "    @{0}{1}\n".format(
                            tag.name, "({0})".format(tag.arg) if tag.arg else ""
                        )
                    )
            output += "    {0}{1}: {2}".format(
                id_prefix,
                colorful.bold_white(scenario.keyword),
                colorful.bold_white(scenario.sentence),
            )
        write(output)
Exemplo n.º 19
0
    def console_write(self, features, marker):
        """
            Writes the endreport for all features

            :param list features: all features
        """
        stats = {
            "features": {"amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0},
            "scenarios": {"amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0},
            "steps": {"amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0},
        }
        pending_steps = []
        duration = timedelta()
        for feature in features:
            if not feature.has_to_run(world.config.scenarios):
                continue
            stats["features"]["amount"] += 1
            stats["features"][feature.state] += 1

            if feature.state in [Step.State.PASSED, Step.State.FAILED]:
                duration += feature.duration

            for scenario in feature.all_scenarios:
                if not scenario.has_to_run(world.config.scenarios):
                    continue

                if isinstance(scenario, ScenarioOutline):  # skip ScenarioOutlines
                    continue
                if isinstance(scenario, ScenarioLoop):  # skip ScenarioLoop
                    continue

                stats["scenarios"]["amount"] += 1
                stats["scenarios"][scenario.state] += 1
                for step in scenario.steps:
                    stats["steps"]["amount"] += 1
                    stats["steps"][step.state] += 1

                    if step.state == Step.State.PENDING:
                        pending_steps.append(step)

        colored_closing_paren = colorful.bold_white(")")
        colored_comma = colorful.bold_white(", ")
        passed_word = colorful.bold_green("{0} passed")
        failed_word = colorful.bold_red("{0} failed")
        skipped_word = colorful.cyan("{0} skipped")
        pending_word = colorful.bold_yellow("{0} pending")

        output = colorful.bold_white("{0} features (".format(stats["features"]["amount"]))
        output += passed_word.format(stats["features"]["passed"])
        if stats["features"]["failed"]:
            output += colored_comma + failed_word.format(stats["features"]["failed"])
        if stats["features"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["features"]["skipped"])
        if stats["features"]["pending"]:
            output += colored_comma + pending_word.format(stats["features"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white("{} scenarios (".format(stats["scenarios"]["amount"]))
        output += passed_word.format(stats["scenarios"]["passed"])
        if stats["scenarios"]["failed"]:
            output += colored_comma + failed_word.format(stats["scenarios"]["failed"])
        if stats["scenarios"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["scenarios"]["skipped"])
        if stats["scenarios"]["pending"]:
            output += colored_comma + pending_word.format(stats["scenarios"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white("{} steps (".format(stats["steps"]["amount"]))
        output += passed_word.format(stats["steps"]["passed"])
        if stats["steps"]["failed"]:
            output += colored_comma + failed_word.format(stats["steps"]["failed"])
        if stats["steps"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["steps"]["skipped"])
        if stats["steps"]["pending"]:
            output += colored_comma + pending_word.format(stats["steps"]["pending"])
        output += colored_closing_paren

        if pending_steps:
            sr = StepRegistry()
            pending_step_implementations = make_unique_obj_list(pending_steps, lambda x: x.definition_func)
            output += colorful.white("\nYou have {0} pending step implementation{1} affecting {2} step{3}:\n  {4}\n\nNote: this could be the reason for some failing subsequent steps".format(
                len(pending_step_implementations),
                "s" if len(pending_step_implementations) is not 1 else "",
                len(pending_steps),
                "s" if len(pending_steps) is not 1 else "",
                "\n  ".join(["-  '{0}' @ {1}".format(sr.get_pattern(s.definition_func), get_func_code(s.definition_func).co_filename) for s in pending_step_implementations])
            ))

        output += "\n"
        output += colorful.cyan("Run {0} finished within {1}".format(marker, humanize.naturaldelta(duration)))

        write(output)
Exemplo n.º 20
0
def test_step_matches_configs(
    match_config_files, basedirs, cover_min_percentage=None, cover_show_missing=False
):
    """
    Test if the given match config files matches the actual
    matched step implementations.
    """
    if cover_min_percentage is not None and float(cover_min_percentage) > 100:
        sys.stderr.write(
            str(
                colorful.magenta(
                    "You are a little cocky to think you can reach a minimum coverage of {0:.2f}%\n".format(
                        float(cover_min_percentage)
                    )
                )
            )
        )
        return 3

    # load user's custom python files
    for basedir in basedirs:
        load_modules(basedir)

    steps = StepRegistry().steps

    if not steps:
        sys.stderr.write(
            str(
                colorful.magenta(
                    "No step implementations found in {0}, thus doesn't make sense to continue".format(
                        basedirs
                    )
                )
            )
        )
        return 4

    failed = 0
    passed = 0
    covered_steps = set()

    for match_config_file in match_config_files:
        # load the given match config file
        with codecs.open(match_config_file, "r", "utf-8") as f:
            match_config = yaml.safe_load(f)

        if not match_config:
            print(
                colorful.magenta(
                    "No sentences found in {0} to test against".format(
                        match_config_file
                    )
                )
            )
            return 5

        print(
            colorful.yellow(
                "Testing sentences from {0}:".format(
                    colorful.bold_yellow(match_config_file)
                )
            )
        )
        failed_sentences, passed_senteces = test_step_matches(match_config, steps)
        failed += failed_sentences
        passed += passed_senteces

        covered_steps = covered_steps.union(
            x["should_match"] for x in match_config if "should_match" in x
        )

        # newline
        sys.stdout.write("\n")

    report = colorful.bold_white("{0} sentences (".format(failed + passed))
    if passed > 0:
        report += colorful.bold_green("{0} passed".format(passed))

    if passed > 0 and failed > 0:
        report += colorful.bold_white(", ")

    if failed > 0:
        report += colorful.bold_red("{0} failed".format(failed))
    report += colorful.bold_white(")")
    print(report)

    step_coverage = 100.0 / len(steps) * len(covered_steps)
    coverage_report = colorful.bold_white(
        "Covered {0} of {1} step implementations".format(len(covered_steps), len(steps))
    )

    ret = 0 if failed == 0 else 1

    if cover_min_percentage:
        coverage_color = (
            colorful.bold_green
            if step_coverage >= float(cover_min_percentage)
            else colorful.bold_red
        )
        coverage_report += colorful.bold_white(" (coverage: ")
        coverage_report += coverage_color("{0:.2f}%".format(step_coverage))
        if float(cover_min_percentage) > step_coverage:
            coverage_report += colorful.bold_white(
                ", expected a minimum of {0}".format(
                    colorful.bold_green(cover_min_percentage + "%")
                )
            )
            if failed == 0:
                ret = 2
            # if tests have passed and coverage is too low we fail with exit code 2
        coverage_report += colorful.bold_white(")")

    print(coverage_report)

    if cover_show_missing:
        missing_steps = get_missing_steps(steps, covered_steps)
        if missing_steps:
            missing_step_report = colorful.bold_yellow("Missing steps:\n")
            for step in missing_steps:
                missing_step_report += "- {0} at ".format(colorful.cyan(step[0]))
                missing_step_report += colorful.cyan(step[1]) + "\n"
            sys.stdout.write(missing_step_report)

    return ret
Exemplo n.º 21
0
def inspect_tree(root_path=CURRENT_PATH,
                 options={},
                 level=ROOT_LEVEL,
                 **kwargs):
    options = dict(options or {}, **kwargs)

    indent = options.get('indent', None)
    silent = options.get('silent', None)

    if silent != False:
        silent = bool(silent) or DEFAULT_SILENT

    if not indent == False:
        indent = bool(indent) or DEFAULT_INDENT

    else:
        indent = 0

    output = ''

    try:
        if (level == ROOT_LEVEL):
            root_item_name = color.darkGray(root_path)

            output += '\n'
            output += root_item_name

        items = None

        try:
            items = get_tree(root_path, dict(options, **dict(silent=False)))

            if items is None:
                raise Error(
                    'Could not get file system tree object: {0}'.format(
                        root_path))

        except Exception as error:
            raise error

        prefix = None

        item_name = None
        item_output = None

        item_count = len(items)
        last_item = items[-1]

        for item in items:
            prefix = indent * level * SPACE

            if item == last_item:
                prefix += color.darkGray(BRANCH_ITEM_PREFIX_LAST)

            else:
                prefix += color.darkGray(BRANCH_ITEM_PREFIX)

            if item.is_directory:
                if item.is_link:
                    item_name = SPACE.join([
                        str(color.bold_white(item.name)),
                        str(color.darkGray(BRANCH_ITEM_LINK_SUFFIX)),
                        str(color.darkGray(item.resolved_relative_path)),
                        str(item.meta),
                    ])

                else:
                    item_name = SPACE.join([
                        str(color.bold_white(item.name)),
                        str(item.meta),
                    ])

            elif item.is_file:
                if item.is_link:
                    item_name = SPACE.join([
                        str(color.white(item.name)),
                        str(color.darkGray(BRANCH_ITEM_LINK_SUFFIX)),
                        str(color.darkGray(item.resolved_relative_path)),
                        str(item.meta),
                    ])

                else:
                    item_name = SPACE.join([
                        str(color.white(item.name)),
                        str(item.meta),
                    ])

            else:
                if item.is_link:
                    item_name = SPACE.join([
                        str(color.red(item.name)),
                        str(color.darkGray(BRANCH_ITEM_LINK_SUFFIX)),
                        str(color.darkGray(BRANCH_ITEM_LINK_BROKEN)),
                        str(item.meta),
                    ])

                else:
                    item_name = SPACE.join([
                        str(color.red(item.name)),
                        str(item.meta),
                    ])

            output += '\n'
            output += prefix
            output += item_name  # BUG: unicode issue in Python 2

            if item.children:
                try:
                    item_output = inspect_tree(item.path, options, level + 1)

                except Exception as error:
                    pass

                if isinstance(item_output, str):
                    output += item_output

        if (level == ROOT_LEVEL):
            output += '\n\n'

        return output

    except Exception as error:
        if not silent:
            raise error

        output += '\n\n'
        output += '    {0}'.format(color.red(str(error).split(' - {')[0]))
        output += '\n\n'

        return output
Exemplo n.º 22
0
    def console_write(self, features, marker):
        """
            Writes the endreport for all features

            :param list features: all features
        """
        stats = {
            "features": {
                "amount": 0,
                "passed": 0,
                "failed": 0,
                "skipped": 0,
                "untested": 0,
                "pending": 0,
            },
            "scenarios": {
                "amount": 0,
                "passed": 0,
                "failed": 0,
                "skipped": 0,
                "untested": 0,
                "pending": 0,
            },
            "steps": {
                "amount": 0,
                "passed": 0,
                "failed": 0,
                "skipped": 0,
                "untested": 0,
                "pending": 0,
            },
        }
        pending_steps = []
        duration = timedelta()
        for feature in features:
            if not feature.has_to_run(world.config.scenarios):
                continue
            stats["features"]["amount"] += 1
            stats["features"][feature.state] += 1

            if feature.state in [Step.State.PASSED, Step.State.FAILED]:
                duration += feature.duration

            for scenario in feature.all_scenarios:
                if not scenario.has_to_run(world.config.scenarios):
                    continue

                if isinstance(scenario, ScenarioOutline):  # skip ScenarioOutlines
                    continue
                if isinstance(scenario, ScenarioLoop):  # skip ScenarioLoop
                    continue

                stats["scenarios"]["amount"] += 1
                stats["scenarios"][scenario.state] += 1
                for step in scenario.steps:
                    stats["steps"]["amount"] += 1
                    stats["steps"][step.state] += 1

                    if step.state == Step.State.PENDING:
                        pending_steps.append(step)

        colored_closing_paren = colorful.bold_white(")")
        colored_comma = colorful.bold_white(", ")
        passed_word = colorful.bold_green("{0} passed")
        failed_word = colorful.bold_red("{0} failed")
        skipped_word = colorful.cyan("{0} skipped")
        pending_word = colorful.bold_yellow("{0} pending")

        output = colorful.bold_white(
            "{0} features (".format(stats["features"]["amount"])
        )
        output += passed_word.format(stats["features"]["passed"])
        if stats["features"]["failed"]:
            output += colored_comma + failed_word.format(stats["features"]["failed"])
        if stats["features"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["features"]["skipped"])
        if stats["features"]["pending"]:
            output += colored_comma + pending_word.format(stats["features"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white(
            "{} scenarios (".format(stats["scenarios"]["amount"])
        )
        output += passed_word.format(stats["scenarios"]["passed"])
        if stats["scenarios"]["failed"]:
            output += colored_comma + failed_word.format(stats["scenarios"]["failed"])
        if stats["scenarios"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["scenarios"]["skipped"])
        if stats["scenarios"]["pending"]:
            output += colored_comma + pending_word.format(stats["scenarios"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white("{} steps (".format(stats["steps"]["amount"]))
        output += passed_word.format(stats["steps"]["passed"])
        if stats["steps"]["failed"]:
            output += colored_comma + failed_word.format(stats["steps"]["failed"])
        if stats["steps"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["steps"]["skipped"])
        if stats["steps"]["pending"]:
            output += colored_comma + pending_word.format(stats["steps"]["pending"])
        output += colored_closing_paren

        if pending_steps:
            sr = StepRegistry()
            pending_step_implementations = make_unique_obj_list(
                pending_steps, lambda x: x.definition_func
            )
            output += colorful.white(
                "\nYou have {0} pending step implementation{1} affecting {2} step{3}:\n  {4}\n\nNote: this could be the reason for some failing subsequent steps".format(
                    len(pending_step_implementations),
                    "s" if len(pending_step_implementations) is not 1 else "",
                    len(pending_steps),
                    "s" if len(pending_steps) is not 1 else "",
                    "\n  ".join(
                        [
                            "-  '{0}' @ {1}".format(
                                sr.get_pattern(s.definition_func),
                                get_func_code(s.definition_func).co_filename,
                            )
                            for s in pending_step_implementations
                        ]
                    ),
                )
            )

        output += "\n"

        if world.config.wip:
            if stats["scenarios"]["passed"] > 0:
                output += colorful.red(
                    "\nThe --wip switch was used, so I didn't expect anything to pass. These scenarios passed:\n"
                )

                has_passed_scenarios = False
                for feature in features:
                    passed_scenarios = list(
                        filter(
                            lambda s: s.state == Step.State.PASSED,
                            feature.all_scenarios,
                        )
                    )
                    for scenario in passed_scenarios:
                        output += colorful.red(
                            "\n - {}: {}".format(feature.path, scenario.sentence)
                        )
                        has_passed_scenarios = True

                if has_passed_scenarios:
                    output += "\n"
            else:
                output += colorful.green(
                    "\nThe --wip switch was used, so the failures were expected. All is good.\n"
                )

        output += colorful.cyan(
            "Run {0} finished within {1}".format(
                marker, humanize.naturaldelta(duration)
            )
        )

        write(output)
Exemplo n.º 23
0
def format_integer(i: int) -> str:
    s = '{i:+d}'.format(i=i)
    if i == 0:
        return s
    else:
        return cf.bold_white(s)
Exemplo n.º 24
0
    def console_writer_after_each_scenario(self, scenario):
        """
            If the scenario is a ExampleScenario it will write the Examples header

            :param Scenario scenario: the scenario which was ran.
        """
        output = ""
        if isinstance(scenario, ScenarioOutline):
            output += u"\n    {0}:\n".format(
                colorful.bold_white(scenario.example_keyword))
            output += colorful.bold_white(u"        {0}| {1} |".format(
                self.get_id_padding(len(scenario.scenarios), offset=2),
                u" | ".join("{1: <{0}}".format(scenario.get_column_width(i), x)
                            for i, x in enumerate(scenario.examples_header))))
        elif isinstance(scenario, ScenarioLoop):
            output += u"\n    {0}: {1}".format(
                colorful.bold_white(scenario.iterations_keyword),
                colorful.cyan(scenario.iterations))
        elif isinstance(scenario.parent, ScenarioOutline):
            colored_pipe = colorful.bold_white("|")
            color_func = self.get_color_func(scenario.state)
            output += u"{0}        {1}{2} {3} {2}".format(
                self.get_line_jump_seq(),
                self.get_id_sentence_prefix(scenario, colorful.bold_cyan,
                                            len(scenario.parent.scenarios)),
                colored_pipe, (u" {0} ").format(colored_pipe).join(
                    str(
                        color_func(u"{1: <{0}}".format(
                            scenario.parent.get_column_width(i), x)))
                    for i, x in enumerate(scenario.example.data)))

            if scenario.state == Step.State.FAILED:
                failed_step = scenario.failed_step
                if world.config.with_traceback:
                    output += u"\n          {0}{1}".format(
                        self.get_id_padding(len(scenario.parent.scenarios)),
                        "\n          ".join([
                            str(colorful.red(l)) for l in
                            failed_step.failure.traceback.split("\n")[:-2]
                        ]))
                output += u"\n          {0}{1}: {2}".format(
                    self.get_id_padding(len(scenario.parent.scenarios)),
                    colorful.bold_red(failed_step.failure.name),
                    colorful.red(failed_step.failure.reason))
        elif isinstance(scenario.parent, ScenarioLoop):
            colored_pipe = colorful.bold_white("|")
            color_func = self.get_color_func(scenario.state)
            output += u"{0}        {1}{2} {3: <18} {2}".format(
                self.get_line_jump_seq(),
                self.get_id_sentence_prefix(scenario, colorful.bold_cyan,
                                            len(scenario.parent.scenarios)),
                colored_pipe, str(color_func(scenario.iteration)))

            if scenario.state == Step.State.FAILED:
                failed_step = scenario.failed_step
                if world.config.with_traceback:
                    output += u"\n          {0}{1}".format(
                        self.get_id_padding(len(scenario.parent.scenarios)),
                        "\n          ".join([
                            str(colorful.red(l)) for l in
                            failed_step.failure.traceback.split("\n")[:-2]
                        ]))
                output += u"\n          {0}{1}: {2}".format(
                    self.get_id_padding(len(scenario.parent.scenarios)),
                    colorful.bold_red(failed_step.failure.name),
                    colorful.red(failed_step.failure.reason))

        if output:
            write(output)
Exemplo n.º 25
0
    def console_writer_after_each_step(self, step):
        """
            Writes the step to the console after it was run

            :param Step step: the step to write to the console
        """
        if not isinstance(step.parent.parent, Feature):
            return

        color_func = self.get_color_func(step.state)
        line_jump_seq = self.get_line_jump_seq() * (
            ((len(step.raw_text) + 3) if step.text else 1) +
            (len(step.table) + 1 if step.table_header else 0))
        output = u'{0}        '.format(line_jump_seq)

        if isinstance(step.parent, ScenarioOutline):
            # Highlight ScenarioOutline placeholders e.g. '<method>'
            output += (u''.join(
                str(
                    colorful.white(item) if (
                        self._placeholder_regex.search(item)
                        and item.strip('<>') in step.parent.examples_header
                    ) else color_func(item))
                for item in self._placeholder_regex.split(step.sentence)))
        else:
            output += u"{0}{1}".format(
                self.get_id_sentence_prefix(step, colorful.bold_cyan),
                color_func(step.sentence))

        if step.text:
            id_padding = self.get_id_padding(len(step.parent.steps))
            output += colorful.bold_white(
                u'\n            {0}"""'.format(id_padding))
            output += colorful.cyan(u"".join([
                "\n                {0}{1}".format(id_padding, l)
                for l in step.raw_text
            ]))
            output += colorful.bold_white(
                u'\n            {0}"""'.format(id_padding))

        if step.table_header:
            colored_pipe = colorful.bold_white("|")
            col_widths = self.get_table_col_widths([step.table_header] +
                                                   step.table_data)

            # output table header
            output += u"\n          {0} {1} {0}".format(
                colored_pipe, (" {0} ").format(colored_pipe).join(
                    str(colorful.white(u"{1: <{0}}".format(col_widths[i], x)))
                    for i, x in enumerate(step.table_header)))

            # output table data
            for row in step.table_data:
                output += u"\n          {0} {1} {0}".format(
                    colored_pipe, (" {0} ").format(colored_pipe).join(
                        str(color_func(u"{1: <{0}}".format(col_widths[i], x)))
                        for i, x in enumerate(row)))

        if step.state == step.State.FAILED:
            if world.config.with_traceback:
                output += u"\n          {0}{1}".format(
                    self.get_id_padding(len(step.parent.steps) - 2),
                    "\n          ".join([
                        str(colorful.red(l))
                        for l in step.failure.traceback.split("\n")[:-2]
                    ]))
            output += u"\n          {0}{1}: {2}".format(
                self.get_id_padding(len(step.parent.steps) - 2),
                colorful.bold_red(step.failure.name),
                colorful.red(step.failure.reason))

        write(output)
Exemplo n.º 26
0
 def write_feature_header(self, feature):
     print(cf.bold_white(str(feature.path)) + ":", end=" ", flush=True)
Exemplo n.º 27
0
def test_step_matches_configs(match_config_files,
                              basedirs,
                              cover_min_percentage=None,
                              cover_show_missing=False):
    """
    Test if the given match config files matches the actual
    matched step implementations.
    """
    if cover_min_percentage is not None and float(cover_min_percentage) > 100:
        sys.stderr.write(
            str(
                colorful.magenta(
                    'You are a little cocky to think you can reach a minimum coverage of {0:.2f}%\n'
                    .format(float(cover_min_percentage)))))
        return 3

    # load user's custom python files
    for basedir in basedirs:
        load_modules(basedir)

    steps = StepRegistry().steps

    if not steps:
        sys.stderr.write(
            str(
                colorful.magenta(
                    'No step implementations found in {0}, thus doesn\'t make sense to continue'
                    .format(basedirs))))
        return 4

    failed = 0
    passed = 0
    covered_steps = set()

    for match_config_file in match_config_files:
        # load the given match config file
        with codecs.open(match_config_file, "r", "utf-8") as f:
            match_config = yaml.safe_load(f)

        if not match_config:
            print(
                colorful.magenta(
                    'No sentences found in {0} to test against'.format(
                        match_config_file)))
            return 5

        print(
            colorful.yellow('Testing sentences from {0}:'.format(
                colorful.bold_yellow(match_config_file))))
        failed_sentences, passed_senteces = test_step_matches(
            match_config, steps)
        failed += failed_sentences
        passed += passed_senteces

        covered_steps = covered_steps.union(x['should_match']
                                            for x in match_config
                                            if 'should_match' in x)

        # newline
        sys.stdout.write('\n')

    report = colorful.bold_white('{0} sentences ('.format(failed + passed))
    if passed > 0:
        report += colorful.bold_green('{0} passed'.format(passed))

    if passed > 0 and failed > 0:
        report += colorful.bold_white(', ')

    if failed > 0:
        report += colorful.bold_red('{0} failed'.format(failed))
    report += colorful.bold_white(')')
    print(report)

    step_coverage = 100.0 / len(steps) * len(covered_steps)
    coverage_report = colorful.bold_white(
        'Covered {0} of {1} step implementations'.format(
            len(covered_steps), len(steps)))

    ret = 0 if failed == 0 else 1

    if cover_min_percentage:
        coverage_color = colorful.bold_green if step_coverage >= float(
            cover_min_percentage) else colorful.bold_red
        coverage_report += colorful.bold_white(' (coverage: ')
        coverage_report += coverage_color('{0:.2f}%'.format(step_coverage))
        if float(cover_min_percentage) > step_coverage:
            coverage_report += colorful.bold_white(
                ', expected a minimum of {0}'.format(
                    colorful.bold_green(cover_min_percentage + '%')))
            if failed == 0:
                ret = 2
            # if tests have passed and coverage is too low we fail with exit code 2
        coverage_report += colorful.bold_white(')')

    print(coverage_report)

    if cover_show_missing:
        missing_steps = get_missing_steps(steps, covered_steps)
        if missing_steps:
            missing_step_report = colorful.bold_yellow('Missing steps:\n')
            for step in missing_steps:
                missing_step_report += '- {0} at '.format(
                    colorful.cyan(step[0]))
                missing_step_report += colorful.cyan(step[1]) + '\n'
            sys.stdout.write(missing_step_report)

    return ret
Exemplo n.º 28
0
def main():
    args = docopt(__doc__)

    # check if file exits
    if not os.path.exists(args["FILE"]):
        print(cf.red("{} doesn't exist\n".format(args["FILE"])))
        exit(1)

    # create the output file
    of = args["-o"]
    open(of, "w").close()

    f = open(args["FILE"])
    grammar = f.read()
    f.close()

    # clear console
    clear()

    # print banner
    print(cf.yellow_bold(BANNER))

    print(cf.bold_white("Input Grammar:\n"))
    pgrammar = gm.parse(grammar)
    orignal = pgrammar
    gm.pprint(pgrammar)
    print()

    write_to_output(of, "Input Grammar", gm.str_pgrammar(pgrammar))

    choices = [
        "Remove Null Productions",
        "Remove Unit Productions",
        "Remove Left Recursion",
        "First Sets",
        "Follow Sets",
        "Parsing Table",
    ]

    misc_choices = [
        "Restore Original Grammar",
        "Print Current Grammar",
        "Clear",
        "Exit",
    ]

    question = {
        "type": "list",
        "name": "ops",
        "message": "Which operation would you like to perform?",
        "choices": choices + [Separator()] + misc_choices,
    }

    reuse_confirm = {
        "type": "confirm",
        "name": "last_grammar",
        "message": "Use this grammar output for subsequent operations?",
        "default": True,
    }

    reverse_confirm = {
        "type": "confirm",
        "name": "reverse_grammar",
        "message": "Reverse Non-Terminal ordering?",
        "default": True,
    }

    start_input = {
        "type": "input",
        "name": "start_sym",
        "message": "Enter Start Symbol:",
    }

    # will contain the last output
    output = None
    # if last output was a grammar (as opposed to follow sets)
    output_grammar = False

    while True:
        answer = prompt(question)
        choice = answer["ops"]

        if choice == misc_choices[0]:
            pgrammar = orignal
            print(cf.white_bold("\nRestored\n"))
            gm.pprint(pgrammar)
            print()
            write_to_output(of, choice, gm.str_pgrammar(pgrammar))
            continue

        elif choice == misc_choices[1]:
            print()
            gm.pprint(pgrammar)
            print()
            continue

        elif choice == misc_choices[2]:
            clear()
            continue

        if choice == misc_choices[3]:
            print(cf.cyan("Bye!"))
            print("Remember! Logs are saved in", of)
            print()
            break

        # --------

        if choice == choices[0]:
            output = elim_null(pgrammar)

        elif choice == choices[1]:
            output = elim_unit(pgrammar)
            output = remove_same_rules(output, False, False)

        elif choice == choices[2]:
            continuel = False
            for rule in pgrammar.values():
                if "eps" in rule:
                    print(
                        cf.red(
                            "\nGrammar contains null productions. This operation can't"
                            " be performed\n"))
                    continuel = True
                    break

            if continuel:
                continue

            answer = prompt(reverse_confirm)
            reverse = answer["reverse_grammar"]
            if reverse:
                print(
                    "\nReversing the order of non-terminals for Left Recursion Removal."
                )

                ng = gm.reverse_grammar(pgrammar)
            else:
                ng = pgrammar

            ng = elim_lr(ng)
            if reverse:
                ng = gm.reverse_grammar(ng)
            output = ng

        elif choice == choices[3]:
            fs = first_sets(pgrammar)
            output = fs

        elif choice == choices[4]:
            start_input["validate"] = lambda x: x in pgrammar.keys()
            answer = prompt(start_input)
            fs = first_sets(pgrammar)
            fls = follow_sets(answer["start_sym"], pgrammar, fs)
            output = fls

        elif choice == choices[5]:
            start_input["validate"] = lambda x: x in pgrammar.keys()
            answer = prompt(start_input)
            fs = first_sets(pgrammar)
            fls = follow_sets(answer["start_sym"], pgrammar, fs)

            output = parsing_table(pgrammar, fs, fls)

        if choice != choices[5]:

            # flag to check if the last operation output a pgrammar
            # only the first 3 operations output a grammar
            output_grammar = choices.index(choice) in range(3)

            # print output
            print(cf.bold_green("\n=>\n"))

            (gm.pprint if output_grammar else gm.set_print)(output)

            # log to the output file
            write_to_output(
                of,
                choice,
                (gm.str_pgrammar if output_grammar else gm.str_set)(output),
            )

            print()

            # ask to use grammar from last operation
            if output_grammar:
                answer = prompt(reuse_confirm)
                if answer["last_grammar"]:
                    pgrammar = output

        else:
            # print parsing table
            print("\n" + output + "\n")
            write_to_output(
                of,
                choice,
                output,
            )
Exemplo n.º 29
0
# import termcolor
# string = "type-name-function-location"
# string = string.replace('-', termcolor.colored('-', 'red'))
# print (string)



import colorful as cf

# create a colored string using clever method translation
print(cf.bold_white('Hello World'))
# create a colored string using `str.format()`
print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=cf))

# nest colors
print(cf.red('red {0} red'.format(cf.white('white'))))
print(cf.red('red' + cf.white(' white ', nested=True) + 'red'))

# combine styles with strings
print(cf.bold & cf.red | 'Hello World')

# use true colors
cf.use_true_colors()

# extend default color palette
cf.update_palette({'mint': '#c5e8c8'})
print(cf.mint_on_snow('Wow, this is actually mint'))

# choose a predefined style
cf.use_style('solarized')
# print the official solarized colors
Exemplo n.º 30
0
Arquivo: main.py Projeto: yyht/PRS
def main():
    args = parser.parse_args()
    logger = setup_logger()

    ## Use below for slurm setting.
    # slurm_job_id = os.getenv('SLURM_JOB_ID', 'nojobid')
    # slurm_proc_id = os.getenv('SLURM_PROC_ID', None)

    # unique_identifier = str(slurm_job_id)
    # if slurm_proc_id is not None:
    #     unique_identifier = unique_identifier + "_" + str(slurm_proc_id)
    unique_identifier = ''

    # Load config
    config_path = args.config
    episode_path = args.episode

    if args.resume_ckpt and not args.config:
        base_dir = os.path.dirname(os.path.dirname(args.resume_ckpt))
        config_path = os.path.join(base_dir, 'config.yaml')
        episode_path = os.path.join(base_dir, 'episode.yaml')
    config = yaml.load(open(config_path), Loader=yaml.FullLoader)
    episode = yaml.load(open(episode_path), Loader=yaml.FullLoader)
    config['data_schedule'] = episode

    # Override options
    for option in args.override.split('|'):
        if not option:
            continue
        address, value = option.split('=')
        keys = address.split('.')
        here = config
        for key in keys[:-1]:
            if key not in here:
                raise ValueError('{} is not defined in config file. '
                                 'Failed to override.'.format(address))
            here = here[key]
        if keys[-1] not in here:
            raise ValueError('{} is not defined in config file. '
                             'Failed to override.'.format(address))
        here[keys[-1]] = yaml.load(value, Loader=yaml.FullLoader)


    # Set log directory
    config['log_dir'] = os.path.join(args.log_dir, unique_identifier)
    if not args.resume_ckpt and os.path.exists(config['log_dir']):
        logger.warning('%s already exists' % config['log_dir'])
        input('Press enter to continue')

    # print the configuration
    print(colorful.bold_white("configuration:").styled_string)
    pprint(config)
    print(colorful.bold_white("configuration end").styled_string)

    if args.resume_ckpt and not args.log_dir:
        config['log_dir'] = os.path.dirname(
            os.path.dirname(args.resume_ckpt)
        )

    # Save config
    os.makedirs(config['log_dir'], mode=0o755, exist_ok=True)
    if not args.resume_ckpt or args.config:
        config_save_path = os.path.join(config['log_dir'], 'config.yaml')
        episode_save_path = os.path.join(config['log_dir'], 'episode.yaml')
        yaml.dump(config, open(config_save_path, 'w'))
        yaml.dump(episode, open(episode_save_path, 'w'))
        print(colorful.bold_yellow('config & episode saved to {}'.format(config['log_dir'])).styled_string)

    # Build components
    data_scheduler = DataScheduler(config)

    writer = SummaryWriter(config['log_dir'])
    model = MODEL[config['model_name']](config, writer)

    if args.resume_ckpt:
        model.load_state_dict(torch.load(args.resume_ckpt))
    model.to(config['device'])
    train_model(config, model, data_scheduler, writer)

    print(colorful.bold_white("\nThank you and Good Job Computer").styled_string)