예제 #1
0
    def _get_note_from_ipod(self, uid):
        """
        Gets a note from the ipod, If the pickled shadow copy exists
        then return that
        """
        rawNoteURI = os.path.join(self._get_shadow_dir(),uid)
        if os.path.exists(rawNoteURI):
            raw = open(rawNoteURI,'rb')
            try:
                n = pickle.load(raw)
                raw.close()
                return n
            except:
                raw.close()

        noteURI = os.path.join(self.dataDir, uid)
        noteFile = File.File(URI=noteURI)
        #get the contents from the note, get the raw from the raw copy.
        #the UID for notes from the ipod is the filename
        n = Note.Note(
                    title=uid,
                    contents=noteFile.get_contents_as_text().replace(
                        self.ENCODING_DECLARATION, '', 1),
                    )
        n.set_UID(uid)
        n.set_mtime(noteFile.get_mtime())
        n.set_open_URI(noteURI)
        return n
예제 #2
0
def save_removable_volume_group_file(folderUri, folderGroupName):
    """
    Saves a file on the root of the drive, in ini format, 
    containing the uri and group
    
    e.g.
    [DEFAULT]
    relative/uri/from/volume/root = group name
    """
    f = File.File(URI=folderUri)
    if f.is_on_removale_volume():
        #write to the /volume/root/.conduit file
        rooturi, path = get_removable_volume_info(f)
        if rooturi and path:
            conf = ConfigParser.SafeConfigParser()
            conf.read(path)

            log.debug("Saving group (%s = %s) to %s" %
                      (folderUri, folderGroupName, path))
            conf.set("DEFAULT", folderUri.replace(rooturi, ""),
                     folderGroupName)
            fp = open(path, 'w')
            conf.write(fp)
            fp.close()
            return True
    return False
예제 #3
0
    def refresh(self):
        DataProvider.DataSource.refresh(self)
        self.db.execute("DELETE FROM files")
        #Make a whole bunch of threads to go and scan the directories
        for oid, uri, groupname in self.db.select(
                "SELECT oid,URI,GROUP_NAME FROM config WHERE TYPE = ?",
            (TYPE_FOLDER, )):
            self.make_thread(
                uri,
                False,  #include hidden
                False,  #follow symlinks
                self._on_scan_folder_progress,
                self._on_scan_folder_completed,
                oid,
                groupname)

        #All threads must complete - otherwise we might miss some items
        self.join_all_threads()

        #now add the single files to the list
        for oid, uri in self.db.select(
                "SELECT oid,URI FROM config WHERE TYPE = ?", (TYPE_FILE, )):
            f = File.File(URI=uri)
            if f.exists():
                self.db.insert(
                    table="files",
                    values=(uri, "", ""
                            )  #single files dont have basepath and groupname
                )
            else:
                self.db.delete(table="config", oid=oid)
예제 #4
0
 def get(self, LUID):
     DataProvider.DataSource.get(self, LUID)
     basepath, group = self.db.select_one(
         "SELECT BASEPATH,GROUPNAME FROM files WHERE URI = ?", (LUID, ))
     f = File.File(URI=LUID, basepath=basepath, group=group)
     f.set_open_URI(LUID)
     f.set_UID(LUID)
     return f
예제 #5
0
 def refresh(self):
     d = File.File(URI=self.folder)
     if not d.exists():
         try:
             d.make_directory_and_parents()
         except:
             raise Exceptions.RefreshError("Error Creating Directory")
     FileDataProvider.FolderTwoWay.refresh(self)
예제 #6
0
 def get(self, uid):
     DataProvider.TwoWay.get(self, uid)
     f = File.File(URI=uid,
                   basepath=self.folder,
                   group=self.folderGroupName)
     f.set_open_URI(uid)
     f.set_UID(uid)
     return f
예제 #7
0
    def get(self, LUID):
        DataProvider.TwoWay.get(self, LUID)
        fullpath = os.path.join(self.dataDir, LUID)
        f = File.File(URI=fullpath)

        event = Event.Event()
        event.set_from_ical_string(f.get_contents_as_text())
        event.set_open_URI(fullpath)
        event.set_mtime(f.get_mtime())
        event.set_UID(LUID)
        return event
예제 #8
0
    def get(self, LUID):
        DataProvider.TwoWay.get(self, LUID)
        fullpath = os.path.join(self.dataDir, LUID)
        f = File.File(URI=fullpath)

        contact = Contact.Contact()
        contact.set_from_vcard_string(f.get_contents_as_text())
        contact.set_open_URI(fullpath)
        contact.set_mtime(f.get_mtime())
        contact.set_UID(LUID)
        return contact
