import atexit
import threading, time
import pickle
import requests
from getchar import getch
import models
import logging
import yaml
from optparse import OptionParser

DEFAULT_RATE = 2

# Sets up logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())

config_file = 'config.yml'

config = yaml.load(open(config_file, 'r'))  
sites = models.get_sites(config['Sites'])

if 'refresh_rate' in config.keys():
  repeat_rate = int(config['refresh_rate'])
else:
  repeat_rate = DEFAULT_RATE # How often will a site be 'pinged'

logger.info("Site refresh rate set to {0}".format(repeat_rate))

def input_thread(stop_flag):
    print("Press a key to quit...")
    input()
    stop_flag.append(None)
    return

def check_site(site):
    ''' This function pings the site['url'] and attempts
        to find site['content']. It will run every x secs
        in a thread '''

    logger.debug("Checking site: {0}".format(site))
    r = requests.get(site.url)
    logger.info("Site {0} returned code {1}".format(site.url, r.status_code))
    # add response time to time-series
    site.add_time(r.elapsed.total_seconds())
    
    logger.debug(r.elapsed.total_seconds())
    logger.debug(r.headers['content-type'])
    logger.debug(r.encoding)
    logger.debug(r.url)
    logger.info('Found string in site {0}: {1}'.format(site.url, site.content_str in r.text))
    t = threading.Timer(repeat_rate, check_site, [site])
    # set the daemon flag to exit the application, once the main
    # process stops
    t.daemon=True
    t.start()

def dump_sites():
    global sites
    models.pickle_to_file(sites)
    return

def main():
    atexit.register(dump_sites)
    #stop_flag = []
    #thread = threading.Thread(target=input_thread, args=(stop_flag,))
    #thread.start()
    #print("Processing {0} sites".format(len(sites['Sites'])))
    global repeat_rate
    global sites

    for site in sites:
        threading.Thread(target=check_site, args=(site,), daemon=True).start()

    input()

if __name__ == '__main__':

  parser = OptionParser()
  parser.add_option("-l", "--loglevel", dest="loglevel", default="INFO",
                    help="Set logging level")
  parser.add_option("-r", "--refresh_rate", dest="rate", default=2,
                    help="Set ping refresh rate")

  (options, args) = parser.parse_args()  
  logger.setLevel(getattr(logging, options.loglevel))
  main()

import atexit
import threading, time
import pickle
import requests
from getchar import getch
import models
import logging
import yaml
from optparse import OptionParser

DEFAULT_RATE = 2

# Sets up logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())

config_file = 'config.yml'

def input_thread(stop_flag):
    print("Press a key to quit...")
    input()
    stop_flag.append(None)
    return

def check_site(site):
    ''' This function pings the site['url'] and attempts
        to find site['content']. It will run every x secs
        in a thread '''

    logger.debug("Checking site: {0}".format(site))
    r = requests.get(site.url)
    logger.info("Site {0} returned code {1}".format(site.url, r.status_code))
    # add response time to time-series
    site.add_time(r.elapsed.total_seconds())
    
    logger.debug(r.elapsed.total_seconds())
    logger.debug(r.headers['content-type'])
    logger.debug(r.encoding)
    logger.debug(r.url)
    logger.info('Found string in site {0}: {1}'.format(site.url, site.content_str in r.text))
    t = threading.Timer(repeat_rate, check_site, [site])
    # set the daemon flag to exit the application, once the main
    # process stops
    t.daemon=True
    t.start()

def dump_sites():
    global sites
    models.pickle_to_file(sites)
    return

def main(repeat_rate, sites):
    atexit.register(dump_sites)
    #stop_flag = []
    #thread = threading.Thread(target=input_thread, args=(stop_flag,))
    #thread.start()
    #print("Processing {0} sites".format(len(sites['Sites'])))

    for site in sites:
        threading.Thread(target=check_site, args=(site,), daemon=True).start()

    input()

if __name__ == '__main__':

  parser = OptionParser()
  parser.add_option("-l", "--loglevel", dest="loglevel", default="INFO",
                    help="Set logging level")
  parser.add_option("-r", "--refresh_rate", dest="rate",
                    help="Set ping refresh rate")

  (options, args) = parser.parse_args()  
  config = yaml.load(open(config_file, 'r'))  
  sites = models.get_sites(config['Sites'])
  
  logger.setLevel(getattr(logging, options.loglevel))
  
  if options.rate:
    repeat_rate = int(options.rate)
  elif 'refresh_rate' in config.keys():
    repeat_rate = int(config['refresh_rate'])
  else:
    repeat_rate = DEFAULT_RATE # How often will a site be 'pinged'
  
  logger.info("Site refresh rate set to {0}".format(repeat_rate))
  main(repeat_rate, sites)

remove code executing from module and global vars, add logic to use refresh rate from command-line if specified