예제 #1
0
 def execute(self):
     for rec in sane_yaml.load_all(sys.stdin):
         for fn in self.ns.fieldname:
             if fn in rec:
                 del rec[fn]
         print("---")
         sys.stdout.write(sane_yaml.dump(rec))
예제 #2
0
파일: sort.py 프로젝트: gjenca/yacite
    def execute(self):

        l = list(sane_yaml.load_all(sys.stdin))
        l.sort(key=cmp_to_key(self.cmp_keys))
        for d in l:
            print("---")
            sys.stdout.write(sane_yaml.dump(d))
예제 #3
0
파일: execc.py 프로젝트: gjenca/yacite
 def execute(self):
     exceptions=0
     for i,rec in enumerate(sane_yaml.load_all(sys.stdin)):
         try:
             exec(self.ns.statement, self.mods,rec)
         except:
             if self.ns.failed:
                 print("---")
                 sys.stdout.write(sane_yaml.dump(rec))
             elif self.ns.keep_going:
                 exceptions+=1
                 print("exec: Warning: failed on %s" % describe_record(i,rec), file=sys.stderr)
                 print("exec: The exception was %s" % sys.exc_info()[0], file=sys.stderr)
             else:
                 raise
         if not self.ns.no_output and not self.ns.failed:
             print("---")
             sys.stdout.write(sane_yaml.dump(rec))
     if exceptions and not self.ns.failed:
         print("exec: Warning: there were %d exceptions" % exceptions, file=sys.stderr)
예제 #4
0
파일: unappend.py 프로젝트: gjenca/yacite
 def execute(self):
     ustrings = set(self.ns.string)
     for i, rec in enumerate(sane_yaml.load_all(sys.stdin)):
         if self.ns.fieldname in rec:
             if type(rec[self.ns.fieldname]) is not list:
                 raise DataError(
                     "unappend: expecting list under fieldname %s in %s, got %s instead"
                     % (self.ns.fieldname, describe_record(
                         i, rec), type(rec[self.ns.fieldname])))
             rec[self.ns.fieldname] = list(
                 set(rec[self.ns.fieldname]) - ustrings)
         print("---")
         sys.stdout.write(sane_yaml.dump(rec))
예제 #5
0
파일: append.py 프로젝트: gjenca/yacite
 def execute(self):
     for i,rec in enumerate(sane_yaml.load_all(sys.stdin)):
         if self.ns.fieldname in rec:
             value=rec[self.ns.fieldname]
             if type(value) is not list:
                 raise DataError("append: expecting a list under %s in %s, got %s instead" %
                     (self.ns.fieldname,describe_record(i,rec),type(value)))
             value.extend(self.ns.string)
             value=list(set(value))
             rec[self.ns.fieldname]=value
         else:
             rec[self.ns.fieldname]=[s for s in self.ns.string]
         print("---")
         sys.stdout.write(sane_yaml.dump(rec))
예제 #6
0
 def execute(self):
     everything = [BibRecord(d) for d in sane_yaml.load_all(sys.stdin)]
     if any("key" not in rec for rec in everything):
         print("checkdups: All records MUST have a key field",
               file=sys.stderr)
     dupkeys = []
     for rec1 in everything:
         for rec2 in everything:
             if rec1["key"] <= rec2["key"]:
                 continue
             if all(
                     strongly_similar(rec1, rec2, fn)
                     for fn in self.ns.fieldname):
                 dupkeys.append([rec1["key"], rec2["key"]])
     sys.stdout.write(sane_yaml.dump(dupkeys))
예제 #7
0
 def execute(self):
     for i,rec in enumerate(sane_yaml.load_all(sys.stdin)):
         if self.ns.fieldname in rec:
             if type(rec[self.ns.fieldname]) in (str,str):
                 for pat,repl_with in self.rules: 
                     rec[self.ns.fieldname]=pat.sub(repl_with,rec[self.ns.fieldname])
             elif type(rec[self.ns.fieldname]) is not list:
                 raise DataError("rewrite: %s in %s is %s, expected a string or list" % 
                     (self.ns.fieldname,describe_record(i,rec),type(rec[self.ns.fieldname])))
             for pat,repl_with in self.rules:
                 new=[]
                 for s in rec[self.ns.fieldname]:
                     new.append(pat.sub(repl_with,s))
                 rec[self.ns.fieldname]=new
         print("---")
         sys.stdout.write(sane_yaml.dump(rec))
예제 #8
0
 def save(self):
     if not self.dirty:
         return
     if "year" not in self:
         self["year"] = "NOYEAR"
     if "title" not in self:
         self["title"] = "NOTITLE"
     if not "key" in self:
         self["key"] = makekey(self, self.datadir)
     if self.path is None:
         if self.datadir is None:
             raise SaveError("Cannot save: no path and no datadir given")
         if ("myown" in self) and self["myown"]:
             pathdir = "%s/myown/%s/" % (self.datadir.dirname, self["year"])
         else:
             pathdir = "%s/%s/" % (self.datadir.dirname, self["year"])
         mkdir_p(pathdir)
         self.path = pathdir + ("%s.yaml" % self["key"])
     f = tempfile.NamedTemporaryFile(delete=False, mode="w")
     f.write(sane_yaml.dump(dict(self)))
     f.close()
     shutil.move(f.name, self.path)
예제 #9
0
파일: read.py 프로젝트: gjenca/yacite
 def execute(self):
     dd = Datadir(self.ns.datadir)
     for rec in dd:
         print("---")
         sys.stdout.write(sane_yaml.dump(dict(rec)))
예제 #10
0
 def execute(self):
     for i, rec in enumerate(sane_yaml.load_all(sys.stdin)):
         rec[self.ns.fieldname] = eval(self.ns.value, {})
         print("---")
         sys.stdout.write(sane_yaml.dump(rec))