Exemplo n.º 1
0
 def test_delete_file(self):
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_3'))
     cp.ExportToPIF('package_3.zip')
     cp2 = imscp.ContentPackage('package_3.zip')
     self.dList.append(cp2.dPath)
     r1 = cp2.manifest.get_element_by_id('test1')
     r2 = cp2.manifest.get_element_by_id('test2')
     r1_len = len(r1.File)
     r2_len = len(r2.File)
     cp2.DeleteFile('file_1.xml')
     self.assertTrue(len(r1.File) == r1_len - 1)
     self.assertTrue(len(r2.File) == r2_len - 1)
Exemplo n.º 2
0
 def test_file_table(self):
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_3'))
     ft = cp.fileTable
     self.assertTrue(len(ft) == 6)
     self.assertTrue(
         len(ft[cp.FilePath('file_1.xml')]) == 2 and
         isinstance(ft[cp.FilePath('file_1.xml')][0], imscp.File))
     self.assertTrue(len(ft[cp.FilePath('file_4.xml')]) == 0, imscp.File)
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_3'))
     cp.SetIgnoreFiles("\\..*|.*4.*")
     cp.RebuildFileTable()
     ft = cp.fileTable
     self.assertTrue(len(ft) == 5)
Exemplo n.º 3
0
 def test_zip_write(self):
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_1.zip'))
     self.dList.append(cp.dPath)
     cp.ExportToPIF('Package2.zip')
     cp2 = imscp.ContentPackage('Package2.zip')
     self.dList.append(cp2.dPath)
     resources = cp2.manifest.root.Resources
     f = resources.Resource[0].File[0]
     doc = xmlns.Document(baseURI=f.resolve_uri(f.href))
     doc.read()
     self.assertTrue(doc.root.xmlname == 'tag' and
                     doc.root.get_value() ==
                     u8(b'Unicode Test: \xe8\x8b\xb1\xe5\x9b\xbd'))
Exemplo n.º 4
0
 def test_clean_up(self):
     cp = imscp.ContentPackage()
     dpath = cp.dPath
     self.assertTrue(dpath.isdir())
     cp.Close()
     self.assertTrue(cp.manifest is None, "Manifest not removed on close")
     self.assertFalse(dpath.isdir(), "Temp directory not deleted on close")
     cp = imscp.ContentPackage('package')
     dpath = cp.dPath
     self.assertTrue(dpath.isdir())
     cp.Close()
     self.assertTrue(cp.manifest is None, "Manifest not removed on close")
     self.assertTrue(dpath.isdir(), "Non-temp directory removed on close")
Exemplo n.º 5
0
 def test_unique_file(self):
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_3'))
     ft = cp.fileTable
     fpath = cp.GetUniqueFile('file_1.xml')
     self.assertFalse(fpath == 'file_1.xml', "file path must be unique")
     self.assertTrue(str(fpath)[-4:] == '.xml', "Must preserve extension")
     self.assertFalse(fpath in ft, "file path must not be in use")
