def set_periodic_task(self): schedule = self.schedule if schedule: begin = self.schedule_begin end = self.schedule_end schedule_map = { 'task_interval': 'interval', 'task_crontab': 'crontab', 'task_datetime': 'clocked' } options = self.options.export() options['_user'] = self.active_user.name task = { schedule_map[schedule.facade.name]: schedule, 'task': 'zimagi.command.exec', 'user': self.active_user, 'args': dump_json([self.get_full_name()]), 'kwargs': dump_json(options) } if begin: task['start_time'] = begin if end: task['expires'] = end self._scheduled_task.store(self.get_schedule_name(), **task) self.success( "Task '{}' has been scheduled to execute periodically".format( self.get_full_name())) return True return False
def get_replacements(info, replacements, keys=None): if keys is None: keys = [] tag = ".".join(keys) if keys else 'value' if isinstance(info, dict): replacements["<<{}>>".format(tag)] = info replacements["<<>{}>>".format(tag)] = dump_json(info) for key, value in info.items(): get_replacements(value, replacements, keys + [str(key)]) elif isinstance(info, (list, tuple)): replacements["<<{}>>".format(tag)] = info replacements["<<>{}>>".format(tag)] = dump_json(info) for index, value in enumerate(info): get_replacements(value, replacements, keys + [str(index)]) else: replacements["<<{}>>".format(tag)] = info return replacements
def parse(self, value, config): if re.search(self.reference_pattern, value): value = self.parse_reference(value) else: for ref_match in re.finditer(self.reference_value_pattern, value): reference_value = self.parse_reference("&{}".format( ref_match.group(1))) if isinstance(reference_value, (list, tuple)): reference_value = ",".join(reference_value) elif isinstance(reference_value, dict): reference_value = dump_json(reference_value) if reference_value is not None: value = value.replace(ref_match.group(0), reference_value) else: value = None return value
def parse(self, value, config): if not isinstance(value, str) or '#' not in value: return value standalone_function = re.search(self.function_pattern, value) if standalone_function and len(standalone_function.group(0)) == len(value): value = self.exec_function(value, config) else: for ref_match in re.finditer(self.function_value_pattern, value): function_value = self.exec_function("#{}".format(ref_match.group(1)), config) if isinstance(function_value, (list, tuple)): function_value = ",".join(function_value) elif isinstance(function_value, dict): function_value = dump_json(function_value) if function_value: value = value.replace(ref_match.group(0), str(function_value)).strip() return value
def parse(self, value, config): if not isinstance(value, str) or '$' not in value: return value if re.search(self.variable_pattern, value): value = self.parse_variable(value, config) else: for ref_match in re.finditer(self.variable_value_pattern, value): variable_value = self.parse_variable( "${}".format(ref_match.group(1)), config) if isinstance(variable_value, (list, tuple)): variable_value = ",".join(variable_value) elif isinstance(variable_value, dict): variable_value = dump_json(variable_value) if variable_value: value = value.replace(ref_match.group(0), str(variable_value)).strip() return value
def substitute_config(config, replacements): if isinstance(config, dict): config = copy.deepcopy(config) for key in list(config.keys()): real_key = substitute_config(key, replacements) real_value = substitute_config(config[key], replacements) if isinstance(real_key, (dict, list, tuple)) or real_key != key: config.pop(key, None) if isinstance(real_key, dict): for sub_key, sub_value in real_key.items(): config[ sub_key] = sub_value if sub_value is not None else real_value elif isinstance(real_key, (list, tuple)): for sub_key in real_key: config[sub_key] = real_value else: config[real_key] = real_value elif isinstance(config, (list, tuple)): config = copy.deepcopy(config) for index, value in enumerate(config): config[index] = substitute_config(value, replacements) else: for token in replacements.keys(): if str(config) == token: config = replacements[token] else: replacement = replacements[token] if isinstance(replacements[token], (list, tuple, dict)): replacement = dump_json(replacements[token]) if isinstance(config, str): config = config.replace(token, str(replacement)) if isinstance(config, str) and re.match( r'^\<\<.*\>\>$', config): config = None return config
def encode(self, document, **options): indent = options.get('indent') if indent: kwargs = { 'ensure_ascii': False, 'indent': 4, 'separators': (',', ': ') } else: kwargs = { 'ensure_ascii': False, 'indent': None, 'separators': (',', ':') } data_str = dump_json(self._convert_to_data(document), **kwargs) if isinstance(data_str, str): return data_str.encode('utf-8') return data_str
def exec(self): if not self.check_channel_permission(): self.error( "You do not have permission to access the {} channel".format( self.communication_channel)) connection = self.manager.task_connection() if connection: data = { 'user': self.active_user.name, 'time': Time().now_string, 'message': normalize_value(self.communication_message, parse_json=True) } connection.publish( channel_communication_key(self.communication_channel), dump_json(data, indent=2)) self.success("Message sent to channel {}: {}".format( self.communication_channel, self.communication_message))
def to_package(self): json_text = self.to_json() cipher_text = self.cipher.encrypt(json_text).decode( self.cipher.field_decoder) package = dump_json({'package': cipher_text}) + "\n" return package
def to_json(self): return dump_json(self.render())
def to_representation(self, value): return dump_json(value)
def render(self, data, media_type=None, renderer_context=None): indent = int(renderer_context.get('indent', 2)) return dump_json(data, cls=SafeJSONEncoder, indent=indent).encode('utf-8')
def publish_task_message(self, key, data): if self.task_connection(): self._task_connection.publish(channel_message_key(key), dump_json(data))
def _save_service(self, name, id, data=None): if not data: data = {} data['id'] = id save_file(self._service_file(name), dump_json(data, indent=2))