def test_parse(self): f = FileDownloader() f.parse_args([]) self.assertFalse(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure']) self.assertTrue(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure', '--cacert', this_file()]) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure', 'bar', '--cacert', this_file()]) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertEqual(f.target, 'bar') f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--cacert']) self.assertIn('argument --cacert: expected one argument', io.getvalue()) f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--cacert', '--insecure']) self.assertIn('argument --cacert: expected one argument', io.getvalue()) f = FileDownloader() with self.assertRaisesRegexp( RuntimeError, "--cacert='nonexistant_file_name' does " "not refer to a valid file"): f.parse_args(['--cacert', 'nonexistant_file_name']) f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--foo']) self.assertIn('error: unrecognized arguments: --foo', io.getvalue())
def get_example_matches(fm, confidence="medium", number_to_plot=2): matches = fm.get_condifence_matches(confidence) number_matches = len(matches) if number_matches == 0: return 0, 0, None, None index_to_plot_1 = np.random.randint(0, number_matches) index_to_plot_2 = np.random.randint(0, number_matches) match1 = matches[index_to_plot_1] match2 = matches[index_to_plot_2] m1 = match1.spectrum m2 = match2.spectrum ymax = np.max([np.max(m1.values), np.max(m2.values)]) + 50 #string = '%d matches found' % number_matches fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(13, 5)) plt.ylim(ymin=-200, ymax=ymax) ax1.set(xlabel='Shift (cm$^{-1}$)') ax1.set(ylabel='Intensity') ax2.set(xlabel='Shift (cm$^{-1}$)') ax2.set(ylabel='Intensity') m1.plot(ax=ax1) m2.plot(ax=ax2) io = StringIO() fig.savefig(io, format='png') plt.close(fig) return number_matches, base64.encodestring(io.getvalue()), match1, match2
def test_unquoted_key_state_without_whitespace(self): io = StringIO() uks = UnquotedKeyState(io) for c in 'hello_world': self.assertFalse(uks.character(c)) self.assertTrue(uks.character('=')) self.assertEquals('hello_world', io.getvalue())
def _getplotdata(format='png'): import io io = io.StringIO() plt.savefig(io, format=format) val = io.getvalue() io.close() return val
def output_entry(entry, profile, limit_fields=None): # debug build assertion that limit_fields only contains fields we know about if __debug__ and limit_fields is not None: assert len([f for f in limit_fields if f not in _field_order]) == 0 fmt = profile["_formatter"] io = io.StringIO() io.write(out_line("BEGIN", None, "VCARD", None)) io.write(out_line("VERSION", None, profile["_version"], None)) if limit_fields is None: fields = _field_order else: fields = [f for f in _field_order if f in limit_fields] for f in fields: if f in entry and f in profile: func = profile[f] # does it have a limit? (nice scary introspection :-) if "limit" in func.__code__.co_varnames[:func.__code__.co_argcount]: lines = func(entry[f], fmt, limit = profile["_limit"]) else: lines = func(entry[f], fmt) if len(lines): io.write(lines) io.write(out_line("END", None, "VCARD", fmt)) return io.getvalue()
def output_entry(entry, profile, limit_fields=None): # debug build assertion that limit_fields only contains fields we know about if __debug__ and limit_fields is not None: assert len([f for f in limit_fields if f not in _field_order]) == 0 fmt = profile["_formatter"] io = io.StringIO() io.write(out_line("BEGIN", None, "VCARD", None)) io.write(out_line("VERSION", None, profile["_version"], None)) if limit_fields is None: fields = _field_order else: fields = [f for f in _field_order if f in limit_fields] for f in fields: if f in entry and f in profile: func = profile[f] # does it have a limit? (nice scary introspection :-) if "limit" in func.__code__.co_varnames[:func.__code__. co_argcount]: lines = func(entry[f], fmt, limit=profile["_limit"]) else: lines = func(entry[f], fmt) if len(lines): io.write(lines) io.write(out_line("END", None, "VCARD", fmt)) return io.getvalue()
def plot_random_baseline_example(fm, confidence="medium", number_to_plot=2): fig, ax = plt.subplots(1, 1) fm.random_sample_compare_before_subtract_baseline.plot(ax=ax) fm.random_sample_compare_after_subtract_baseline.plot(ax=ax) ax.set_title("Example with/without baseline subtraction") ax.set(xlabel='Shift (cm$^{-1}$)') ax.set(ylabel='Intensity') io = StringIO() fig.savefig(io, format='png') plt.close(fig) baseline_example = base64.encodestring(io.getvalue()) number_matches_med, data, match1, match2 = get_example_matches( fm, "medium", number_to_plot=2) number_matches_high = len(fm.matches.high_confidence) all_matches = len(fm.matches.matches) if number_matches_med == 0: return render_template('plot_data.html', number_matches_med=number_matches_med, number_matches_high=number_matches_high, all_matches=all_matches, number_locations=fm.len, filename=fm.data_filename, material="graphene_oxide", subtract_baseline=False) template_data = fm.material.template fig, (ax1) = plt.subplots(1, 1, sharex=True, sharey=True, figsize=(13, 5)) ax1.set(xlabel='Shift (cm$^{-1}$)') ax1.set(ylabel='Intensity') ax1.plot(template_data) io = StringIO() fig.savefig(io, format='png') plt.close(fig) template = base64.encodestring(io.getvalue()) return render_template('plot_data.html', number_matches_med=number_matches_med, number_matches_high=number_matches_high, all_matches=all_matches, template=template, best_match=template, number_locations=fm.len, match_example=data, baseline_example=baseline_example, filename=fm.data_filename, material="graphene_oxide", subtract_baseline=True, match1=match1.to_dict(), match2=match2.to_dict())
def kill(self, sig): """Send a signal to the subprocess. This may or may not kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ now = time.time() options = self.config.options if not self.pid: msg = ("attempted to kill %s with sig %s but it wasn't running" % (self.config.name, signame(sig))) options.logger.debug(msg) return msg #If we're in the stopping state, then we've already sent the stop #signal and this is the kill signal if self.state == ProcessStates.STOPPING: killasgroup = self.config.killasgroup else: killasgroup = self.config.stopasgroup as_group = "" if killasgroup: as_group = "process group " options.logger.debug( 'killing %s (pid %s) %swith signal %s' % (self.config.name, self.pid, as_group, signame(sig))) # RUNNING/STARTING/STOPPING -> STOPPING self.killing = 1 self.delay = now + self.config.stopwaitsecs # we will already be in the STOPPING state if we're doing a # SIGKILL as a result of overrunning stopwaitsecs self._assertInState(ProcessStates.RUNNING, ProcessStates.STARTING, ProcessStates.STOPPING) self.change_state(ProcessStates.STOPPING) pid = self.pid if killasgroup: # send to the whole process group instead pid = -self.pid try: options.kill(pid, sig) except: io = io.StringIO() traceback.print_exc(file=io) tb = io.getvalue() msg = 'unknown problem killing %s (%s):%s' % (self.config.name, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 self.killing = 0 self.delay = 0 return msg return None
def test_quoted_value_state_eof(self): io = StringIO() qvs = QuotedValueState(io) for c in '\\"this is my string,\\" he said!': self.assertFalse(qvs.character(c)) self.assertFalse(qvs.character('"')) self.assertTrue(qvs.close()) self.assertEquals('"this is my string," he said!', io.getvalue())
def test_quoted_key_state(self): io = StringIO() qks = QuotedKeyState(io) for c in '\\"this is my string,\\" he said!': self.assertFalse(qks.character(c)) self.assertFalse(qks.character('"')) self.assertFalse(qks.character(' ')) self.assertFalse(qks.character('\r')) self.assertTrue(qks.character('=')) self.assertEquals('"this is my string," he said!', io.getvalue())
def read(self, n=-1): data = self.stream.read(n) # FIXME: Support odd length reads assert len(data) % 2 == 0 io = BytesIO() for i in range(0, len(data), 2): io.write(data[i + 1:i + 2]) io.write(data[i:i + 1]) io.seek(0) return io.getvalue()
def get_xml(file): io = StringIO() dumppdf.dumppdf(io, file, [], set(), '', dumpall=True, codec=None, extractdir=None) return io.getvalue()
def read(self, n=-1): data = self.stream.read(n) # FIXME: Support odd length reads assert len(data) % 2 == 0 io = BytesIO() for i in range(0, len(data), 2): io.write(data[i + 1 : i + 2]) io.write(data[i : i + 1]) io.seek(0) return io.getvalue()
def test_value_leading_whitespace_state_quoted_value(self): io = StringIO() vlws = ValueLeadingWhitespaceState(io) self.assertFalse(vlws.character(' ')) self.assertFalse(vlws.character('"')) self.assertFalse(vlws.character('\\')) self.assertFalse(vlws.character('"')) self.assertFalse(vlws.character('"')) self.assertTrue(vlws.character(',')) self.assertEquals('"', io.getvalue())
def test_read(self): io = StringIO() writer = excelcsv.DictWriter(io, [u'1', u'2', u'3']) writer.writeheader() writer.writerow({u'1': u'a', u'2': u'b', u'3': u'c'}) writer.writerow({u'1': u'd', u'2': u'e', u'3': u'f'}) value = io.getvalue() self.assertIsInstance(value, text_type) self.assertEqual(value, u'1,2,3\r\na,b,c\r\nd,e,f\r\n') io.close()
def writePlistToString(rootObject, binary=True): if not binary: rootObject = wrapDataObject(rootObject, binary) if six.PY3: return plistlib.writePlistToBytes(rootObject) else: return plistlib.writePlistToString(rootObject) else: io = BytesIO() writer = PlistWriter(io) writer.writeRoot(rootObject) return io.getvalue()
def exec_console(self): index = self.current_frame_index if index < 0: return # cache frame variables (globals and locals) # because every time we ask for frame.f_locals, a new dict instance # is created - we keep our local cache that may contain some changes if self.frame_vars[index] is None: #print "init", index frame = frame_from_traceback(self.tb, index) self.frame_vars[index] = (dict(frame.f_globals), dict(frame.f_locals)) frame_vars = self.frame_vars[index] #print frame_vars[1] line = self.console.text() try: c = self.compiler(line, "<console>", "single") except (OverflowError, SyntaxError, ValueError) as e: QMessageBox.critical(self, "Error", str(e)) return if c is None: QMessageBox.critical(self, "Error", "Code not complete") return import io io = io.StringIO() if sys.version_info.major >= 3 else io.BytesIO() try: with stdout_redirected(io): exec(c, frame_vars[0], frame_vars[1]) except: etype, value, tb = sys.exc_info() QMessageBox.critical(self, "Error", etype.__name__ + "\n" + str(value)) return stuff = self.console_outs[index] stuff += ">>> " + line + "\n" stuff += io.getvalue() self.console_outs[index] = stuff self.console_out.setPlainText(stuff) self.console_out.setVisible(True) # make sure we are at the end c = self.console_out.textCursor() c.movePosition(QTextCursor.End) self.console_out.setTextCursor(c) self.console_out.ensureCursorVisible() self.console.setText('')
def __exit__(self, etype, e, trace): """ Ensure that non-UserError exceptions rollback the transaction. """ try: show_all_traces = self.ctx and self.ctx.get( 'show_all_traces', False).value except: show_all_traces = False try: if (etype is errors.TestError): self.commit() return False elif (etype is EnvironmentError): self.rollback() return False elif (isinstance(e, errors.UserError) and not show_all_traces): self.commit() err = str(e) log.info('Sending normal exception to user: %s' % err) if (self.queue is not None): self.send_message( self.ctx.get_id(), dict( command='write', text=err, is_error=True, )) return True elif (etype is not None): if (rollback_after_fatal_errors): self.rollback() else: self.commit() import traceback, io io = io.StringIO() traceback.print_exception(etype, e, trace, None, io) log.error('Sending fatal exception to user: %s' % str(e)) if (self.queue is not None): self.send_message( self.ctx.get_id(), dict( command='write', text=io.getvalue(), is_error=True, )) return True else: self.commit() finally: self.flush()
def test_fetch_url_content(monkeypatch): response = pretend.stub( raise_for_status=pretend.call_recorder(lambda: None), content=b"fake content") requests = pretend.stub(get=pretend.call_recorder(lambda url: response)) monkeypatch.setattr(utils, "requests", requests) io = utils.fetch_url_content("hxxp://fake_url.com") assert requests.get.calls == [pretend.call("hxxp://fake_url.com")] assert response.raise_for_status.calls == [pretend.call()] assert io.getvalue() == b"fake content"
def generate_thumb(img, thumb_size, format): """ Generates a thumbnail image and returns a ContentFile object with the thumbnail Parameters: =========== img File object thumb_size desired thumbnail size, ie: (200,120) format format of the original image ('jpeg','gif','png',...) (this format will be used for the generated thumbnail, too) """ img.seek(0) # see http://code.djangoproject.com/ticket/8222 for details image = Image.open(img) # Convert to RGB if necessary if image.mode not in ('L', 'RGB', 'RGBA'): image = image.convert('RGB') # get size thumb_w, thumb_h = thumb_size # If you want to generate a square thumbnail if thumb_w == thumb_h: # quad xsize, ysize = image.size # get minimum size minsize = min(xsize, ysize) # largest square possible in the image xnewsize = (xsize - minsize) / 2 ynewsize = (ysize - minsize) / 2 # crop it image2 = image.crop( (xnewsize, ynewsize, xsize - xnewsize, ysize - ynewsize)) # load is necessary after crop image2.load() # thumbnail of the cropped image (with ANTIALIAS to make it look better) image2.thumbnail(thumb_size, Image.ANTIALIAS) else: # not quad image2 = image image2.thumbnail(thumb_size, Image.ANTIALIAS) io = io.StringIO() # PNG and GIF are the same, JPG is JPEG if format.upper() == 'JPG': format = 'JPEG' image2.save(io, format) return ContentFile(io.getvalue())
def generate_thumb(img, thumb_size, format): """ Generates a thumbnail image and returns a ContentFile object with the thumbnail Parameters: =========== img File object thumb_size desired thumbnail size, ie: (200,120) format format of the original image ('jpeg','gif','png',...) (this format will be used for the generated thumbnail, too) """ img.seek(0) # see http://code.djangoproject.com/ticket/8222 for details image = Image.open(img) # Convert to RGB if necessary if image.mode not in ('L', 'RGB'): image = image.convert('RGB') # get size thumb_w, thumb_h = thumb_size # If you want to generate a square thumbnail if thumb_w == thumb_h and False: # quad xsize, ysize = image.size # get minimum size minsize = min(xsize,ysize) # largest square possible in the image xnewsize = (xsize-minsize)/2 ynewsize = (ysize-minsize)/2 # crop it image2 = image.crop((xnewsize, ynewsize, xsize-xnewsize, ysize-ynewsize)) # load is necessary after crop image2.load() # thumbnail of the cropped image (with ANTIALIAS to make it look better) image2.thumbnail(thumb_size, Image.ANTIALIAS) else: # not quad image2 = image image2.thumbnail(thumb_size, Image.ANTIALIAS) io = io.StringIO() # PNG and GIF are the same, JPG is JPEG if format.upper()=='JPG': format = 'JPEG' image2.save(io, format) return ContentFile(io.getvalue())
def cleanup_conn(cls, dump_log=False): logs = None if hasattr(_thread_locals, 'conn'): _thread_locals.conn.cleanup() if dump_log: io = io.StringIO.StringIO() _thread_locals.conn.dump_log(io) logs = io.getvalue() del _thread_locals.conn else: logs = ['No thread locals?'] return logs
def test_to_ini_empty(self): io = io.StringIO() self.options.write_ini(io) contents = io.getvalue() lines = [] lines.append('[options]') lines.append('true = 1') lines.append('false = 0') lines.append('') lines.append('[ints]') lines.append('num99 = 99') lines.append('') lines.append('') self.assertEqual('\n'.join(lines), contents)
def encode_cfg(data): import io import configparser cfg = configparser.ConfigParser() io = io.StringIO() for key, val in sorted(data.items()): if isinstance(val, dict): cfg.add_section(key) for option, option_val in sorted(val.items()): cfg.set(key, option, str(option_val)) else: cfg.set("DEFAULT", key, str(val)) cfg.write(io) return io.getvalue()
def create_random_image(image_format, shape): """Creates an image with random values. Args: image_format: An image format (PNG or JPEG). shape: A tuple with image shape (including channels). Returns: A tuple (<numpy ndarray>, <a string with encoded image>) """ image = np.random.randint(low=0, high=255, size=shape, dtype='uint8') io = io.StringIO() image_pil = PILImage.fromarray(image) image_pil.save(io, image_format, subsampling=0, quality=100) return image, io.getvalue()
def wild_assumptions(module, language): '''Assume that any exception is not our fault.''' try: generator = module and getattr(module, language + "HTMLGenerator") if generator: io = StringIO() generator().generate_html(io, '\n'.join(content)) html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue() else: html = '<div class="code-block">\n%s\n</div>\n' % '<br>\n'.join( content) raw = docutils.nodes.raw('', html, format='html') return [raw] except Exception: # Return html as shown. Lines are separated by <br> elements. trace('Assume that the bug occurred in code that was called.') return [None]
def send(self, data, flags=0): io = BytesIO() io.write(data) buffer = io.getvalue() self.__send_lock.acquire() try: return self.__iowait(self._connection.send, buffer, flags) except OpenSSL.SSL.SysCallError as e: if e.args[0] == -1 and not data: # errors when writing empty strings are expected and can be ignored return 0 raise finally: self.__send_lock.release()
def send(self, data, flags=0): io = StringIO() io.write(data) buffer = io.getvalue() self.__send_lock.acquire() try: return self.__iowait(self._connection.send, buffer, flags) except OpenSSL.SSL.SysCallError as e: if e.args[0] == -1 and not data: # errors when writing empty strings are expected and can be ignored return 0 raise finally: self.__send_lock.release()
def __exit__(self, etype, e, trace): """ Ensure that non-UserError exceptions rollback the transaction. """ try: show_all_traces = self.ctx and self.ctx.get('show_all_traces', False).value except: show_all_traces = False try: if(etype is errors.TestError): self.commit() return False elif(etype is EnvironmentError): self.rollback() return False elif(isinstance(e, errors.UserError) and not show_all_traces): self.commit() err = str(e) log.info('Sending normal exception to user: %s' % err) if(self.queue is not None): self.send_message(self.ctx.get_id(), dict( command = 'write', text = err, is_error = True, )) return True elif(etype is not None): if(rollback_after_fatal_errors): self.rollback() else: self.commit() import traceback, io io = io.StringIO() traceback.print_exception(etype, e, trace, None, io) log.error('Sending fatal exception to user: %s' % str(e)) if(self.queue is not None): self.send_message(self.ctx.get_id(), dict( command = 'write', text = io.getvalue(), is_error = True, )) return True else: self.commit() finally: self.flush()
def code_block(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): language = arguments[0] try: module = getattr(SilverCity, language) generator = getattr(module, language + "HTMLGenerator") except AttributeError: error = state_machine.reporter.error( "No SilverCity lexer found for language '%s'." % language, docutils.nodes.literal_block(block_text, block_text), line=lineno) return [error] io = io.StringIO() generator().generate_html(io, '\n'.join(content)) html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue() raw = docutils.nodes.raw('', html, format='html') return [raw]
def image(filename): try: w = int(request.args['w']) h = int(request.args['h']) except (KeyError, ValueError): return send_from_directory('.', filename) try: im = Image.open(filename) im.thumbnail((w, h), Image.ANTIALIAS) io = io.StringIO() im.save(io, format='JPEG') return Response(io.getvalue(), mimetype='image/jpeg') except IOError: abort(404) return send_from_directory('.', filename)
def more(self): if self.finished: return '' try: response = self.callback() if response is NOT_DONE_YET: return NOT_DONE_YET self.finished = True return self.sendresponse(response) except: io = io.StringIO() traceback.print_exc(file=io) # this should go to the main supervisor log file self.request.channel.server.logger.log('Web interface error', io.getvalue()) self.finished = True self.request.error(500)
def generate_thumb(self, img, thumb_size, file_format): # return img import io from PIL import Image img.seek( 0) # see http://code.djangoproject.com/ticket/8222 for details image = Image.open(img) # Convert to RGB if necessary if image.mode not in ('L', 'RGB', 'RGBA'): image = image.convert('RGB') # get size thumb_w, thumb_h = thumb_size # If you want to generate a square thumbnail if thumb_w == thumb_h and False: # quad xsize, ysize = image.size # get minimum size minsize = min(xsize, ysize) # largest square possible in the image xnewsize = (xsize - minsize) / 2 ynewsize = (ysize - minsize) / 2 # crop it image2 = image.crop( (xnewsize, ynewsize, xsize - xnewsize, ysize - ynewsize)) # load is necessary after crop image2.load() # thumbnail of the cropped image (with ANTIALIAS to make it look better) image2.thumbnail(thumb_size, Image.ANTIALIAS) else: # not quad image2 = image image2.thumbnail(thumb_size, Image.ANTIALIAS) io = io.StringIO() # PNG and GIF are the same, JPG is JPEG if file_format.upper() == 'JPG': file_format = 'JPEG' image2.save(io, file_format) return ContentFile(io.getvalue())
def plot_coverage(self, siteid): """ Makes a bar plot (ganntt like) for the time coverage of datasets at a site """ session = db.Session() web.setmime('image/png') try: import matplotlib matplotlib.use('Agg', warn=False) import pylab as plt import numpy as np ds = session.query(db.Dataset).filter_by( _site=int(siteid)).order_by(db.Dataset._source, db.Dataset._valuetype, db.Dataset.start).all() left = plt.date2num([d.start for d in ds]) right = plt.date2num([d.end for d in ds]) btm = np.arange(-.5, -len(ds), -1) # return 'left=' + str(left) + ' right=' + str(right) + ' btm=' + str(btm) fig = plt.figure() ax = fig.gca() ax.barh(left=left, width=right - left, bottom=btm, height=0.9, fc='0.75', ec='0.5') for l, b, d in zip(left, btm, ds): ax.text(l, b + .5, '#%i' % d.id, color='k', va='center') ax.xaxis_date() ax.set_yticks(btm + .5) ax.set_yticklabels( [d.source.name + '/' + d.valuetype.name for d in ds]) ax.set_position([0.3, 0.05, 0.7, 0.9]) ax.set_title('Site #' + siteid) ax.set_ylim(-len(ds) - .5, .5) ax.grid() st = io.BytesIO() fig.savefig(st, dpi=100) finally: session.close() return io.getvalue()
def download(): if request.method == 'POST': data = request.form['csv'] data_length = request.form['length'] csv = data.split(',') final_csv = [] temp_arr = [] for index, val in enumerate(csv): index = index + 1 temp_arr.append(str(val)) if index % int(data_length) == 0 and index != 1: final_csv.append(temp_arr) temp_arr = [] sheet = pe.Sheet(final_csv) io = StringIO.StringIO() sheet.save_to_memory("csv", io) output = make_response(io.getvalue()) output.headers["Content-Disposition"] = "attachment; filename=customer_export.csv" output.headers["Content-type"] = "text/csv" return output
def gather_archives(mailbox_url, search_terms, destio): """Download the mailbox at mailbox_url, and write every message containing one of the terms in search_terms, or a message (transitively) in reply to one of those messages, to destio, the output mailbox stream.""" io = cStringIO.StringIO() fetch_https_securely(mailbox_url, io, username=passwords.get_w3c_username(), password=passwords.get_w3c_password()) month_message_str = io.getvalue() io.close() message_ids_included = set() for message in generate_messages(month_message_str): include = False message_id = None for [header_name, header_value] in generate_headers(message): header_name = header_name.lower() if header_name == "message-id": message_id = header_value elif header_name == "in-reply-to": if not include: if header_value in message_ids_included: # This is a reply to a message in our set. include = True elif header_name == "references": if not include: for reference in ws_re.split(header_value): if reference in message_ids_included: # This is a reply to a message in our set. include = True break if not include: for term in search_terms: if message.find(term) != -1: include = True break if include: message_ids_included.add(message_id) destio.write(message)
def transform_image(content, mtype, size, img_format): """ Transforms (resizes, converts) the image and returns the resulting bytes and mimetype """ content_io = io.StringIO(content) img = Image.open(content_io) try: size = int(size) except (ValueError, TypeError): size = None if img.mode not in ('RGB', 'RGBA'): img = img.convert('RGB') if img_format: mtype = 'image/%s' % img_format else: img_format = mtype[mtype.find('/') + 1:] if size: img = img.resize((size, size), Image.ANTIALIAS) # If it's a RGBA image, composite it onto a white background for JPEG if img.mode == 'RGBA': background = Image.new('RGB', img.size) draw = ImageDraw.Draw(background) draw.rectangle((-1, -1, img.size[0] + 1, img.size[1] + 1), fill=(255, 255, 255)) del draw img = Image.composite(img, background, img) io = io.StringIO() img.save(io, img_format.upper()) content = io.getvalue() return content, mtype
def test(): r = request # convert string of image data to uint8 nparr = np.fromstring(r.data, np.uint8) # decode image filename = 'prinytoe.jpg' img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) save_original_image(img, filename) add_to_origin_table(filename) path_to_original = os.path.abspath(os.curdir) + '\\Original\\' + filename recieved_counter = 0 edited_name = 'edited' + str(recieved_counter) + '.jpg' path_to_edited = os.path.abspath(os.curdir) + '\\Edited\\' + edited_name watermark_photo(path_to_original, edited_name, '2.jpg', position=(1, 1)) recieved_counter = recieved_counter + 1 im = Image.open(path_to_edited) io = StringIO.StringIO() im.save(io, format='JPEG') return Response(io.getvalue(), mimetype='image/jpeg')
def transform_image(content, mtype, size, img_format): """ Transforms (resizes, converts) the image and returns the resulting bytes and mimetype """ content_io = io.StringIO(content) img = Image.open(content_io) try: size = int(size) except (ValueError, TypeError): size = None if img.mode not in ('RGB', 'RGBA'): img = img.convert('RGB') if img_format: mtype = 'image/%s' % img_format else: img_format = mtype[mtype.find('/')+1:] if size: img = img.resize((size, size), Image.ANTIALIAS) # If it's a RGBA image, composite it onto a white background for JPEG if img.mode == 'RGBA': background = Image.new('RGB', img.size) draw = ImageDraw.Draw(background) draw.rectangle((-1, -1, img.size[0]+1, img.size[1]+1), fill=(255, 255, 255)) del draw img = Image.composite(img, background, img) io = io.StringIO() img.save(io, img_format.upper()) content = io.getvalue() return content, mtype
def plot_example_match(fm): number_matches_med, data, match1, match2 = get_example_matches( fm, "medium", number_to_plot=2) number_matches_high = len(fm.matches.high_confidence) all_matches = len(fm.matches.matches) template_data = fm.material.template fig, (ax1) = plt.subplots(1, 1, sharex=True, sharey=True, figsize=(13, 5)) ax1.set(xlabel='Shift (cm$^{-1}$)') ax1.set(ylabel='Intensity') ax1.plot(template_data) io = StringIO() fig.savefig(io, format='png') plt.close(fig) template = base64.encodestring(io.getvalue()) if number_matches_med == 0: return render_template('plot_data.html', number_matches_med=number_matches_med, number_matches_high=number_matches_high, all_matches=all_matches, template=template, number_locations=fm.len, filename=fm.data_filename, material="graphene_oxide", subtract_baseline=False) return render_template('plot_data.html', number_matches_med=number_matches_med, number_matches_high=number_matches_high, all_matches=all_matches, template=template, number_locations=fm.len, match_example=data, filename=fm.data_filename, material="graphene_oxide", subtract_baseline=False, match1=match1.to_dict(), match2=match2.to_dict())
def kill(self, sig): """Send a signal to the subprocess. This may or may not kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ now = time.time() options = self.config.options if not self.pid: msg = ("attempted to kill %s with sig %s but it wasn't running" % (self.config.name, signame(sig))) options.logger.debug(msg) return msg #If we're in the stopping state, then we've already sent the stop #signal and this is the kill signal if self.state == ProcessStates.STOPPING: killasgroup = self.config.killasgroup else: killasgroup = self.config.stopasgroup as_group = "" if killasgroup: as_group = "process group " options.logger.debug('killing %s (pid %s) %swith signal %s' % (self.config.name, self.pid, as_group, signame(sig)) ) # RUNNING/STARTING/STOPPING -> STOPPING self.killing = 1 self.delay = now + self.config.stopwaitsecs # we will already be in the STOPPING state if we're doing a # SIGKILL as a result of overrunning stopwaitsecs self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING, ProcessStates.STOPPING) self.change_state(ProcessStates.STOPPING) pid = self.pid if killasgroup: # send to the whole process group instead pid = -self.pid try: options.kill(pid, sig) except: io = io.StringIO() traceback.print_exc(file=io) tb = io.getvalue() msg = 'unknown problem killing %s (%s):%s' % (self.config.name, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 self.killing = 0 self.delay = 0 return msg return None
def create(self, cr, uid, ids, datas, context=None): if context is None: context = {} registry = openerp.registry(cr.dbname) product_ids = ids if 'location_id' in context: location_id = context['location_id'] else: warehouse_id = registry['stock.warehouse'].search(cr, uid, [])[0] location_id = registry['stock.warehouse'].browse( cr, uid, warehouse_id).lot_stock_id.id loc_ids = registry['stock.location'].search( cr, uid, [('location_id', 'child_of', [location_id])]) now = time.strftime('%Y-%m-%d') dt_from = now dt_to = now names = dict(registry['product.product'].name_get( cr, uid, product_ids)) for name in names: names[name] = names[name].encode('utf8') products = {} ctx = context.copy() ctx['location_id'] = loc_ids prods = registry['product.product']._product_available(cr, uid, product_ids, context=ctx) for prod in list(prods.keys()): products[prod] = [(now, prods[prod]['qty_available'])] prods[prod] = 0 if not loc_ids or not product_ids: return (False, 'pdf') cr.execute( "select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_id IN %s" "and product_id IN %s" "group by date,product_id", ( ('confirmed', 'assigned', 'waiting'), tuple(loc_ids), tuple(product_ids), )) for (qty, dt, prod_id) in cr.fetchall(): if dt <= dt_from: dt = (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt, -qty)) cr.execute( "select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_dest_id IN %s" "and product_id IN %s" "group by date,product_id", ( ('confirmed', 'assigned', 'waiting'), tuple(loc_ids), tuple(product_ids), )) for (qty, dt, prod_id) in cr.fetchall(): if dt <= dt_from: dt = (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt, qty)) dt = dt_from qty = 0 io = io.StringIO() gt = stock_graph.stock_graph(io) for prod_id in products: prod_name = names.get(prod_id, 'Unknown') if isinstance(prod_name, str): prod_name = prod_name.decode('utf-8') prod_name = unicodedata.normalize('NFKD', prod_name) prod_name = prod_name.encode('ascii', 'replace') gt.add(prod_id, prod_name, products[prod_id]) gt.draw() gt.close() self.obj = external_pdf(io.getvalue()) self.obj.render() return (self.obj.pdf, 'pdf')
def test_escaped_character_state(self): for c in 'a\\\',"= _-1#': io = StringIO() ecs = EscapedCharacterState(io) self.assertTrue(ecs.character(c)) self.assertEquals(c, io.getvalue())