Exemplo n.º 6
0
 def test_unicode(self):
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_1'))
     resources = cp.manifest.root.Resources
     r = resources.Resource[0]
     self.assertTrue(len(r.File) == 1)
     f = r.File[0]
     self.assertTrue(isinstance(f, imscp.File) and
                     str(f.href) == "%E8%8B%B1%E5%9B%BD.xml", "File path")
     doc = xmlns.Document(baseURI=f.resolve_uri(f.href))
     doc.read()
     self.assertTrue(doc.root.xmlname == 'tag' and
                     doc.root.get_value() ==
                     u8(b'Unicode Test: \xe8\x8b\xb1\xe5\x9b\xbd'))
     cp2 = imscp.ContentPackage(
         TEST_DATA_DIR.join(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd')))
     self.assertTrue(
         cp2.GetPackageName() == u8(b'\xe8\x8b\xb1\xe5\x9b\xbd'),
         "Unicode package name test")
Exemplo n.º 7
0
 def __init__(self):
     cmd.Cmd.__init__(self)
     self.cp = imscp.ContentPackage()
     self.prompt = "%s.%s$ " % (self.cp.GetPackageName(),
                                self.cp.manifest.root.id)
     # paging controls
     self.ClearPages()
     self.pageSize = 30
     # options and settings; very complex so use a dictionary
     self.options = DEFAULT_OPTIONS
Exemplo n.º 8
0
 def setUp(self):  # noqa
     self.dataPath = os.path.join(
         os.path.split(os.path.abspath(__file__))[0],
         'data_imscc_profilev1p0')
     self.cwd = os.getcwd()
     self.tmpPath = mkdtemp('.d', 'pyslet-test_imscpv1p2-')
     os.chdir(self.tmpPath)
     cp = imscp.ContentPackage(os.path.join(self.dataPath, 'sample_1'))
     cp.ExportToPIF('sample_1.zip')
     self.cc = imscc.CommonCartridge('sample_1.zip')
Exemplo n.º 9
0
 def test_package_paths(self):
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_3'))
     good_path = FilePath(cp.dPath, 'hello', 'world')
     self.assertTrue(cp.PackagePath(good_path) ==
                     FilePath('hello', 'world'))
     bad_path = TEST_DATA_DIR.join('package_3x', 'hello', 'world')
     self.assertTrue(cp.PackagePath(bad_path) is None, bad_path)
     bad_path = TEST_DATA_DIR.join('package_x')
     self.assertTrue(cp.PackagePath(bad_path) is None, bad_path)
     bad_path = TEST_DATA_DIR
     self.assertTrue(cp.PackagePath(bad_path) is None, bad_path)
Exemplo n.º 10
0
 def test_constructor(self):
     cp = imscp.ContentPackage()
     self.assertTrue(cp.dPath.isdir(),
                     "Default constructor must create a temp directory")
     self.assertTrue(cp.GetPackageName() == 'imscp',
                     "Default package name is not empty string")
     # Ensure the temporary directory is cleaned up
     self.dList.append(cp.dPath)
     url = uri.URI.from_octets(cp.manifest.get_base())
     self.assertTrue(isinstance(cp.manifest, xmlns.XMLNSDocument) and
                     isinstance(cp.manifest.root, imscp.Manifest),
                     "Constructor must create manifest")
     self.assertTrue(url.get_virtual_file_path().split()[1] ==
                     'imsmanifest.xml', "Manifest file name")
     self.assertTrue(isinstance(cp.manifest.root, imscp.Manifest),
                     "Constructor must create manifest element")
     id = cp.manifest.root.id
     self.assertTrue(cp.manifest.get_element_by_id(id) is cp.manifest.root,
                     "Manifest identifief not declared")
     self.assertTrue(url.get_virtual_file_path().isfile(),
                     "Constructor must create manifest file")
     cp = imscp.ContentPackage('newpackage')
     self.assertTrue(
         cp.dPath.isdir() and FilePath('newpackage').abspath() == cp.dPath,
         "Constructor creates specified directory")
     self.assertTrue(cp.GetPackageName() == 'newpackage',
                     "Package name not taken from directory")
     cp = imscp.ContentPackage('package')
     self.assertTrue(FilePath('package').abspath() == cp.dPath,
                     "Constructor with existing directory, no manifest")
     cp = imscp.ContentPackage('mpackage')
     self.assertTrue(cp.manifest.root.id == "MANIFEST-QTI-1",
                     "Constructor with existing directory and manifest")
     self.assertTrue(cp.GetPackageName() == 'mpackage',
                     "Package name wrongly affected by manifest")
     cp = imscp.ContentPackage(FilePath('mpackage', 'imsmanifest.xml'))
     self.assertTrue(
         cp.dPath.isdir() and FilePath('mpackage').abspath() == cp.dPath,
         "Constructor identifies pkg dir from manifest file")
     self.assertTrue(cp.manifest.root.id == "MANIFEST-QTI-1",
                     "Constructor from manifest file")
Exemplo n.º 11
0
 def do_open(self, args):
     self.ClearPages()
     if not os.path.exists(args):
         print "Warning: file or directory does not exist."
         reply = raw_input(
             "Create a content package directory at %s? (Yes/No)> " % args)
         if reply.lower() != "yes"[0:len(reply)]:
             return
     try:
         newCP = imscp.ContentPackage(args)
     except imscp.CPException, e:
         self.PrintException(e)
Exemplo n.º 12
0
 def test_zip_read(self):
     cp = imscp.ContentPackage(TEST_DATA_DIR.join('package_1.zip'))
     self.assertTrue(cp.dPath.isdir(),
                     "Zip constructor must create a temp directory")
     # Ensure the temporary directory is cleaned up
     self.dList.append(cp.dPath)
     self.assertTrue(cp.GetPackageName() == 'package_1',
                     "Zip extension not removed for name")
     resources = cp.manifest.root.Resources
     f = resources.Resource[0].File[0]
     doc = xmlns.Document(baseURI=f.resolve_uri(f.href))
     doc.read()
     self.assertTrue(doc.root.xmlname == 'tag' and
                     doc.root.get_value() ==
                     u8(b'Unicode Test: \xe8\x8b\xb1\xe5\x9b\xbd'))
Exemplo n.º 13
0
 def test_constructor(self):
     cc = imscc.CommonCartridge()
     self.assertTrue(isinstance(cc.cp, imscp.ContentPackage))
     self.cpList.append(cc.cp)
     self.assertTrue(len(cc.laoTable) == 0)
     cc = imscc.CommonCartridge(os.path.join(self.dataPath, 'sample_1'))
     self.assertTrue(len(cc.laoTable) == 3)
     cp = imscp.ContentPackage(os.path.join(self.dataPath, 'sample_1'))
     self.cpList.append(cp)
     cc = imscc.CommonCartridge(cp)
     self.assertTrue(len(cc.laoTable) == 3)
     dlist = sorted([cc.laoTable[x][0] for x in cc.laoTable])
     self.assertTrue(dlist == ['l0001', 'l0002', 'l0003'])
     # r6=cp.manifest.get_element_by_id('R0006')
     head, acr = cc.laoTable['R0006']
     self.assertTrue(acr is cp.manifest.get_element_by_id('R0007'))
     self.assertTrue(len(acr.File) == 3)
Exemplo n.º 14
0
 def testCaseConstructor(self):
     cc = CommonCartridge()
     self.assertTrue(isinstance(cc.cp, imscp.ContentPackage))
     self.cpList.append(cc.cp)
     self.assertTrue(len(cc.laoTable.keys()) == 0)
     cc = CommonCartridge(os.path.join(self.dataPath, 'sample_1'))
     self.assertTrue(len(cc.laoTable.keys()) == 3)
     cp = imscp.ContentPackage(os.path.join(self.dataPath, 'sample_1'))
     self.cpList.append(cp)
     cc = CommonCartridge(cp)
     self.assertTrue(len(cc.laoTable.keys()) == 3)
     dList = map(lambda x: cc.laoTable[x][0], cc.laoTable.keys())
     dList.sort()
     self.assertTrue(dList == ['l0001', 'l0002', 'l0003'])
     # r6=cp.manifest.get_element_by_id('R0006')
     head, acr = cc.laoTable['R0006']
     self.assertTrue(acr is cp.manifest.get_element_by_id('R0007'))
     self.assertTrue(len(acr.File) == 3)
Exemplo n.º 15
0
 def testCaseOutputV2(self):
     if vobject is None:
         logging.warn(
             "QTI v1 to v2 migration tests skipped: vobject required")
         return
     self.cp.manifest.root.set_id('outputv2')
     dPath = os.path.join(self.dataPath, 'input')
     fList = []
     for f in os.listdir(dPath):
         if self.cp.IgnoreFile(f):
             continue
         stem, ext = os.path.splitext(f)
         if ext.lower() == '.xml':
             fList.append(f)
     fList.sort()
     for f in fList:
         doc = QTIDocument(
             baseURI=str(uri.URI.from_path(os.path.join(dPath, f))))
         doc.read()
         doc.MigrateV2(self.cp)
     # Having migrated everything in the input folder, we now check our CP
     # against the output
     cp2 = imscp.ContentPackage(os.path.join(self.dataPath, 'outputv2'))
     # To do....
     # Compare the manifests
     # Compare each file
     fList1 = self.cp.fileTable.keys()
     fList1.sort()
     fList2 = cp2.fileTable.keys()
     fList2.sort()
     if fList1 != fList2:
         diagnosis = []
         for f in fList1:
             if f not in fList2:
                 diagnosis.append("Extra file found: %s" % f)
         for f in fList2:
             if f not in fList1:
                 diagnosis.append("Missing file: %s" % f)
         self.fail("File lists:\n  %s" % string.join(diagnosis, '\n  '))
     logging.debug(str(self.cp.manifest))
     logging.debug(str(cp2.manifest))
     output = self.cp.manifest.diff_string(cp2.manifest)
     self.assertTrue(self.cp.manifest.root == cp2.manifest.root,
                     "Manifests differ:\n%s" % output)
     checkFiles = {}
     for r in cp2.manifest.root.Resources.Resource:
         # Check the entry-point of each resource
         f = r.GetEntryPoint()
         if f:
             fPath = f.PackagePath(cp2)
             qtiDoc = qtiv2.core.QTIDocument(baseURI=str(
                 uri.URI.from_virtual_path(self.cp.dPath.join(fPath))))
             qtiDoc.read()
             # print str(qtiDoc)
             qtiDoc2 = qtiv2.core.QTIDocument(baseURI=str(
                 uri.URI.from_virtual_path(cp2.dPath.join(fPath))))
             qtiDoc2.read()
             # print str(qtiDoc2)
             output = qtiDoc.diff_string(qtiDoc2)
             result = (qtiDoc.root == qtiDoc2.root)
             if not result and output is None:
                 # This should not happen
                 self.PrintPrettyWeird(qtiDoc.root, qtiDoc2.root)
             self.assertTrue(
                 qtiDoc.root == qtiDoc2.root,
                 "QTI Files differ at %s (actual output shown first)\n%s" %
                 (fPath, output))
         for f in r.File:
             if f.href is None or f.href.is_absolute():
                 continue
             fPath = f.PackagePath(cp2)
             fAbsPath = self.cp.dPath.join(fPath)
             fAbsPath2 = cp2.dPath.join(fPath)
             baseURI = str(uri.URI.from_virtual_path(fAbsPath))
             baseURI2 = str(uri.URI.from_virtual_path(fAbsPath2))
             if fAbsPath.splitext()[1].lower() == '.xml':
                 # Two xml files, compare with simple XMLElement
                 doc = xml.Document(baseURI=baseURI)
                 doc.read()
                 doc2 = xml.Document(baseURI=baseURI2)
                 doc2.read()
                 output = doc.diff_string(doc2)
                 result = (doc.root == doc2.root)
                 if not result and output is None:
                     # This should not happen
                     self.PrintPrettyWeird(doc.root, doc2.root)
                 self.assertTrue(
                     doc.root == doc2.root,
                     "XML Files differ at %s (actual output shown first)\n%s"
                     % (fPath, output))
             else:
                 # Binary compare the two files.
                 f = fAbsPath.open('rb')
                 f2 = fAbsPath2.open('rb')
                 while True:
                     fData = f.read(1024)
                     fData2 = f2.read(1024)
                     self.assertTrue(fData == fData2,
                                     "Binary files don't match: %s" % fPath)
                     if not fData:
                         break
Exemplo n.º 16
0
 def setUp(self):
     self.cwd = os.getcwd()
     self.dataPath = os.path.join(
         os.path.split(__file__)[0], 'data_imsqtiv1p2p1')
     self.cp = imscp.ContentPackage()
Exemplo n.º 17
0
	def __init__(self):
		self.pkg=cp.ContentPackage()
		self.xmlTypes={".xml":True}
		self.migrationLogs={}