<s> import re <EOL> import syslog <EOL> import urllib <EOL> import urllib2 <EOL> import hashlib <EOL> import time <EOL> import datetime <EOL> import hmac <EOL> import base64 <EOL> from logging import Handler <EOL> from logging . handlers import SysLogHandler <EOL> def generate_api_key ( length = <NUM_LIT:10> ) : <EOL> return re . sub ( '<STR_LIT>' , '<STR_LIT>' , hashlib . sha1 ( str ( time . time ( ) ) ) . digest ( ) . encode ( '<STR_LIT>' ) . strip ( ) ) [ : length ] <EOL> def generate_demo_client_api_key ( secret , day = None ) : <EOL> if day is None : <EOL> day = datetime . date . today ( ) <EOL> key = '<STR_LIT>' . format ( secret ) <EOL> message = '<STR_LIT>' . format ( day . toordinal ( ) ) <EOL> api_key = list ( base64 . urlsafe_b64encode ( hmac . new ( key , message ) . digest ( ) [ : <NUM_LIT:8> ] ) . rstrip ( '<STR_LIT:=>' ) ) <EOL> api_key [ - <NUM_LIT:2> ] = '<STR_LIT:A>' <EOL> return '<STR_LIT>' . join ( api_key ) <EOL> def check_demo_client_api_key ( secret , api_key , max_age = <NUM_LIT:7> ) : <EOL> if len ( api_key ) == <NUM_LIT:11> and api_key [ - <NUM_LIT:2> ] == '<STR_LIT:A>' : <EOL> day = datetime . date . today ( ) <EOL> for i in range ( max_age ) : <EOL> if api_key == generate_demo_client_api_key ( secret , day ) : <EOL> return True <EOL> day += datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> return False <EOL> def is_uuid ( s ) : <EOL> """<STR_LIT>""" <EOL> return bool ( re . match ( r'<STR_LIT>' , s ) ) <EOL> def is_int ( s ) : <EOL> try : <EOL> int ( s ) <EOL> except ValueError : <EOL> return False <EOL> return True <EOL> def is_foreignid ( s ) : <EOL> return bool ( re . match ( r'<STR_LIT>' , s ) ) <EOL> def singular ( plural ) : <EOL> """<STR_LIT>""" <EOL> if plural . endswith ( '<STR_LIT>' ) : <EOL> return plural [ : - <NUM_LIT:3> ] + '<STR_LIT:y>' <EOL> if plural . endswith ( '<STR_LIT:s>' ) : <EOL> return plural [ : - <NUM_LIT:1> ] <EOL> raise ValueError ( '<STR_LIT>' % ( plural , ) ) <EOL> def provider ( value ) : <EOL> """<STR_LIT>""" <EOL> def func ( ) : <EOL> return value <EOL> return func <EOL> class LocalSysLogHandler ( Handler ) : <EOL> """<STR_LIT>""" <EOL> facility_names = { <EOL> "<STR_LIT>" : syslog . LOG_AUTH , <EOL> "<STR_LIT>" : syslog . LOG_CRON , <EOL> "<STR_LIT>" : syslog . LOG_DAEMON , <EOL> "<STR_LIT>" : syslog . LOG_KERN , <EOL> "<STR_LIT>" : syslog . LOG_LPR , <EOL> "<STR_LIT>" : syslog . LOG_MAIL , <EOL> "<STR_LIT>" : syslog . LOG_NEWS , <EOL> "<STR_LIT>" : syslog . LOG_SYSLOG , <EOL> "<STR_LIT:user>" : syslog . LOG_USER , <EOL> "<STR_LIT>" : syslog . LOG_UUCP , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL0 , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL1 , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL2 , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL3 , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL4 , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL5 , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL6 , <EOL> "<STR_LIT>" : syslog . LOG_LOCAL7 , <EOL> } <EOL> priority_map = { <EOL> "<STR_LIT>" : syslog . LOG_DEBUG , <EOL> "<STR_LIT>" : syslog . LOG_INFO , <EOL> "<STR_LIT>" : syslog . LOG_WARNING , <EOL> "<STR_LIT>" : syslog . LOG_ERR , <EOL> "<STR_LIT>" : syslog . LOG_CRIT <EOL> } <EOL> def __init__ ( self , ident = None , facility = syslog . LOG_USER , log_pid = False ) : <EOL> Handler . __init__ ( self ) <EOL> self . facility = facility <EOL> if isinstance ( facility , basestring ) : <EOL> self . facility = self . facility_names [ facility ] <EOL> options = <NUM_LIT:0> <EOL> if log_pid : <EOL> options |= syslog . LOG_PID <EOL> syslog . openlog ( ident , options , self . facility ) <EOL> self . formatter = None <EOL> def close ( self ) : <EOL> Handler . close ( self ) <EOL> syslog . closelog ( ) <EOL> def emit ( self , record ) : <EOL> try : <EOL> msg = self . format ( record ) <EOL> if isinstance ( msg , unicode ) : <EOL> msg = msg . encode ( '<STR_LIT:utf-8>' ) <EOL> priority = self . priority_map [ record . levelname ] <EOL> for m in msg . splitlines ( ) : <EOL> syslog . syslog ( self . facility | priority , m ) <EOL> except StandardError : <EOL> self . handleError ( record ) <EOL> def call_internal_api ( config , func , ** kwargs ) : <EOL> url = config . cluster . base_master_url . rstrip ( '<STR_LIT:/>' ) + '<STR_LIT>' + func <EOL> data = dict ( kwargs ) <EOL> data [ '<STR_LIT>' ] = config . cluster . secret <EOL> urllib2 . urlopen ( url , urllib . urlencode ( data ) ) </s>
