Added a method for copying a file or directory from a UFOReader. This should (I'll find out tomorrow) be helpful for dealing with /data and /images in save as operations in higher level objects.

git-svn-id: http://svn.robofab.com/branches/ufo3k@426 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
This commit is contained in:
Tal Leming 2011-10-19 02:18:56 +00:00
parent b47970f049
commit b8e639ac31
2 changed files with 39 additions and 0 deletions

View File

@ -720,6 +720,25 @@ class UFOWriter(object):
# remove the file # remove the file
self._removeFileForPath(path, raiseErrorIfMissing=True) self._removeFileForPath(path, raiseErrorIfMissing=True)
def copyFromReader(self, reader, sourcePath, destPath):
"""
Copy the sourcePath in the provided UFOReader to destPath
in this writer. The paths must be relative. They may represent
directories or paths. This uses the most memory effecient
method possible for copying the data possible.
"""
if not isinstance(reader, UFOReader):
raise UFOLibError("The reader must be an instance of UFOReader.")
fullSourcePath = os.path.join(reader._path, sourcePath)
if not reader._checkForFile(fullSourcePath):
raise UFOLibError("No file named \"%s\" to copy from." % sourcePath)
self._buildDirectoryTree(destPath)
fullDestPath = os.path.join(self._path, destPath)
if os.path.isdir(fullSourcePath):
shutil.copytree(fullSourcePath, fullDestPath)
else:
shutil.copy(fullSourcePath, fullDestPath)
# metainfo.plist # metainfo.plist
def _writeMetaInfo(self): def _writeMetaInfo(self):

View File

@ -4231,6 +4231,26 @@ class UFO3WriteDataTestCase(unittest.TestCase):
self.assertRaises(UFOLibError, writer.removeFileForPath, path="data/org.unifiedfontobject.doesNotExist.txt") self.assertRaises(UFOLibError, writer.removeFileForPath, path="data/org.unifiedfontobject.doesNotExist.txt")
self.tearDownUFO() self.tearDownUFO()
def testUFOWriterCopy(self):
sourceDir = self.dstDir.replace(".ufo", "") + "-copy source" + ".ufo"
dataPath = "data/org.unifiedfontobject.copy/level1/level2/file1.txt"
writer = UFOWriter(sourceDir, formatVersion=3)
writer.writeBytesToPath(dataPath, "test")
# copy a file
reader = UFOReader(sourceDir)
writer = UFOWriter(self.dstDir, formatVersion=3)
writer.copyFromReader(reader, dataPath, dataPath)
path = os.path.join(self.dstDir, dataPath)
self.assertEqual(os.path.exists(path), True)
self.tearDownUFO()
# copy a directory
reader = UFOReader(sourceDir)
writer = UFOWriter(self.dstDir, formatVersion=3)
p = "data/org.unifiedfontobject.copy"
writer.copyFromReader(reader, p, p)
path = os.path.join(self.dstDir, dataPath)
self.assertEqual(os.path.exists(path), True)
self.tearDownUFO()
# --------------- # ---------------
# layerinfo.plist # layerinfo.plist