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)
def exec_unmatched(self, file): e = str.strip(self.variables["exec_unmatched"]) if e != "": e = common.replace_variables(e.replace("%file%", os.path.realpath(file).replace("'", "\'")), self.variables) common.info("Executing: %s" % e) subprocess.Popen(e, shell = True) else: common.debug("Nothing to execute for %s. exec_unmatched is empty" % file)
def get_dest_retained_structure(self, file, dest, depth, makedir = False): if depth > 0 and self.retain_structure(): dirs = file.split(os.sep)[-(depth + 1):] for d in dirs[:-1]: tmp = os.path.join(dest, d) if not os.path.exists(tmp): common.debug("Making sub directory in destination. %s." % tmp) if makedir: os.mkdir(tmp) dest = tmp dest = os.path.join(dest, dirs[-1]) return dest
def set_bookmarks(self, bookmarks): """Load bookmark file""" if bookmarks == "": if hasattr(self.variables, "bookmarks"): bookmarks = self.variables["bookmarks"] else: bookmarks = pybookmark.get_conf_location() common.debug("Reading bookmarks from %s" % bookmarks) self.bookmarks = pybookmark.read_bookmarks(bookmarks) self.variables["bookmarks"] = bookmarks
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)
def get_weighted_table(self, file): """Returns sorted [(bookmark, weight)] list. Best match first.""" common.debug("Weighing %s" % file) res = {} for r in self.rules: w = res.get(r.bookmark, 0.0) dw = r.weigh(file, self.variables) res[r.bookmark] = w + dw if dw > 0.0: common.info("Matched %s on %s using %s rule (%s)" % (file, r.bookmark, r.match_token, r.text)) return filter(lambda a: a[1] >= float(self.variables["threshold"]), sorted(res.iteritems(), key=lambda a: a[1], reverse=True))
def _exec_help(self, key, file, match, depth = 0): bms = pybookmark.get_bookmark(match[0], self.bookmarks) if len(bms) != 1 and os.path.isdir(match[0]): bms = [(match[0], match[0])] if len(bms) == 1: x = bms[0] e = self.variables[key] if e != "": file = os.path.realpath(file) dest = self.get_dest_retained_structure(file, os.path.realpath(x[1]), depth, makedir = False) e = e.replace("%file%", pipes.quote(file)) e = e.replace("%bookmark%", pipes.quote(x[0])) e = e.replace("%match%", pipes.quote(dest)) e = common.replace_variables(e, self.variables) common.info("Executing: %s" % e) if self.interactive: t = common.theme no = common.NOCOLOR print print ( "=" * 80) print print ("%s%s%s %s===>%s %s%s%s" % (t[2], file, no, t[1], no, t[3], dest, no)) print ("%sMatched on %s%s" % (t[1], x[0], no)) print ("%sThe following command is about to be executed%s" % (t[1], no)) print print (" ", e) print proceed= raw_input("Proceed? [Y/n/q] ").lower() if proceed == "": proceed = "y" print if proceed[0] == "n": return elif proceed[0] == 'q': os.sys.exit(0) dest = self.get_dest_retained_structure(file, os.path.realpath(x[1]), depth, makedir =True) subprocess.Popen(e, shell = True) else: common.debug("Nothing to execute for %s. %s is empty." % (file, key)) else: common.warning("Bookmark matches too many directories. Can't move file.")
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)
def logged_match(m, file, variables): common.debug("Testing %s against %s rule (%s)" % (file, self.match_token[0], " ".join(slist))) return m(file, variables)
def match_recursively(self, dir, depth = 0): common.debug("Recursing on directory %s" % dir) map(lambda a: self.match(os.path.join(dir, a), depth = depth + 1), os.listdir(dir))
def parse_file(self, file): common.debug("Parsing configuration file %s" % file) f = open(file, "r") c = f.read() f.close() return self.parse(c)