예제 #9
0
 def bookmark_to_desktop(self, bookmark):
     # Creates .desktop file
     desktop = "[Desktop Entry]\n"
     desktop += "Version=1.0\n"
     desktop += "Encoding=UTF-8\n"
     desktop += "Name=%s\n" % bookmark.get_title()
     desktop += "Type=Link\n"
     desktop += "URL=%s\n" % bookmark.get_uri()
     desktop += "Icon=gnome-fs-bookmark\n"
     f = File.TempFile(desktop)
     f.force_new_filename(self.get_bookmark_filename(bookmark) + ".desktop")
     return f
예제 #10
0
 def _get_proxyfile(self, key):
     """
     @param key: Key for which C{ProxyFile} should be returned.
     @type key: C{boto.s3.key.Key}
     @returns: C{ProxyFile} stored under supplied parameter C{key}.
     """
     httpurl = key.generate_url(AmazonS3TwoWay.AWS_URL_EXPIRE_SECONDS)
     # BUG This will fail with "Access denied"
     # (see http://bugzilla.gnome.org/show_bug.cgi?id=545000)
     return File.ProxyFile(
         httpurl, key.name,
         Utils.datetime_from_timestamp(long(key.get_metadata("mtime"))),
         long(key.get_metadata("size")))
예제 #11
0
def new_tempfile(contents, **kwargs):
    """
    Returns a new File onject, which has been created in the 
    system temporary directory, and that has been filled with
    contents
    
    The file is closed when it is returned
    
    @param contents: The data to write into the file
    @returns: a L{conduit.datatypes.File}
    """
    import conduit.datatypes.File as File
    return File.TempFile(contents, **kwargs)
예제 #12
0
 def bookmark_to_webloc(self, bookmark):
     # Create .webloc as used by FireFox on Mac OSX
     webloc = '<?xml version="1.0" encoding="UTF-8"?>\n'
     webloc += '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
     webloc += '<plist version="1.0">\n'
     webloc += '<dict>\n'
     webloc += '    <key>URL</key>\n'
     webloc += '    <string>%s</string>\n' % bookmark.get_uri()
     webloc += '</dict>\n'
     webloc += '</plist>\n'
     f = File.TempFile(webloc)
     f.force_new_filename(self.get_bookmark_filename(bookmark) + '.webloc')
     return f
예제 #13
0
    def put(self, photo, overwrite, LUID=None):
        """
        Accepts a vfs file. Must be made local.
        I also store a md5 of the photos uri to check for duplicates
        """
        DataProvider.DataSink.put(self, photo, overwrite, LUID)

        originalName = photo.get_filename()
        #Gets the local URI (/foo/bar). If this is a remote file then
        #it is first transferred to the local filesystem
        photoURI = photo.get_local_uri()
        mimeType = photo.get_mimetype()
        tags = photo.get_tags()
        caption = photo.get_caption()

        uploadInfo = UploadInfo(photoURI, mimeType, originalName, tags,
                                caption)

        #Check if we have already uploaded the photo
        if LUID != None:
            info = self._get_photo_info(LUID)
            #check if a photo exists at that UID
            if info != None:
                if overwrite == True:
                    #replace the photo
                    return self._replace_photo(LUID, uploadInfo)
                else:
                    #Only upload the photo if it is newer than the Remote one
                    url = self._get_raw_photo_url(info)
                    remoteFile = File.File(url)

                    #this is a limited test for equality type comparison
                    comp = photo.compare(remoteFile, True)
                    log.debug(
                        "Compared %s with %s to check if they are the same (size). Result = %s"
                        % (photo.get_filename(), remoteFile.get_filename(),
                           comp))
                    if comp != conduit.datatypes.COMPARISON_EQUAL:
                        raise Exceptions.SynchronizeConflictError(
                            comp, photo, remoteFile)
                    else:
                        return conduit.datatypes.Rid(uid=LUID)

        log.debug(
            "Uploading Photo URI = %s, Mimetype = %s, Original Name = %s" %
            (photoURI, mimeType, originalName))

        #upload the file
        return self._upload_photo(uploadInfo)
예제 #14
0
def read_removable_volume_group_file(folderUri):
    items = []
    f = File.File(URI=folderUri)
    if f.is_on_removale_volume():
        #read from the /volume/root/.conduit file
        rooturi, path = get_removable_volume_info(f)
        if rooturi and path:
            conf = ConfigParser.SafeConfigParser()
            conf.read(path)
            for p, n in conf.items("DEFAULT"):
                log.debug("Read group (%s = %s)" % (p, n))
                #check the path still exists on the volume
                if Vfs.uri_exists(rooturi + p):
                    items.append((p, n))
    return items
