Esempio n. 1
0
    def __init__(self, bracket):
        """Setup the bracket object by reading the passed in dictionary."""

        self.style = bracket.get("style", BH_STYLE)
        self.open = bre.compile_search("\\A" + bracket.get("open", ""), bre.MULTILINE | bre.IGNORECASE)
        self.close = bre.compile_search(bracket.get("close", "") + "\\Z", bre.MULTILINE | bre.IGNORECASE)
        self.name = bracket["name"]
        sub_search = bracket.get("sub_bracket_search", BH_SUB_BRACKET)
        self.sub_search_only = sub_search == "only"
        self.sub_search = self.sub_search_only is True or sub_search == "true"
        self.compare = bracket.get("compare", BH_COMPARE_MATCH)
        self.post_match = bracket.get("post_match", BH_POST_MATCH)
        self.validate = bracket.get("validate", BH_VALIDATE_MATCH)
        self.scopes = bracket["scopes"]
        self.highlighting = bracket.get("highlighting", BH_HIGHLIGHTING)
        self.enabled = True
        if self.open.groups != 1 or self.close.groups != 1:
            self.enabled = False
Esempio n. 2
0
    def __init__(self, bracket):
        """Setup the bracket object by reading the passed in dictionary."""

        self.style = bracket.get("style", BH_STYLE)
        self.open = bre.compile_search("\\A" + bracket.get("open", ""),
                                       bre.MULTILINE | bre.IGNORECASE)
        self.close = bre.compile_search(
            bracket.get("close", "") + "\\Z", bre.MULTILINE | bre.IGNORECASE)
        self.name = bracket["name"]
        sub_search = bracket.get("sub_bracket_search", BH_SUB_BRACKET)
        self.sub_search_only = sub_search == "only"
        self.sub_search = self.sub_search_only is True or sub_search == "true"
        self.compare = bracket.get("compare", BH_COMPARE_MATCH)
        self.post_match = bracket.get("post_match", BH_POST_MATCH)
        self.validate = bracket.get("validate", BH_VALIDATE_MATCH)
        self.scopes = bracket["scopes"]
        self.highlighting = bracket.get("highlighting", BH_HIGHLIGHTING)
        self.enabled = True
        if self.open.groups != 1 or self.close.groups != 1:
            self.enabled = False
Esempio n. 3
0
    def parse_bracket_definition(self, language, loaded_modules):
        """Parse the bracket defintion."""

        names = []
        subnames = []
        find_regex = []
        sub_find_regex = []
        self.sub_pattern = None
        self.pattern = None

        for params in self.bracket_rules:
            if is_valid_definition(params, language):
                try:
                    bh_plugin.load_modules(params, loaded_modules)
                    entry = BracketDefinition(params)
                    if not self.check_compare and entry.compare is not None:
                        self.check_compare = True
                    if not self.check_validate and entry.validate is not None:
                        self.check_validate = True
                    if not self.check_post_match and entry.post_match is not None:
                        self.check_post_match = True
                    if not self.highlighting and entry.highlighting is not None:
                        self.highlighting = True
                    self.brackets.append(entry)
                    if not entry.find_in_sub_search_only:
                        find_regex.append(params["open"])
                        find_regex.append(params["close"])
                        names.append(params["name"])
                    else:
                        find_regex.append(r"([^\s\S])")
                        find_regex.append(r"([^\s\S])")

                    if entry.find_in_sub_search:
                        sub_find_regex.append(params["open"])
                        sub_find_regex.append(params["close"])
                        subnames.append(params["name"])
                    else:
                        sub_find_regex.append(r"([^\s\S])")
                        sub_find_regex.append(r"([^\s\S])")
                except Exception as e:
                    log(e)

        if len(self.brackets):
            self.brackets = tuple(self.brackets)
            debug("Bracket Pattern: (%s)\n" % ','.join(names) +
                  "    (Opening|Closing):     (?:%s)\n" % '|'.join(find_regex))
            debug("SubBracket Pattern: (%s)\n" % ','.join(subnames) +
                  "    (Opening|Closing): (?:%s)\n" % '|'.join(sub_find_regex))
            self.sub_pattern = bre.compile_search(
                "(?:%s)" % '|'.join(sub_find_regex),
                bre.MULTILINE | bre.IGNORECASE)
            self.pattern = bre.compile_search("(?:%s)" % '|'.join(find_regex),
                                              bre.MULTILINE | bre.IGNORECASE)
            if (self.sub_pattern.groups != len(sub_find_regex)
                    or self.pattern.groups != len(find_regex)):
                if self.sub_pattern.groups != len(sub_find_regex):
                    log(BRACKET_ERROR % (len(sub_find_regex), 'sub-pattern',
                                         self.sub_pattern.groups,
                                         "(?:%s)" % '|'.join(sub_find_regex)))
                if self.pattern.groups != len(find_regex):
                    log(BRACKET_ERROR %
                        (len(find_regex), 'pattern', self.pattern.groups,
                         "(?:%s)" % '|'.join(find_regex)))
                self.brackets = []
                self.sub_pattern = None
                self.pattern = None
Esempio n. 4
0
 def parse_bracket_definition(self, language, loaded_modules):
     names = []
     subnames = []
     find_regex = []
     sub_find_regex = []
     self.sub_pattern = None
     self.pattern = None
     for params in self.bracket_rules:
         if is_valid_definition(params, language):
             try:
                 bh_plugin.load_modules(params, loaded_modules)
                 entry = BracketDefinition(params)
                 if not self.check_compare and entry.compare is not None:
                     self.check_compare = True
                 if not self.check_validate and entry.validate is not None:
                     self.check_validate = True
                 if not self.check_post_match and entry.post_match is not None:
                     self.check_post_match = True
                 if not self.highlighting and entry.highlighting is not None:
                     self.highlighting = True
                 self.brackets.append(entry)
                 if not entry.find_in_sub_search_only:
                     find_regex.append(params["open"])
                     find_regex.append(params["close"])
                     names.append(params["name"])
                 else:
                     find_regex.append(r"([^\s\S])")
                     find_regex.append(r"([^\s\S])")
                 if entry.find_in_sub_search:
                     sub_find_regex.append(params["open"])
                     sub_find_regex.append(params["close"])
                     subnames.append(params["name"])
                 else:
                     sub_find_regex.append(r"([^\s\S])")
                     sub_find_regex.append(r"([^\s\S])")
             except Exception as e:
                 log(e)
     if len(self.brackets):
         self.brackets = tuple(self.brackets)
         self.sub_pattern = bre.compile_search("(?:%s)" % '|'.join(sub_find_regex), bre.MULTILINE | bre.IGNORECASE)
         self.pattern = bre.compile_search("(?:%s)" % '|'.join(find_regex), bre.MULTILINE | bre.IGNORECASE)
         if (
             self.sub_pattern.groups != len(sub_find_regex) or
             self.pattern.groups != len(find_regex)
         ):
             if self.sub_pattern.groups != len(sub_find_regex):
                 log(
                     BRACKET_ERROR % (
                         len(sub_find_regex),
                         'sub-pattern',
                         self.sub_pattern.groups,
                         "(?:%s)" % '|'.join(sub_find_regex)
                     )
                 )
             if self.pattern.groups != len(find_regex):
                 log(
                     BRACKET_ERROR % (
                         len(find_regex),
                         'pattern',
                         self.pattern.groups,
                         "(?:%s)" % '|'.join(find_regex)
                     )
                 )
             self.brackets = []
             self.sub_pattern = None
             self.pattern = None