Ejemplo n.º 1
0
    def __list(self, path, removed):
        folder= self.__client.metadata(path)
        if not folder["is_dir"]:
            raise RuntimeError("Path is not a folder")

        notes= {}
        pos= len(path) + 1
        for file in folder["contents"]:
            if not file["is_dir"]:
                name= self.__fileUuid(file["path"][pos:])
                if isUuid(name):
                    notes[name]= NoteStatus(name, getLastModified(file), removed)

        if not removed:
            folder= self.__client.metadata(self.__photosPath)
            if not folder["is_dir"]:
                raise RuntimeError("Path is not a folder")

            pos= len(self.__photosPath) + 1
            for file in folder["contents"]:
                name= file["path"][pos:]
                if not file["is_dir"] and name.endswith(".jpg"):
                    name= name[:-4]
                    if name in notes:
                        notes[name].hasPhoto= True

        return notes
Ejemplo n.º 2
0
    def sync(self):
        noteStatus = {}
        pageToken = None
        while True:
            _LOG.info("Listing all notes and photos")
            fields = "files(id,name,modifiedTime),nextPageToken"
            result = self._service().list(spaces=self.FOLDER, fields=fields, pageSize=1000, pageToken=pageToken)\
                .execute()
            pageToken = result.get("nextPageToken")

            for item in filter(
                    lambda i: i["name"].endswith(self.NOTE_EXTENSION),
                    result["files"]):
                name = item["name"][:-6]
                if isUuid(name):
                    ns = NoteStatus(name, self.__lastModified(item))
                    ns.noteId = item["id"]
                    noteStatus[name] = ns

            for item in filter(
                    lambda i: i["name"].endswith(self.PHOTO_EXTENSION),
                    result["files"]):
                name = item["name"][:-4]
                if name in noteStatus:
                    ns = noteStatus[name]
                    ns.hasPhoto = True
                    ns.photoId = item["id"]

            for item in filter(
                    lambda i: i["name"].endswith(self.REMOVED_NOTE_EXTENSION),
                    result["files"]):
                name = item["name"][:-8]
                if isUuid(name):
                    ns = NoteStatus(name, self.__lastModified(item), True)
                    ns.noteId = item["id"]
                    if name in noteStatus:
                        nso = noteStatus[name]
                        if ns.lastModified >= nso.lastModified:
                            _LOG.warning(
                                "Sync integrity check deleting note: %s", name)
                            self.__remove(nso.noteId)
                            if nso.hasPhoto:
                                _LOG.warning(
                                    "Sync integrity check deleting photo: %s",
                                    name)
                                self.__remove(nso.photoId)
                        else:
                            _LOG.warning(
                                "Sync integrity check deleting REMOVED note: %s",
                                name)
                            self.__remove(ns.noteId)
                            continue
                    noteStatus[name] = ns

            if not pageToken:
                break
        self.__noteStatusCache = noteStatus
        return noteStatus
Ejemplo n.º 3
0
    def sync(self):
        _LOG.info("Listing all notes and photos")
        folder = self.__client.files_list_folder(self.__basePath, recursive=True)
        files = list(filter(lambda e: e is not None, map(FileEntry.fromMetadata, folder.entries)))
        while folder.has_more:
            folder = self.__client.files_list_folder_continue(folder.cursor)
            files.extend(filter(lambda e: e is not None, map(FileEntry.fromMetadata, folder.entries)))

        notes = {}
        for file in filter(lambda f: f.folder == self.__notesPath and isUuid(self.__normalizeNoteName(f.name)), files):
            uuid = self.__normalizeNoteName(file.name)
            notes[uuid] = NoteStatus(uuid, file.lastModified)

        for file in filter(lambda f: f.folder == self.__photosPath and f.name.endswith(".jpg"), files):
            uuid = file.name[:-4]
            if uuid in notes:
                notes[uuid].hasPhoto = True

        for file in filter(lambda f: f.folder == self.__removedNotesPath and isUuid(self.__normalizeNoteName(f.name)),
                           files):
            uuid = self.__normalizeNoteName(file.name)
            if uuid in notes:
                if file.lastModified >= notes[uuid].lastModified:
                    _LOG.warning("Sync integrity check deleting note: %s", uuid)
                    try:
                        self.__client.files_delete(self.__notePath(uuid))
                    except ApiError:
                        _LOG.warning("Note %s not found", uuid)
                    if notes[uuid].hasPhoto:
                        _LOG.warning("Sync integrity check deleting photo: %s", uuid)
                        try:
                            self.__client.files_delete(self.__photoPath(uuid))
                        except ApiError:
                            _LOG.warning("Photo %s not found", uuid)
                    del notes[uuid]
                else:
                    _LOG.warning("Sync integrity check deleting REMOVED note: %s", uuid)
                    try:
                        self.__client.files_delete(self.__removedNotePath(uuid))
                    except ApiError:
                        _LOG.warning("REMOVED note %s not found", uuid)
                    continue
            notes[uuid] = NoteStatus(uuid, file.lastModified, True)

        self.__notesCache = notes
        return notes
