7.1.1.1.7. isbg.utils module

Utils for isbg - IMAP Spam Begone.

isbg.utils.detect_enc(byte_sring)

Try to detect a encoding.

It uses the cchardet and chardet modules to detect the encoding. If none of them are installed, it returns None.

Parameters:byte_string (str | bytes) – the byte string to detect.
Returns:
with at least the ‘encoding’ informed as returned by
cchardet.detect() and chardet.detect().
Return type:dict
isbg.utils.hexdigit(char)

Tanslate a hexadecimal character his decimal (int) value.

Parameters:char (str) – A hexadecimal number in base 16.
Returns:the base 10 representation of the number.
Return type:int
Raises:ValueError – if char is not a valid hexadecimal character.
isbg.utils.hexof(string)

Translate a string to a string with its hexadecimal value.

Parameters:string (str) – A string to be translated.
Returns:The translated string.
Return type:str

Examples

>>> isbg.utils.hexof('isbg')
'69736267'
isbg.utils.dehexof(string)

Tanslate a hexadecimal string to his string value.

Parameters:string (str) – A string containing a hexadecimal.
Returns:The translated string.
Return type:str
isbg.utils.get_ascii_or_value(value)

Try to convert the contents of value to ascii string.

When the value cannot be converted to an ascii string, it returns the value.

Parameters:value (dict, list, str) – The value to convert.
Returns:The value object with its contents translated if it was possible.

Note

We get the uids info as binary when using the methods of isbg.imaputils.IsbgImap4.

Examples

>>> get_ascii_or_value('isbg - IMAP Spam Begone')
'isbg - IMAP Spam Begone'
>>> d = {'isbg': (u'IMAP', [b'Spam', r'Begone'])}
>>> get_ascii_or_value(d)
{'isbg': ('IMAP', ['Spam', 'Begone'])}
isbg.utils.popen(cmd)

Create a subprocess.Popen instance.

It calls Popen(cmd, stdin=PIPE, stdout=PIPE, close_fds=True).

Parameters:cmd (str) – The command to use in the call to Popen.
Returns:The Popen object.
Return type:subprocess.Popen
isbg.utils.score_from_mail(mail)

Search the spam score from a mail as a string.

The returning format is d.d/d.d<br> and it contains the score found in the email.

Parameters:mail (str) – A email.message.Message decoded.
Returns:The score found in the mail message.
Return type:str
isbg.utils.shorten(inp, length)

Short a dict or a list a tuple or a string to a maximus length.

Parameters:
  • inp (dict, list, tuple, str) – The object to short.
  • length (int) – The length.
Returns:

the shorted object.

isbg.utils.truncate(inp, length)

Truncate a string to a maximum length.

Parameters:
  • inp (str) – The string to be shortened to his maximum length.
  • length (int) – The length.
Returns:

the shorted string.

It adds at the end if it is shortened.

Return type:

(str)

Raises:

ValueError – If length is low than 1.

class isbg.utils.BraceMessage(fmt, *args, **kwargs)

Bases: object

Comodity class to format a string.

You can call it using: py: class: ~__

Example

>> > from isbg.utils import __ >> > __(“ffoo, boo {}”.format(a))

__init__(fmt, *args, **kwargs)

Initialize the object.

fmt = None

The string to be formated.

args = None

The *args

kwargs = None

The **kwargs**

__str__()

Return the string formated.

__repr__()

Return the representation formated.

isbg.utils.__

alias of isbg.utils.BraceMessage