def read(filename, encoding="utf-8", errors="replace", encode=None): """ Returns file data. If the filename is invalid, None is returned. """ try: data = codecs.open(filename, "r", encoding, errors).read() except IOError: d.log_exc() return None if encode: return data.encode(encode) else: return data
def check( user_ip=None, user_agent_id=None, user_id=None, comment_type=None, comment_content=None, ): """ Submits a piece of content to Akismet to check if the content is considered as spam. :param user_ip: The IP address of the submitter of the content. Required. :param user_agent_id: The user agent ID of the UA that submitted the content; see `login.get_user_agent_id`. Required. :param user_id: If provided, submits the user's login_name and email address to the Akismet backend as an additional set of data points, which can increase performance. Optional. :param comment_type: A string that describes the content being sent. Optional. :param comment_content: The submitted content. :return: akismet.SpamStatus object (Ham, Unknown, ProbableSpam, DefiniteSpam). If the type is DefiniteSpam, the submission can be immediately dropped as it is blatant spam. If spam filtering is disabled, SpamStatus.Ham is always returned. """ if user_id: login_name, email = d.engine.execute(""" SELECT login_name, email FROM login WHERE userid = %(id)s """, id=user_id).first() else: login_name = email = None payload = { "user_ip": user_ip, "user_agent": _get_user_agent_from_id(id=user_agent_id), "comment_author": login_name, "comment_author_email": email, "comment_type": comment_type, "comment_content": comment_content, "is_test": _IS_ENVIRONMENT_TESTING, } if FILTERING_ENABLED: try: return _akismet.check(**payload) except RequestsConnectionError: # Don't fail just because of a connection issue to the Akismet backend; but log that we failed. d.log_exc(level=logging.WARNING) return SpamStatus.Ham else: return SpamStatus.Ham
def from_string(filedata): try: return Image.from_buffer(filedata) except SanperaError: log_exc(level=logging.DEBUG) raise WeasylError('imageDecodeError')
def read(filename): try: return Image.read(filename) except SanperaError: log_exc(level=logging.DEBUG) raise WeasylError('imageDecodeError')