Example #1
0
    def parse_line(self, line):
        line = str.strip(line)
        if line == "" or line[0] == "#":
            return

        i = line.find("set ")
        if i == 0:
            if not self.rules_started:
                return self.parse_variable_assignment(line[4:])
            else:
                common.error("Error: variable assignments after rules.")

        i = line.find(" matches ")

        if i != -1:
            bm = line[:i]
            if self.is_valid_bookmark(bm):
                self.parse_rule(bm, line[i + 9:])
                self.rules_started = True
            elif os.path.isdir(bm):
                common.debug("%s is a directory." % bm)
                self.parse_rule(os.path.realpath(bm), line[i + 9:])
                self.rules_started = True
            else:
                common.warning("Unknown tag '%s'. Ignoring rule: %s" % (bm, line))
        else:
            common.warning("Invalid syntax: %s" % line)
Example #2
0
    def match(self, fileOrDir, depth = 0):

        if os.path.isdir(fileOrDir):
            self.match_directory(fileOrDir, depth = depth)
        elif os.path.isfile(fileOrDir):
            self.match_file(fileOrDir, depth = depth)
        else:
            common.error("File not found '%s'" % fileOrDir)
Example #3
0
def get_bookmark(tag, bookmarks):
    """Returns the bookmark(s) fitting 'tag'""" 
    if str.isdigit(tag):
        t = int(tag)
        if 0 <= t < len(bookmarks):
            return [bookmarks[t]]
        else:
            common.error("Index out of bounds: %d" % (t))
    possible = []
    for t, dest in bookmarks:
        if t.startswith(tag):
            possible.append((t, dest))
    return possible
Example #4
0
 def parse_variable_assignment(self, line):
     s = self.re_variable.search(line)
     if s:
         g = s.groups()
         var = str.lower(g[0])
         common.debug("Setting %s to %s" % (g[0], g[1]))
         if var == "loglevel":
             common.loglevel = int(g[1])
         elif var == "bookmarks":
             self.set_bookmarks(g[1])
         self.variables[g[0]] = g[1]
     else:
         common.error("Couldn't parse variable assignment: %s" % line)
Example #5
0
    def parse_rule(self, bookmark, string):

        s = string
        weight,partition = self.get_weight(s)
        if partition >= 0:
            s = s[:partition]

        args = s.split()
        match_token =args[0].lower()

        found = False
        for p in self.parsers:
            if match_token in p.match_token:
                r = p.get_Rule(args[1:], bookmark, weight)
                if r is not None: 
                    self.rules.append(r)
                    common.debug("Added new %s rule on %s (%s)" % (r.match_token, r.bookmark, r.text))
                    found = True

        if not found:
            common.error("Couldn't parse rule: %s" % string)