def main(): import StringIO sys.stdin = StringIO.StringIO(test_text) mapper() sys.stdin = sys.__stdin__
def check_write_table(test_def, table): out = io.StringIO() asciitable.write(table, out, **test_def['kwargs']) print('Expected:\n%s' % test_def['out']) print('Actual:\n%s' % out.getvalue()) assert out.getvalue().splitlines() == test_def['out'].splitlines()
def setUp(self): self._user_story_runner_logging_stub = None self._formatted_exception_buffer = StringIO.StringIO() self._original_formatter = exception_formatter.PrintFormattedException
for file_path in to_rewrite(): if 'generated' in file_path: continue if (file_path.endswith('.h') or file_path.endswith('.c') or file_path.endswith('.m') or file_path.endswith('.mm') or file_path.endswith('.inc') or file_path.endswith('.fp') or file_path.endswith('.cc') or file_path.endswith('.cpp')): # Read the whole file into memory. lines = open(file_path).readlines() # Write it back out again line by line with substitutions for #includes. output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb') includes = [] for line in lines: parts = line.replace('<', '"').replace('>', '"').split('"') if (len(parts) == 3 and '#' in parts[0] and 'include' in parts[0] and os.path.basename(parts[1]) in headers): header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.')) includes.append(parts[0] + '"%s"' % header + parts[2]) else: for inc in sorted(includes): output.write(inc.strip('\n') + '\n') includes = [] output.write(line.strip('\n') + '\n')
''' def isalpha(ch): return ch.isalpha() def get_words_count(fp): result = {} for line in fp: for word in line.split(): word = filter(isalpha, word.lower()) if word in result: result[word] += 1 else: result[word] = 1 return result print get_words_count(StringIO.StringIO(text)) print '*' * 11, ' alternative ', '*' * 11 # alternative from collections import defaultdict def get_words_count_2(fp): result = defaultdict(int) for line in fp: for word in line.split(): word = filter(isalpha, word.lower()) result[word] += 1 return result print get_words_count_2(StringIO.StringIO(text))
def __init__(self, data): self._stm = StringIO.StringIO(data)
def __init__(self, dialect, connection, **kw): self.connection = connection self.buffer = StringIO.StringIO() self.preparer = dialect.identifier_preparer self.dialect = dialect
max_limit = 40 limit = 20 while limit < max_limit: scripdata_dict = get_hist_data_as_dataframes_dict(metadata=metadata, limit=limit) pan = pd.Panel(scripdata_dict) then0 = time.time() pr = cProfile.Profile() pr.enable() pan2 = pan.transpose(2, 0, 1) cl = pan2['close'] cl2 = cl[cl.iloc[:, -1] > cl.iloc[:, -2]] pan11 = pan[cl2.index] pr.disable() pr.dump_stats('vector.stats') s = StringIO.StringIO() sort_by = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sort_by) ps.print_stats(0.1) now0 = time.time() #print (limit, now0 - then0) #print (len(cl2.index)) #print (s.getvalue()) limit *= 2
def testParseInt(self): # 1 byte data = StringIO.StringIO("\x10\x00") plist = binplist.BinaryPlist(data) self.assertEqual(0, plist._ParseObject()) # 2 bytes data = StringIO.StringIO("\x11\x00\x01") plist = binplist.BinaryPlist(data) self.assertEqual(1, plist._ParseObject()) data = StringIO.StringIO("\x11\x01\x00") plist = binplist.BinaryPlist(data) self.assertEqual(256, plist._ParseObject()) # 4 bytes data = StringIO.StringIO("\x12\x00\x00\x00\x01") plist = binplist.BinaryPlist(data) self.assertEqual(1, plist._ParseObject()) # 8 bytes - should be unsigned data = StringIO.StringIO("\x13\x00\x00\x00\x00\x00\x00\x00\x01") plist = binplist.BinaryPlist(data) self.assertEqual(1, plist._ParseObject()) # Now with version 00 - signed data = StringIO.StringIO("\x13\x00\x00\x00\x00\x00\x00\x00\x01") plist = binplist.BinaryPlist(data) plist.version = "00" self.assertEqual(1, plist._ParseObject()) # 8 bytes - should be unsigned data = StringIO.StringIO("\x13\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE") plist = binplist.BinaryPlist(data) self.assertEqual((1 << 64) - 2, plist._ParseObject()) # Now with version 00 - signed data = StringIO.StringIO("\x13\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE") plist = binplist.BinaryPlist(data) plist.version = "00" self.assertEqual(-2, plist._ParseObject()) # 16 bytes - should be unsigned raw = "\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" data = StringIO.StringIO(raw) plist = binplist.BinaryPlist(data) self.assertEqual(1, plist._ParseObject()) raw = "\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" data = StringIO.StringIO(raw) plist = binplist.BinaryPlist(data) plist.version = "00" self.assertEqual(1, plist._ParseObject()) # 16 bytes - should be unsigned raw = "\x14\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" data = StringIO.StringIO(raw) plist = binplist.BinaryPlist(data) self.assertEqual((1 << 128) - 2, plist._ParseObject()) # Now with version 00 - signed raw = "\x14\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE" data = StringIO.StringIO(raw) plist = binplist.BinaryPlist(data) plist.version = "00" self.assertEqual(-2, plist._ParseObject()) # Test incomplete data data = StringIO.StringIO("\x10") plist = binplist.BinaryPlist(data) int_object = plist._ParseObject() self.assertTrue(isinstance(int_object, binplist.RawValue)) data = StringIO.StringIO("\x11\x12") plist = binplist.BinaryPlist(data) int_object = plist._ParseObject() self.assertTrue(isinstance(int_object, binplist.RawValue)) # Test unknown size data = StringIO.StringIO("\x16\x00\x00") plist = binplist.BinaryPlist(data) int_object = plist._ParseObject() self.assertEqual(int_object, binplist.RawValue("\x00\x00"))
def add_repo(repo, rdir_in): """This will add a git repo into the mongo modulestore""" # pylint: disable=R0915 # Set defaults even if it isn't defined in settings mongo_db = { 'host': 'localhost', 'user': '', 'password': '', 'db': 'xlog', } # Allow overrides if hasattr(settings, 'MONGODB_LOG'): for config_item in [ 'host', 'user', 'password', 'db', ]: mongo_db[config_item] = settings.MONGODB_LOG.get( config_item, mongo_db[config_item]) if not os.path.isdir(GIT_REPO_DIR): raise GitImportError(GitImportError.NO_DIR) # pull from git if not (repo.endswith('.git') or repo.startswith( ('http:', 'https:', 'git:', 'file:'))): raise GitImportError(GitImportError.URL_BAD) if rdir_in: rdir = os.path.basename(rdir_in) else: rdir = repo.rsplit('/', 1)[-1].rsplit('.git', 1)[0] log.debug('rdir = {0}'.format(rdir)) rdirp = '{0}/{1}'.format(GIT_REPO_DIR, rdir) if os.path.exists(rdirp): log.info('directory already exists, doing a git pull instead ' 'of git clone') cmd = [ 'git', 'pull', ] cwd = rdirp else: cmd = [ 'git', 'clone', repo, ] cwd = GIT_REPO_DIR cwd = os.path.abspath(cwd) try: ret_git = cmd_log(cmd, cwd=cwd) except subprocess.CalledProcessError as ex: log.exception('Error running git pull: %r', ex.output) raise GitImportError(GitImportError.CANNOT_PULL) # get commit id cmd = [ 'git', 'log', '-1', '--format=%H', ] try: commit_id = cmd_log(cmd, cwd=rdirp) except subprocess.CalledProcessError as ex: log.exception('Unable to get git log: %r', ex.output) raise GitImportError(GitImportError.BAD_REPO) ret_git += '\nCommit ID: {0}'.format(commit_id) # get branch cmd = [ 'git', 'symbolic-ref', '--short', 'HEAD', ] try: branch = cmd_log(cmd, cwd=rdirp) except subprocess.CalledProcessError as ex: # I can't discover a way to excercise this, but git is complex # so still logging and raising here in case. log.exception('Unable to determine branch: %r', ex.output) raise GitImportError(GitImportError.BAD_REPO) ret_git += '{0}Branch: {1}'.format(' \n', branch) # Get XML logging logger and capture debug to parse results output = StringIO.StringIO() import_log_handler = logging.StreamHandler(output) import_log_handler.setLevel(logging.DEBUG) logger_names = [ 'xmodule.modulestore.xml_importer', 'git_add_course', 'xmodule.modulestore.xml', 'xmodule.seq_module', ] loggers = [] for logger_name in logger_names: logger = logging.getLogger(logger_name) logger.setLevel(logging.DEBUG) logger.addHandler(import_log_handler) loggers.append(logger) try: management.call_command('import', GIT_REPO_DIR, rdir, nostatic=not GIT_IMPORT_STATIC) except CommandError: raise GitImportError(GitImportError.XML_IMPORT_FAILED) except NotImplementedError: raise GitImportError(GitImportError.UNSUPPORTED_STORE) ret_import = output.getvalue() # Remove handler hijacks for logger in loggers: logger.setLevel(logging.NOTSET) logger.removeHandler(import_log_handler) course_id = 'unknown' location = 'unknown' # extract course ID from output of import-command-run and make symlink # this is needed in order for custom course scripts to work match = re.search('(?ms)===> IMPORTING course to location ([^ \n]+)', ret_import) if match: location = match.group(1).strip() log.debug('location = {0}'.format(location)) course_id = location.replace('i4x://', '').replace('/course/', '/').split('\n')[0].strip() cdir = '{0}/{1}'.format(GIT_REPO_DIR, course_id.split('/')[1]) log.debug('Studio course dir = {0}'.format(cdir)) if os.path.exists(cdir) and not os.path.islink(cdir): log.debug(' -> exists, but is not symlink') log.debug( subprocess.check_output([ 'ls', '-l', ], cwd=os.path.abspath(cdir))) try: os.rmdir(os.path.abspath(cdir)) except OSError: log.exception('Failed to remove course directory') if not os.path.exists(cdir): log.debug(' -> creating symlink between {0} and {1}'.format( rdirp, cdir)) try: os.symlink(os.path.abspath(rdirp), os.path.abspath(cdir)) except OSError: log.exception('Unable to create course symlink') log.debug( subprocess.check_output([ 'ls', '-l', ], cwd=os.path.abspath(cdir))) # store import-command-run output in mongo mongouri = 'mongodb://{user}:{password}@{host}/{db}'.format(**mongo_db) try: if mongo_db['user'] and mongo_db['password']: mdb = mongoengine.connect(mongo_db['db'], host=mongouri) else: mdb = mongoengine.connect(mongo_db['db'], host=mongo_db['host']) except mongoengine.connection.ConnectionError: log.exception('Unable to connect to mongodb to save log, please ' 'check MONGODB_LOG settings') cil = CourseImportLog( course_id=course_id, location=location, repo_dir=rdir, created=timezone.now(), import_log=ret_import, git_log=ret_git, ) cil.save() log.debug('saved CourseImportLog for {0}'.format(cil.course_id)) mdb.disconnect()
# # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see http://www.gnu.org/licenses/. # from distutils.core import setup import sys try: import tabnanny except ImportError: pass else: import StringIO stdout, stderr = sys.stdout, sys.stderr sys.stdout = co = StringIO.StringIO() sys.stderr = ce = StringIO.StringIO() # Tabnanny doesn't provide any mechanism other than print outs so we have # to capture the output tabnanny.check("ossie") sys.stdout = stdout sys.stderr = stderr if len(co.getvalue().strip()) != 0: print "Incosistent tab usage:" print co.getvalue() sys.exit(-1) ossiepy = [ 'ossie', 'ossie/apps', 'ossie/apps/qtbrowse', 'ossie/apps/rhlauncher', 'ossie/apps/rhlauncher/ui', 'ossie/cf', 'ossie/cf/CF', 'ossie/cf/CF__POA', 'ossie/cf/PortTypes', 'ossie/cf/PortTypes__POA', 'ossie/cf/StandardEvent',
def run_from_puppet(self): self.out = StringIO.StringIO() self.run() return self.out.getvalue()
def get_disp_fc3_txt(structure, parameters_data, force_sets): from phono3py.file_IO import write_cell_yaml from phonopy.structure.cells import get_supercell from aiida_phonopy.common.utils import phonopy_atoms_from_structure import StringIO dataset = force_sets.get_datasets3() supercell = get_supercell(phonopy_atoms_from_structure(structure), parameters_data.dict.supercell, symprec=parameters_data.dict.symmetry_tolerance) w = StringIO.StringIO() w.write("natom: %d\n" % dataset['natom']) num_first = len(dataset['first_atoms']) w.write("num_first_displacements: %d\n" % num_first) if 'cutoff_distance' in dataset: w.write("cutoff_distance: %f\n" % dataset['cutoff_distance']) num_second = 0 num_disp_files = 0 for d1 in dataset['first_atoms']: num_disp_files += 1 num_second += len(d1['second_atoms']) for d2 in d1['second_atoms']: if 'included' in d2: if d2['included']: num_disp_files += 1 else: num_disp_files += 1 w.write("num_second_displacements: %d\n" % num_second) w.write("num_displacements_created: %d\n" % num_disp_files) w.write("first_atoms:\n") count1 = 1 count2 = num_first + 1 for disp1 in dataset['first_atoms']: disp_cart1 = disp1['displacement'] w.write("- number: %5d\n" % (disp1['number'] + 1)) w.write(" displacement:\n") w.write(" [%20.16f,%20.16f,%20.16f ] # %05d\n" % (disp_cart1[0], disp_cart1[1], disp_cart1[2], count1)) w.write(" second_atoms:\n") count1 += 1 included = None atom2 = -1 for disp2 in disp1['second_atoms']: if atom2 != disp2['number']: atom2 = disp2['number'] if 'included' in disp2: included = disp2['included'] pair_distance = disp2['pair_distance'] w.write(" - number: %5d\n" % (atom2 + 1)) w.write(" distance: %f\n" % pair_distance) if included is not None: if included: w.write(" included: %s\n" % "true") else: w.write(" included: %s\n" % "false") w.write(" displacements:\n") disp_cart2 = disp2['displacement'] w.write(" - [%20.16f,%20.16f,%20.16f ] # %05d\n" % (disp_cart2[0], disp_cart2[1], disp_cart2[2], count2)) count2 += 1 write_cell_yaml(w, supercell) w.seek(0) lines = w.read() w.close() return lines
def load_bytes(self, data): import StringIO self.__loaded_bytes = data dfile = StringIO.StringIO(data) self.load(dfile)
def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False): OutputFileList = [] if self.FvFileType is not None: FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FvFileType, self.FvFileExtension) if IsSect : return FileList, self.Alignment Num = SecNum MaxFvAlignment = 0 for FvFileName in FileList: FvAlignmentValue = 0 if os.path.isfile(FvFileName): FvFileObj = open (FvFileName,'rb') FvFileObj.seek(0) # PI FvHeader is 0x48 byte FvHeaderBuffer = FvFileObj.read(0x48) # FV alignment position. FvAlignmentValue = 1 << (ord (FvHeaderBuffer[0x2E]) & 0x1F) FvFileObj.close() if FvAlignmentValue > MaxFvAlignment: MaxFvAlignment = FvAlignmentValue OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + Ffs.SectionSuffix.get("FV_IMAGE")) GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile) OutputFileList.append(OutputFile) # MaxFvAlignment is larger than or equal to 1K if MaxFvAlignment >= 0x400: if MaxFvAlignment >= 0x100000: #The max alignment supported by FFS is 16M. if MaxFvAlignment >= 0x1000000: self.Alignment = "16M" else: self.Alignment = str(MaxFvAlignment / 0x100000) + "M" else: self.Alignment = str (MaxFvAlignment / 0x400) + "K" else: # MaxFvAlignment is less than 1K self.Alignment = str (MaxFvAlignment) return OutputFileList, self.Alignment # # Generate Fv # if self.FvName is not None: Buffer = StringIO.StringIO('') Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName) if Fv is not None: self.Fv = Fv FvFileName = Fv.AddToBuffer(Buffer, self.FvAddr, MacroDict = Dict, Flag=IsMakefile) if Fv.FvAlignment is not None: if self.Alignment is None: self.Alignment = Fv.FvAlignment else: if GenFdsGlobalVariable.GetAlignment (Fv.FvAlignment) > GenFdsGlobalVariable.GetAlignment (self.Alignment): self.Alignment = Fv.FvAlignment else: if self.FvFileName is not None: FvFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FvFileName) if os.path.isfile(FvFileName): FvFileObj = open (FvFileName,'rb') FvFileObj.seek(0) # PI FvHeader is 0x48 byte FvHeaderBuffer = FvFileObj.read(0x48) # FV alignment position. FvAlignmentValue = 1 << (ord (FvHeaderBuffer[0x2E]) & 0x1F) # FvAlignmentValue is larger than or equal to 1K if FvAlignmentValue >= 0x400: if FvAlignmentValue >= 0x100000: #The max alignment supported by FFS is 16M. if FvAlignmentValue >= 0x1000000: self.Alignment = "16M" else: self.Alignment = str(FvAlignmentValue / 0x100000) + "M" else: self.Alignment = str (FvAlignmentValue / 0x400) + "K" else: # FvAlignmentValue is less than 1K self.Alignment = str (FvAlignmentValue) FvFileObj.close() else: if len (mws.getPkgPath()) == 0: EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in WORKSPACE: %s" % self.FvFileName, GenFdsGlobalVariable.WorkSpaceDir) else: EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in packages path:\n\t%s" % (self.FvFileName, '\n\t'.join(mws.getPkgPath()))) else: EdkLogger.error("GenFds", GENFDS_ERROR, "FvImageSection Failed! %s NOT found in FDF" % self.FvName) # # Prepare the parameter of GenSection # OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + SecNum + Ffs.SectionSuffix.get("FV_IMAGE")) GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile) OutputFileList.append(OutputFile) return OutputFileList, self.Alignment
def setUp(self): notrailer = "bplist00" # header self.notrailer = StringIO.StringIO(notrailer) # A bplist of size 32, which lacks a full trailer # A dumb parser will try to start parsing the trailer at the header shorttrailer = ( "bplist\x00\x00" # header "\x00\x00\x00\x00\x00" # unused "\x00" # sortversion "\x00" # offset int size "\x00" # object ref size "\x00\x00\x00\x00\x00\x00\x00\x00" # num objects "\x00\x00\x00\x00\x00\x00\x00\x00" # top object ) # This could be interpreted as # "bplis" # unused # "t" # sortversion # "\x00" # offset int size # "\x00" # object ref size # "\x00\x00\x00\x00\x00\x00\x00\x00" # num objects # "\x00\x00\x00\x00\x00\x00\x00\x00" # top object # "\x00\x00\x00\x00\x00\x00\x00\x00" # object table offset # Note that this is an invalid plist for OSX self.shorttrailer = StringIO.StringIO(shorttrailer) # The smallest possible non-overlapping bplist # This is still not a valid plist for OSX minimal = ( "bplist00" # header "\x00\x00\x00\x00\x00" # unused "\x01" # sortversion "\x00" # offset int size "\x00" # object ref size "\x00\x00\x00\x00\x00\x00\x00\x00" # num objects "\x00\x00\x00\x00\x00\x00\x00\x00" # top object "\x00\x00\x00\x00\x00\x00\x00\x00" # offset to offtable ) self.minimal = StringIO.StringIO(minimal) # bplist with a single element. This is the smallest possible plist # that"s accepted by OSX. single = ( "bplist00" # header "\x09" # offset table, points to the next byte "\x09" # True object "\x00\x00\x00\x00\x00" # unused "\x01" # sortversion "\x01" # offset int size "\x00" # object ref size "\x00\x00\x00\x00\x00\x00\x00\x01" # num objects "\x00\x00\x00\x00\x00\x00\x00\x00" # top object "\x00\x00\x00\x00\x00\x00\x00\x08" # offset to offtable ) self.single = StringIO.StringIO(single) # bplist with more offsets than objects (shouldn't be a problem) short = ( "bplist00" # header "\x09\x0a\x0b\x0c\x0d\x0e" # offset table "\x09" # True object "\x00\x00\x00\x00\x00" # unused "\x01" # sortversion "\x01" # offset int size "\x00" # object ref size "\x00\x00\x00\x00\x00\x00\x00\x03" # num objects "\x00\x00\x00\x00\x00\x00\x00\x00" # top object "\x00\x00\x00\x00\x00\x00\x00\x08" # offset to offtable ) self.short = StringIO.StringIO(short) # bplist with an offset table that starts past the file overflow = ( "bplist00" # header "\x09" # offset table, points to the next byte "\x09" # True object "\x00\x00\x00\x00\x00" # unused "\x01" # sortversion "\x01" # offset int size "\x00" # object ref size "\x00\x00\x00\x00\x00\x00\x00\x01" # num objects "\x00\x00\x00\x00\x00\x00\x00\x00" # top object "\x00\x00\x00\x00\x00\x00\xFF\xFF" # offset to offtable (OF) ) self.overflow = StringIO.StringIO(overflow) # The minimal valid XML plist. self.min_xml = StringIO.StringIO( '<?xml version="1.0" encoding="UTF-8"?>' '<plist></plist>')
def to_json(obj): "\n Converts 'obj' to an ASCII JSON string representation.\n " stm = StringIO.StringIO('') _to_json_object(stm, obj) return stm.getvalue()
def testParseArray(self): values = [ # (ref_size, object_offsets, expected_result, data) # Array of 0 objects (1, [], [], "\xA0"), # Array of 1 object ( 1, [0, 2], [False], ( "\xA1" # Array of 1 object "\x01" # Reference to object False "\x08" # False object )), # Array of 2 objects ( 1, [0, 3, 4], [False, True], ( "\xA2" # Array of 2 objects "\x01" # Reference to object False "\x02" # Reference to object True "\x08" # False object "\x09" # True object )), # Array of 2 objects, 1 nonexistant ( 1, [0, 3, 4], [False, binplist.CorruptReference], ( "\xA2" # Array of 2 objects "\x01" # Reference to object False "\x09" # Reference to a nonexistant object "\x08" # False object "\x09" # True object )), # Array of 2 objects, 1 out of bounds ( 1, [0, 3, 200], [False, binplist.CorruptReference], ( "\xA2" # Array of 2 objects "\x01" # Reference to object False "\x02" # Reference out of bounds "\x08" # False object "\x09" # True object )), # Array of 2 objects, 1 circular reference to the array itself ( 1, [0, 3, 4], [False, binplist.CorruptReference], ( "\xA2" # Array of 2 objects "\x01" # Reference to object False "\x00" # circular reference to the array "\x08" # False object "\x09" # True object )), # Array of 2 objects, one a False value. The other is an array that # has a reference to the first array. # Tests deep circular reference detection ( 1, [0, 3, 4], [False, [binplist.CorruptReference]], ( "\xA2" # Array of 2 objects "\x01" # Reference to object False "\x02" # Reference to the second array "\x08" # False object "\xA1" # Array of 1 object, points to the first array "\x00" # circular reference to the first array )), # Array with not enough elements. This is hardly possible # in a real world scenario because of the trailer always being # past the objects. However on a corrupt bplist the object offset might # be pointing to the last elements of the trailer, one of them # being interpreted as an array... Thus why this scenario. ( 1, [0, 3, 4], [binplist.CorruptReference, binplist.CorruptReference], ( "\xA2" # Array of 2 objects "\x01" # Reference to a nonexistant object )), ] for value in values: (ref_size, object_offsets, expected_result, data) = value fd = StringIO.StringIO(data) plist = binplist.BinaryPlist(fd) # Fill objects_traversed with the current value as if we had been called # by a normal _Parse plist.objects_traversed = {0} plist.object_ref_size = ref_size plist.object_offsets = object_offsets plist.object_count = len(object_offsets) result = plist._ParseObject() self.assertListEqual(expected_result, result) # Test that the circular reference detection helper is cleaned properly self.assertSetEqual(plist.objects_traversed, {0})
# else: # rawFileName = 'savnw_conp{}'.format(RawFileIndicator) rawFileName = refRaw.replace('.raw', '') savFile = rawFileName + '.sav' snpFile = rawFileName + '.snp' print('Event: {}'.format(event)) # get the nominal voltages as well as the fault impedance in ohms FaultBusNomVolt = float(BusDataDict[str(FaultBus)].NominalVolt) Zbase = FaultBusNomVolt**2 / Sbase # float since Sbase is a float Rohm = FaultRpu * Zbase # fault impedance in ohms # run simulation till just before the fault output = StringIO.StringIO() with silence(output): # load the sav and snp file psspy.case(savFile) psspy.rstr(snpFile) #output = StringIO.StringIO() with silence(output): ierr = psspy.strt(0, out_file) ierr = psspy.run(0, 0.1, 1, 1, 1) ierr = psspy.dist_branch_trip(L1Bus1, L1Bus2, L1cktID) #output = StringIO.StringIO() with silence(output): ierr = psspy.run(0, 0.2, 1, 1, 1) #fault on time outputStr = output.getvalue()
def testParseDict(self): values = [ # (ref_size, object_offsets, expected_result, data) # Dict of 0 objects (1, [], {}, "\xD0"), # Dict of 1 entry ( 1, [0, 3, 5], { "a": True }, ( "\xD1" # Dict of 1 entry "\x01" # Ref to key#1 "\x02" # Ref to val#1 "\x51a" # "a" (key#1) "\x09" # True (val#1) )), # Dict of 1 entry, a key being an integer ( 1, [0, 3, 5], { 1: True }, ( "\xD1" # Dict of 1 entry "\x01" # Ref to key#1 "\x02" # Ref to val#1 "\x10\x01" # 1 (key#1) "\x09" # True (val#1) )), # Dict of 1 entry, has a circular key ( 1, [0, 3, 5], { "corrupt:0": True }, ( "\xD1" # Dict of 1 entry "\x00" # Circular key "\x02" # Ref to val#1 "\x10\x01" # 1 (key#1) "\x09" # True (val#1) )), # Dict of 1 entry, has a circular value ( 1, [0, 3, 5], { 1: binplist.CorruptReference }, ( "\xD1" # Dict of 1 entry "\x01" # Ref to key#1 "\x00" # Circular value "\x10\x01" # 1 (key#1) "\x09" # True (val#1) )), # Dict of 1 entry, has both a circular key and a circular value ( 1, [0, 3, 5], { "corrupt:0": binplist.CorruptReference }, ( "\xD1" # Dict of 1 entry "\x00" # Circular key "\x00" # Circular value "\x10\x01" # 1 (key#1) "\x09" # True (val#1) )), # Dict of 1 entry, value is a list that contains a circular value ( 1, [0, 3, 5, 8], { "a": [binplist.CorruptReference, 1] }, ( "\xD1" # Dict of 1 entry "\x01" # key#1 "\x02" # val#1 "\x51a" # "a" (key#1) "\xA2\x00\x03" # Array with 2 elements. The dict and an integer "\x10\x01" # 1 )), # Dict of 2 entries ( 1, [0, 5, 7, 9, 10], { "a": False, "b": True }, ( "\xD2" # Dict of 2 entries "\x01" # key#1 "\x02" # key#2 "\x03" # val#2 "\x04" # val#2 "\x51a" # "a" "\x51b" # "b" "\x08" # False object "\x09" # True object )), # Dict with not enough references ( 1, [0], { "corrupt:1": binplist.CorruptReference }, ( "\xD1" # Dict of 1 entry "\x01" # key#1 )), # Dict with a nonexistant reference ( 1, [0], { "corrupt:32": binplist.CorruptReference }, ( "\xD1" # Dict of 1 entry "\x20" # key#1 "\x99" # val#1 )), ] for value in values: (ref_size, object_offsets, expected_result, data) = value fd = StringIO.StringIO(data) plist = binplist.BinaryPlist(fd) # Fill objects_traversed with the current value as if we had been called # by a normal _Parse plist.objects_traversed = {0} plist.object_ref_size = ref_size plist.object_offsets = object_offsets plist.object_count = len(object_offsets) result = plist._ParseObject() self.assertEqual(expected_result, result) # Test that the circular reference detection helper is cleaned properly self.assertSetEqual(plist.objects_traversed, {0})
try: pop.login(user, passwd) except "POP3Error", errmsg: err_handler("Error logging to POP server: %s" % errmsg, pop, smtp) msgdict = pop.list() for msgnum in msgdict.keys(): try: msg = pop.retrieve(msgnum) except "POP3Error", errmsg: err_handler("error retrieving message %d" % msgnum, pop, smtp) f = StringIO(string.joinfields(msg, '')) m = Message(f) (addr, name) = m.getaddr('From') try: smtp.mail_from('<' + addr + '>') except: err_handler("error sending message, returning it to host", pop, smtp) try: smtp.mail_to(user) except: err_handler("error sending message, returning it to host", pop, smtp)
def test_incomplete_input(self): s = StringIO.StringIO("X''.") self.assertRaises(EOFError, self.module.load, s)
def OpenBlob(self, blob_key): """Get blob contents as stream.""" return StringIO.StringIO(self._blobs[blobstore.BlobKey( unicode(blob_key))])
def __init__(self, response_string): self.response_string = response_string self._buffer = StringIO.StringIO(response_string)
def __init__(self, template): self._buffer = StringIO.StringIO() self._stream = None self.template = template self.delayedCall = None
def testGetBlob(self): out = StringIO.StringIO() self.api.Client(client_id=self.client_urn.Basename()).File( "fs/tsk/c/bin/rbash").GetBlob().WriteToStream(out) self.assertEqual(out.getvalue(), "Hello world")
install(package) # module doesn't exist, deal with it. try: __import__(package) print('Success with package, ' + package) except ImportError, e: print('Still could not import package ' + package + '. Please install this manually!') source_filename = os.getcwd() + '\Spiruel-beamprofiler-54ba931' if not os.path.exists(source_filename): zip_file_url = 'https://github.com/Spiruel/beamprofiler/zipball/master/' file_name = 'BiLBO' print('Downloading files...') import requests r = requests.get(zip_file_url, stream=True) z = zipfile.ZipFile(StringIO.StringIO(r.content)) z.extractall() else: print('\nzip already in working directory!') try: import cv2 print( '\n>>>BiLBO should now be fully installed onto your computer (under Spiruel-beamprofiler-*******). Simply run get_profiler.py!' ) except: print( '\n>>>BiLBO should now be downloaded onto your computer (under Spiruel-beamprofiler-*******). \nYou have not yet installed OpenCV and will need to do so in order for the programme to work.' ) else:
def testGetTimelineAsCsv(self): out = StringIO.StringIO() self.api.Client(client_id=self.client_urn.Basename()).File( "fs").GetTimelineAsCsv().WriteToStream(out) self.assertTrue(out.getvalue())
def add_metadata_and_convert(self, song_path, metadata=None, album_artwork=None, callback=_place_holder_callback): """ Uses pydub and mutagen to add specified metadata to the given song and uses pydub to convert the given song to mp3 :param song_path: Location of the song that needs to be opened :param metadata: metadata to add to the song, is a dictionary where keys are in ffmpeg format (see self.METADATA) :param album_artwork: file path that leads to a jpeg, png or url. :param callback: Called and passed a numeric value that represents the progress of this function :return: none """ self.logger.info("Adding metadata to %s" % song_path) self.logger.debug( "Album Artwork type: %s, length: %s" % (type(album_artwork), len(album_artwork) if album_artwork is not None else "n/a")) message = "None" if metadata is not None: message = "" for key, value in metadata.items(): if len(value) > 0: message += "{}: '{}' \n".format(key, value) else: del metadata[key] self.logger.debug("Metadata: " + message) callback(0.3) try: song = pydub.AudioSegment.from_file(song_path, self.audio.extension) song_path = song_path[:-4] + ".mp3" # removes extension from the given song path and replaces it with .mp3 if len(metadata) > 0: song.export(song_path, format='mp3', tags=metadata) else: song.export(song_path, format='mp3') callback(0.4) if album_artwork is not None: self.logger.info("Album artwork has been given") song = MP3(song_path, ID3=ID3) aa_extension = album_artwork[-3:] self.logger.debug( "Album artwork extension: {}".format(aa_extension)) if aa_extension in ['jpg', 'png']: song.tags.add( APIC( encoding=3, # 3 is for utf-8 # this specifies what type of image to use mime='image/{}'.format('jpeg' if aa_extension == 'jpg' else aa_extension), type=3, # this is for a cover image desc=u'Cover', data=open(album_artwork).read() if os.path.exists(album_artwork) is True else StringIO.StringIO( self.get_artwork(album_artwork)).read())) song.save() callback(0.7) else: raise MetadataError( "Given album artwork located at %s is not a png or jpg." % album_artwork) else: self.logger.info("No album artwork given to add as metadata") try: # when pydub song is exported, it doesn't cleanup the old audio file. old_song_path = song_path[:-4] + "." + self.audio.extension # old song is not mp3- must use old extension self.logger.info("Removing old {} file located at {}".format( self.audio.extension, old_song_path)) os.remove(old_song_path) callback(0.9) except OSError: self.logger.warning("Failed to remove old song") except: self.logger.error("Failed to add metadata and/or convert to mp3", exc_info=True) raise else: self.logger.info( "Metadata adding process and converting process completed.") callback(1)
def pprint_str(thing): sio = StringIO.StringIO() pprint(thing, '', sio) return sio.getvalue()