本文整理汇总了Python中storm.locals.Store.close方法的典型用法代码示例。如果您正苦于以下问题:Python Store.close方法的具体用法?Python Store.close怎么用?Python Store.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类storm.locals.Store
的用法示例。
在下文中一共展示了Store.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def setUp(self):
helpers.init_glsettings_for_unit_tests()
GLSettings.db_path = os.path.join(GLSettings.ramdisk_path, 'db_test')
os.mkdir(GLSettings.db_path)
db_name = 'glbackend-%d.db' % DATABASE_VERSION
db_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'db', 'populated', db_name)
shutil.copyfile(db_path, os.path.join(GLSettings.db_path, db_name))
self.db_file = os.path.join(GLSettings.db_path, db_name)
GLSettings.db_uri = GLSettings.make_db_uri(self.db_file)
# place a dummy version in the current db
store = Store(create_database(GLSettings.db_uri))
prv = config.PrivateFactory(store)
self.dummy_ver = '2.XX.XX'
prv.set_val('version', self.dummy_ver)
self.assertEqual(prv.get_val('version'), self.dummy_ver)
store.commit()
store.close()
# backup various mocks that we will use
self._bck_f = config.is_cfg_valid
GLConfig['private']['xx_smtp_password'] = GLConfig['private'].pop('smtp_password')
self.dp = u'yes_you_really_should_change_me'
示例2: test_version_change_success
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def test_version_change_success(self):
migration.perform_data_update(self.db_file)
store = Store(create_database(GLSettings.db_uri))
prv = config.PrivateFactory(store)
self.assertEqual(prv.get_val('version'), __version__)
store.close()
示例3: test_check_unmodifiable_strings
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def test_check_unmodifiable_strings(self):
# This test case asserts that data migration updates unmodifiable l10n strings
self._initStartDB(34)
notification_l10n = NotificationL10NFactory(self.store)
t0 = notification_l10n.get_val("export_template", "it")
notification_l10n.set_val("export_template", "it", "")
t1 = notification_l10n.get_val("export_template", "it")
self.assertEqual(t1, "")
self.store.commit()
# place a dummy version in the current db
store = Store(create_database(GLSettings.db_uri))
prv = config.PrivateFactory(store)
self.dummy_ver = "2.XX.XX"
prv.set_val("version", self.dummy_ver)
self.assertEqual(prv.get_val("version"), self.dummy_ver)
store.commit()
store.close()
migration.perform_data_update(self.db_file)
store = Store(create_database(GLSettings.db_uri))
notification_l10n = NotificationL10NFactory(store)
t2 = notification_l10n.get_val("export_template", "it")
self.assertEqual(t2, t0)
store.commit()
store.close()
shutil.rmtree(GLSettings.db_path)
示例4: test_migration_error_with_removed_language
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def test_migration_error_with_removed_language(self):
store = Store(create_database(GLSettings.db_uri))
zyx = EnabledLanguage('zyx')
store.add(zyx)
store.commit()
store.close()
self.assertRaises(Exception, migration.perform_data_update, self.db_file)
示例5: test_ver_change_exception
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def test_ver_change_exception(self):
# Explicity throw an exception in managed_ver_update via is_cfg_valid
config.is_cfg_valid = apply_gen(throw_excep)
self.assertRaises(IOError, migration.perform_data_update, self.db_file)
store = Store(create_database(GLSettings.db_uri))
prv = config.PrivateFactory(store)
self.assertEqual(prv.get_val('version'), self.dummy_ver)
store.close()
示例6: test_version_change_not_ok
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def test_version_change_not_ok(self):
# Set is_config_valid to false during managed ver update
config.is_cfg_valid = apply_gen(mod_bool)
self.assertRaises(Exception, migration.perform_data_update, self.db_file)
# Ensure the rollback has succeeded
store = Store(create_database(GLSettings.db_uri))
prv = config.PrivateFactory(store)
self.assertEqual(prv.get_val('version'), self.dummy_ver)
store.close()
示例7: test_detect_and_fix_cfg_change
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def test_detect_and_fix_cfg_change(self):
store = Store(create_database(GLSettings.db_uri))
ret = config.is_cfg_valid(store)
self.assertFalse(ret)
store.close()
migration.perform_data_update(self.db_file)
store = Store(create_database(GLSettings.db_uri))
prv = config.PrivateFactory(store)
self.assertEqual(prv.get_val('version'), __version__)
self.assertEqual(prv.get_val('xx_smtp_password'), self.dp)
ret = config.is_cfg_valid(store)
self.assertTrue(ret)
store.close()
示例8: createTable
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def createTable(model, transactor, database):
"""
Create the table for the specified model.
Specification of a transactor and database is useful in unittesting.
"""
if not transactor:
from oonib.db import transactor
if not database:
from oonib.db import database
store = Store(database)
create_query = generateCreateQuery(model)
try:
store.execute(create_query)
# XXX trap the specific error that is raised when the table exists
except StormError, e:
print "Failed to create table!"
print e
store.close()
示例9: createTable
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def createTable(model, transactor, database):
"""
Create the table for the specified model.
It will default to using globaleaks.db transactor and database if none is
specified.
Specification of a transactor and database is useful in unittesting.
"""
if not transactor:
from globaleaks.db import transactor
if not database:
from globaleaks.db import database
store = Store(database)
create_query = generateCreateQuery(model)
try:
store.execute(create_query)
# XXX trap the specific error that is raised when the table exists
# seem to be OperationalError raised, but not a specific error exists.
except StormError, e:
print "Failed to create table!", e
store.close()
示例10: close_connection
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def close_connection(self):
Store.close(tlocal.store)
self.isClosed = True
示例11: perform_version_update
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def perform_version_update(version):
"""
@param version:
@return:
"""
to_delete_on_fail = []
to_delete_on_success = []
if version < FIRST_DATABASE_VERSION_SUPPORTED:
GLSettings.print_msg("Migrations from DB version lower than %d are no more supported!" % FIRST_DATABASE_VERSION_SUPPORTED)
GLSettings.print_msg("If you can't create your Node from scratch, contact us asking for support.")
quit()
tmpdir = os.path.abspath(os.path.join(GLSettings.db_path, 'tmp'))
orig_db_file = os.path.abspath(os.path.join(GLSettings.db_path, 'glbackend-%d.db' % version))
final_db_file = os.path.abspath(os.path.join(GLSettings.db_path, 'glbackend-%d.db' % DATABASE_VERSION))
shutil.rmtree(tmpdir, True)
os.mkdir(tmpdir)
shutil.copy2(orig_db_file, tmpdir)
try:
while version < DATABASE_VERSION:
old_db_file = os.path.abspath(os.path.join(tmpdir, 'glbackend-%d.db' % version))
new_db_file = os.path.abspath(os.path.join(tmpdir, 'glbackend-%d.db' % (version + 1)))
GLSettings.db_file = new_db_file
GLSettings.enable_input_length_checks = False
to_delete_on_fail.append(new_db_file)
to_delete_on_success.append(old_db_file)
GLSettings.print_msg("Updating DB from version %d to version %d" % (version, version + 1))
store_old = Store(create_database('sqlite:' + old_db_file))
store_new = Store(create_database('sqlite:' + new_db_file))
# Here is instanced the migration script
MigrationModule = importlib.import_module("globaleaks.db.migrations.update_%d" % (version + 1))
migration_script = MigrationModule.MigrationScript(migration_mapping, version, store_old, store_new)
GLSettings.print_msg("Migrating table:")
try:
try:
migration_script.prologue()
except Exception as exception:
GLSettings.print_msg("Failure while executing migration prologue: %s" % exception)
raise exception
for model_name, _ in migration_mapping.iteritems():
if migration_script.model_from[model_name] is not None and migration_script.model_to[model_name] is not None:
try:
migration_script.migrate_model(model_name)
# Commit at every table migration in order to be able to detect
# the precise migration that may fail.
migration_script.commit()
except Exception as exception:
GLSettings.print_msg("Failure while migrating table %s: %s " % (model_name, exception))
raise exception
try:
migration_script.epilogue()
migration_script.commit()
except Exception as exception:
GLSettings.print_msg("Failure while executing migration epilogue: %s " % exception)
raise exception
finally:
# the database should bee always closed before leaving the application
# in order to not keep leaking journal files.
migration_script.close()
GLSettings.print_msg("Migration stats:")
# we open a new db in order to verify integrity of the generated file
store_verify = Store(create_database('sqlite:' + new_db_file))
for model_name, _ in migration_mapping.iteritems():
if model_name == 'ApplicationData':
continue
if migration_script.model_from[model_name] is not None and migration_script.model_to[model_name] is not None:
count = store_verify.find(migration_script.model_to[model_name]).count()
if migration_script.entries_count[model_name] != count:
if migration_script.fail_on_count_mismatch[model_name]:
raise AssertionError("Integrity check failed on count equality for table %s: %d != %d" % \
(model_name, count, migration_script.entries_count[model_name]))
else:
GLSettings.print_msg(" * %s table migrated (entries count changed from %d to %d)" % \
(model_name, migration_script.entries_count[model_name], count))
else:
GLSettings.print_msg(" * %s table migrated (%d entry(s))" % \
(model_name, migration_script.entries_count[model_name]))
version += 1
store_verify.close()
except Exception as exception:
#.........这里部分代码省略.........
示例12: TableReplacer
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
#.........这里部分代码省略.........
if self.start_ver + 1 == DATABASE_VERSION:
log.msg('{} Acquire SQL schema {}'.format(self.debug_info, GLSetting.db_schema_file))
if not os.access(GLSetting.db_schema_file, os.R_OK):
log.msg('Unable to access', GLSetting.db_schema_file)
raise IOError('Unable to access db schema file')
with open(GLSetting.db_schema_file) as f:
create_queries = ''.join(f).split(';')
for create_query in create_queries:
try:
self.store_new.execute(create_query + ';')
except OperationalError:
log.msg('OperationalError in "{}"'.format(create_query))
self.store_new.commit()
return
# return here and manage the migrant versions here:
for k, v in self.table_history.iteritems():
create_query = self.get_right_sql_version(k, self.start_ver + 1)
if not create_query:
# table not present in the version
continue
try:
self.store_new.execute(create_query + ';')
except OperationalError as excep:
log.msg('{} OperationalError in [{}]'.format(self.debug_info, create_query))
raise excep
self.store_new.commit()
def close(self):
self.store_old.close()
self.store_new.close()
def initialize(self):
pass
def epilogue(self):
pass
def get_right_model(self, table_name, version):
table_index = (version - 8)
if table_name not in self.table_history:
msg = 'Not implemented usage of get_right_model {} ({} {})'.format(
__file__, table_name, self.start_ver)
raise NotImplementedError(msg)
if version > DATABASE_VERSION:
raise ValueError('Version supplied must be less or equal to {}'.format(
DATABASE_VERSION))
if self.table_history[table_name][table_index]:
return self.table_history[table_name][table_index]
# else, it's none, and we've to take the previous valid version
while version >= 0:
if self.table_history[table_name][table_index]:
return self.table_history[table_name][table_index]
table_index -= 1
# This never want happen
示例13: db_update_schema
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
def db_update_schema(database):
"""
Check for pending database schema updates.
If any are found, apply them and bump the version.
"""
# Connect to the database
db_store = Store(database)
# Check if the DB schema has been loaded
db_exists = False
try:
db_store.execute(Select(DBSchema.version))
db_exists = True
except:
db_store.rollback()
logging.debug("Failed to query schema table.")
if not db_exists:
logging.info("Creating database")
schema_file = sorted(glob.glob("schema/schema-*.sql"))[-1]
schema_version = schema_file.split(".")[0].split("-")[-1]
logging.debug("Using '%s' to deploy schema '%s'" % (schema_file,
schema_version))
with open(schema_file, "r") as fd:
try:
for line in fd.read().replace("\n", "").split(";"):
if not line:
continue
db_store.execute("%s;" % line)
db_commit(db_store)
logging.info("Database created")
except:
logging.critical("Failed to initialize the database")
return False
# Get schema version
version = db_store.execute(Select(Max(DBSchema.version))).get_one()[0]
if not version:
logging.critical("No schema version.")
return False
# Apply updates
for update_file in sorted(glob.glob("schema/update-*.sql")):
update_version = update_file.split(".")[0].split("-")[-1]
if int(update_version) > version:
logging.info("Using '%s' to deploy update '%s'" % (update_file,
update_version))
with open(update_file, "r") as fd:
try:
for line in fd.read().replace("\n", "").split(";"):
if not line:
continue
db_store.execute("%s;" % line)
db_commit(db_store)
except:
logging.critical("Failed to load schema update")
return False
# Get schema version
new_version = db_store.execute(Select(Max(DBSchema.version))).get_one()[0]
if new_version > version:
logging.info("Database schema successfuly updated from '%s' to '%s'" %
(version, new_version))
db_store.close()
示例14: StormStorageBackend
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
class StormStorageBackend(StorageBackend):
"""Storage back-end based on the Storm ORM framework."""
def __init__(self):
self.store = None
def set_config(self, **kwargs):
"""Set the configuration of this back-end."""
uri = kwargs['uri']
database = create_database(uri)
self.store = Store(database)
self.logger = logging.getLogger('StormStorageBackend')
handler = logging.StreamHandler()
formatter = logging.Formatter(kwargs['log_format'])
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(
logging.__getattribute__(kwargs['log_level']))
def create_node(self, node, jid, node_config):
"""Create a PubSub node with the given configuration.
Creates the Node, NodeConfig, Affiliation and Subscription model for
the given node.
"""
self.logger.debug('Creating node %s for jid %s with config %s' %
(node, jid, node_config))
new_node = Node(node)
self.store.add(new_node)
config = copy.deepcopy(DEFAULT_CONFIG)
config.update(node_config)
for key, value in config.items():
new_node_config = NodeConfig(node, key, value)
new_node_config.updated = datetime.utcnow()
self.store.add(new_node_config)
affiliation = Affiliation(node, jid, u'owner', datetime.utcnow())
self.store.add(affiliation)
subscription = Subscription(node, jid, jid, u'subscribed',
datetime.utcnow())
self.store.add(subscription)
def create_channel(self, jid):
"""Create a channel for the given JID.
Creates all the required PubSub nodes that constitute a channel, with
the appropriate permissions.
"""
self.logger.debug('Creating channel for %s' % jid)
creation_date = unicode(datetime.utcnow().isoformat())
self.create_node(u'/user/%s/posts' % jid, jid,
{u'channelType': u'personal',
u'creationDate': creation_date,
u'defaultAffiliation': u'publisher',
u'description': u'buddycloud channel for %s' % jid,
u'title': jid})
self.create_node(u'/user/%s/geo/current' % jid, jid,
{u'creationDate': creation_date,
u'description': u'Where %s is at now' % jid,
u'title': u'%s Current Location' % jid})
self.create_node(u'/user/%s/geo/next' % jid, jid,
{u'creationDate': creation_date,
u'description': u'Where %s intends to go' % jid,
u'title': u'%s Next Location' % jid})
self.create_node(u'/user/%s/geo/previous' % jid, jid,
{u'creationDate': creation_date,
u'description': u'Where %s has been before' % jid,
u'title': u'%s Previous Location' % jid})
self.create_node(u'/user/%s/status' % jid, jid,
{u'creationDate': creation_date,
u'description': u'M000D',
u'title': u'%s status updates' % jid})
self.create_node(u'/user/%s/subscriptions' % jid, jid,
{u'creationDate': creation_date,
u'description': u'Browse my interests',
u'title': u'%s subscriptions' % jid})
self.store.commit()
def get_node(self, node):
"""Get the requested PubSub node."""
self.logger.debug('Getting node %s' % node)
the_node = self.store.get(Node, node)
self.logger.debug('Returning node %s' % the_node)
return the_node
def get_nodes(self):
"""Get a list of all the available PubSub nodes."""
self.logger.debug('Getting list of available nodes.')
node_list = self.store.find(Node)
self.logger.debug('Returning list of available node %s' % node_list)
return node_list
def add_item(self, node, item_id, item):
"""Add an item to the requested PubSub node."""
new_item = Item(node, unicode(item_id), datetime.utcnow(), item)
self.store.add(new_item)
self.store.commit()
def shutdown(self):
"""Shut down this storage module - flush, commit and close the
store."""
#.........这里部分代码省略.........
示例15: TableReplacer
# 需要导入模块: from storm.locals import Store [as 别名]
# 或者: from storm.locals.Store import close [as 别名]
class TableReplacer(object):
"""
This is the base class used by every Updater
"""
def __init__(self, table_history, old_db_file, new_db_file, start_ver):
self.table_history = table_history
self.old_db_file = old_db_file
self.new_db_file = new_db_file
self.start_ver = start_ver
self.std_fancy = " ł "
self.debug_info = " [%d => %d] " % (start_ver, start_ver + 1)
for k, v in table_history.iteritems():
length = DATABASE_VERSION + 1 - FIRST_DATABASE_VERSION_SUPPORTED
if len(v) != length:
msg = 'Expecting a table with {} statuses ({})'.format(length, k)
raise TypeError(msg)
log.msg('{} Opening old DB: {}'.format(self.debug_info, old_db_file))
old_database = create_database('sqlite:' + self.old_db_file)
self.store_old = Store(old_database)
GLSettings.db_file = new_db_file
new_database = create_database('sqlite:' + new_db_file)
self.store_new = Store(new_database)
if self.start_ver + 1 == DATABASE_VERSION:
log.msg('{} Acquire SQL schema {}'.format(self.debug_info, GLSettings.db_schema_file))
if not os.access(GLSettings.db_schema_file, os.R_OK):
log.msg('Unable to access', GLSettings.db_schema_file)
raise IOError('Unable to access db schema file')
with open(GLSettings.db_schema_file) as f:
create_queries = ''.join(f).split(';')
for create_query in create_queries:
try:
self.store_new.execute(create_query + ';')
except OperationalError:
log.msg('OperationalError in "{}"'.format(create_query))
self.store_new.commit()
return
# return here and manage the migrant versions here:
for k, v in self.table_history.iteritems():
create_query = self.get_right_sql_version(k, self.start_ver + 1)
if not create_query:
# table not present in the version
continue
try:
self.store_new.execute(create_query + ';')
except OperationalError as excep:
log.msg('{} OperationalError in [{}]'.format(self.debug_info, create_query))
raise excep
self.store_new.commit()
def close(self):
self.store_old.close()
self.store_new.close()
def initialize(self):
pass
def epilogue(self):
pass
def get_right_model(self, table_name, version):
table_index = (version - FIRST_DATABASE_VERSION_SUPPORTED)
if table_name not in self.table_history:
msg = 'Not implemented usage of get_right_model {} ({} {})'.format(
__file__, table_name, self.start_ver)
raise NotImplementedError(msg)
if version > DATABASE_VERSION:
raise ValueError('Version supplied must be less or equal to {}'.format(
DATABASE_VERSION))
if self.table_history[table_name][table_index]:
return self.table_history[table_name][table_index]
# else, it's none, and we've to take the previous valid version
while version >= 0:
if self.table_history[table_name][table_index]:
return self.table_history[table_name][table_index]
table_index -= 1
# This never want happen
return None
def get_right_sql_version(self, model_name, version):
"""
@param model_name:
@param version:
#.........这里部分代码省略.........