<s> """<STR_LIT>""" <EOL> import copy <EOL> import json <EOL> import logging <EOL> import re <EOL> import sys <EOL> from wsgiref import simple_server <EOL> import six <EOL> from webob . dec import wsgify <EOL> from webob . exc import status_map <EOL> import imap_cli <EOL> from imap_cli import config <EOL> from imap_cli import const <EOL> from imap_cli import fetch <EOL> from imap_cli import search <EOL> conf = config . new_context_from_file ( section = '<STR_LIT>' ) <EOL> imap_account = None <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> routes = [ ] <EOL> @ wsgify <EOL> def read_controller ( req ) : <EOL> params = req . params <EOL> inputs = { <EOL> '<STR_LIT>' : params . get ( '<STR_LIT>' ) or const . DEFAULT_DIRECTORY , <EOL> '<STR_LIT>' : req . urlvars . get ( '<STR_LIT>' ) , <EOL> } <EOL> if inputs [ '<STR_LIT>' ] is None : <EOL> return '<STR_LIT>' <EOL> imap_cli . change_dir ( imap_account , <EOL> inputs [ '<STR_LIT>' ] or const . DEFAULT_DIRECTORY ) <EOL> fetched_mail = fetch . read ( imap_account , inputs [ '<STR_LIT>' ] ) <EOL> if fetched_mail is None : <EOL> return '<STR_LIT>' <EOL> return_json = copy . deepcopy ( fetched_mail ) <EOL> for part in return_json [ '<STR_LIT>' ] : <EOL> if not part [ '<STR_LIT>' ] . startswith ( '<STR_LIT:text>' ) : <EOL> del part [ '<STR_LIT:data>' ] <EOL> return json . dumps ( return_json , indent = <NUM_LIT:2> ) <EOL> @ wsgify <EOL> def search_controller ( req ) : <EOL> params = req . params <EOL> inputs = { <EOL> '<STR_LIT>' : params . get ( '<STR_LIT>' ) or const . DEFAULT_DIRECTORY , <EOL> '<STR_LIT>' : params . getall ( '<STR_LIT>' ) or None , <EOL> '<STR_LIT:text>' : params . get ( '<STR_LIT:text>' ) or None , <EOL> } <EOL> imap_cli . change_dir ( imap_account , inputs [ '<STR_LIT>' ] ) <EOL> search_criterion = search . create_search_criterion ( tags = inputs [ '<STR_LIT>' ] , <EOL> text = inputs [ '<STR_LIT:text>' ] ) <EOL> mail_set = search . fetch_uids ( imap_account , <EOL> search_criterion = search_criterion or [ ] ) <EOL> mails_info = list ( <EOL> search . fetch_mails_info ( imap_account , mail_set = mail_set ) <EOL> ) <EOL> return json . dumps ( mails_info , indent = <NUM_LIT:2> ) <EOL> @ wsgify <EOL> def status_controller ( req ) : <EOL> return json . dumps ( sorted ( imap_cli . status ( imap_account ) , <EOL> key = lambda obj : obj [ '<STR_LIT>' ] ) , indent = <NUM_LIT:2> ) <EOL> routings = [ <EOL> ( '<STR_LIT:GET>' , '<STR_LIT>' , status_controller ) , <EOL> ( '<STR_LIT:GET>' , '<STR_LIT>' , search_controller ) , <EOL> ( '<STR_LIT:GET>' , '<STR_LIT>' , search_controller ) , <EOL> ( '<STR_LIT:GET>' , '<STR_LIT>' , read_controller ) , <EOL> ] <EOL> @ wsgify <EOL> def router ( req ) : <EOL> """<STR_LIT>""" <EOL> split_path_info = req . path_info . split ( '<STR_LIT:/>' ) <EOL> assert not split_path_info [ <NUM_LIT:0> ] , split_path_info <EOL> for methods , regex , app , vars in routes : <EOL> if methods is None or req . method in methods : <EOL> match = regex . match ( req . path_info ) <EOL> if match is not None : <EOL> if getattr ( req , '<STR_LIT>' , None ) is None : <EOL> req . urlvars = { } <EOL> req . urlvars . update ( dict ( <EOL> ( name , <EOL> value . decode ( '<STR_LIT:utf-8>' ) if value is not None else None ) <EOL> for name , value in match . groupdict ( ) . iteritems ( ) <EOL> ) ) <EOL> req . urlvars . update ( vars ) <EOL> req . script_name += req . path_info [ : match . end ( ) ] <EOL> req . path_info = req . path_info [ match . end ( ) : ] <EOL> return req . get_response ( app ) <EOL> return status_map [ <NUM_LIT> ] ( ) <EOL> def main ( ) : <EOL> global imap_account <EOL> global routes <EOL> if conf is None : <EOL> return <NUM_LIT:1> <EOL> logging . basicConfig ( level = logging . INFO , stream = sys . stdout ) <EOL> for routing in routings : <EOL> methods , regex , app = routing [ : <NUM_LIT:3> ] <EOL> if isinstance ( methods , six . string_types ) : <EOL> methods = ( methods , ) <EOL> vars = routing [ <NUM_LIT:3> ] if len ( routing ) >= <NUM_LIT:4> else { } <EOL> routes . append ( ( methods , re . compile ( regex ) , app , vars ) ) <EOL> log . info ( '<STR_LIT>' . format ( regex [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) ) <EOL> try : <EOL> imap_account = imap_cli . connect ( ** conf ) <EOL> httpd = simple_server . make_server ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> , router ) <EOL> log . info ( '<STR_LIT>' ) <EOL> httpd . serve_forever ( ) <EOL> except KeyboardInterrupt : <EOL> log . info ( '<STR_LIT>' ) <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