예제 #15
0
    def tomboy_note_to_file(self, note, **kwargs):
        content = note.get_xml()
        #Old tomboy made this note, fallback to plain text
        if content == None:
            content = note.get_contents()

        #replace the following characters in the note
        #title with an underscore
        filename = note.get_title().translate(
            string.maketrans(self.ILLEGAL_TITLE_CHARS,
                             "_" * len(self.ILLEGAL_TITLE_CHARS)))
        f = File.TempFile(content)
        f.force_new_filename(filename)
        f.force_new_file_extension(self.NOTE_EXTENSION)
        return f
예제 #16
0
    def get(self, url):
        DataProvider.DataSource.get(self, url)
        #Make a file
        f = File.File(URI=url)
        f.set_UID(url)
        f.set_open_URI(url)
        name, ext = f.get_filename_and_extension()

        #create the correct filename and retain the original extension
        try:
            title, t, idx = self._files[url]
            f.force_new_filename(title)
            f.force_new_file_extension(ext)
        except:
            log.warn("Error setting filename\n%s" % traceback.format_exc())

        return f
예제 #17
0
    def get(self, LUID):
        DataProvider.TwoWay.get(self, LUID)
        url = self._get_raw_file_url(LUID)
        f = File.File(URI=url, group=self.foldername)
        try:
            #gnomevfs doesnt like unicode
            f.force_new_filename(str(self.files[LUID]))
        except KeyError:
            #occurs on put() returning get() because we have
            #not refreshed since. Not a problem because the point
            #of put returning get() is to make the rids() in the same
            #scheme, and not actually do something with the returned file.
            pass
        f.set_open_URI(url)
        f.set_UID(LUID)

        return f
예제 #18
0
    def add(self, LUID):
        f = File.File(URI=LUID)
        if f.exists():
            oid = self.db.select_one("SELECT oid FROM files WHERE URI = ?",
                                     (LUID, ))
            if oid != None:
                log.debug("Could not add (already added): %s" % LUID)
                return False

            if f.is_directory():
                log.debug("Adding folder: %s" % LUID)
                self._add_folder(LUID, "FIXME")
            else:
                log.debug("Adding file: %s" % LUID)
                self._add_file(LUID)
        else:
            log.warn("Could not add: %s" % LUID)
            return False
        return True
예제 #19
0
    def put(self, file, overwrite, LUID=None):
        """
        Puts the file in the sink, this uploads the file if it is not present yet
        or updates it if necessary
        """
        DataProvider.TwoWay.put(self, file, overwrite, LUID)

        originalName = file.get_filename()
        #Gets the local URI (/foo/bar). If this is a remote file then
        #it is first transferred to the local filesystem
        fileURI = file.get_local_uri()
        mimeType = file.get_mimetype()

        if LUID == None:
            log.debug(
                "Uploading file URI = %s, Mimetype = %s, Original Name = %s" %
                (fileURI, mimeType, originalName))
            LUID = self._upload_file(fileURI, originalName)
        else:
            #check if a file exists at that UID
            id = self._get_file_info(LUID)
            if id != None:
                if overwrite == True:
                    log.debug(
                        "Replacing file URI = %s, Mimetype = %s, Original Name = %s"
                        % (fileURI, mimeType, originalName))
                    LUID = self._replace_file(LUID, fileURI, originalName)
                else:
                    #Only upload the file if it is newer than the Remote one
                    url = self._get_raw_file_url(id)
                    remoteFile = File.File(url)

                    #this is a limited test for equality type comparison
                    comp = file.compare(remoteFile, True)
                    log.debug(
                        "Compared %s with %s to check if they are the same (size). Result = %s"
                        %
                        (file.get_filename(), remoteFile.get_filename(), comp))
                    if comp != conduit.datatypes.COMPARISON_EQUAL:
                        raise Exceptions.SynchronizeConflictError(
                            comp, file, remoteFile)

        return self.get(LUID).get_rid()
예제 #20
0
    def add_attachment(self, path):
        #Create a multipart message and each attachment gets a part
        if not self.email.is_multipart():
            newemail = MIMEMultipart()
            newemail['Subject'] = self.email['Subject']
            newemail['To'] = self.email['To']
            newemail['From'] = self.email['From']
            newemail.preamble = 'There are attachments\n'
            newemail.epilogue = ''
            self.email = newemail

        f = File.File(path)
        filename = f.get_filename()
        mt = f.get_mimetype()
        maintype, subtype = mt.split('/', 1)
        if maintype == 'text':
            fp = open(path)
            #We should handle calculating the charset
            msg = MIMEText(fp.read(), _subtype=subtype)
            fp.close()
        elif maintype == 'image':
            fp = open(path, 'rb')
            msg = MIMEImage(fp.read(), _subtype=subtype)
            fp.close()
        elif maintype == 'audio':
            fp = open(path, 'rb')
            msg = MIMEAudio(fp.read(), _subtype=subtype)
            fp.close()
        else:
            fp = open(path, 'rb')
            msg = MIMEBase('application', 'octet-stream')
            msg.set_payload(fp.read())
            fp.close()
            # Encode the payload using Base64
            Encoders.encode_base64(msg)
        # Set the filename parameter
        msg.add_header('Content-Disposition', 'attachment', filename=filename)
        self.email.attach(msg)
        self.attachments.append(path)
