def testCourseNameExport(self): testFilePath = os.path.join(os.path.dirname(__file__), 'data/modulestore_sample.json') pickleFilePath = os.path.join(os.path.dirname(__file__), 'data/modulestore_sample.pkl') importer = ModulestoreImporter(testFilePath, pickleCachePath=pickleFilePath) dest = tempfile.NamedTemporaryFile(prefix='oolala', suffix='.csv') importer.exportCourseNameLookup(dest, addHeader=True) truthFile = open(os.path.join(os.path.dirname(__file__),"data/modulestore_sampleTruth1.csv"), 'r') if UPDATE_TRUTH: self.updateTruth(dest.name, truthFile.name) else: self.assertFileContentEquals(truthFile, dest.name) dest.close()
def ensureHashMapper(cls): ''' Creates facility, ModulestoreImporter, which turns OpenEdX resource IDs into human readable strings. the ModulestoreImporter instance is placed in class variable hashMapper. The Creation can fail, so that variable may remain None after calling this method. Callers must check for this condition. :param cls: this class instance; passed transparently by Python :type cls: Utils ''' # Create a facility that can map resource name hashes # to human-readable strings: if Utils.hashMapper is None and not Utils.attemptedMakeHashMapper: try: Utils.hashMapper = ModulestoreImporter(os.path.join( os.path.dirname(__file__), '../json_to_relation/data/modulestore_latest.json'), useCache=True) except Exception as e: print( "Could not create a ModulestoreImporter in addAnonToActivityGradesTable.py: %s" % ` e `) Utils.attemptedMakeHashMapper = True
def testModulestoreImportLookups(self): testFilePath = os.path.join(os.path.dirname(__file__), 'data/modulestore_sample.json') pickleFilePath = os.path.join(os.path.dirname(__file__), 'data/modulestore_sample.pkl') importer = ModulestoreImporter(testFilePath, useCache=True, pickleCachePath=pickleFilePath) # The hash info: self.assertEqual(importer.getDisplayName("Introduction_to_Sociology"), 'Introduction to Sociology') self.assertEqual(importer.getDisplayName("0c6cf38317be42e0829d10cc68e7451b"), 'Quiz') self.assertEqual(importer.getDisplayName("0d6e5f3139e74c88adfdf4c90773f87f"), 'New Unit') self.assertEqual(importer.getOrg("0d6e5f3139e74c88adfdf4c90773f87f"), 'Medicine') self.assertEqual(importer.getCourseShortName("dc9cb4ff0c9241d2b9e075806490f992"), 'HRP258') self.assertEqual(importer.getCategory("Annotation"), 'annotatable') self.assertEqual(importer.getRevision("18b21999d6424f4ca04fe2bbe188fc9e"), 'draft') # Short coursenames to long coursenames: self.assertEqual(importer['SOC131'], 'LaneCollege/SOC131/Introduction_to_Sociology') self.assertEqual(importer.keys(), [u'SOC131']) self.assertEqual(importer.values(), [u'LaneCollege/SOC131/Introduction_to_Sociology']) self.assertEqual(importer.items(), [(u'SOC131', u'LaneCollege/SOC131/Introduction_to_Sociology')])
# Add json_to_relation source dir to $PATH # for duration of this execution: source_dir = [ os.path.join(os.path.dirname(os.path.abspath(__file__)), "../json_to_relation/") ] source_dir.extend(sys.path) sys.path = source_dir from modulestoreImporter import ModulestoreImporter from edxTrackLogJSONParser import EdXTrackLogJSONParser if __name__ == '__main__': USAGE = 'Usage: lookupOpenEdxHash.py hashStr1 hashstr2 ...' if len(sys.argv) < 2: print(USAGE) sys.exit() hashLookup = ModulestoreImporter(os.path.join( os.path.dirname(__file__), '../json_to_relation/data/modulestore_latest.json'), useCache=True) for hashStr in sys.argv[1:]: match = EdXTrackLogJSONParser.findHashPattern.search(hashStr) if match is not None: print(hashLookup.getDisplayName(match.group(1))) else: print 'None'
@author: paepcke ''' import os import re import sys from edxTrackLogJSONParser import EdXTrackLogJSONParser from modulestoreImporter import ModulestoreImporter from unidecode import unidecode idExtractPat = re.compile(r'^"([^"]*)') seqIDExtractPat = re.compile(r'","([^"]*)') hashLookup = ModulestoreImporter(os.path.join(os.path.dirname(__file__), 'data/modulestore_latest.json'), useCache=True) def makeInsertSafe(unsafeStr): ''' Makes the given string safe for use as a value in a MySQL INSERT statement. Looks for embedded CR or LFs, and turns them into semicolons. Escapes commas and single quotes. Backslash is replaced by double backslash. This is needed for unicode, like \0245 (invented example) @param unsafeStr: string that possibly contains unsafe chars @type unsafeStr: String @return: same string, with unsafe chars properly replaced or escaped @rtype: String '''
# long strings in tracking logs that contain that hex number somewhere # inside it # Add json_to_relation source dir to $PATH # for duration of this execution: source_dir = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "../json_to_relation/")] source_dir.extend(sys.path) sys.path = source_dir from modulestoreImporter import ModulestoreImporter from edxTrackLogJSONParser import EdXTrackLogJSONParser if __name__ == '__main__': USAGE = 'Usage: lookupOpenEdxHash.py hashStr1 hashstr2 ...' if len(sys.argv) < 2: print(USAGE) sys.exit() hashLookup = ModulestoreImporter(os.path.join(os.path.dirname(__file__),'../json_to_relation/data/modulestore_latest.json'), useCache=True) for hashStr in sys.argv[1:]: match = EdXTrackLogJSONParser.findHashPattern.search(hashStr) if match is not None: print(hashLookup.getDisplayName(match.group(1))) else: print 'None'