def exampledata_to_file(self, data, **kwargs): """ Converts exampledata to a file containing the text """ f = Utils.new_tempfile( contents=data.get_string() ) return f
def put(self, event, overwrite, LUID=None): DataProvider.TwoWay.put(self, event, overwrite, LUID) if LUID != None: f = Utils.new_tempfile(event.get_ical_string()) f.transfer(os.path.join(self.dataDir, LUID), overwrite=True) f.set_UID(LUID) return f.get_rid() return _string_to_unqiue_file(event.get_ical_string(), self.dataDir, 'event')
def put(self, event, overwrite, LUID=None): DataProvider.TwoWay.put(self, event, overwrite, LUID) if LUID != None: f = Utils.new_tempfile(event.get_ical_string()) f.transfer(os.path.join(self.dataDir, LUID), overwrite=True) f.set_UID(LUID) return f.get_rid() return _string_to_unqiue_file(event.get_ical_string(), self.dataDir, "event")
def new_file(filename): if filename == None: f = Utils.new_tempfile(Utils.random_string()) else: files = get_files_from_data_dir(filename) f = File.File(URI=files[0]) uri = f._get_text_uri() f.set_UID(uri) f.set_open_URI(uri) return f
def _string_to_unqiue_file(txt, base_uri, prefix, postfix=""): for i in range(1, 10000): filename = prefix + str(i) + postfix uri = os.path.join(base_uri, filename) f = File.File(uri) if not f.exists(): break temp = Utils.new_tempfile(txt) temp.transfer(uri, True) temp.set_UID(filename) return temp.get_rid()
def _save_note_to_ipod(self, uid, note): """ Save a simple iPod note in /Notes If the note has raw then also save that in shadowdir uid is the note title. """ # the normal note viewed by the iPod # inject an encoding declaration if it is missing. contents = note.get_contents() if not self.ENCODING_DECLARATION in contents: contents = ''.join([self.ENCODING_DECLARATION, contents]) ipodnote = Utils.new_tempfile(contents) ipodnote.transfer(os.path.join(self.dataDir,uid), overwrite=True) ipodnote.set_mtime(note.get_mtime()) ipodnote.set_UID(uid) #the raw pickled note for sync raw = open(os.path.join(self._get_shadow_dir(),uid),'wb') pickle.dump(note, raw, -1) raw.close() return ipodnote.get_rid()
def _save_note_to_ipod(self, uid, note): """ Save a simple iPod note in /Notes If the note has raw then also save that in shadowdir uid is the note title. """ # the normal note viewed by the iPod # inject an encoding declaration if it is missing. contents = note.get_contents() if not self.ENCODING_DECLARATION in contents: contents = "".join([self.ENCODING_DECLARATION, contents]) ipodnote = Utils.new_tempfile(contents) ipodnote.transfer(os.path.join(self.dataDir, uid), overwrite=True) ipodnote.set_mtime(note.get_mtime()) ipodnote.set_UID(uid) # the raw pickled note for sync raw = open(os.path.join(self._get_shadow_dir(), uid), "wb") pickle.dump(note, raw, -1) raw.close() return ipodnote.get_rid()
def event_to_file(self, event, **kwargs): #get ical data f = Utils.new_tempfile(event.get_ical_string()) return f
def contact_to_file(self, contact, **kwargs): #get vcard data f = Utils.new_tempfile(contact.get_vcard_string()) return f
def text_to_file(self, text, **kwargs): return Utils.new_tempfile(text.get_string())
def _string_to_unqiue_file(txt, base_uri, prefix, postfix=''): temp = Utils.new_tempfile(txt) uri = os.path.join(base_uri, prefix+temp.get_filename()+postfix) temp.transfer(uri, True) temp.set_UID(os.path.basename(uri)) return temp.get_rid()
def add(self, LUID): #Add a temp file to folder tmpfile = Utils.new_tempfile(Utils.random_string()) self.put(tmpfile, True, None)
import os import time import datetime import tempfile try: f = File.File() except: ok("Base: Must specify URI", True) null = File.File("/foo/bar") ok("Base: non-existant file", null.exists() == False) # test tempfile handling temp = Utils.new_tempfile(Utils.random_string()) ok("Base: Detected tempfile", temp.is_local() and temp._is_tempfile()) uri = temp.get_local_uri() ok("Base: Tempfile in temp dir", uri and uri.startswith(tempfile.gettempdir())) temp.delete() gone = File.File(uri) ok("Base: Delete tempfile", not gone.exists()) # test making directories tmpdir = Utils.new_tempdir() tmpdir2 = os.path.join(tmpdir, "subdir") f = File.File(tmpdir2) ok("Base: make directory", f.make_directory() == True)
else: impls = ("GIO", "GnomeVfs") for impl in impls: ok("--- TESTING FILE IMPL: %s" % impl, True) try: f = File.File(implName=impl) except: ok("Base: Must specify URI", True) null = File.File("/foo/bar",implName=impl) ok("Base: non-existant file", null.exists() == False) #test tempfile handling temp = Utils.new_tempfile(Utils.random_string(), implName=impl) ok("Base: Detected tempfile", temp.is_local() and temp._is_tempfile()) uri = temp.get_local_uri() ok("Base: Tempfile in temp dir", uri and uri.startswith(tempfile.gettempdir())) temp.delete() gone = File.File(uri,implName=impl) ok("Base: Delete tempfile", not gone.exists()) #test making directories tmpdir = Utils.new_tempdir() tmpdir2 = os.path.join(tmpdir, "subdir") f = File.File(tmpdir2,implName=impl) ok("Base: make directory", f.make_directory() == True)
# common sets up the conduit environment from common import * import conduit.datatypes.Email as Email import conduit.utils as Utils e = Email.Email(to="me", subject="foo", content="bar") s = e.get_email_string() ok("Email created ok", len(s) > 0) ok("Email has no attachments", e.has_attachments() == False) f = Utils.new_tempfile("I AM A TEXT FILE ATTACHMENT") e.add_attachment(f.get_local_uri()) ok("Email has attachment", e.has_attachments() == True) s2 = e.get_email_string() ok("Email OK", len(s) > 0 and len(s2) > len(s)) h1 = e.get_hash() e2 = Email.Email() e2.set_from_email_string(s2) h2 = e2.get_hash() ok("Email serialize and deserialize OK", h1 == h2) finished()
def generate_sample(self): f = Utils.new_tempfile(Utils.random_string()) uri = f._get_text_uri() f.set_UID(uri) f.set_open_URI(uri) return f
def exampledata_to_file(self, data, **kwargs): """ Converts exampledata to a file containing the text """ f = Utils.new_tempfile(contents=data.get_string()) return f
ok("Dont escape path characters",Vfs.uri_escape(safe+unsafe) == safe+safeunsafe) ok("Unescape back to original",Vfs.uri_unescape(safe+safeunsafe) == safe+unsafe) ok("Get protocol", Vfs.uri_get_protocol("file:///foo/bar") == "file://") name, ext = Vfs.uri_get_filename_and_extension("file:///foo/bar.ext") ok("Get filename (%s,%s)" % (name,ext), name == "bar" and ext == ".ext") ok("file:///home exists", Vfs.uri_exists("file:///home") == True) ok("/home exists", Vfs.uri_exists("/home") == True) ok("/home is folder", Vfs.uri_is_folder("/home") == True) ok("/foo/bar does not exist", Vfs.uri_exists("/foo/bar") == False) ok("format uri", Vfs.uri_format_for_display("file:///foo") == "/foo") tmpdiruri = Utils.new_tempdir() # Test the folder scanner theading stuff fileuri = Utils.new_tempfile("bla").get_local_uri() stm = VfsFile.FolderScannerThreadManager(maxConcurrentThreads=1) def prog(*args): pass def done(*args): pass t1 = stm.make_thread("file:///tmp", False, False, prog, done) t2 = stm.make_thread("file://"+tmpdiruri, False, False, prog, done) stm.join_all_threads() ok("Scanned /tmp ok - found %s" % fileuri, "file://"+fileuri in t1.get_uris()) ok("Scanned %s ok (empty)" % tmpdiruri, t2.get_uris() == []) # Test the volume management stuff ntfsUri = get_external_resources('folder')['ntfs-volume'] if Vfs.uri_exists(ntfsUri):
#check construction arg type checking try: i = Utils.datetime_from_timestamp("Foo") ok("datetime_from_timestamp only accepts numbers", False) except: ok("datetime_from_timestamp only accepts numbers", True) try: i = Utils.datetime_from_timestamp("Foo") ok("datetime_get_timestamp only accepts datetimes", False) except: ok("datetime_get_timestamp only accepts datetimes", True) #make another local file local = Utils.new_tempfile(Utils.random_string()) #get timestamp and mtimes dt = local.get_mtime() ts = Utils.datetime_get_timestamp(dt) #now get timestamp using os.stat pts = os.stat(local.get_local_uri()).st_mtime pdt = Utils.datetime_from_timestamp(pts) ok("Timestamps are equal (%s)" % ts, ts == pts) ok("Datetimes are equal (%s)" % dt, dt == pdt) #Check that we ignore any microsecond timestamps f = ts + 0.01234 fdt = Utils.datetime_from_timestamp(f)
def add(self, LUID): #Add a temp file to folder tmpfile = Utils.new_tempfile(Utils.random_string()) self.put(tmpfile,True,None)
Vfs.uri_make_directory(tempdir2) #create some test files f1Name, f1URI = create_file(tempdir) f2Name, f2URI = create_file(tempdir2) #create a test dataprovider dp = FileDataProvider.FolderTwoWay( folder=dpdir, folderGroupName=GROUP_NAME, includeHidden=False, compareIgnoreMtime=False, followSymlinks=False) # Scenario 1) # File came from a foreign DP like tomboy. No concept of relative path # or group. Goes into the folder and keeps its name plainFile = Utils.new_tempfile("TomboyNote") plainFileName = plainFile.get_filename() rid = dp.put( vfsFile=plainFile, overwrite=False, LUID=None) ok("Put plain file", rid.get_UID() == Vfs.uri_join(dpdir,plainFileName)) # Scehario 2a) # File came from another folder dp with the same group # Goes into the folder, keeps its relative path f1 = File.File( URI=f1URI, basepath=tempdir, group=GROUP_NAME) rid = dp.put(