本文整理汇总了Python中pyramid.compat.NativeIO类的典型用法代码示例。如果您正苦于以下问题:Python NativeIO类的具体用法?Python NativeIO怎么用?Python NativeIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NativeIO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Test_copy_dir
class Test_copy_dir(unittest.TestCase):
def setUp(self):
import tempfile
from pyramid.compat import NativeIO
self.dirname = tempfile.mkdtemp()
self.out = NativeIO()
self.fixturetuple = ('pyramid.tests.test_scaffolds',
'fixture_scaffold')
def tearDown(self):
import shutil
shutil.rmtree(self.dirname, ignore_errors=True)
self.out.close()
def _callFUT(self, *arg, **kw):
kw['out_'] = self.out
from pyramid.scaffolds.copydir import copy_dir
return copy_dir(*arg, **kw)
def test_copy_source_as_pkg_resource(self):
vars = {'package':'mypackage'}
self._callFUT(self.fixturetuple,
self.dirname,
vars,
1, False,
template_renderer=dummy_template_renderer)
result = self.out.getvalue()
self.assertTrue('Creating %s/mypackage/' % self.dirname in result)
self.assertTrue(
'Copying fixture_scaffold/+package+/__init__.py_tmpl to' in result)
source = pkg_resources.resource_filename(
'pyramid.tests.test_scaffolds',
'fixture_scaffold/+package+/__init__.py_tmpl')
target = os.path.join(self.dirname, 'mypackage', '__init__.py')
with open(target, 'r') as f:
tcontent = f.read()
with open(source, 'r') as f:
scontent = f.read()
self.assertEqual(scontent, tcontent)
def test_copy_source_as_dirname(self):
vars = {'package':'mypackage'}
source = pkg_resources.resource_filename(*self.fixturetuple)
self._callFUT(source,
self.dirname,
vars,
1, False,
template_renderer=dummy_template_renderer)
result = self.out.getvalue()
self.assertTrue('Creating %s/mypackage/' % self.dirname in result)
self.assertTrue('Copying __init__.py_tmpl to' in result)
source = pkg_resources.resource_filename(
'pyramid.tests.test_scaffolds',
'fixture_scaffold/+package+/__init__.py_tmpl')
target = os.path.join(self.dirname, 'mypackage', '__init__.py')
with open(target, 'r') as f:
tcontent = f.read()
with open(source, 'r') as f:
scontent = f.read()
self.assertEqual(scontent, tcontent)
示例2: print_tb
def print_tb(self):
if self.isexc and self.exc_value:
out = NativeIO()
traceback.print_exception(
self.exc_type, self.exc_value, self.exc_traceback, file=out)
return out.getvalue()
else:
return self.exc
示例3: to_string
def to_string(self, obj):
"""
Converts the given resource to a string representation and returns
it.
"""
stream = NativeIO()
self.to_stream(obj, stream)
return stream.getvalue()
示例4: TestPServeCommand
class TestPServeCommand(unittest.TestCase):
def setUp(self):
from pyramid.compat import NativeIO
self.out_ = NativeIO()
def out(self, msg):
self.out_.write(msg)
def _get_server(*args, **kwargs):
def server(app):
return ''
return server
def _getTargetClass(self):
from pyramid.scripts.pserve import PServeCommand
return PServeCommand
def _makeOne(self, *args):
effargs = ['pserve']
effargs.extend(args)
cmd = self._getTargetClass()(effargs)
cmd.out = self.out
return cmd
def test_run_no_args(self):
inst = self._makeOne()
result = inst.run()
self.assertEqual(result, 2)
self.assertEqual(self.out_.getvalue(), 'You must give a config file')
def test_get_options_no_command(self):
inst = self._makeOne()
inst.args = ['foo', 'a=1', 'b=2']
result = inst.get_options()
self.assertEqual(result, {'a': '1', 'b': '2'})
def test_parse_vars_good(self):
from pyramid.tests.test_scripts.dummy import DummyApp
inst = self._makeOne('development.ini', 'a=1', 'b=2')
inst.loadserver = self._get_server
app = DummyApp()
def get_app(*args, **kwargs):
app.global_conf = kwargs.get('global_conf', None)
inst.loadapp = get_app
inst.run()
self.assertEqual(app.global_conf, {'a': '1', 'b': '2'})
def test_parse_vars_bad(self):
inst = self._makeOne('development.ini', 'a')
inst.loadserver = self._get_server
self.assertRaises(ValueError, inst.run)
示例5: Api
def Api(request):
""" Rest API interface """
response = request.response
# authentication by token
token = request.environ.get('HTTP_X_AUTH_TOKEN')
if token:
secret = ptah.get_settings(ptah.CFG_ID_PTAH, request.registry)['secret']
try:
timestamp, userid, tokens, user_data = parse_ticket(
secret, '%s!' % token, '0.0.0.0')
except BadTicket:
userid = None
if userid:
ptah.auth_service.set_userid(userid)
# search service and action
service = request.matchdict['service']
subpath = request.matchdict['subpath']
if subpath:
action = subpath[0]
arguments = subpath[1:]
if ':' in action:
action, arg = action.split(':', 1)
arguments = (arg,) + arguments
else:
action = 'apidoc'
arguments = ()
request.environ['SCRIPT_NAME'] = '/__rest__/%s' % service
response.headerslist = {'Content-Type': 'application/json'}
# execute action for specific service
try:
result = config.get_cfg_storage(ID_REST)[service](
request, action, *arguments)
except WSGIHTTPException as exc:
response.status = exc.status
result = {'message': str(exc)}
except Exception as exc:
response.status = 500
out = NativeIO()
traceback.print_exc(file=out)
result = {'message': str(exc),
'traceback': out.getvalue()}
if isinstance(result, Response):
return result
response.text = text_(
dumps(result, indent=True, default=dthandler), 'utf-8')
return response
示例6: get_traceback
def get_traceback():
"""
Fetches the last traceback from :var:`sys.exc_info` and returns it as a
formatted string.
:returns: formatted traceback (string)
"""
buf = NativeIO()
traceback.print_exc(file=buf)
return buf.getvalue()
示例7: string_from_data
def string_from_data(self, data_element):
"""
Converts the given data element into a string representation.
:param data_element: object implementing
:class:`everest.representers.interfaces.IExplicitDataElement`
:returns: string representation (using the MIME content type
configured for this representer)
"""
stream = NativeIO()
self.data_to_stream(data_element, stream)
return stream.getvalue()
示例8: test_populate_no_params
def test_populate_no_params(self):
sys.argv[:] = ['ptah-populate', 'ptah.ini']
stdout = sys.stdout
out = NativeIO()
sys.stdout = out
populate.main()
sys.stdout = stdout
val = out.getvalue()
self.assertIn(
'usage: ptah-populate [-h] [-l] [-a] config [step [step ...]]', val)
示例9: run
def run(self):
# print defaults
if self.options.printcfg:
data = config.get_cfg_storage(SETTINGS_OB_ID).export(True)
parser = configparser.ConfigParser(dict_type=OrderedDict)
for key, val in sorted(data.items()):
parser.set(configparser.DEFAULTSECT,
key, val.replace('%', '%%'))
fp = NativeIO()
try:
parser.write(fp)
finally:
pass
print (fp.getvalue())
return
if self.options.all:
section = ''
else:
section = self.options.section
# print description
groups = sorted(config.get_cfg_storage(ID_SETTINGS_GROUP).items(),
key = lambda item: item[1].__title__)
for name, group in groups:
if section and name != section:
continue
print ('')
title = group.__title__ or name
print (grpTitleWrap.fill('{0} ({1})'.format(title, name)))
if group.__description__:
print (grpDescriptionWrap.fill(
group.__description__))
print ('')
for node in group.__fields__.values():
default = '<required>' if node.required else node.default
print (nameWrap.fill(
('%s.%s: %s (%s: %s)' % (
name, node.name, node.title,
node.__class__.__name__, default))))
print (nameTitleWrap.fill(node.description))
print ('')
示例10: setUp
def setUp(self):
import tempfile
from pyramid.compat import NativeIO
self.dirname = tempfile.mkdtemp()
self.out = NativeIO()
self.fixturetuple = ('pyramid.tests.test_scaffolds',
'fixture_scaffold')
self.src_content = """\
These are not the droids
that you are looking for."""
self.dest_content = """\
These are the droids for
whom you are looking;
now you have found them."""
self.src_fn = os.path.join(self.dirname, 'mypackage', '__init__.py')
self.dest_fn = os.path.join(self.dirname, 'mypackage', '__init__.py')
# query_interactive is only normally executed when the destination
# is discovered to be already occupied by existing files, so ...
# create the required occupancy.
from pyramid.scaffolds.copydir import copy_dir
copy_dir(self.fixturetuple,
self.dirname,
{'package':'mypackage'},
0, False,
template_renderer=dummy_template_renderer)
示例11: test_manage_no_params
def test_manage_no_params(self):
self.init_ptah()
sys.argv[:] = ['ptah-manage', 'ptah.ini']
stdout = sys.stdout
out = NativeIO()
sys.stdout = out
manage.main(False)
sys.stdout = stdout
val = out.getvalue()
self.assertIn(
'[-h] [--list-modules] [--list-models] config', val)
示例12: test_no_params
def test_no_params(self, m_bs):
m_bs.return_value = {
'registry': self.registry, 'request': self.request}
sys.argv[:] = ['amdjs', 'pyramid_amdjs.ini']
stdout = sys.stdout
out = NativeIO()
sys.stdout = out
amd.main()
sys.stdout = stdout
val = out.getvalue()
self.assertIn('[-h] [-b] [-m] [--deps] [--no-min] config', val)
示例13: data_element_tree_to_string
def data_element_tree_to_string(data_element):
"""
Creates a string representation of the given data element tree.
"""
# FIXME: rewrite this as a visitor to use the data element tree traverser.
def __dump(data_el, stream, offset):
name = data_el.__class__.__name__
stream.write("%s%s" % (" " * offset, name))
offset += 2
ifcs = provided_by(data_el)
if ICollectionDataElement in ifcs:
stream.write("[")
first_member = True
for member_data_el in data_el.get_members():
if first_member:
stream.write("%s" % os.linesep + " " * offset)
first_member = False
else:
stream.write(",%s" % os.linesep + " " * offset)
__dump(member_data_el, stream, offset)
stream.write("]")
else:
stream.write("(")
if ILinkedDataElement in ifcs:
stream.write(
"url=%s, kind=%s, relation=%s" % (data_el.get_url(), data_el.get_kind(), data_el.get_relation())
)
else:
first_attr = True
for attr_name, attr_value in iteritems_(data_el.data):
if first_attr:
first_attr = False
else:
stream.write(",%s" % os.linesep + " " * (offset + len(name) + 1))
if attr_value is None:
continue
if not IResourceDataElement in provided_by(attr_value):
stream.write("%s=%s" % (attr_name, attr_value))
else:
stream.write("%s=" % attr_name)
__dump(attr_value, stream, offset)
stream.write(")")
stream = NativeIO()
__dump(data_element, stream, 0)
return stream.getvalue()
示例14: to_strings
def to_strings(self, resource):
"""
Dumps the all resources reachable from the given resource to a map of
string representations using the specified content_type (defaults
to CSV).
:returns: dictionary mapping resource member classes to string
representations
"""
collections = self.__collect(resource)
# Build a map of representations.
rpr_map = OrderedDict()
for (mb_cls, coll) in iteritems_(collections):
strm = NativeIO('w')
dump_resource(coll, strm, content_type=self.__content_type)
rpr_map[mb_cls] = strm.getvalue()
return rpr_map
示例15: test_list_modules
def test_list_modules(self, m_bs):
m_bs.return_value = {
'registry': self.registry, 'request': self.request}
self.config.add_amd_js(
'test', 'pyramid_amdjs:tests/dir/test.js', 'Test module')
sys.argv[1:] = ['-m', 'pyramid_amdjs.ini']
stdout = sys.stdout
out = NativeIO()
sys.stdout = out
amd.main()
sys.stdout = stdout
val = out.getvalue()
self.assertIn('* test: pyramid_amdjs:tests/dir/test.js', val)
self.assertIn('Test module', val)