<s> from __future__ import ( unicode_literals , division , absolute_import , print_function ) <EOL> import re <EOL> from imaplib import IMAP4_SSL_PORT , IMAP4_SSL , IMAP4 <EOL> from collections import namedtuple <EOL> from powerline . lib . threaded import KwThreadedSegment <EOL> from powerline . segments import with_docstring <EOL> _IMAPKey = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class EmailIMAPSegment ( KwThreadedSegment ) : <EOL> interval = <NUM_LIT> <EOL> @ staticmethod <EOL> def key ( username , password , server = '<STR_LIT>' , port = IMAP4_SSL_PORT , folder = '<STR_LIT>' , use_ssl = None , ** kwargs ) : <EOL> if use_ssl is None : <EOL> use_ssl = ( port == IMAP4_SSL_PORT ) <EOL> return _IMAPKey ( username , password , server , port , folder , use_ssl ) <EOL> def compute_state ( self , key ) : <EOL> if not key . username or not key . password : <EOL> self . warn ( '<STR_LIT>' ) <EOL> return None <EOL> if key . use_ssl : <EOL> mail = IMAP4_SSL ( key . server , key . port ) <EOL> else : <EOL> mail = IMAP4 ( key . server , key . port ) <EOL> mail . login ( key . username , key . password ) <EOL> rc , message = mail . status ( key . folder , '<STR_LIT>' ) <EOL> unread_str = message [ <NUM_LIT:0> ] . decode ( '<STR_LIT:utf-8>' ) <EOL> unread_count = int ( re . search ( '<STR_LIT>' , unread_str ) . group ( <NUM_LIT:1> ) ) <EOL> return unread_count <EOL> @ staticmethod <EOL> def render_one ( unread_count , max_msgs = None , ** kwargs ) : <EOL> if not unread_count : <EOL> return None <EOL> elif type ( unread_count ) != int or not max_msgs : <EOL> return [ { <EOL> '<STR_LIT>' : str ( unread_count ) , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } ] <EOL> else : <EOL> return [ { <EOL> '<STR_LIT>' : str ( unread_count ) , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : min ( unread_count * <NUM_LIT> / max_msgs , <NUM_LIT:100> ) , <EOL> } ] <EOL> email_imap_alert = with_docstring ( EmailIMAPSegment ( ) , <EOL> ( '''<STR_LIT>''' ) . format ( ssl_port = IMAP4_SSL_PORT ) ) </s>
