本文整理汇总了Python中contextlib.ExitStack.close方法的典型用法代码示例。如果您正苦于以下问题:Python ExitStack.close方法的具体用法?Python ExitStack.close怎么用?Python ExitStack.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类contextlib.ExitStack
的用法示例。
在下文中一共展示了ExitStack.close方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Fixture
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class Fixture(object):
def __init__(self, goal, workspace):
self.fixtures = ExitStack()
self.goal = goal
self.workspace = workspace
def __exit__(self, *args):
self.fixtures.close()
示例2: __init__
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class StreamDecoder:
def __init__(self, file):
self._file = file
self._crc = 0
self._pipe = PipeWriter()
self._cleanup = ExitStack()
coroutine = self._pipe.coroutine(self._receive())
self._cleanup.enter_context(coroutine)
def close(self):
self._pipe.close()
del self._pipe
self._cleanup.close()
def feed(self, data):
self._pipe.write(data)
def _receive(self):
while True:
data = self._pipe.buffer
pos = data.find(b"=")
if pos >= 0:
data = data[:pos]
data = data.replace(b"\r", b"").replace(b"\n", b"")
data = data.translate(self.TABLE)
# TODO: check data size overflow
self._crc = crc32(data, self._crc)
self._file.write(data)
if pos >= 0: # Escape character (equals sign)
self._pipe.buffer = self._pipe.buffer[pos + 1:]
while True:
byte = yield from self._pipe.read_one()
if byte not in b"\r\n":
break
# TODO: check for size overflow
[byte] = byte
data = bytes(((byte - 64 - 42) & bitmask(8),))
self._crc = crc32(data, self._crc)
self._file.write(data)
else:
try:
self._pipe.buffer = yield
except EOFError:
break
def flush(self):
pass
def getCrc32(self):
return format(self._crc, "08x")
TABLE = bytes(range(256))
TABLE = TABLE[-42:] + TABLE[:-42]
示例3: PulpWritter
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class PulpWritter(object):
"""Use this to create a pulp db."""
def __init__(self, db_name, msg_dumper=None, idx_dumpers=None):
self.dir_path = os.path.abspath(db_name)
self.keys_path = os.path.join(self.dir_path, 'keys')
if os.path.isdir(self.dir_path):
shutil.rmtree(self.dir_path)
os.makedirs(self.dir_path)
os.makedirs(self.keys_path)
self.master_table = None
self.key_tables = {}
self.table_stack = None
self.msg_dumper = msg_dumper
self.idx_dumpers = idx_dumpers
def __enter__(self):
self.table_stack = ExitStack().__enter__()
table = MasterTable(self.dir_path, 'w', dumper=self.msg_dumper)
self.add_table(None, table)
return self
def __exit__(self, exc_type, exc_value, traceback):
if self.table_stack is not None:
self.table_stack.close()
def add_table(self, key, table):
if key == None:
self.master_table = table
else:
self.key_tables[key] = table
self.table_stack.enter_context(table)
def append(self, data, index_map):
msg_num = self.master_table.append(data)
for key, value in index_map.items():
table = self.key_tables.get(key, None)
if not table:
if self.idx_dumpers is not None:
dumper = self.idx_dumpers.get(key, None)
else:
dumper = None
table = KeyTable(self.keys_path, key, 'w', dumper=dumper)
self.add_table(key, table)
if isinstance(value, (tuple, list, set)):
for v in value:
table.append(v, msg_num)
else:
table.append(value, msg_num)
示例4: TestAzureBounce
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class TestAzureBounce(TestCase):
def mock_out_azure_moving_parts(self):
self.patches.enter_context(
mock.patch.object(DataSourceAzure, 'invoke_agent'))
self.patches.enter_context(
mock.patch.object(DataSourceAzure, 'wait_for_files'))
self.patches.enter_context(
mock.patch.object(DataSourceAzure, 'list_possible_azure_ds_devs',
mock.MagicMock(return_value=[])))
self.patches.enter_context(
mock.patch.object(DataSourceAzure,
'find_fabric_formatted_ephemeral_disk',
mock.MagicMock(return_value=None)))
self.patches.enter_context(
mock.patch.object(DataSourceAzure,
'find_fabric_formatted_ephemeral_part',
mock.MagicMock(return_value=None)))
self.patches.enter_context(
mock.patch.object(DataSourceAzure, 'get_metadata_from_fabric',
mock.MagicMock(return_value={})))
self.patches.enter_context(
mock.patch.object(DataSourceAzure.util, 'read_dmi_data',
mock.MagicMock(return_value='test-instance-id')))
def setUp(self):
super(TestAzureBounce, self).setUp()
self.tmp = tempfile.mkdtemp()
self.waagent_d = os.path.join(self.tmp, 'var', 'lib', 'waagent')
self.paths = helpers.Paths({'cloud_dir': self.tmp})
self.addCleanup(shutil.rmtree, self.tmp)
DataSourceAzure.BUILTIN_DS_CONFIG['data_dir'] = self.waagent_d
self.patches = ExitStack()
self.mock_out_azure_moving_parts()
self.get_hostname = self.patches.enter_context(
mock.patch.object(DataSourceAzure, 'get_hostname'))
self.set_hostname = self.patches.enter_context(
mock.patch.object(DataSourceAzure, 'set_hostname'))
self.subp = self.patches.enter_context(
mock.patch('cloudinit.sources.DataSourceAzure.util.subp'))
def tearDown(self):
self.patches.close()
def _get_ds(self, ovfcontent=None):
if ovfcontent is not None:
populate_dir(os.path.join(self.paths.seed_dir, "azure"),
{'ovf-env.xml': ovfcontent})
return DataSourceAzure.DataSourceAzureNet(
{}, distro=None, paths=self.paths)
def get_ovf_env_with_dscfg(self, hostname, cfg):
odata = {
'HostName': hostname,
'dscfg': {
'text': b64e(yaml.dump(cfg)),
'encoding': 'base64'
}
}
return construct_valid_ovf_env(data=odata)
def test_disabled_bounce_does_not_change_hostname(self):
cfg = {'hostname_bounce': {'policy': 'off'}}
self._get_ds(self.get_ovf_env_with_dscfg('test-host', cfg)).get_data()
self.assertEqual(0, self.set_hostname.call_count)
@mock.patch('cloudinit.sources.DataSourceAzure.perform_hostname_bounce')
def test_disabled_bounce_does_not_perform_bounce(
self, perform_hostname_bounce):
cfg = {'hostname_bounce': {'policy': 'off'}}
self._get_ds(self.get_ovf_env_with_dscfg('test-host', cfg)).get_data()
self.assertEqual(0, perform_hostname_bounce.call_count)
def test_same_hostname_does_not_change_hostname(self):
host_name = 'unchanged-host-name'
self.get_hostname.return_value = host_name
cfg = {'hostname_bounce': {'policy': 'yes'}}
self._get_ds(self.get_ovf_env_with_dscfg(host_name, cfg)).get_data()
self.assertEqual(0, self.set_hostname.call_count)
@mock.patch('cloudinit.sources.DataSourceAzure.perform_hostname_bounce')
def test_unchanged_hostname_does_not_perform_bounce(
self, perform_hostname_bounce):
host_name = 'unchanged-host-name'
self.get_hostname.return_value = host_name
cfg = {'hostname_bounce': {'policy': 'yes'}}
self._get_ds(self.get_ovf_env_with_dscfg(host_name, cfg)).get_data()
self.assertEqual(0, perform_hostname_bounce.call_count)
@mock.patch('cloudinit.sources.DataSourceAzure.perform_hostname_bounce')
def test_force_performs_bounce_regardless(self, perform_hostname_bounce):
host_name = 'unchanged-host-name'
self.get_hostname.return_value = host_name
cfg = {'hostname_bounce': {'policy': 'force'}}
self._get_ds(self.get_ovf_env_with_dscfg(host_name, cfg)).get_data()
self.assertEqual(1, perform_hostname_bounce.call_count)
def test_different_hostnames_sets_hostname(self):
expected_hostname = 'azure-expected-host-name'
self.get_hostname.return_value = 'default-host-name'
#.........这里部分代码省略.........
示例5: RateLimiterTestCase
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class RateLimiterTestCase(unittest.TestCase):
def setUp(self):
self._stack = ExitStack()
self.mock_clock = self._stack.enter_context(
patch.object(RateLimiter, '_clock'))
self.mock_sleep = self._stack.enter_context(
patch.object(RateLimiter, '_sleep'))
self.mock_func = MagicMock()
def tearDown(self):
self._stack.close()
def test_min_delay(self):
min_delay = 3.5
self.mock_clock.side_effect = [1]
rl = RateLimiter(self.mock_func, min_delay_seconds=min_delay)
# First call -- no delay
clock_first = 10
self.mock_clock.side_effect = [clock_first, clock_first] # no delay here
rl(sentinel.arg, kwa=sentinel.kwa)
self.mock_sleep.assert_not_called()
self.mock_func.assert_called_once_with(sentinel.arg, kwa=sentinel.kwa)
# Second call after min_delay/3 seconds -- should be delayed
clock_second = clock_first + (min_delay / 3)
self.mock_clock.side_effect = [clock_second, clock_first + min_delay]
rl(sentinel.arg, kwa=sentinel.kwa)
self.mock_sleep.assert_called_with(min_delay - (clock_second - clock_first))
self.mock_sleep.reset_mock()
# Third call after min_delay*2 seconds -- no delay again
clock_third = clock_first + min_delay + min_delay * 2
self.mock_clock.side_effect = [clock_third, clock_third]
rl(sentinel.arg, kwa=sentinel.kwa)
self.mock_sleep.assert_not_called()
def test_max_retries(self):
self.mock_clock.return_value = 1
rl = RateLimiter(self.mock_func, max_retries=3,
return_value_on_exception=sentinel.return_value)
# Non-geopy errors must not be swallowed
self.mock_func.side_effect = ValueError
with self.assertRaises(ValueError):
rl(sentinel.arg)
self.assertEqual(1, self.mock_func.call_count)
self.mock_func.reset_mock()
# geopy errors must be swallowed and retried
self.mock_func.side_effect = GeocoderServiceError
self.assertEqual(sentinel.return_value, rl(sentinel.arg))
self.assertEqual(4, self.mock_func.call_count)
self.mock_func.reset_mock()
# Successful value must be returned
self.mock_func.side_effect = [
GeocoderServiceError, GeocoderServiceError, sentinel.good
]
self.assertEqual(sentinel.good, rl(sentinel.arg))
self.assertEqual(3, self.mock_func.call_count)
self.mock_func.reset_mock()
# When swallowing is disabled, the exception must be raised
rl.swallow_exceptions = False
self.mock_func.side_effect = GeocoderQuotaExceeded
with self.assertRaises(GeocoderQuotaExceeded):
rl(sentinel.arg)
self.assertEqual(4, self.mock_func.call_count)
self.mock_func.reset_mock()
def test_error_wait_seconds(self):
error_wait = 3.3
self.mock_clock.return_value = 1
rl = RateLimiter(self.mock_func, max_retries=3,
error_wait_seconds=error_wait,
return_value_on_exception=sentinel.return_value)
self.mock_func.side_effect = GeocoderServiceError
self.assertEqual(sentinel.return_value, rl(sentinel.arg))
self.assertEqual(4, self.mock_func.call_count)
self.assertEqual(3, self.mock_sleep.call_count)
self.mock_sleep.assert_called_with(error_wait)
self.mock_func.reset_mock()
示例6: TestCase
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class TestCase(unittest.TestCase):
def setUp(self):
super(TestCase, self).setUp()
self.__all_cleanups = ExitStack()
def tearDown(self):
self.__all_cleanups.close()
unittest.TestCase.tearDown(self)
def addCleanup(self, function, *args, **kws):
self.__all_cleanups.callback(function, *args, **kws)
def assertIs(self, expr1, expr2, msg=None):
if expr1 is not expr2:
standardMsg = '%r is not %r' % (expr1, expr2)
self.fail(self._formatMessage(msg, standardMsg))
def assertIn(self, member, container, msg=None):
if member not in container:
standardMsg = '%r not found in %r' % (member, container)
self.fail(self._formatMessage(msg, standardMsg))
def assertNotIn(self, member, container, msg=None):
if member in container:
standardMsg = '%r unexpectedly found in %r'
standardMsg = standardMsg % (member, container)
self.fail(self._formatMessage(msg, standardMsg))
def assertIsNone(self, value, msg=None):
if value is not None:
standardMsg = '%r is not None'
standardMsg = standardMsg % (value)
self.fail(self._formatMessage(msg, standardMsg))
def assertIsInstance(self, obj, cls, msg=None):
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer
default message."""
if not isinstance(obj, cls):
standardMsg = '%s is not an instance of %r' % (repr(obj), cls)
self.fail(self._formatMessage(msg, standardMsg))
def assertDictContainsSubset(self, expected, actual, msg=None):
missing = []
mismatched = []
for k, v in expected.items():
if k not in actual:
missing.append(k)
elif actual[k] != v:
mismatched.append('%r, expected: %r, actual: %r'
% (k, v, actual[k]))
if len(missing) == 0 and len(mismatched) == 0:
return
standardMsg = ''
if missing:
standardMsg = 'Missing: %r' % ','.join(m for m in missing)
if mismatched:
if standardMsg:
standardMsg += '; '
standardMsg += 'Mismatched values: %s' % ','.join(mismatched)
self.fail(self._formatMessage(msg, standardMsg))
示例7: FilesystemMockingTestCase
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class FilesystemMockingTestCase(ResourceUsingTestCase):
def setUp(self):
super(FilesystemMockingTestCase, self).setUp()
self.patched_funcs = ExitStack()
def tearDown(self):
self.patched_funcs.close()
ResourceUsingTestCase.tearDown(self)
def replicateTestRoot(self, example_root, target_root):
real_root = self.resourceLocation()
real_root = os.path.join(real_root, 'roots', example_root)
for (dir_path, _dirnames, filenames) in os.walk(real_root):
real_path = dir_path
make_path = rebase_path(real_path[len(real_root):], target_root)
util.ensure_dir(make_path)
for f in filenames:
real_path = util.abs_join(real_path, f)
make_path = util.abs_join(make_path, f)
shutil.copy(real_path, make_path)
def patchUtils(self, new_root):
patch_funcs = {
util: [('write_file', 1),
('append_file', 1),
('load_file', 1),
('ensure_dir', 1),
('chmod', 1),
('delete_dir_contents', 1),
('del_file', 1),
('sym_link', -1),
('copy', -1)],
}
for (mod, funcs) in patch_funcs.items():
for (f, am) in funcs:
func = getattr(mod, f)
trap_func = retarget_many_wrapper(new_root, am, func)
self.patched_funcs.enter_context(
mock.patch.object(mod, f, trap_func))
# Handle subprocess calls
func = getattr(util, 'subp')
def nsubp(*_args, **_kwargs):
return ('', '')
self.patched_funcs.enter_context(
mock.patch.object(util, 'subp', nsubp))
def null_func(*_args, **_kwargs):
return None
for f in ['chownbyid', 'chownbyname']:
self.patched_funcs.enter_context(
mock.patch.object(util, f, null_func))
def patchOS(self, new_root):
patch_funcs = {
os.path: [('isfile', 1), ('exists', 1),
('islink', 1), ('isdir', 1)],
os: [('listdir', 1), ('mkdir', 1),
('lstat', 1), ('symlink', 2)],
}
for (mod, funcs) in patch_funcs.items():
for f, nargs in funcs:
func = getattr(mod, f)
trap_func = retarget_many_wrapper(new_root, nargs, func)
self.patched_funcs.enter_context(
mock.patch.object(mod, f, trap_func))
def patchOpen(self, new_root):
trap_func = retarget_many_wrapper(new_root, 1, open)
name = 'builtins.open' if PY3 else '__builtin__.open'
self.patched_funcs.enter_context(mock.patch(name, trap_func))
def patchStdoutAndStderr(self, stdout=None, stderr=None):
if stdout is not None:
self.patched_funcs.enter_context(
mock.patch.object(sys, 'stdout', stdout))
if stderr is not None:
self.patched_funcs.enter_context(
mock.patch.object(sys, 'stderr', stderr))
示例8: NosePlugin
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class NosePlugin(Plugin):
configSection = 'ubuntu-image'
snap_mocker = None
def __init__(self):
super().__init__()
self.patterns = []
self.addArgument(self.patterns, 'P', 'pattern',
'Add a test matching pattern')
def getTestCaseNames(self, event):
if len(self.patterns) == 0:
# No filter patterns, so everything should be tested.
return
# Does the pattern match the fully qualified class name?
for pattern in self.patterns:
full_class_name = '{}.{}'.format(
event.testCase.__module__, event.testCase.__name__)
if re.search(pattern, full_class_name):
# Don't suppress this test class.
return
names = filter(event.isTestMethod, dir(event.testCase))
for name in names:
full_test_name = '{}.{}.{}'.format(
event.testCase.__module__,
event.testCase.__name__,
name)
for pattern in self.patterns:
if re.search(pattern, full_test_name):
break
else:
event.excludedNames.append(name)
def handleFile(self, event):
path = event.path[len(TOPDIR)+1:]
if len(self.patterns) > 0:
for pattern in self.patterns:
if re.search(pattern, path):
break
else:
# Skip this doctest.
return
base, ext = os.path.splitext(path)
if ext != '.rst':
return
test = doctest.DocFileTest(
path, package='ubuntu_image',
optionflags=FLAGS,
setUp=setup,
tearDown=teardown)
# Suppress the extra "Doctest: ..." line.
test.shortDescription = lambda: None
event.extraTests.append(test)
def startTestRun(self, event):
# Create a mock for the `sudo snap prepare-image` command. This is an
# expensive command which hits the actual snap store. We want this to
# run at least once so we know our tests are valid. We can cache the
# results in a test-suite-wide temporary directory and simulate future
# calls by just recursively copying the contents to the specified
# directories.
#
# It's a bit more complicated than that though, because it's possible
# that the channel and model.assertion will be different, so we need
# to make the cache dependent on those values.
#
# Finally, to enable full end-to-end tests, check an environment
# variable to see if the mocking should even be done. This way, we
# can make our Travis-CI job do at least one real end-to-end test.
self.resources = ExitStack()
# How should we mock `snap prepare-image`? If set to 'always' (case
# insensitive), then use the sample data in the .zip file. Any other
# truthy value says to use a second-and-onward mock.
should_we_mock = os.environ.get('UBUNTU_IMAGE_MOCK_SNAP', 'yes')
if should_we_mock.lower() == 'always':
mock_class = AlwaysMock
elif as_bool(should_we_mock):
mock_class = SecondAndOnwardMock
else:
mock_class = None
if mock_class is not None:
tmpdir = self.resources.enter_context(TemporaryDirectory())
# Record the actual snap mocker on the class so that other tests
# can temporarily disable it. Some tests need to run the actual
# snap() helper function.
self.__class__.snap_mocker = self.resources.enter_context(
mock_class(tmpdir))
def stopTestRun(self, event):
self.resources.close()
示例9: PulpReader
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import close [as 别名]
class PulpReader(object):
"""
db.idx
db.idx(stuff) == KeyQuery or vQuery
db.stream = StreamQuery
db.stream(stuff) == StreamQuery or vQuery
db.vQuery
# All three main query objects support the same api.
KeyQuery
StreamQuery
vQuery
Thus they can be merged.
"""
def __init__(self, db_name, msg_dumper=None, msg_loader=None, idx_dumpers=None, idx_loaders=None):
self.dir_path = os.path.abspath(db_name)
self.keys_path = os.path.join(self.dir_path, 'keys')
if not all(os.path.exists(p) for p in [self.dir_path, self.keys_path]):
print("Missing directory: one of {}".format([self.dir_path,
self.keys_path]))
self.table_stack = None
self.master_table = None
self.key_tables = {}
self.msg_dumper = msg_dumper
self.msg_loader = msg_loader
self.idx_dumpers = idx_dumpers
self.idx_loaders = idx_loaders
@property
def stream(self):
return StreamQuery(self.master_table)
@property
def idx(self):
return FieldQueryDispatch(self.master_table, self.key_tables)
def __enter__(self):
self.table_stack = ExitStack().__enter__()
table = MasterTable(self.dir_path, 'r', dumper=self.msg_dumper ,loader=self.msg_loader)
self._add_table(None, table)
keys = [os.path.splitext(f)[0] for f in os.listdir(self.keys_path) if f.endswith('.meta')] ## More needed here. More than just .meta files required.
for key in keys:
if self.idx_dumpers is not None:
dumper = self.idx_dumpers.get(key, None)
else:
dumper = None
if self.idx_loaders is not None:
loader = self.idx_loaders.get(key, None)
else:
loader = None
table = KeyTable(self.keys_path, key, 'r', dumper=dumper, loader=loader)
self._add_table(key, table)
return self
def _add_table(self, key, table):
if key == None:
self.master_table = table
else:
self.key_tables[key] = table
self.table_stack.enter_context(table)
def __exit__(self, exc_type, exc_value, traceback):
if self.table_stack is not None:
self.table_stack.close()
def __len__(self):
"""Dispatches to stream"""
return len(self.stream)
def __iter__(self):
return iter(self.stream)
def __getitem__(self, index):
"""Dispatches to stream"""
if isinstance(index, int):
return self.stream[index]
elif isinstance(index, slice):
return self.stream[index]
else:
raise NotImplementedError("index")