本文整理汇总了Python中st2common.util.isotime.format函数的典型用法代码示例。如果您正苦于以下问题:Python format函数的具体用法?Python format怎么用?Python format使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了format函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_notify_triggers_jinja_patterns
def test_notify_triggers_jinja_patterns(self, dispatch):
liveaction_db = LiveActionDB(action='core.local')
liveaction_db.id = bson.ObjectId()
liveaction_db.description = ''
liveaction_db.status = 'succeeded'
liveaction_db.parameters = {'cmd': 'mamma mia', 'runner_foo': 'foo'}
on_success = NotificationSubSchema(message='Command {{action_parameters.cmd}} succeeded.',
data={'stdout': '{{action_results.stdout}}'})
liveaction_db.notify = NotificationSchema(on_success=on_success)
liveaction_db.start_timestamp = date_utils.get_datetime_utc_now()
liveaction_db.end_timestamp = \
(liveaction_db.start_timestamp + datetime.timedelta(seconds=50))
LiveAction.add_or_update(liveaction_db)
execution = MOCK_EXECUTION
execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
execution.status = liveaction_db.status
notifier = Notifier(connection=None, queues=[])
notifier.process(execution)
exp = {'status': 'succeeded',
'start_timestamp': isotime.format(liveaction_db.start_timestamp),
'route': 'notify.default', 'runner_ref': 'local-shell-cmd',
'channel': 'notify.default', 'message': u'Command mamma mia succeeded.',
'data': {'result': '{}', 'stdout': 'stuff happens'},
'action_ref': u'core.local',
'execution_id': str(MOCK_EXECUTION.id),
'end_timestamp': isotime.format(liveaction_db.end_timestamp)}
dispatch.assert_called_once_with('core.st2.generic.notifytrigger', payload=exp,
trace_context={})
notifier.process(execution)
示例2: _format_action_exec_result
def _format_action_exec_result(self, action_node, liveaction_db, created_at, updated_at,
error=None):
"""
Format ActionExecution result so it can be used in the final action result output.
:rtype: ``dict``
"""
assert(isinstance(created_at, datetime.datetime))
assert(isinstance(updated_at, datetime.datetime))
result = {}
execution_db = None
if liveaction_db:
execution_db = ActionExecution.get(liveaction__id=str(liveaction_db.id))
result['id'] = action_node.name
result['name'] = action_node.name
result['execution_id'] = str(execution_db.id) if execution_db else None
result['workflow'] = None
result['created_at'] = isotime.format(dt=created_at)
result['updated_at'] = isotime.format(dt=updated_at)
if error or not liveaction_db:
result['state'] = LIVEACTION_STATUS_FAILED
else:
result['state'] = liveaction_db.status
if error:
result['result'] = error
else:
result['result'] = liveaction_db.result
return result
示例3: pull_to_dict
def pull_to_dict(pull):
result = {}
author = user_to_dict(pull.user)
assignee = user_to_dict(pull.assignee)
merged_by = user_to_dict(pull.merged_by)
result['id'] = pull.id
result['pr_id'] = int(re.sub(r'.*/([0-9]+)(#.*)?', r'\1', pull.html_url))
result['author'] = author
result['assign'] = assignee
result['title'] = pull.title
result['body'] = pull.body
result['url'] = pull.html_url
result['base'] = pull.base.ref
result['head'] = pull.head.ref
result['state'] = pull.state
result['merged'] = pull.merged
result['mergeable_state'] = pull.mergeable_state
result['merge_commit_sha'] = pull.merge_commit_sha
if pull.labels:
labels = [label_to_dict(label) for label in pull.labels]
else:
labels = []
result['labels'] = labels
if pull.get_commits():
commits = [commit_to_dict(commit) for commit in pull.get_commits()]
else:
commits = []
result['commits'] = commits
# Note: We convert it to a serialize type (string)
if pull.created_at:
created_at = isotime.format(pull.created_at)
else:
created_at = None
if pull.closed_at:
closed_at = isotime.format(pull.closed_at)
else:
closed_at = None
if pull.merged_at:
merged_at = isotime.format(pull.merged_at)
else:
merged_at = None
result['created_at'] = created_at
result['closed_at'] = closed_at
result['merged_at'] = merged_at
result['merged_by'] = merged_by
return result
示例4: from_model
def from_model(cls, model, mask_secrets=False):
doc = super(cls, cls)._from_model(model, mask_secrets=mask_secrets)
if model.start_timestamp:
doc['start_timestamp'] = isotime.format(model.start_timestamp, offset=False)
if model.end_timestamp:
doc['end_timestamp'] = isotime.format(model.end_timestamp, offset=False)
if getattr(model, 'notify', None):
doc['notify'] = NotificationsHelper.from_model(model.notify)
return cls(**doc)
示例5: from_model
def from_model(cls, model, mask_secrets=False):
doc = cls._from_model(model, mask_secrets=mask_secrets)
start_timestamp = isotime.format(model.start_timestamp, offset=False)
doc['start_timestamp'] = start_timestamp
end_timestamp = model.end_timestamp
if end_timestamp:
end_timestamp = isotime.format(end_timestamp, offset=False)
doc['end_timestamp'] = end_timestamp
attrs = {attr: value for attr, value in six.iteritems(doc) if value}
return cls(**attrs)
示例6: test_request
def test_request(self):
request, execution = self._submit_request()
self.assertIsNotNone(execution)
self.assertEqual(execution.id, request.id)
self.assertEqual(execution.action, '.'.join([self.actiondb.pack, self.actiondb.name]))
self.assertEqual(execution.context['user'], request.context['user'])
self.assertDictEqual(execution.parameters, request.parameters)
self.assertEqual(execution.status, action_constants.LIVEACTION_STATUS_REQUESTED)
self.assertTrue(execution.notify is not None)
# mongoengine DateTimeField stores datetime only up to milliseconds
self.assertEqual(isotime.format(execution.start_timestamp, usec=False),
isotime.format(request.start_timestamp, usec=False))
示例7: setUpClass
def setUpClass(cls):
super(TestActionExecutionFilters, cls).setUpClass()
cls.dt_base = date_utils.add_utc_tz(datetime.datetime(2014, 12, 25, 0, 0, 0))
cls.num_records = 100
cls.refs = {}
cls.start_timestamps = []
cls.fake_types = [
{
'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']),
'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']),
'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance']),
'rule': copy.deepcopy(fixture.ARTIFACTS['rule']),
'action': copy.deepcopy(fixture.ARTIFACTS['actions']['chain']),
'runner': copy.deepcopy(fixture.ARTIFACTS['runners']['action-chain']),
'liveaction': copy.deepcopy(fixture.ARTIFACTS['liveactions']['workflow']),
'context': copy.deepcopy(fixture.ARTIFACTS['context']),
'children': []
},
{
'action': copy.deepcopy(fixture.ARTIFACTS['actions']['local']),
'runner': copy.deepcopy(fixture.ARTIFACTS['runners']['run-local']),
'liveaction': copy.deepcopy(fixture.ARTIFACTS['liveactions']['task1'])
}
]
def assign_parent(child):
candidates = [v for k, v in cls.refs.items() if v.action['name'] == 'chain']
if candidates:
parent = random.choice(candidates)
child['parent'] = str(parent.id)
parent.children.append(child['id'])
cls.refs[str(parent.id)] = ActionExecution.add_or_update(parent)
for i in range(cls.num_records):
obj_id = str(bson.ObjectId())
timestamp = cls.dt_base + datetime.timedelta(seconds=i)
fake_type = random.choice(cls.fake_types)
data = copy.deepcopy(fake_type)
data['id'] = obj_id
data['start_timestamp'] = isotime.format(timestamp, offset=False)
data['end_timestamp'] = isotime.format(timestamp, offset=False)
data['status'] = data['liveaction']['status']
data['result'] = data['liveaction']['result']
if fake_type['action']['name'] == 'local' and random.choice([True, False]):
assign_parent(data)
wb_obj = ActionExecutionAPI(**data)
db_obj = ActionExecutionAPI.to_model(wb_obj)
cls.refs[obj_id] = ActionExecution.add_or_update(db_obj)
cls.start_timestamps.append(timestamp)
cls.start_timestamps = sorted(cls.start_timestamps)
示例8: setUpClass
def setUpClass(cls):
super(TestActionExecutionFilters, cls).setUpClass()
cls.dt_base = date_utils.add_utc_tz(datetime.datetime(2014, 12, 25, 0, 0, 0))
cls.num_records = 100
cls.refs = {}
cls.start_timestamps = []
cls.fake_types = [
{
"trigger": copy.deepcopy(fixture.ARTIFACTS["trigger"]),
"trigger_type": copy.deepcopy(fixture.ARTIFACTS["trigger_type"]),
"trigger_instance": copy.deepcopy(fixture.ARTIFACTS["trigger_instance"]),
"rule": copy.deepcopy(fixture.ARTIFACTS["rule"]),
"action": copy.deepcopy(fixture.ARTIFACTS["actions"]["chain"]),
"runner": copy.deepcopy(fixture.ARTIFACTS["runners"]["action-chain"]),
"liveaction": copy.deepcopy(fixture.ARTIFACTS["liveactions"]["workflow"]),
"context": copy.deepcopy(fixture.ARTIFACTS["context"]),
"children": [],
},
{
"action": copy.deepcopy(fixture.ARTIFACTS["actions"]["local"]),
"runner": copy.deepcopy(fixture.ARTIFACTS["runners"]["run-local"]),
"liveaction": copy.deepcopy(fixture.ARTIFACTS["liveactions"]["task1"]),
},
]
def assign_parent(child):
candidates = [v for k, v in cls.refs.iteritems() if v.action["name"] == "chain"]
if candidates:
parent = random.choice(candidates)
child["parent"] = str(parent.id)
parent.children.append(child["id"])
cls.refs[str(parent.id)] = ActionExecution.add_or_update(parent)
for i in range(cls.num_records):
obj_id = str(bson.ObjectId())
timestamp = cls.dt_base + datetime.timedelta(seconds=i)
fake_type = random.choice(cls.fake_types)
data = copy.deepcopy(fake_type)
data["id"] = obj_id
data["start_timestamp"] = isotime.format(timestamp, offset=False)
data["end_timestamp"] = isotime.format(timestamp, offset=False)
data["status"] = data["liveaction"]["status"]
data["result"] = data["liveaction"]["result"]
if fake_type["action"]["name"] == "local" and random.choice([True, False]):
assign_parent(data)
wb_obj = ActionExecutionAPI(**data)
db_obj = ActionExecutionAPI.to_model(wb_obj)
cls.refs[obj_id] = ActionExecution.add_or_update(db_obj)
cls.start_timestamps.append(timestamp)
cls.start_timestamps = sorted(cls.start_timestamps)
示例9: from_model
def from_model(cls, model, mask_secrets=True):
if not KeyValuePairAPI.crypto_setup:
KeyValuePairAPI._setup_crypto()
doc = cls._from_model(model, mask_secrets=mask_secrets)
if getattr(model, 'expire_timestamp', None) and model.expire_timestamp:
doc['expire_timestamp'] = isotime.format(model.expire_timestamp, offset=False)
encrypted = False
secret = getattr(model, 'secret', False)
if secret:
encrypted = True
if not mask_secrets and secret:
doc['value'] = symmetric_decrypt(KeyValuePairAPI.crypto_key, model.value)
encrypted = False
scope = getattr(model, 'scope', SYSTEM_SCOPE)
if scope:
doc['scope'] = scope
key = doc.get('name', None)
if (scope == USER_SCOPE or scope == FULL_USER_SCOPE) and key:
doc['user'] = UserKeyReference.get_user(key)
doc['name'] = UserKeyReference.get_name(key)
doc['encrypted'] = encrypted
attrs = {attr: value for attr, value in six.iteritems(doc) if value is not None}
return cls(**attrs)
示例10: from_model
def from_model(cls, model, mask_secrets=False):
instance = cls._from_model(model, mask_secrets=mask_secrets)
if instance.get('occurrence_time', None):
instance['occurrence_time'] = isotime.format(instance['occurrence_time'], offset=False)
return cls(**instance)
示例11: _purge_action_executions
def _purge_action_executions(self):
"""
Purge action executions and corresponding live action, stdout and stderr object which match
the criteria defined in the config.
"""
LOG.info('Performing garbage collection for action executions and related objects')
utc_now = get_datetime_utc_now()
timestamp = (utc_now - datetime.timedelta(days=self._action_executions_ttl))
# Another sanity check to make sure we don't delete new executions
if timestamp > (utc_now - datetime.timedelta(days=MINIMUM_TTL_DAYS)):
raise ValueError('Calculated timestamp would violate the minimum TTL constraint')
timestamp_str = isotime.format(dt=timestamp)
LOG.info('Deleting action executions older than: %s' % (timestamp_str))
assert timestamp < utc_now
try:
purge_executions(logger=LOG, timestamp=timestamp)
except Exception as e:
LOG.exception('Failed to delete executions: %s' % (six.text_type(e)))
return True
示例12: from_model
def from_model(cls, model, mask_secrets=False):
doc = cls._from_model(model, mask_secrets=mask_secrets)
start_timestamp = model.start_timestamp
start_timestamp_iso = isotime.format(start_timestamp, offset=False)
doc['start_timestamp'] = start_timestamp_iso
end_timestamp = model.end_timestamp
if end_timestamp:
end_timestamp_iso = isotime.format(end_timestamp, offset=False)
doc['end_timestamp'] = end_timestamp_iso
doc['elapsed_seconds'] = (end_timestamp - start_timestamp).total_seconds()
for entry in doc.get('log', []):
entry['timestamp'] = isotime.format(entry['timestamp'], offset=False)
attrs = {attr: value for attr, value in six.iteritems(doc) if value}
return cls(**attrs)
示例13: test_get_marker_from_db
def test_get_marker_from_db(self):
marker_dt = date_utils.get_datetime_utc_now() - datetime.timedelta(minutes=5)
marker_db = DumperMarkerDB(marker=isotime.format(marker_dt, offset=False),
updated_at=date_utils.get_datetime_utc_now())
DumperMarker.add_or_update(marker_db)
exec_exporter = ExecutionsExporter(None, None)
export_marker = exec_exporter._get_export_marker_from_db()
self.assertEqual(export_marker, date_utils.add_utc_tz(marker_dt))
示例14: test_schedule
def test_schedule(self):
context = {'user': USERNAME}
parameters = {'hosts': 'localhost', 'cmd': 'uname -a'}
request = ActionExecutionDB(action=ACTION_REF, context=context, parameters=parameters)
request = action_service.schedule(request)
execution = ActionExecution.get_by_id(str(request.id))
self.assertIsNotNone(execution)
self.assertEqual(execution.id, request.id)
action = '.'.join([self.actiondb.pack, self.actiondb.name])
actual_action = execution.action
self.assertEqual(actual_action, action)
self.assertEqual(execution.context['user'], request.context['user'])
self.assertDictEqual(execution.parameters, request.parameters)
self.assertEqual(execution.status, ACTIONEXEC_STATUS_SCHEDULED)
# mongoengine DateTimeField stores datetime only up to milliseconds
self.assertEqual(isotime.format(execution.start_timestamp, usec=False),
isotime.format(request.start_timestamp, usec=False))
示例15: issue_to_dict
def issue_to_dict(issue):
result = {}
author = user_to_dict(issue.user)
assignee = user_to_dict(issue.assignee)
closed_by = user_to_dict(issue.closed_by)
if issue.pull_request:
is_pull_request = True
else:
is_pull_request = False
result['id'] = issue.id
result['repository'] = issue.repository.name
result['author'] = author
result['assign'] = assignee
result['title'] = issue.title
result['body'] = issue.body
result['url'] = issue.html_url
result['state'] = issue.state
result['is_pull_request'] = is_pull_request
if issue.labels:
labels = [label_to_dict(label) for label in issue.labels]
else:
labels = []
result['labels'] = labels
# Note: We convert it to a serialize type (string)
if issue.created_at:
created_at = isotime.format(issue.created_at)
else:
created_at = None
if issue.closed_at:
closed_at = isotime.format(issue.closed_at)
else:
closed_at = None
result['created_at'] = created_at
result['closed_at'] = closed_at
result['closed_by'] = closed_by
return result