<s> from __future__ import print_function , absolute_import , unicode_literals , division <EOL> import argparse <EOL> import sys <EOL> import logging <EOL> import yaml <EOL> from diu . merge import merge <EOL> from diu . updater import ContainerSet , Updater <EOL> from docker import Client as DockerClient <EOL> try : <EOL> from colorlog import ColoredFormatter <EOL> except ImportError : <EOL> ColoredFormatter = None <EOL> class Application ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args = None ) : <EOL> """<STR_LIT>""" <EOL> self . logger = logging . getLogger ( self . __class__ . __name__ ) <EOL> self . parser = self . _create_parser ( ) <EOL> self . args = self . parser . parse_args ( args = args ) <EOL> if self . args . debug : <EOL> logging . getLogger ( ) . setLevel ( logging . DEBUG ) <EOL> self . containerset = [ ] <EOL> if self . args . deprecated_file is not None : <EOL> self . logger . warning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> self . _load_config ( self . args . deprecated_file ) <EOL> else : <EOL> self . _load_config ( * self . args . file ) <EOL> d = DockerClient ( ** self . config . get ( '<STR_LIT>' , { } ) ) <EOL> self . updater = Updater ( d , self . containerset ) <EOL> def _create_parser ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT:file>" , <EOL> help = "<STR_LIT>" , <EOL> nargs = "<STR_LIT:*>" , <EOL> default = ( "<STR_LIT>" , ) , <EOL> ) <EOL> return parser <EOL> def _load_config ( self , * files ) : <EOL> """<STR_LIT>""" <EOL> final_config = { '<STR_LIT>' : { } , '<STR_LIT>' : { } } <EOL> for f in files : <EOL> try : <EOL> data = yaml . safe_load ( open ( f ) ) <EOL> except ( IOError , yaml . parser . ParserError ) as e : <EOL> print ( "<STR_LIT>" . format ( f = f , e = e ) , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> final_config = merge ( final_config , data ) <EOL> except ValueError as e : <EOL> print ( <EOL> "<STR_LIT>" . format ( f = f , e = e ) , <EOL> file = sys . stderr <EOL> ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . config = final_config [ '<STR_LIT>' ] <EOL> for key , value in final_config [ '<STR_LIT>' ] . items ( ) : <EOL> try : <EOL> self . _validate_watch_configuration ( value ) <EOL> except ValueError as e : <EOL> print ( <EOL> "<STR_LIT>" . format ( f = f , e = e ) , <EOL> file = sys . stderr <EOL> ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . containerset . append ( ContainerSet ( <EOL> name = key , <EOL> images = value . get ( '<STR_LIT>' , [ ] ) , <EOL> commands = value . get ( '<STR_LIT>' , [ ] ) , <EOL> ) ) <EOL> def _validate_watch_configuration ( self , watch ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( watch , dict ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not isinstance ( watch . get ( '<STR_LIT>' , [ ] ) , list ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not isinstance ( watch . get ( '<STR_LIT>' , [ ] ) , list ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . updater . do_updates ( ) <EOL> if self . updater . error_count > <NUM_LIT:0> : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def setup_logger ( ) : <EOL> logger = logging . getLogger ( ) <EOL> logger . setLevel ( logging . INFO ) <EOL> console_handler = logging . StreamHandler ( sys . stdout ) <EOL> if sys . stdout . isatty ( ) and ColoredFormatter is not None : <EOL> formatter = ColoredFormatter ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> datefmt = "<STR_LIT>" , <EOL> reset = True , <EOL> log_colors = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> else : <EOL> formatter = logging . Formatter ( <EOL> "<STR_LIT>" , <EOL> datefmt = "<STR_LIT>" , <EOL> ) <EOL> console_handler . setFormatter ( formatter ) <EOL> logger . addHandler ( console_handler ) <EOL> def main ( ) : <EOL> setup_logger ( ) <EOL> Application ( ) . run ( ) </s>