Ejemplo n.º 4
0
 def sync(self):
     noteStatuses = {}
     for row in self.__connection.execute(
             "SELECT uuid, lastModified, active FROM Note;"):
         noteStatuses[row[0]] = NoteStatus(row[0], row[1], not row[2])
     for name in listdir(self.__photosPath):
         if name.endswith(".jpg"):
             name = name[:-4]
             if name in noteStatuses:
                 noteStatuses[name].hasPhoto = True
     return noteStatuses
Ejemplo n.º 5
0
 def sync(self):
     notes = {}
     for name in listdir(self.__notesPath):
         if isUuid(name):
             hasPhoto = isfile(self.__photoPath(name))
             notes[name] = NoteStatus(name,
                                      getModifiedOn(self.__notePath(name)),
                                      hasPhoto=hasPhoto)
     for name in listdir(self.__removedNotesPath):
         if isUuid(name):
             removedNote = NoteStatus(
                 name, getModifiedOn(self.__removedNotePath(name)), True)
             # Clean inconsistent files
             if name in notes:
                 if removedNote.lastModified >= notes[name].lastModified:
                     remove(self.__notePath(name))
                     if notes[name].hasPhoto:
                         remove(self.__photoPath(name))
                 else:
                     remove(self.__removedNotePath(name))
                     continue
             notes[name] = removedNote
     return notes
Ejemplo n.º 6
0
Archivo: gdrive.py Proyecto: vijo/sqink
    def sync(self):
        noteStatus= {}
        pageToken= None
        while True:
            result= self._service().list(q="'%s' in parents" % self.__folderId, maxResults=1000, pageToken=pageToken,
                    fields="items(id,title,modifiedDate),nextPageToken").execute()
            pageToken= result.get("nextPageToken")

            for item in filter(lambda i: i["title"].endswith(self.NOTE_EXTENSION), result["items"]):
                title= item["title"][:-6]
                if isUuid(title):
                    ns= NoteStatus(title, self.__lastModified(item))
                    ns.noteId= item["id"]
                    noteStatus[title]= ns

            for item in filter(lambda i: i["title"].endswith(self.PHOTO_EXTENSION), result["items"]):
                title= item["title"][:-4]
                if title in noteStatus:
                    ns= noteStatus[title]
                    ns.hasPhoto= True
                    ns.photoId= item["id"]

            for item in filter(lambda i: i["title"].endswith(self.REMOVED_NOTE_EXTENSION), result["items"]):
                title= item["title"][:-8]
                if isUuid(title):
                    ns= NoteStatus(title, self.__lastModified(item), True)
                    ns.noteId= item["id"]
                    if title in noteStatus:
                        nso= noteStatus[title]
                        if ns.lastModified >= nso.lastModified:
                            self.__remove(nso.noteId)
                            if nso.hasPhoto:
                                self.__remove(nso.photoId)
                        else:
                            self.__remove(ns.noteId)
                            continue
                    noteStatus[title]= ns

            if not pageToken:
                break
        self.__noteStatusCache= noteStatus
        return noteStatus