예제 #21
0
    def put(self, vfsFile, overwrite, LUID=None):
        """
        Puts vfsFile at the correct location. There are three scenarios
        1) File came from a foreign DP like tomboy
        2) File came from another file dp

        Behaviour:
        1) The foreign DP should have encoded enough information (such as
        the filename) so that we can go ahead and put the file in the dir
        2) First we see if the file has a group attribute.
            a) If so, and the group matches the groupName here then we 
            put the files into the directory.
            b) If not we put the file in a subdir by the name of the group 
            
        We always retain the relative path for the files
        """
        DataProvider.TwoWay.put(self, vfsFile, overwrite, LUID)
        newURI = ""
        if LUID != None:
            newURI = LUID
        elif vfsFile.basePath == "":
            #came from another type of dataprovider such as tomboy
            #where relative path makes no sense. Could also come from
            #the FileSource dp when the user has selected a single file
            log.debug("No basepath. Going to empty dir")
            newURI = Vfs.uri_join(self.folder, vfsFile.get_filename())
        else:
            #Look for corresponding groups
            relpath = vfsFile.get_relative_uri()
            log.debug("Relative path: %s" % relpath)
            if self.folderGroupName == vfsFile.group:
                log.debug("Found corresponding group")
                #put in the folder
                newURI = Vfs.uri_join(self.folder, relpath)
            else:
                log.debug("Recreating group: %s" % vfsFile.group)
                #unknown. Store in the dir but recreate the group
                newURI = Vfs.uri_join(self.folder, vfsFile.group, relpath)

        #escape illegal filesystem characters
        if self.fstype:
            newURI = Vfs.uri_sanitize_for_filesystem(newURI, self.fstype)

        #overwrite is the easy case, as for it to be true, requires specific user
        #interaction
        if overwrite == True:
            self._transfer_file(vfsFile, newURI, overwrite)
        else:
            #check for conflicts
            destFile = File.File(URI=newURI)
            if destFile.exists():
                comp = vfsFile.compare(destFile,
                                       sizeOnly=self.compareIgnoreMtime)

                if LUID != None and comp == DataType.COMPARISON_NEWER:
                    #we were expecting an existing file, we found it, but
                    #we are newer, so overwrite it
                    self._transfer_file(vfsFile, newURI, True)
                elif comp == DataType.COMPARISON_EQUAL:
                    #in File.compare, the files are compared based on size, if
                    #their mtimes are the same, so this case is true when
                    # 1) The sizes are the same, and the user told us
                    #    to ignore the mtimes
                    # 2) The mtimes and size is the same, and we checked both
                    pass
                else:
                    raise Exceptions.SynchronizeConflictError(
                        comp, vfsFile, destFile)
            else:
                self._transfer_file(vfsFile, newURI, overwrite)

        return self.get(newURI).get_rid()
예제 #22
0
 def setting_to_file(self, setting):
     f = File.TempFile(self._to_text(setting))
     f.force_new_filename(setting.key.replace("/", "_"))
     f.force_new_file_extension(".txt")
     return f
예제 #23
0
 def delete(self, LUID):
     obj = File.File(URI=os.path.join(self.dataDir, LUID))
     if obj.exists():
         obj.delete()
예제 #24
0
 def add(self, LUID):
     f = File.File(URI=LUID)
     if f.exists() and f.is_directory():
         self.folder = f._get_text_uri()
         return True
     return False
예제 #25
0
    def delete(self, LUID):
        IPodBase.delete(self, LUID)

        raw = File.File(URI=os.path.join(self._get_shadow_dir(), LUID))
        if raw.exists():
            raw.delete()
예제 #26
0
 def delete(self, LUID):
     f = File.File(URI=LUID)
     if f.exists():
         f.delete()
예제 #27
0
 def get(self, LUID):
     DataProvider.DataSource.get(self, LUID)
     f = File.File(URI=LUID)
     f.set_open_URI(LUID)
     f.set_UID(LUID)
     return f
예제 #28
0
 def note_to_file(self, note, **kwargs):
     f = File.TempFile(note.get_contents())
     f.force_new_filename(note.get_title())
     f.force_new_file_extension(".txt")
     return f
예제 #29
0
 def email_to_file(self, email, **kwargs):
     f = File.TempFile(email.get_email_string())
     return f