def __init__(self, hkey=None, client=None, hostNum=None): self.hkey = hkey self.skey = checksum(str(random.random()))[:8] self.client = client or AnkiRequestsClient() self.postVars = {} self.hostNum = hostNum self.prefix = "sync/"
def addFilesFromZip(self, zipData): "Extract zip data; true if finished." f = io.BytesIO(zipData) z = zipfile.ZipFile(f, "r") media = [] # get meta info first meta = json.loads(z.read("_meta").decode("utf8")) # then loop through all files cnt = 0 for i in z.infolist(): if i.filename == "_meta": # ignore previously-retrieved meta continue else: data = z.read(i) csum = checksum(data) name = meta[i.filename] # normalize name name = unicodedata.normalize("NFC", name) # save file with open(name, "wb") as f: f.write(data) # update db media.append((name, csum, self._mtime(name), 0)) cnt += 1 if media: self.db.executemany( "insert or replace into media values (?,?,?,?)", media) return cnt
def scmhash(self, m): "Return a hash of the schema, to see if models are compatible." s = "" for f in m['flds']: s += f['name'] for t in m['tmpls']: s += t['name'] return checksum(s)
def writeData(self, opath, data, typeHint=None): # if fname is a full path, use only the basename fname = os.path.basename(opath) # if it's missing an extension and a type hint was provided, use that if not os.path.splitext(fname)[1] and typeHint: # mimetypes is returning '.jpe' even after calling .init(), so we'll do # it manually instead typeMap = { "image/jpeg": ".jpg", "image/png": ".png", } if typeHint in typeMap: fname += typeMap[typeHint] # make sure we write it in NFC form (pre-APFS Macs will autoconvert to NFD), # and return an NFC-encoded reference fname = unicodedata.normalize("NFC", fname) # ensure it's a valid filename base = self.cleanFilename(fname) (root, ext) = os.path.splitext(base) def repl(match): n = int(match.group(1)) return " (%d)" % (n+1) # find the first available name csum = checksum(data) while True: fname = root + ext path = os.path.join(self.dir(), fname) # if it doesn't exist, copy it directly if not os.path.exists(path): with open(path, "wb") as f: f.write(data) return fname # if it's identical, reuse with open(path, "rb") as f: if checksum(f.read()) == csum: return fname # otherwise, increment the index in the filename reg = r" \((\d+)\)$" if not re.search(reg, root): root = root + " (1)" else: root = re.sub(reg, repl, root)
def _imgLink(col, latex, model): "Return an img link for LATEX, creating if necesssary." txt = _latexFromHtml(col, latex) if model.get("latexsvg", False): ext = "svg" else: ext = "png" # is there an existing file? fname = "latex-%s.%s" % (checksum(txt.encode("utf8")), ext) link = '<img class=latex src="%s">' % fname if os.path.exists(fname): return link # building disabled? if not build: return "[latex]%s[/latex]" % latex err = _buildImg(col, txt, fname, model) if err: return err else: return link
def _checksum(self, path): with open(path, "rb") as f: return checksum(f.read())
def ensureNameUnique(self, m): for mcur in self.all(): if (mcur['name'] == m['name'] and mcur['id'] != m['id']): m['name'] += "-" + checksum(str(time.time()))[:5] break