Ejemplo n.º 1
0
def scan_lookup(statements, lookup, length=80):
    """Lookup a statement with example usage.

    Args:
        statements (Statement): Statement instance will all known statements.
        lookup           (str): String to lookup after in all statements.
    """

    Log.debug("Lookup after %s ..." % lookup)
    known_statements = statements.get_actions()
    actions = []

    # Check available statements
    if len(known_statements) == 0:
        return Log.error("Statements are missing")

    # Search in all statemnts
    for stmt in known_statements:
        if lookup.lower() in stmt.parse_description().lower():
            if stmt not in actions:
                actions.append(stmt)
        else:
            for exp in stmt.parse_statements():
                if lookup.lower() in exp.lower():
                    if stmt not in actions:
                        actions.append(stmt)

    # Handle missing action
    if len(actions) == 0:
        return Log.error("Nothing found for '%s'" % lookup)

    # Print lookup
    for action in actions:
        delimiter = "-" * (length)
        description = action.parse_description()
        desc = u"\033[96m %-{}s \033[0m".format(length - 2) % description
        print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
        print(u"\033[90m|\033[0m" + desc + "\033[90m|\033[0m")
        print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
        for text in action.parse_statements():
            stmt = u"\033[95m %-{}s \033[0m".format(length - 6) % text.strip()
            print(u"\033[90m|   |\033[0m" + stmt + "\033[90m|\033[0m")
        print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
        for text in action.parse("sample"):
            sample = u"\033[93m %-{}s \033[0m".format(length -
                                                      6) % text.strip()
            print(u"\033[90m|   |\033[0m" + sample + "\033[90m|\033[0m")

    # Done
    print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
    Log.debug("Done...")
Ejemplo n.º 2
0
    def check_payload(self, content):
        """Check line from build steps if it's an instruction payload.

        Args:
            content (list): Build step lines ahead of current cursor.

        Returns:
            self: Self instance.
        """

        if self.last_step is None:
            return self
        if self.last_step.punct != Instruction.RunType.ARGS:
            return self
        newlines, borders = 0, []
        for i, next_line in enumerate(content):
            if len(borders) == 0 and newlines > 1:
                raise self.NoPayloadError(self.last_step)
            if next_line.strip() == "":
                newlines += 1
                continue
            if next_line.strip() == self.delimiter:
                borders.append(i)
            if len(borders) == 2:
                break
        if len(borders) == 1:
            raise self.UnclosedPayloadError(self.last_step)
        try:
            a, z = borders
            payload = content[1 + a:z]
            if Placeholder.scan_list(payload):
                payload = Placeholder.parse_list(payload)
            self.last_step.set_payload(payload)
            self.skip_line(lines=z + 1)
            self.last_step = None
        except Exception as e:
            Log.error(str(e))
            raise self.BadFormatError(self.last_step)
        return self
Ejemplo n.º 3
0
def analyze(statement, length=80):
    """Analyze all statements and print visual results.

    Returns:
        bool: True if all statements are ok, otherwise False.
    """

    lines = scan_statements(statement)
    results = self_analyze(lines)
    Log.debug("Listing all statements...")
    for action in statement.get_actions():
        try:
            text = action.parse_description()
        except Exception:
            text = "no description"
        delimiter = "-" * (length - 4)
        c_txt = u"\033[96m %-{}s \033[0m".format(length - 6)
        r_txt = u"\033[91m %-{}s \033[0m".format(length - 6)
        g_txt = u"\033[92m %-{}s \033[0m".format(length - 6)
        print(u"\033[90m|---|%-{}s|\033[0m".format(length - 4) % delimiter)
        print(u"\033[90m|   |\033[0m" + fmt(c_txt, text) + "\033[90m|\033[0m")
        print(u"\033[90m|---|%-{}s|\033[0m".format(length - 4) % delimiter)
        for line in action.parse_statements():
            if lines[line] > 1:
                status = UnicodeIcon.INVALID
                line_text = fmt(r_txt, line.strip())
            else:
                status = UnicodeIcon.VALID
                line_text = fmt(g_txt, line.strip())
            print_line = u"\033[90m|\033[0m " + status + u" \033[90m|\033[0m"
            print_line += line_text + u"\033[90m|\033[0m"
            print(print_line)
    print(u"\033[90m|---|%-{}s|\033[0m".format(length - 4) % delimiter)
    if not results:
        Log.error("Duplicated statements found!")
        for line, times in lines.iteritems():
            if times > 1:
                err = (times, line.strip())
                Log.error(" - line duplicated %d times: %s" % err)
        Log.error("Please correct these problems before running again.")
    else:
        Log.debug("Everything looks OK!")
    return results