def try_to_send_mail_notices(self, notice_key, **get_notice_data_kwargs): if not g.n6_mail_notices_api.is_active(notice_key): msg = ('No e-mail notices sent as they are not configured ' 'for notice_key={!a}.'.format(ascii_str(notice_key))) flash(msg, 'warning') return try: notice_data = self.get_notice_data(**get_notice_data_kwargs) notice_recipients = list(self.get_notice_recipients(notice_data)) if not notice_recipients: raise NoRecipients('no matching non-blocked user(s) could be found') except NoRecipients as exc: flash(f'No e-mail notices could be sent because {exc}!', 'error') return notice_lang = self.get_notice_lang(notice_data) assert notice_lang is None or isinstance(notice_lang, str) and len(notice_lang) == 2 gathered_ok_recipients = [] with g.n6_mail_notices_api.dispatcher(notice_key, suppress_and_log_smtp_exc=True) as dispatch: for email in notice_recipients: ok_recipients, _ = dispatch(email, notice_data, notice_lang) if ok_recipients: gathered_ok_recipients.extend(ok_recipients) else: msg = 'Failed to send an e-mail notice to {}!'.format(ascii_str(email)) flash(msg, 'warning') if gathered_ok_recipients: recipients_str = ', '.join(map(ascii_str, gathered_ok_recipients)) flash('E-mail notices sent to: {}.'.format(recipients_str)) else: flash('No e-mail notices could be sent!', 'error')
def serialize_record(record): if not _should_publish(record): return DoNotPublish record_attrs = record_attrs_proto.copy() record_attrs.update(( limit_string(ascii_str(key), char_limit=record_key_max_length), value) for key, value in vars(record).items()) record_attrs['message'] = record.getMessage() record_attrs['asctime'] = formatter.formatTime(record) exc_info = record_attrs.pop('exc_info', None) if exc_info: if not record_attrs['exc_text']: record_attrs['exc_text'] = formatter.formatException( exc_info) record_attrs['exc_type_repr'] = repr(exc_info[0]) record_attrs['exc_ascii_str'] = ascii_str(exc_info[1]) stack_items = record_attrs.pop('formatted_call_stack_items', None) if stack_items: del stack_items[ -1] # this item is from this AMQPHandler.emit() while stack_items and match_useless_stack_item_regex( stack_items[-1]): del stack_items[-1] record_attrs['formatted_call_stack'] = ''.join(stack_items) return json_encode(record_attrs)
def _after_status_transition_to_other(self, org_request, old_status, target_status, concerned_org_id): assert isinstance(org_request, models.RegistrationRequest) flash('Status of the registration request changed from ' '"{}" to "{}".'.format(old_status, target_status)) LOGGER.info('Successfully changed status of %a - from %a to %a', org_request, ascii_str(old_status), ascii_str(target_status))
def list_ca(manage_api, arguments): print 'CA label | CA profile | CA subject key identifier | CA authority key identifier' for ca in manage_api.iter_all_ca_data(): profile = ca.profile or '-' subject_key_identifier = ascii_str(ca.subject_key_identifier or '-').strip() authority_key_identifier = ascii_str(ca.authority_key_identifier or '-').strip() print '{} | {} | {} | {}'.format(ca.ca_label, profile, subject_key_identifier, authority_key_identifier)
def should_try_to_verify_client(self): if self.broker_username in self.EXPLICITLY_ILLEGAL_USERNAMES: LOGGER.error("The '%a' username is explicitly considered illegal!", ascii_str(self.broker_username)) return False if self.password is not None: LOGGER.error( "Authentication by password is not supported - cannot authenticate '%a'!", ascii_str(self.broker_username)) return False return super().should_try_to_verify_client()
def _after_status_transition_to_accepted(self, org_request, old_status, target_status, concerned_org_id): assert isinstance(org_request, models.RegistrationRequest) flash('Registration request accepted. Organization ' '"{}" created.'.format(concerned_org_id)) LOGGER.info( 'Successfully changed status of %a - from %a to %a. ' 'Successfully added organization %a.', org_request, ascii_str(old_status), ascii_str(target_status), ascii_str(concerned_org_id)) self.try_to_send_mail_notices(notice_key='new_org_and_user_created', user_login=org_request.email)
def _validate_value(self, value): forbidden_characters = self._get_additionally_forbidden_characters() illegal_characters = forbidden_characters.intersection(value) if illegal_characters: raise FieldValueError( public_message= '"{value}" contains illegal character(s): {chars}.'.format( value=ascii_str(value), chars=', '.join( sorted("'{}'".format(ascii_str(ch)) for ch in illegal_characters)))) super(EmailCustomizedField, self)._validate_value(value)
def from_exc(cls, exc): # type: (BaseException) -> Optional[DuplicateEntryErrorInfo] if is_specific_db_error(exc, MYSQL_ERROR_CODE_DUP_ENTRY): orig_message = _get_orig_message(exc) return cls( exc_type=type(exc), ascii_exc=ascii_str(exc), ascii_message=(ascii_str(orig_message) if orig_message else None), ascii_sql_statement=_get_sql_statement_as_ascii(exc), ascii_probable_culprit=cls._extract_probable_culprit_as_ascii( orig_message), involved_values=list(_iter_involved_values(exc))) return None
def _extract_probable_culprit_as_ascii(orig_message): if orig_message is None: return assert isinstance(orig_message, str) EXPECTED_PREFIX = 'Duplicate entry ' if orig_message.startswith(EXPECTED_PREFIX): rest = orig_message[len(EXPECTED_PREFIX):] if rest.startswith("'"): return repr(ascii_str(rest.split("'")[1])) else: rest_parts = rest.split() if rest_parts: return ascii_str(rest_parts[0]) return None
def _after_status_transition_to_other(self, org_request, old_status, target_status, concerned_org_id): assert isinstance(org_request, models.OrgConfigUpdateRequest) flash('Status of the organization config update request changed ' 'from "{}" to "{}".'.format(old_status, target_status)) LOGGER.info('Successfully changed status of %a - from %a to %a', org_request, ascii_str(old_status), ascii_str(target_status)) if target_status == _STATUS_DISCARDED: assert org_request.id is not None assert g.n6_org_config_info is not None assert g.n6_org_config_info['org_id'] == concerned_org_id self.try_to_send_mail_notices( notice_key='org_config_update_rejected', req_id=org_request.id)
def _after_status_transition_to_accepted(self, org_request, old_status, target_status, concerned_org_id): assert isinstance(org_request, models.OrgConfigUpdateRequest) flash('Organization config update request accepted. ' 'Organization "{}" updated.'.format(concerned_org_id)) LOGGER.info( 'Successfully changed status of %a - from %a to %a. ' 'Successfully updated organization %a.', org_request, ascii_str(old_status), ascii_str(target_status), ascii_str(concerned_org_id)) assert org_request.id is not None assert g.n6_org_config_info is not None assert g.n6_org_config_info['org_id'] == concerned_org_id self.try_to_send_mail_notices(notice_key='org_config_update_applied', req_id=org_request.id)
def _time_object_from_string(cls, value): try: return datetime.datetime.strptime(value, cls.time_format).time() except (TypeError, ValueError): raise FieldValueError( public_message='"{}" is not a valid *hour:minute* time ' 'specification'.format(ascii_str(value)))
def _get_sql_statement_as_ascii(exc): # type: (DBAPIError) -> Optional[str] assert isinstance(exc, DBAPIError) assert hasattr(exc, 'statement') if exc.statement is not None: return ascii_str(exc.statement) return None
def main(): COMMAND_NAME_TO_FUNC = { 'list-ca': list_ca, 'add-cert': add_cert, 'make-cert': make_cert, 'revoke-cert': revoke_cert, 'dump-cert': dump_cert, 'dump-crl': dump_crl, } arguments = _parse_arguments(sys.argv) logging.basicConfig() command_func = COMMAND_NAME_TO_FUNC.get(arguments.command_name, _command_not_implemented) try: manage_api = ManageAPI() command_func(manage_api, arguments) except Exception as exc: print >> sys.stderr, 'FATAL ERROR:', ascii_str(exc) print >> sys.stderr if arguments.debug: traceback.print_exc() print >> sys.stderr print >> sys.stderr, 'Arguments:', arguments else: print >> sys.stderr, '(use --debug to see error tracebacks)' sys.exit(1)
def _format_values_as_ascii(values): # type: (Iterable) -> str assert _is_regular_iterable_collection(values) #return ', '.join(map(ascii, values)) #3: uncomment this line and remnove the next statement return ', '.join( repr(ascii_str(obj)) if isinstance(obj, basestring) else repr(obj) for obj in values)
def __log_warnings_from_database(self, caption, query_or_proc=None, args=None): conn = self.connection if conn is None or not conn.warning_count(): return for level, code, msg in conn.show_warnings(): log_msg = '[{}] {} (code: {}), {}'.format(ascii_str(level), ascii_str(msg), ascii_str(code), caption) if query_or_proc or args: log_msg_format = (log_msg.replace('%', '%%') + ': %r, ' + 'ARGS: %r') DB_WARN_LOGGER.warning(log_msg_format, query_or_proc, args) else: DB_WARN_LOGGER.warning(log_msg)
def _log_nonstandard_name( self, value, category, _already_logged=_already_logged_nonstandard_names): if (category, value) not in _already_logged: category_sublogger = NONSTANDARD_NAMES_LOGGER.getChild(category) category_sublogger.warning(ascii_str(value)) _already_logged[(category, value)] = None
def __init__( self, msg_or_underlying_exc=None, # type: Union[str, Exception, None] ): if isinstance(msg_or_underlying_exc, Exception): # (only the exception type's name as we don't want to reveal too much...) msg_or_underlying_exc = type(msg_or_underlying_exc).__name__ self._msg = ascii_str(msg_or_underlying_exc or '<unknown error>') super(AbstractJWTError, self).__init__(self._msg)
def conv_web_url(opt_value): # type: (...) -> str value = Config.BASIC_CONVERTERS['str'](opt_value) if value != ascii_str(value): raise ValueError('contains non-ASCII characters') if not value.lower().startswith(_LEGAL_WEB_URL_PREFIXES): raise ValueError('does not start with {}'.format( ' or '.join(map(repr, _LEGAL_WEB_URL_PREFIXES)))) return value
def _get_by_primary_key(self, model_class, value): try: obj = self._db_session.query(model_class).get(value) except ObjectDeletedError: obj = None if obj is None: raise AuthDatabaseAPILookupError('{} "{}" does not exist.'.format( model_class.__name__, ascii_str(value))) return obj
def run_from_commandline(cls): parser = cls.make_argument_parser() arguments = cls.parse_arguments(parser) try: script = cls(**arguments) script.run() except sqlalchemy.exc.StatementError as exc: sys.exit(ascii_str(exc)) else: script.msg('Done.')
def getter(self): api = getattr(self.request.registry, api_name) if api is None: raise RuntimeError( 'the {!a} view cannot make use of `{}` because it was ' 'not specified (or was specified as `None`) when the ' 'web app object was being configured'.format( self, ascii_str(api_name))) return api
def _pure_addr(self, addr_item): addr = addr_item.addr_spec assert isinstance(addr, str) if not EMAIL_OVERRESTRICTED_SIMPLE_REGEX.search(addr): raise ValueError( 'e-mail address {!r} does not match ' 'n6lib.common_helpers.EMAIL_OVERRESTRICTED_SIMPLE_REGEX'. format(addr)) assert addr == ascii_str(addr) return addr
def verify_required_input_items_collected(self, required_input_names): # type: (AbstractSet) -> None uncollected = required_input_names - self._collected_input_names if uncollected: raise DataConversionError( 'the following required items are missing or have ' 'been skipped as effectively NULL-like (i.e., not ' 'carrying any meaningful data): {}'.format(', '.join( sorted( repr(ascii_str(input_name)) for input_name in uncollected))))
def _format_path_item(self, path_item): # type: (Union[NameOrIndex, List[NameOrIndex]]) -> str if isinstance(path_item, (str, int)): return ascii_str(path_item) # This is a list of multiple name/index alternatives, so # let's present them in the `{foo,bar,spam}`-like form. assert (isinstance(path_item, list) and all(isinstance(alt, (str, int)) for alt in path_item) and len(path_item) > 1 ), 'bug in implementation of DataConversionError?!' return '{' + ','.join(map(ascii_str, path_item)) + '}'
def _check_x509_extensions(self): opt_location = self.ca_opt_pattern.format(X509_EXTENSIONS_OPT_NAME) if not self.parsed_config.contains(opt_location): logging.warning( "The option {!r} in OpenSSL config (section {!r}) is missing; " "the section, which is referred to by the option, is not " "required, but most likely " "should be configured".format(X509_EXTENSIONS_OPT_NAME, ascii_str(self.ca_sect_name))) else: sect_name = self.parsed_config.get_opt_value(opt_location) try: self._check_nonempty_sect_provided(sect_name) except SSLConfigEmptySectError: logging.warning( "The section {!r} in OpenSSL config, referred to by the option " "{!r}, is empty; although it is not required, it most likely " "should be configured".format( ascii_str(sect_name), X509_EXTENSIONS_OPT_NAME, ascii_str(self.ca_sect_name)))
def serialize_record(record): if not _should_publish(record): return DoNotPublish record_attrs = record_attrs_proto.copy() record_attrs.update( (limit_str(ascii_str(key), char_limit=record_key_max_length), value) for key, value in vars(record).items()) record_attrs['message'] = record.getMessage() record_attrs['asctime'] = formatter.formatTime(record) exc_info = record_attrs.pop('exc_info', None) if exc_info: if not record_attrs['exc_text']: record_attrs['exc_text'] = formatter.formatException( exc_info) record_attrs['exc_type_repr'] = ascii(exc_info[0]) record_attrs['exc_ascii_str'] = ascii_str(exc_info[1]) formatted_call_stack = _try_to_extract_formatted_call_stack( record_attrs) if formatted_call_stack: record_attrs['formatted_call_stack'] = formatted_call_stack return json_encode(record_attrs)
def unicode_adjuster(self, value): if isinstance(value, str): return value assert isinstance(value, (bytes, bytearray)) try: return value.decode('utf-8') except UnicodeDecodeError as exc: raise ValueError( "could not decode text from binary data {!a} using " "utf-8 encoding ({}); please note that it is parser's " "responsibility to ensure that input objects passed " "into unicode adjusters are either *unicode strings* " "(`str`) or *utf-8-encoded data* (as objects of type " "`bytes` or `bytearray`)".format(value, ascii_str(exc)))
def parse(self, data): rows = csv.reader(StringIO(data['raw']), delimiter=',', quotechar='"') for row in rows: # SOURCE FIELDS FORMAT: # firstseen,url,filetype,md5,sha256,signature with self.new_record_dict(data) as parsed: parsed['time'] = row[0] parsed['url'] = row[1] parsed['md5'] = row[3] parsed['sha256'] = row[4] name = ascii_str(row[5]).strip().lower() if name not in ('', 'none', 'null'): parsed['name'] = name yield parsed
def _ascii_only_ldap_safe_str_strip(val): val = _verified_as_ascii_only_str(val) val = val.strip() if val.startswith('#'): raise FieldValueError( public_message='Value {value!r} starts with illegal "#" prefix.'.format(value=val)) illegal_characters = LDAP_UNSAFE_CHARACTERS.intersection(val) if illegal_characters: raise FieldValueError( public_message='Value {value!r} contains illegal character(s): {chars}.'.format( value=val, chars=', '.join(sorted("'{}'".format(ascii_str(ch)) for ch in illegal_characters)))) return val