本文整理汇总了Python中charmhelper.core.unitdata.kv方法的典型用法代码示例。如果您正苦于以下问题:Python unitdata.kv方法的具体用法?Python unitdata.kv怎么用?Python unitdata.kv使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类charmhelper.core.unitdata
的用法示例。
在下文中一共展示了unitdata.kv方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getrange
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def getrange(self, key_prefix, strip=False):
"""
Get a range of keys starting with a common prefix as a mapping of
keys to values.
:param str key_prefix: Common prefix among all keys
:param bool strip: Optionally strip the common prefix from the key
names in the returned dict
:return dict: A (possibly empty) dict of key-value mappings
"""
self.cursor.execute("select key, data from kv where key like ?",
['%s%%' % key_prefix])
result = self.cursor.fetchall()
if not result:
return {}
if not strip:
key_prefix = ''
return dict([
(k[len(key_prefix):], json.loads(v)) for k, v in result])
示例2: unsetrange
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def unsetrange(self, keys=None, prefix=""):
"""
Remove a range of keys starting with a common prefix, from the database
entirely.
:param list keys: List of keys to remove.
:param str prefix: Optional prefix to apply to all keys in ``keys``
before removing.
"""
if keys is not None:
keys = ['%s%s' % (prefix, key) for key in keys]
self.cursor.execute('delete from kv where key in (%s)' % ','.join(['?'] * len(keys)), keys)
if self.revision and self.cursor.rowcount:
self.cursor.execute(
'insert into kv_revisions values %s' % ','.join(['(?, ?, ?)'] * len(keys)),
list(itertools.chain.from_iterable((key, self.revision, json.dumps('DELETED')) for key in keys)))
else:
self.cursor.execute('delete from kv where key like ?',
['%s%%' % prefix])
if self.revision and self.cursor.rowcount:
self.cursor.execute(
'insert into kv_revisions values (?, ?, ?)',
['%s%%' % prefix, self.revision, json.dumps('DELETED')])
示例3: _init
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def _init(self):
self.cursor.execute('''
create table if not exists kv (
key text,
data text,
primary key (key)
)''')
self.cursor.execute('''
create table if not exists kv_revisions (
key text,
revision integer,
data text,
primary key (key, revision)
)''')
self.cursor.execute('''
create table if not exists hooks (
version integer primary key autoincrement,
hook text,
date text
)''')
self.conn.commit()
示例4: get
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def get(self, key, default=None, record=False):
self.cursor.execute('select data from kv where key=?', [key])
result = self.cursor.fetchone()
if not result:
return default
if record:
return Record(json.loads(result[0]))
return json.loads(result[0])
示例5: unset
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def unset(self, key):
"""
Remove a key from the database entirely.
"""
self.cursor.execute('delete from kv where key=?', [key])
if self.revision and self.cursor.rowcount:
self.cursor.execute(
'insert into kv_revisions values (?, ?, ?)',
[key, self.revision, json.dumps('DELETED')])
示例6: gethistory
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def gethistory(self, key, deserialize=False):
self.cursor.execute(
'''
select kv.revision, kv.key, kv.data, h.hook, h.date
from kv_revisions kv,
hooks h
where kv.key=?
and kv.revision = h.version
''', [key])
if deserialize is False:
return self.cursor.fetchall()
return map(_parse_history, self.cursor.fetchall())
示例7: debug
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def debug(self, fh=sys.stderr):
self.cursor.execute('select * from kv')
pprint.pprint(self.cursor.fetchall(), stream=fh)
self.cursor.execute('select * from kv_revisions')
pprint.pprint(self.cursor.fetchall(), stream=fh)
示例8: __init__
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def __init__(self):
self.kv = kv()
self.conf = None
self.rels = None
示例9: __call__
# 需要导入模块: from charmhelper.core import unitdata [as 别名]
# 或者: from charmhelper.core.unitdata import kv [as 别名]
def __call__(self):
from charmhelpers.core import hookenv
hook_name = hookenv.hook_name()
with self.kv.hook_scope(hook_name):
self._record_charm_version(hookenv.charm_dir())
delta_config, delta_relation = self._record_hook(hookenv)
yield self.kv, delta_config, delta_relation