<s> import os <EOL> import sys <EOL> import platform <EOL> import logging <EOL> import time <EOL> import threading <EOL> import ConfigParser <EOL> from optparse import OptionParser <EOL> from plex_scrobble . lastfm import LastFm <EOL> from plex_scrobble . plex_monitor import monitor_log <EOL> from plex_scrobble . scrobble_cache import ScrobbleCache <EOL> from plex_scrobble . pre_check import PLSSanity <EOL> def platform_log_directory ( ) : <EOL> """<STR_LIT>""" <EOL> LOG_DEFAULTS = { <EOL> '<STR_LIT>' : os . path . expanduser ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : os . path . join ( os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> return LOG_DEFAULTS [ platform . system ( ) ] <EOL> def cache_retry ( config ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> logger . info ( '<STR_LIT>' ) <EOL> cache = ScrobbleCache ( config ) <EOL> if cache . length ( ) > <NUM_LIT:0> : <EOL> cache . retry_queue ( ) <EOL> cache . close ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> def main ( config ) : <EOL> """<STR_LIT>""" <EOL> logger . info ( '<STR_LIT>' ) <EOL> log_watch = threading . Thread ( target = monitor_log , args = ( config , ) ) <EOL> log_watch . daemon = True <EOL> log_watch . start ( ) <EOL> cache_thread = threading . Thread ( target = cache_retry , args = ( config , ) ) <EOL> cache_thread . daemon = True <EOL> cache_thread . start ( ) <EOL> log_watch . join ( ) <EOL> cache_thread . join ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> p = OptionParser ( ) <EOL> p . add_option ( '<STR_LIT:-c>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = '<STR_LIT>' ) <EOL> p . set_defaults ( config_file = os . path . expanduser ( <EOL> '<STR_LIT>' ) ) <EOL> ( options , args ) = p . parse_args ( ) <EOL> if not os . path . exists ( options . config_file ) : <EOL> print '<STR_LIT>' . format ( <EOL> options . config_file ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> config = ConfigParser . ConfigParser ( defaults = { <EOL> '<STR_LIT>' : options . config_file , <EOL> '<STR_LIT>' : os . path . expanduser ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : platform_log_directory ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> try : <EOL> config . read ( options . config_file ) <EOL> except ConfigParser . Error : <EOL> print '<STR_LIT>' . format ( <EOL> file = options . config_file ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> FORMAT = '<STR_LIT>' <EOL> logging . basicConfig ( filename = config . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , format = FORMAT , level = logging . DEBUG ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> for key in config . items ( '<STR_LIT>' ) : <EOL> logger . debug ( '<STR_LIT>' . format ( key [ <NUM_LIT:0> ] , key [ <NUM_LIT:1> ] ) ) <EOL> if options . precheck : <EOL> pc = PLSSanity ( config ) <EOL> pc . run ( ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if ( not os . path . exists ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) or <EOL> options . authenticate ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> last_fm = LastFm ( config ) <EOL> last_fm . last_fm_auth ( ) <EOL> print '<STR_LIT>' <EOL> logger . warn ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> logger . debug ( '<STR_LIT>' . format ( <EOL> key = config . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> mtime = time . ctime ( os . path . getmtime ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) ) ) <EOL> m = main ( config ) </s>
