Ejemplo n.º 1
0
    def castToWrite(self, in_value):

        if isinstance(in_value, datetime):

            value = toTimestamp(in_value, self.__class__.timeZone)

        elif isinstance(in_value, (int, long)):

            if not in_value:
                raise ValueError("Bad value for {}.{}: {}.".format(self._metaobj, self.name, in_value))

            try:
                datetime.fromtimestamp(in_value)
            except:
                raise
            else:
                value = in_value
        else:
            sAllowedTypes = tuple(t.__name__ for t in (datetime, int, long))
            raise TypeError(
                "Got {} for {}.{}. Expected {}".format(type(in_value), self._metaobj, self.name, sAllowedTypes)
            )

        # MongoDb timestamps are expressed in milliseconds.
        return value * 1000
Ejemplo n.º 2
0
def report(foundList, errorList, sDbPath=""):

    def _u(v):
        return unicode_(v) if isinstance(v, basestring) else v

    numNodes = len(foundList)
    foundList = sorted((e for e in foundList if e), key=lambda e:e.dbMtime)
    table = []
    for rcEntry in foundList:
        try:
            dbnode = rcEntry._dbnode
            sOnlineTime = strftime(long(dbnode.synced_online) / 1000) if dbnode.synced_online else ""

            texts = map(_u, (rcEntry.dbPath(),
                             rcEntry.author,
                             rcEntry.origin,
                             strftime(toTimestamp(rcEntry.dbMtime)),
                             cmpStr(rcEntry.dbMtime, rcEntry.fsMtime),
                             strftime(toTimestamp(rcEntry.fsMtime)),
                             repr(rcEntry.fileSize),
                             cmpStr(rcEntry.fileSize, rcEntry.sourceSize),
                             repr(rcEntry.sourceSize),
                             sOnlineTime,
                             ))
        except Exception as e:
            print toStr(e), ":", rcEntry.absPath()
            continue

        table.append(texts)

    headers = ["file", "author", "site", "published", "", "modified",
               "current size", "", "source size", "synced online"]
    print tabulate(table, headers, tablefmt="simple")
    print len(foundList), "bad on", numNodes, "files - scan errors:", len(errorList)

    sFileName = sDbPath.strip("/").replace("/", "_") + "_report.html"
    sHtmlPath = pathResolve("%USERPROFILE%/Documents/{}".format(sFileName))

    sCharset = '<head>\n<meta charset="UTF-8">\n</head>\n'
    with codecs.open(sHtmlPath, "w", "utf_8") as fo:
        fo.writelines(sCharset + tabulate(table, headers, tablefmt="html"))

    for rcEntry, e in errorList:
        print rcEntry.dbPath(), type(e), e.message, toDisplayText(rcEntry.dbMtime)