def test_upload_media(self): media_file = six.StringIO('nothing') with HTTMock(wechat_api_mock): media = self.client.media.upload('image', media_file) self.assertEqual('image', media['type']) self.assertEqual('12345678', media['media_id'])
def setUp(self): self._testroot = tempfile.mkdtemp() self._output = six.StringIO()
from __future__ import unicode_literals import datetime import logging import os.path import signal import sys import typing # noqa: F401 pylint: disable=unused-import import colorama import six from . import exceptions as exc _LOGFILE_STREAM = six.StringIO() def write_logfile(): # type: () -> None """Write a DEBUG log file COMMAND-YYYYMMDD-HHMMSS.ffffff.log.""" command = os.path.basename(os.path.realpath(os.path.abspath(sys.argv[0]))) now = datetime.datetime.now().strftime('%Y%m%d-%H%M%S.%f') filename = '{}-{}.log'.format(command, now) with open(filename, 'w') as logfile: logfile.write(_LOGFILE_STREAM.getvalue()) def handle_unexpected_exception(exc): # type: (Exception) -> typing.Union[str, Exception] """Return an error message and write a log file if logging was not enabled.
def init(): template_base_dir = resource_filename("runestone", "common/project_template") config_stuff = resource_string("runestone", "common/project_template/conf.tmpl") paver_stuff = resource_string("runestone", "common/project_template/pavement.tmpl") conf_dict = {} print( "This will create a new Runestone project in your current directory.") click.confirm("Do you want to proceed? ", abort=True, default=True) print( "Next we need to gather a few pieces of information to create your configuration files" ) conf_dict["dynamic_pages"] = click.prompt( "Build book for dynamic page service?", type=bool, default=False) if conf_dict["dynamic_pages"] == False: conf_dict["use_services"] = click.prompt("Use Runestone Web Services ", default="false") else: conf_dict["use_services"] = "true" conf_dict["author"] = click.prompt("Your Name ", default=getpass.getuser()) conf_dict["project_title"] = click.prompt("Title for this project ", default="Runestone Default") conf_dict["python3"] = click.prompt("Use Simple Python3 Semantics ", default="false") conf_dict["default_ac_lang"] = click.prompt("Default ActiveCode language", default="python") if conf_dict["use_services"] == "true": conf_dict[ "project_name"] = "os.path.basename(os.path.dirname(os.path.abspath(__file__)))" conf_dict["build_dir"] = "./build" conf_dict["dest"] = "./published" conf_dict["login_req"] = click.prompt("Require login ", default="false") conf_dict["master_url"] = "" conf_dict["log_level"] = (10 if click.prompt( "Log student actions? ", type=bool, default=True) else 0) conf_dict["dburl"] = click.prompt( "DataBase Connection URL", default="postgresql://*****:*****@localhost/runestone", ) conf_dict["enable_chatcodes"] = click.prompt( "Enable Enable the chatcode feature)", type=bool, default=False) # See the comments in ``conf.tmpl`` on server-side grading for an explanation of these conditions. if conf_dict["log_level"] and conf_dict["login_req"]: conf_dict["server_side_grading"] = click.prompt( "Grade questions on the server where possible?", type=bool, default=False, ) else: conf_dict["server_side_grading"] = False conf_dict["allow_pairs"] = click.prompt( "Enable Pair Programming feature(s)", type=bool, default=False) else: conf_dict["project_name"] = click.prompt( "Project name: (one word, no spaces)") while " " in conf_dict["project_name"]: conf_dict["project_name"] = click.prompt( "Project name: (one word, NO SPACES)") # Add quotes around the project name for use in the template. conf_dict["project_name"] = repr(conf_dict["project_name"]) conf_dict["build_dir"] = click.prompt("Path to build dir ", default="./build") conf_dict["dest"] = click.prompt("Path to deploy built site ", default="../../static") conf_dict["login_req"] = "false" conf_dict["master_url"] = "http://127.0.0.1:8000" conf_dict["log_level"] = 0 conf_dict["dburl"] = "" conf_dict["enable_chatcodes"] = "false" conf_dict["server_side_grading"] = False conf_dict["allow_pairs"] = "false" conf_dict["short_name"] = conf_dict["project_name"] conf_dict["downloads_enabled"] = click.prompt( "Enable inline Activecode downloads by default (single activecode downloads may be enabled with the :enabledownload: flag)", default="false", ) shutil.copytree(os.path.join(template_base_dir, "_sources"), "_sources") os.makedirs(conf_dict["build_dir"]) paver_final = paver_stuff.decode("utf-8") % conf_dict config_final = config_stuff.decode("utf-8") % conf_dict # On Windows, Python 3.6, the bytes read from ``template_base_dir`` and ``config_stuff`` contain Windows-style ``\n\r``. Unfortunately, `resource_string <http://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access>`_ does no universal newline support, so these remain intact. When written out, this is changed to ``\n\n``, making the file double-spaced. Python 3's `StringIO <https://docs.python.org/3/library/io.html#io.StringIO>`_ class provides universal newline support, while Python 2's `StringIO <https://docs.python.org/2/library/stringio.html#StringIO.StringIO>`__ doesn't. if six.PY3: # Per the `TextIOWrapper docs <https://docs.python.org/3/library/io.html#io.TextIOWrapper>`_, ``newline=None`` selects universal newline mode. The Python 3 StringIO_ class's ``newline`` argument works the same. paver_final = six.StringIO(paver_final, newline=None).read() config_final = six.StringIO(config_final, newline=None).read() with codecs.open("pavement.py", "w", encoding="utf8") as pvf: pvf.write(paver_final) with codecs.open("conf.py", "w", encoding="utf8") as pvf: pvf.write(config_final) print("Done. Type runestone build to build your project")
def child(): with mock.patch('blessed.terminal.HAS_TTY', False): term = TestTerminal(stream=six.StringIO()) stime = time.time() assert term.kbhit(timeout=1.1) is False assert math.floor(time.time() - stime) == 0
def test_report_before_fit(self): # TODO: check report content _buffer = six.StringIO() _fit = self._get_fit() _fit.report(output_stream=_buffer) self.assertNotEqual(_buffer.getvalue(), "")
def get_stringio(self): return six.StringIO()
def template(src, dest, user=None, group=None, mode=None, create_remote_dir=True, state=None, host=None, **data): ''' Generate a template using jinja2 and write it to the remote system. + src: local template filename + dest: remote filename + user: user to own the files + group: group to own the files + mode: permissions of the files + create_remote_dir: create the remote directory if it doesn't exist ``create_remote_dir``: If the remote directory does not exist it will be created using the same user & group as passed to ``files.put``. The mode will *not* be copied over, if this is required call ``files.directory`` separately. Notes: Common convention is to store templates in a "templates" directory and have a filename suffix with '.j2' (for jinja2). For information on the template syntax, see `the jinja2 docs <https://jinja.palletsprojects.com>`_. Examples: .. code:: python files.template( name='Create a templated file', src='templates/somefile.conf.j2', dest='/etc/somefile.conf', ) files.template( name='Create service file', src='templates/myweb.service.j2', dest='/etc/systemd/system/myweb.service', mode='755', user='******', group='root', ) # Example showing how to pass python variable to template file. # The .j2 file can use `{{ foo_variable }}` to be interpolated. foo_variable = 'This is some foo variable contents' files.template( name='Create a templated file', src='templates/foo.j2', dest='/tmp/foo', foo_variable=foo_variable, ) ''' dest = escape_unix_path(dest) if state.deploy_dir: src = os_path.join(state.deploy_dir, src) # Ensure host is always available inside templates data['host'] = host data['inventory'] = state.inventory # Render and make file-like it's output try: output = get_template(src).render(data) except (TemplateSyntaxError, UndefinedError) as e: _, _, trace = sys.exc_info() # Jump through to the *second last* traceback, which contains the line number # of the error within the in-memory Template object while trace.tb_next: if trace.tb_next.tb_next: trace = trace.tb_next else: # pragma: no cover break line_number = trace.tb_frame.f_lineno # Quickly read the line in question and one above/below for nicer debugging with open(src, 'r') as f: template_lines = f.readlines() template_lines = [line.strip() for line in template_lines] relevant_lines = template_lines[max(line_number - 2, 0):line_number + 1] raise OperationError( 'Error in template: {0} (L{1}): {2}\n...\n{3}\n...'.format( src, line_number, e, '\n'.join(relevant_lines), )) output_file = six.StringIO(output) # Set the template attribute for nicer debugging output_file.template = src # Pass to the put function yield put( output_file, dest, user=user, group=group, mode=mode, add_deploy_dir=False, create_remote_dir=create_remote_dir, state=state, host=host, )
def __init__(self): self.stdout = six.StringIO() self.stderr = six.StringIO()
def testLogToNonTerminal_doesNothingFancy(self): stream = six.StringIO() handler = util.LogHandler(stream) handler.setFormatter(logging.Formatter('BOO! %(message)s')) handler.emit(make_record(logging.INFO, 'hi')) self.assertEqual('BOO! hi\n', stream.getvalue())
def _consume_all_iter(): bytes_read = 0 data = six.StringIO("*" * BYTES) for chunk in utils.LimitingReader(data, BYTES - 1): bytes_read += len(chunk)
def TerminalStringIO(): stream = six.StringIO() stream.isatty = lambda: True return stream
def testLogEphemeralOnNonTerminal_doesNothing(self): stream = six.StringIO() handler = util.LogHandler(stream) handler.setFormatter(logging.Formatter('%(message)s')) handler.emit(make_ephemeral_record(logging.INFO, 'hi')) self.assertEqual('', stream.getvalue())
def testLogAnsiCodesWhenNotLoggingToATerminal_stripsAnsiCodes(self): stream = six.StringIO() handler = util.LogHandler(stream) handler.setFormatter(logging.Formatter('%(message)s')) handler.emit(make_record(logging.INFO, util.Ansi.RED + 'hi')) self.assertEqual('hi\n', stream.getvalue())
def initialize(self, geom, run_num=0, cframe=0): """ Initialize the detector :param geom: The *-end.data file which characterizes the geometry profile. :param run_num: The run_num containing the background, rms and gain and the other pixel pixel properties. :param cframe: The desired coordinate frame, 0 for psana and 1 for lab conventions. :return: None """ # Redirect the output stream old_stdout = sys.stdout f = six.StringIO() # f = open('Detector_initialization.log', 'w') sys.stdout = f ########################################################################################### # Initialize the geometry configuration ############################################################################################ self.geometry = GeometryAccess(geom, cframe=cframe) self.run_num = run_num # Set coordinate in real space (convert to m) temp = [xp.asarray(t) * 1e-6 for t in self.geometry.get_pixel_coords(cframe=cframe)] temp_index = [xp.asarray(t) for t in self.geometry.get_pixel_coord_indexes(cframe=cframe)] self.panel_num = np.prod(temp[0].shape[:-2]) self._distance = float(temp[2].mean()) self._shape = (self.panel_num, temp[0].shape[-2], temp[0].shape[-1]) self.pixel_position = xp.zeros(self._shape + (3,)) self.pixel_index_map = xp.zeros(self._shape + (2,)) for n in range(3): self.pixel_position[..., n] = temp[n].reshape(self._shape) for n in range(2): self.pixel_index_map[..., n] = temp_index[n].reshape(self._shape) self.pixel_index_map = self.pixel_index_map.astype(xp.int64) # Get the range of the pixel index self.detector_pixel_num_x = asnumpy( xp.max(self.pixel_index_map[..., 0]) + 1) self.detector_pixel_num_y = asnumpy( xp.max(self.pixel_index_map[..., 1]) + 1) self.panel_pixel_num_x = np.array([self.pixel_index_map.shape[1], ] * self.panel_num) self.panel_pixel_num_y = np.array([self.pixel_index_map.shape[2], ] * self.panel_num) self.pixel_num_total = np.sum(np.multiply(self.panel_pixel_num_x, self.panel_pixel_num_y)) tmp = float(self.geometry.get_pixel_scale_size() * 1e-6) # Convert to m self.pixel_width = xp.ones( (self.panel_num, self.panel_pixel_num_x[0], self.panel_pixel_num_y[0])) * tmp self.pixel_height = xp.ones( (self.panel_num, self.panel_pixel_num_x[0], self.panel_pixel_num_y[0])) * tmp # Calculate the pixel area self.pixel_area = xp.multiply(self.pixel_height, self.pixel_width) ########################################################################################### # Initialize the pixel effects ########################################################################################### # first we should parse the path parsed_path = geom.split('/') self.exp = parsed_path[-5] if self.exp == 'calib': self.exp = parsed_path[-6] self.group = parsed_path[-4] self.source = parsed_path[-3] self._pedestals = None self._pixel_rms = None self._pixel_mask = None self._pixel_bkgd = None self._pixel_status = None self._pixel_gain = None if psana_version==1: try: cbase = self._get_cbase() self.calibdir = '/'.join(parsed_path[:-4]) pbits = 255 gcp = GenericCalibPars(cbase, self.calibdir, self.group, self.source, run_num, pbits) self._pedestals = gcp.pedestals() self._pixel_rms = gcp.pixel_rms() self._pixel_mask = gcp.pixel_mask() self._pixel_bkgd = gcp.pixel_bkgd() self._pixel_status = gcp.pixel_status() self._pixel_gain = gcp.pixel_gain() except NotImplementedError: # No GenericCalibPars information. pass else: try: self.det = self._get_det_id(self.group) except NotImplementedError: # No GenericCalibPars information. self.det = None # Redirect the output stream sys.stdout = old_stdout
def dedent_block(code_string): """Dedents a code so that its first line starts at row zero.""" code_string = _unfold_continuations(code_string) token_gen = tokenize.generate_tokens(six.StringIO(code_string).readline) block_indentation = None tokens = [] try: for tok in token_gen: tokens.append(tok) except tokenize.TokenError: # Resolution of lambda functions may yield incomplete code, which can # in turn generate this error. We silently ignore this error because the # parser may still be able to deal with it. pass for tok in tokens: tok_type, tok_string, _, _, _ = tok if tok_type == tokenize.INDENT: block_indentation = tok_string block_level = len(block_indentation) break elif tok_type not in (tokenize.NL, tokenize.NEWLINE, tokenize.STRING, tokenize.COMMENT): block_indentation = '' break if not block_indentation: return code_string block_level = len(block_indentation) first_indent_uses_tabs = '\t' in block_indentation for i, tok in enumerate(tokens): tok_type, tok_string, _, _, _ = tok if tok_type == tokenize.INDENT: if ((' ' in tok_string and first_indent_uses_tabs) or ('\t' in tok_string and not first_indent_uses_tabs)): # TODO(mdan): We could attempt to convert tabs to spaces by unix rule. # See: # https://docs.python.org/3/reference/lexical_analysis.html#indentation raise errors.UnsupportedLanguageElementError( 'code mixing tabs and spaces for indentation is not allowed' ) if len(tok_string) >= block_level: tok_string = tok_string[block_level:] tokens[i] = (tok_type, tok_string) new_code = tokenize.untokenize(tokens) # Note: untokenize respects the line structure, but not the whitespace within # lines. For example, `def foo()` may be untokenized as `def foo ()` # So instead of using the output of dedent, we match the leading whitespace # on each line. dedented_code = [] for line, new_line in zip(code_string.split('\n'), new_code.split('\n')): original_indent = re.match(_LEADING_WHITESPACE, line).group() new_indent = re.match(_LEADING_WHITESPACE, new_line).group() if len(original_indent) > len(new_indent): dedented_line = line[len(original_indent) - len(new_indent):] else: dedented_line = line dedented_code.append(dedented_line) new_code = '\n'.join(dedented_code) return new_code
def load(self, name, encoding=None): if name not in self.items: raise IOError(2, 'No such file or directory', name) ret = six.StringIO(self.items[name]) ret.name = name return ret
class ShellTest(utils.TestCase): def make_env(self, exclude=None, fake_env=FAKE_ENV): env = dict((k, v) for k, v in fake_env.items() if k != exclude) self.useFixture(fixtures.MonkeyPatch('os.environ', env)) def setUp(self): super(ShellTest, self).setUp() self.useFixture(fixtures.MonkeyPatch( 'novaclient.client.get_client_class', mock.MagicMock)) self.nc_util = mock.patch( 'novaclient.openstack.common.cliutils.isunauthenticated').start() self.nc_util.return_value = False def shell(self, argstr, exitcodes=(0,)): orig = sys.stdout orig_stderr = sys.stderr try: sys.stdout = six.StringIO() sys.stderr = six.StringIO() _shell = novaclient.shell.OpenStackComputeShell() _shell.main(argstr.split()) except SystemExit: exc_type, exc_value, exc_traceback = sys.exc_info() self.assertIn(exc_value.code, exitcodes) finally: stdout = sys.stdout.getvalue() sys.stdout.close() sys.stdout = orig stderr = sys.stderr.getvalue() sys.stderr.close() sys.stderr = orig_stderr return (stdout, stderr) def test_help_unknown_command(self): self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo') def test_invalid_timeout(self): for f in [0, -1, -10]: cmd_text = '--timeout %s' % (f) stdout, stderr = self.shell(cmd_text, exitcodes=[0, 2]) required = [ 'argument --timeout: %s must be greater than 0' % (f), ] for r in required: self.assertIn(r, stderr) def test_help(self): required = [ '.*?^usage: ', '.*?^\s+root-password\s+Change the root password', '.*?^See "nova help COMMAND" for help on a specific command', ] stdout, stderr = self.shell('help') for r in required: self.assertThat((stdout + stderr), matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) def test_help_on_subcommand(self): required = [ '.*?^usage: nova root-password', '.*?^Change the root password', '.*?^Positional arguments:', ] stdout, stderr = self.shell('help root-password') for r in required: self.assertThat((stdout + stderr), matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) def test_help_no_options(self): required = [ '.*?^usage: ', '.*?^\s+root-password\s+Change the root password', '.*?^See "nova help COMMAND" for help on a specific command', ] stdout, stderr = self.shell('') for r in required: self.assertThat((stdout + stderr), matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) def test_bash_completion(self): stdout, stderr = self.shell('bash-completion') # just check we have some output required = [ '.*--matching', '.*--wrap', '.*help', '.*secgroup-delete-rule', '.*--priority'] for r in required: self.assertThat((stdout + stderr), matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) def test_no_username(self): required = ('You must provide a username or user id' ' via --os-username, --os-user-id,' ' env[OS_USERNAME] or env[OS_USER_ID]') self.make_env(exclude='OS_USERNAME') try: self.shell('list') except exceptions.CommandError as message: self.assertEqual(required, message.args[0]) else: self.fail('CommandError not raised') def test_no_user_id(self): required = ('You must provide a username or user id' ' via --os-username, --os-user-id,' ' env[OS_USERNAME] or env[OS_USER_ID]') self.make_env(exclude='OS_USER_ID', fake_env=FAKE_ENV2) try: self.shell('list') except exceptions.CommandError as message: self.assertEqual(required, message.args[0]) else: self.fail('CommandError not raised') def test_no_tenant_name(self): required = ('You must provide a tenant name or tenant id' ' via --os-tenant-name, --os-tenant-id,' ' env[OS_TENANT_NAME] or env[OS_TENANT_ID]') self.make_env(exclude='OS_TENANT_NAME') try: self.shell('list') except exceptions.CommandError as message: self.assertEqual(required, message.args[0]) else: self.fail('CommandError not raised') def test_no_tenant_id(self): required = ('You must provide a tenant name or tenant id' ' via --os-tenant-name, --os-tenant-id,' ' env[OS_TENANT_NAME] or env[OS_TENANT_ID]',) self.make_env(exclude='OS_TENANT_ID', fake_env=FAKE_ENV2) try: self.shell('list') except exceptions.CommandError as message: self.assertEqual(required, message.args) else: self.fail('CommandError not raised') def test_no_auth_url(self): required = ('You must provide an auth url' ' via either --os-auth-url or env[OS_AUTH_URL] or' ' specify an auth_system which defines a default url' ' with --os-auth-system or env[OS_AUTH_SYSTEM]',) self.make_env(exclude='OS_AUTH_URL') try: self.shell('list') except exceptions.CommandError as message: self.assertEqual(required, message.args) else: self.fail('CommandError not raised') @mock.patch('sys.stdin', side_effect=mock.MagicMock) @mock.patch('getpass.getpass', return_value='password') def test_password(self, mock_getpass, mock_stdin): mock_stdin.encoding = "utf-8" # default output of empty tables differs depending between prettytable # versions if (hasattr(prettytable, '__version__') and dist_version.StrictVersion(prettytable.__version__) < dist_version.StrictVersion('0.7.2')): ex = '\n' else: ex = '\n'.join([ '+----+------+--------+------------+-------------+----------+', '| ID | Name | Status | Task State | Power State | Networks |', '+----+------+--------+------------+-------------+----------+', '+----+------+--------+------------+-------------+----------+', '' ]) self.make_env(exclude='OS_PASSWORD') stdout, stderr = self.shell('list') self.assertEqual((stdout + stderr), ex) @mock.patch('sys.stdin', side_effect=mock.MagicMock) @mock.patch('getpass.getpass', side_effect=EOFError) def test_no_password(self, mock_getpass, mock_stdin): required = ('Expecting a password provided' ' via either --os-password, env[OS_PASSWORD],' ' or prompted response',) self.make_env(exclude='OS_PASSWORD') try: self.shell('list') except exceptions.CommandError as message: self.assertEqual(required, message.args) else: self.fail('CommandError not raised') def _test_service_type(self, version, service_type, mock_client): if version is None: cmd = 'list' else: cmd = ('--service_type %s --os-compute-api-version %s list' % (service_type, version)) self.make_env() self.shell(cmd) _, client_kwargs = mock_client.call_args_list[0] self.assertEqual(service_type, client_kwargs['service_type']) @mock.patch('novaclient.client.Client') def test_default_service_type(self, mock_client): self._test_service_type(None, 'compute', mock_client) @mock.patch('novaclient.client.Client') def test_v1_1_service_type(self, mock_client): self._test_service_type('1.1', 'compute', mock_client) @mock.patch('novaclient.client.Client') def test_v2_service_type(self, mock_client): self._test_service_type('2', 'compute', mock_client) @mock.patch('novaclient.client.Client') def test_v3_service_type(self, mock_client): self._test_service_type('3', 'computev3', mock_client) @mock.patch('novaclient.client.Client') def test_v_unknown_service_type(self, mock_client): self._test_service_type('unknown', 'compute', mock_client) @mock.patch('sys.argv', ['nova']) @mock.patch('sys.stdout', six.StringIO()) @mock.patch('sys.stderr', six.StringIO()) def test_main_noargs(self): # Ensure that main works with no command-line arguments try: novaclient.shell.main() except SystemExit: self.fail('Unexpected SystemExit') # We expect the normal usage as a result self.assertIn('Command-line interface to the OpenStack Nova API', sys.stdout.getvalue()) @mock.patch.object(novaclient.shell.OpenStackComputeShell, 'main') def test_main_keyboard_interrupt(self, mock_compute_shell): # Ensure that exit code is 130 for KeyboardInterrupt mock_compute_shell.side_effect = KeyboardInterrupt() try: novaclient.shell.main() except SystemExit as ex: self.assertEqual(ex.code, 130)
assert test.encryption_context == {} assert test.algorithm is None assert test.frame_length == FRAME_LENGTH def test_decryptor_config_defautls(): test = DecryptorConfig(**BASE_KWARGS) assert test.max_body_length is None @pytest.mark.parametrize( "kwargs, stream_type", ( (dict(source=b"", materials_manager=FakeCryptoMaterialsManager()), io.BytesIO), (dict(source=b"", key_provider=FakeMasterKeyProvider()), io.BytesIO), (dict(source="", materials_manager=FakeCryptoMaterialsManager()), io.BytesIO), (dict(source=io.BytesIO(), materials_manager=FakeCryptoMaterialsManager()), io.BytesIO), (dict(source=six.StringIO(), materials_manager=FakeCryptoMaterialsManager()), six.StringIO), ), ) def test_client_config_converts(kwargs, stream_type): test = _ClientConfig(**kwargs) assert isinstance(test.source, stream_type) if test.key_provider is not None: assert isinstance(test.materials_manager, DefaultCryptoMaterialsManager)
def raw_request(self, *args, **kwargs): fixture = self._request(*args, **kwargs) body_iter = http.ResponseBodyIterator(six.StringIO(fixture[1])) return FakeResponse(fixture[0]), body_iter
def _readPackageMetadata(distribution): """Get a metadata object associated with a python package.""" metadata_string = distribution.get_metadata(distribution.PKG_INFO) metadata = distutils.dist.DistributionMetadata() metadata.read_pkg_file(six.StringIO(metadata_string)) return metadata
def setUp(self): sys.stdout = six.StringIO() sys.stderr = six.StringIO()
def child(): term = TestTerminal(stream=six.StringIO()) stime = time.time() assert term._keyboard_fd is None assert not term.kbhit(timeout=1.1) assert math.floor(time.time() - stime) == 1.0
def to_stream(self): """Create an StringIO object from the current editor text.""" return six.StringIO(str(self))
def child(): term = TestTerminal(stream=six.StringIO()) stime = time.time() y, x = term.get_location(timeout=0) assert (math.floor(time.time() - stime) == 0.0) assert (y, x) == (-1, -1)
def advertiser_stats_proxy(request, token, path): """ Proxy to use cross domain Ajax GET requests request: Django request object """ user = REGISTRY.get('user', None) trading_desk = user.profile.trading_desk.first() if trading_desk: token = trading_desk.trading_desk_key else: advertiser = user.profile.advertisers.first() if advertiser is None: raise PermissionDeniedException() token = advertiser.advertiser_key if request.GET.get('source_type') == '2': url = ''.join([ settings.TWITTER_STATS_API_DOMAIN, settings.ADVERTISER_STATS_API_PATH, path ]) else: url = ''.join([ settings.STATS_API_DOMAIN, settings.ADVERTISER_STATS_API_PATH, path ]) trading_desk_ids = map( str, TradingDesk.TradingDesk.objects.values_list('trading_desk_id', flat=True)) get_params = copy(request.GET) should_cache = get_params.get('should_cache') if should_cache: del get_params['should_cache'] # get end date (frontend user timezone) user timezone yesterday could be today in stats API timezone end_date = get_params['date_to'] # get today str (Monarch, Stats API timezone) _today = datetime.date.today().strftime('%Y-%m-%d') should_cache = _today > end_date and datetime.datetime.now().hour > 7 filter_by_trading_desk_id = get_params.get('filter_by_trading_desk_id', '') if filter_by_trading_desk_id != '': filter_ids = get_params['filter_by_trading_desk_id'].split(',') trading_desk_ids = map( str, [i for i in trading_desk_ids if i in filter_ids]) ad_id = get_params.get('filter_by_ad_id') ad_group_id = get_params.get('filter_by_ad_group_id') if ad_id and not ad_group_id: try: ad_group_id = Ad.Ad.objects.get(pk=ad_id).ad_group_id.pk get_params['filter_by_ad_group_id'] = ad_group_id except: pass campaign_id = get_params.get('filter_by_campaign_id') if ad_group_id and not campaign_id: try: campaign_id = AdGroup.AdGroup.objects.get( pk=ad_group_id).campaign_id.pk get_params['filter_by_campaign_id'] = campaign_id except: pass advertiser_id = get_params.get('filter_by_advertiser_id') if campaign_id and not advertiser_id: try: advertiser_id = Campaign.Campaign.objects.get( pk=campaign_id).advertiser_id.pk get_params['filter_by_advertiser_id'] = advertiser_id except: pass if not trading_desk_ids: return http.JsonResponse({ 'HTTP-STATUS': 403, 'success': False }, safe=True, status=403) get_params['filter_by_trading_desk_id'] = ','.join(trading_desk_ids) break_by = get_params.get('break_by', '') break_by = break_by.split(',') if break_by else [] for param in get_params.keys(): if param[:7] == 'filter_' and param[-5:] == '_like': filter_name = param[7:-5] if filter_name not in break_by: break_by.append(filter_name) get_params['break_by'] = ','.join(break_by) params = ['%s=%s' % (k, get_params[k]) for k in get_params.keys()] query = '&'.join(params) data = redis_cache.get(query, False) if data: content_type = 'application/json' status_code = 200 else: url = url + '?' + query headers = {'Authorization': 'token token="%s"' % token} response = requests.get(url, headers=headers, verify=False) status_code = int(response.status_code) data = response.text content_type = response.headers.get('content-type', 'application/json') if content_type == 'text/json': content_type = 'application/json' if content_type == 'text/csv': csv_reader = csv.DictReader(six.StringIO(response.text)) permitted_fields = set( request.user.get_permitted_model_fields( 'metrics', 'read', csv_reader.fieldnames)) remap_fields = {} if request.user.profile.advertisers.exists(): # IMPORTANT: If user has advertiser than it's advertiser user and we should display cost as a spend. remap_fields['cost'] = 'spend' out = six.StringIO() field_names = [ f for f in csv_reader.fieldnames if f in permitted_fields ] csv_writer = csv.DictWriter( out, [remap_fields.get(f, f) for f in field_names]) csv_writer.writeheader() for row in csv_reader: csv_writer.writerow({ remap_fields.get(k, k): v for k, v in six.iteritems(row) if k in field_names }) data = out.getvalue() elif content_type == 'application/json': json_data = json.loads(data) entity_by_id_by_model_name = collections.defaultdict(dict) for name in {'advertiser', 'campaign', 'ad_group', 'ad'}: key = '{}_id'.format(name) ids = list( set( core.safe_to_int(item_id) for item_id in (i.get(key) for i in json_data) if item_id)) if ids: model = _MODEL_BY_NAME[name] fields = _FIELDS_BY_MODEL_NAME[name] if key not in fields: fields += (key, ) for item in model.objects.filter(**{ '{}__in'.format(key): ids }).values(*fields): entity_by_id_by_model_name[name][item[key]] = { k.split('__')[-1]: v for k, v in six.iteritems(item) if k != key } for i in json_data: item_id = core.safe_to_int(i[key]) if item_id in entity_by_id_by_model_name[name]: i.update(entity_by_id_by_model_name[name][item_id]) data = json.dumps(json_data) if status_code != 200: data = json.dumps({'info': data}) elif should_cache and content_type != 'text/csv': # cache only if status_code is 200 # expire after 2 days redis_cache.set(query, data, 60 * 60 * 24 * 2) response = HttpResponse(data, status=status_code, content_type=content_type) if content_type == 'text/csv': response['Content-Disposition'] = 'inline; filename="report.csv"' return response
def decompress_gzip(data): import gzip f = six.StringIO(data) with gzip.GzipFile(mode="rb", compresslevel=9, fileobj=f) as z: return z.read()
def _http_request(self, url, method, **kwargs): """Send an http request with the specified characteristics. Wrapper around httplib.HTTP(S)Connection.request to handle tasks such as setting headers and error handling. """ # Copy the kwargs so we can reuse the original in case of redirects kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {})) kwargs['headers'].setdefault('User-Agent', USER_AGENT) api_versions.update_headers(kwargs["headers"], self.api_version) if self.auth_token: kwargs['headers'].setdefault('X-Auth-Token', self.auth_token) self.log_curl_request(method, url, kwargs) conn = self.get_connection() try: conn_url = self._make_connection_url(url) conn.request(method, conn_url, **kwargs) resp = conn.getresponse() except socket.gaierror as e: message = ("Error finding address for %(url)s: %(e)s" % dict(url=url, e=e)) raise exceptions.EndpointNotFound(message) except (socket.error, socket.timeout) as e: endpoint = self.endpoint message = ("Error communicating with %(endpoint)s %(e)s" % dict(endpoint=endpoint, e=e)) raise exceptions.ConnectionRefused(message) body_iter = ResponseBodyIterator(resp) # Read body into string if it isn't obviously image data body_str = None if resp.getheader('content-type', None) != 'application/octet-stream': # decoding byte to string is necessary for Python 3.4 compatibility # this issues has not been found with Python 3.4 unit tests # because the test creates a fake http response of type str # the if statement satisfies test (str) and real (bytes) behavior body_list = [ chunk.decode("utf-8") if isinstance(chunk, bytes) else chunk for chunk in body_iter ] body_str = ''.join(body_list) self.log_http_response(resp, body_str) body_iter = six.StringIO(body_str) else: self.log_http_response(resp) if 400 <= resp.status < 600: LOG.warning("Request returned failure status.") error_json = _extract_error_json(body_str) raise exceptions.from_response( resp, error_json.get('faultstring'), error_json.get('debuginfo'), method, url) elif resp.status in (301, 302, 305): # Redirected. Reissue the request to the new location. return self._http_request(resp['location'], method, **kwargs) elif resp.status == 300: raise exceptions.from_response(resp, method=method, url=url) return resp, body_iter