def cmd_remove(self, args): if len(args) != 1: raise ArgumentError( "git tessera remove takes identifier as argument") key = args[0] tessera_file = None tessera_path = None for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: tessera_file = "%s/tessera" % tessera_path break if not tessera_file: raise TesseraError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) stdout.write("remove tessera %s: %s ? [Y/n] " % (key, t.get_attribute("title"))) try: answer = stdin.readline().strip() except KeyboardInterrupt: return False if not answer or answer.lower() == "y": files = [ "%s/%s" % (tessera_path, x) for x in os.listdir(tessera_path) ] self.git.rm(files, "tessera removed: %s" % t.get_attribute("title")) from shutil import rmtree rmtree(tessera_path)
def cmd_remove(self, args): if len(args) != 1: raise ArgumentError("git tessera remove takes identifier as argument") key = args[0] tessera_file = None tessera_path = None for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: tessera_file = "%s/tessera" % tessera_path break if not tessera_file: raise TesseraError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) stdout.write("remove tessera %s: %s ? [Y/n] " % (key, t.get_attribute("title"))) try: answer = stdin.readline().strip() except KeyboardInterrupt: return False if not answer or answer.lower() == "y": files = ["%s/%s" % (tessera_path, x) for x in os.listdir(tessera_path)] self.git.rm(files, "tessera removed: %s" % t.get_attribute("title")) from shutil import rmtree rmtree(tessera_path)
def cmd_tag(self, args): if len(args) != 2: raise ArgumentError( "git tessera show takes identifier as argument and new tag") key = args[0] for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: break if not tessera_path: raise ArgumentError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) t.add_tag(args[1]) files = [ os.path.join(t.tessera_path, "tessera"), os.path.join(t.tessera_path, "info") ] self.git.add( files, "tessera updated: add tag %s to %s" % (args[1], t.get_attribute("title"))) return True
def ls(self, args=[]): if not os.path.exists(self.tesserae): return False try: idx = args.index("--sort") sortfunc = GitTessera.SORTING[args[idx + 1]] except ValueError: sortfunc = GitTessera.SORTING["status"] except IndexError: colorful.out.bold_red("Please specify a sort algorithm. Available: %s" % (", ".join(GitTessera.SORTING.keys()))) return [] except KeyError: colorful.out.bold_red("No sort algorithm for '%s' available" % args[idx + 1]) return [] try: idx = args.index("--tags") tags = args[idx + 1].split(",") except ValueError: tags = None except IndexError: colorful.out.bold_red("Please specify minimum one tag.") return [] contents = [self.tesserae + "/" + x for x in os.listdir(self.tesserae) if stat.S_ISDIR(os.lstat(self.tesserae + "/" + x).st_mode)] tesserae = [] for tessera_path in contents: t = Tessera(tessera_path) te_tags = t.get_attribute("tags") if not tags or any(x in te_tags for x in tags): tesserae.append(t) tesserae = sorted(tesserae, cmp=sortfunc) return tesserae
def cmd_create(self, args): if len(args) < 1: stderr.write("git tessera create needs arguments\n") return False #if self.git.is_dirty(): # stderr.write("repo is dirty\n") # return False if args: title = " ".join(args) else: title = "tessera title goes here" uuid = uuid1() tessera_path = "%s/%s" % (Tessera._tesserae, uuid) tessera_file = "%s/tessera" % tessera_path os.mkdir(tessera_path) fin = open("%s/template" % Tessera._tesserae, "r") fout = open(tessera_file, "w") for line in fin.readlines(): if line == "@title@\n": line = "# %s\n" % title fout.write(line) fin.close() fout.close() p = Popen(["sensible-editor", tessera_file]) p.communicate() p.wait() t = Tessera(tessera_path) self.git_add(tessera_file, "tessera created: %s" % t.get_attribute("title")) return True
def cmd_edit(self, args): if len(args) < 1: raise ArgumentError( "git tessera edit takes one or more identifier as argument") tessera_paths = [] for key in args: tessera_path = None found = False for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: found = True break if not found: raise TesseraError("git tessera %s not found" % key) tessera_paths.append(tessera_path) while True: tessera_files = ["%s/tessera" % x for x in tessera_paths] _edit(tessera_files, self._config) # if self.git.is_dirty(): failed = [] while tessera_paths: tessera_path = tessera_paths.pop() t = Tessera(tessera_path, self._config) if not t.error: t._write_info() files = [ "%s/tessera" % tessera_path, "%s/info" % tessera_path ] self.git.add( files, "tessera updated: %s" % t.get_attribute("title")) continue # failed parsing failed.append(tessera_path) if failed: stdout.write("Abort ? [y/N] ") try: answer = stdin.readline().strip() except KeyboardInterrupt: break if answer and answer.lower() == "y": break tessera_paths = failed else: break return True
def ls(self, args=[]): if not os.path.exists(self.tesserae): return False try: idx = args.index("--sort") sortfunc = GitTessera.SORTING[args[idx + 1]] except ValueError: sortfunc = GitTessera.SORTING["status"] except IndexError: raise ArgumentError( "Please specify a sort algorithm. Available: %s" % (", ".join(GitTessera.SORTING.keys()))) except KeyError: raise ArgumentError("No sort algorithm for '%s' available" % args[idx + 1]) try: idx = args.index("--tags") tags = args[idx + 1].split(",") except ValueError: tags = None except IndexError: raise ArgumentError("Please specify minimum one tag.") contents = [ self.tesserae + "/" + x for x in os.listdir(self.tesserae) if stat.S_ISDIR(os.lstat(self.tesserae + "/" + x).st_mode) ] tesserae = [] for tessera_path in contents: t = Tessera(tessera_path, self._config) if t.get_attribute("updated") == 0: tessera_info = "%s/info" % tessera_path fout = open(tessera_info, "w") author, author_time = self.git.read_author(tessera_path) import re r = re.compile("^([^\<]+) \<([^\>]+)\>$") m = r.search(author) if m: fout.write("author: %s\n" % m.group(1)) fout.write("email: %s\n" % m.group(2)) fout.write("updated: %d\n" % author_time) fout.close() te_tags = t.get_attribute("tags") if not tags or any(x in te_tags for x in tags): tesserae.append(t) tesserae = sorted(tesserae, cmp=sortfunc) return tesserae
def remove(self, tessera_id): """ Removes a tessera by it's id. """ tessera = Tessera(tessera_id, os.path.join(self.tesseraepath, tessera_id)) tessera.remove() if not self._git.rm_tessera(tessera): print("error: cannot remove tessera") return False print("Removed tessera with id '%s'" % tessera.id) return True
def cmd_edit(self, args): if len(args) < 1: raise ArgumentError("git tessera edit takes one or more identifier as argument") tessera_paths = [] for key in args: tessera_path = None found = False for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: found = True break if not found: raise TesseraError("git tessera %s not found" % key) tessera_paths.append(tessera_path) while True: tessera_files = ["%s/tessera" % x for x in tessera_paths] _edit(tessera_files, self._config) # if self.git.is_dirty(): failed = [] while tessera_paths: tessera_path = tessera_paths.pop() t = Tessera(tessera_path, self._config) if not t.error: t._write_info() files = [ "%s/tessera" % tessera_path, "%s/info" % tessera_path ] self.git.add( files, "tessera updated: %s" % t.get_attribute("title")) continue # failed parsing failed.append(tessera_path) if failed: stdout.write("Abort ? [y/N] ") try: answer = stdin.readline().strip() except KeyboardInterrupt: break if answer and answer.lower() == "y": break tessera_paths = failed else: break return True
def create(self, title="tessera title goes here"): """ create a new tessera with title {title}. @returns Tessera object of the new Tessera """ uuid = uuid1() tessera_path = os.path.join(Tessera._tesserae, str(uuid)) tessera_file = "%s/tessera" % tessera_path os.mkdir(tessera_path) fin = open(os.path.join(Tessera._tesserae, "template"), "r") fout = open(tessera_file, "w") for line in fin.readlines(): if line == "@title@\n": line = "# %s\n" % title fout.write(line) fin.close() fout.close() tessera_info = "%s/info" % tessera_path fout = open(tessera_info, "w") c = StackedConfig(StackedConfig.default_backends()) fout.write("author: %s\n" % c.get("user", "name")) fout.write("email: %s\n" % c.get("user", "email")) fout.write("updated: %d\n" % int(time())) fout.close() return Tessera(tessera_path, self._config)
def ls(self, args=[]): if not os.path.exists(self.tesserae): return False try: idx = args.index("--sort") sortfunc = GitTessera.SORTING[args[idx + 1]] except ValueError: sortfunc = GitTessera.SORTING["status"] except IndexError: raise ArgumentError("Please specify a sort algorithm. Available: %s" % (", ".join(GitTessera.SORTING.keys()))) except KeyError: raise ArgumentError("No sort algorithm for '%s' available" % args[idx + 1]) try: idx = args.index("--tags") tags = args[idx + 1].split(",") except ValueError: tags = None except IndexError: raise ArgumentError("Please specify minimum one tag.") contents = [self.tesserae + "/" + x for x in os.listdir(self.tesserae) if stat.S_ISDIR(os.lstat(self.tesserae + "/" + x).st_mode)] tesserae = [] for tessera_path in contents: t = Tessera(tessera_path, self._config) if t.get_attribute("updated") == 0: tessera_info = "%s/info" % tessera_path fout = open(tessera_info, "w") author, author_time = self.git.read_author(tessera_path) import re r = re.compile("^([^\<]+) \<([^\>]+)\>$") m = r.search( author ) if m: fout.write("author: %s\n" % m.group(1)) fout.write("email: %s\n" % m.group(2)) fout.write("updated: %d\n"%author_time) fout.close() te_tags = t.get_attribute("tags") if not tags or any(x in te_tags for x in tags): tesserae.append(t) tesserae = sorted(tesserae, cmp=sortfunc) return tesserae
def cmd_tag(self, args): if len(args) != 2: raise ArgumentError("git tessera show takes identifier as argument and new tag") key = args[0] for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: break if not tessera_path: raise ArgumentError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) t.add_tag(args[1]) self.git.add(t.filename, "tessera updated: add tag %s to %s" % (args[1], t.get_attribute("title"))) return True
def edit(self, tessera_id): """ Edits a tessera by it's id. """ tessera = Tessera(tessera_id, os.path.join(self.tesseraepath, tessera_id)) if not Editor.open(tessera.tessera_file, TesseraConfig(self._configpath)): print("error: cannot updated tessera") return False tessera.update() if not self._git.update_tessera(tessera): print("error: cannot commit updated tessera") return False print("Updated tessera with id %s" % tessera.id) return True
def get(self, key): for i in os.listdir(self.tesserae): tessera_path = os.path.join(self.tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: break if not tessera_path: raise TesseraError("git tessera %s not found" % key) return Tessera(tessera_path, self._config)
def create(self, title): """ Creates a new tessera. """ tessera = Tessera.create(self.tesseraepath, title) if not Editor.open(tessera.tessera_file, TesseraConfig(self._configpath)): tessera.remove() return False if not self._git.add_tessera(tessera): print("error: cannot commit new tessera") tessera.remove() return False print("Created new tessera with id %s" % tessera.id) return True