<commit_before>#!/usr/bin/env python

#import sys
#print('\n'.join(sys.path))

import os
import logging
from aatest.check import State, OK, ERROR
from aatest.events import EV_CONDITION, EV_PROTOCOL_RESPONSE, NoSuchEvent
from aatest.result import Result
from aatest.verify import Verify
from future.backports.urllib.parse import quote_plus
from future.backports.urllib.parse import parse_qs

from aatest.summation import store_test_state
from aatest.session import Done
#from aatest.session import SessionHandler

from saml2.httputil import BadRequest
from saml2.httputil import get_post
from saml2.httputil import Response
from saml2.httputil import ServiceError
from saml2.response import StatusError

from saml2test.idp_test.inut import WebIO
from saml2test.idp_test.setup import setup
from saml2test.idp_test.wb_tool import Tester
from saml2test.request import ServiceProviderRequestHandlerError
from saml2test.session import SessionHandler
from saml2test.checkedconfig import ConfigError

from saml2.entity import Entity
from saml2 import BINDING_HTTP_POST
from saml2 import samlp

import threading
SERVER_LOG_FOLDER = "server_log"
if not os.path.isdir(SERVER_LOG_FOLDER):
    os.makedirs(SERVER_LOG_FOLDER)

try:
    from mako.lookup import TemplateLookup
except Exception as ex:
    raise ex

LOGGER = logging.getLogger("")

def pick_args(args, kwargs):
    return dict([(k, kwargs[k]) for k in args])

def do_next(tester, resp, sh, inut, filename, path):
    tester.conv = tester.sh['conv']
    res = Result(tester.sh, tester.kwargs['profile_handler'])
    try:
        tester.handle_response(resp, {})
        # store_test_state(sh, sh['conv'].events)  this does actually nothing?
        res.store_test_info()

    except StatusError as err:
        # store event to be found in assertion test
        tester.conv.events.store(EV_PROTOCOL_RESPONSE,err)
        msg = "{}: {}".format(err.__class__.__name__, str(err))

    except ServiceProviderRequestHandlerError as err:
        msg = str(err)
        tester.conv.events.store(EV_CONDITION, State('SP Error', ERROR,  message=msg),
                                 sender='do_next')

    tester.conv.index += 1
    lix = len(tester.conv.sequence)
    while tester.conv.sequence[tester.conv.index] != Done:
        resp = tester.run_flow(tester.conv.test_id, index=tester.conv.index)
        store_test_state(sh, sh['conv'].events)
        if isinstance(resp, Response):
            res.store_test_info()
            return resp(inut.environ, inut.start_response)
        elif resp is False:
            break
        if tester.conv.index >= lix:
            break

    _done = False
    for _cond in tester.conv.events.get_data(EV_CONDITION):
        if _cond.test_id == 'Done' and _cond.status == OK:
            _done = True
            break

    if not _done:
        tester.conv.events.store(EV_CONDITION, State('Done', OK),
                                 sender='do_next')

        if 'assert' in tester.conv.flow:
            _ver = Verify(tester.chk_factory, tester.conv)
            try:
                _ver.test_sequence(tester.conv.flow["assert"])
            except NoSuchEvent as err:
                tester.conv.events.store(EV_CONDITION, State('Assertion Error', ERROR, message=msg),
                                         sender='idp_test')
            except Exception as err:
                msg = "ERROR Assertion verification had gone wrong."
                raise Exception(msg)

        store_test_state(sh, sh['conv'].events)
        res.store_test_info()

    html_page = inut.flow_list(filename)
    return html_page

class SessionStore(list):
    def append(self, element):
        key = hex(id(element['session_info']))
        for e in self:
            e_key = hex(id(e['session_info']))
            if e_key == key:
                return
        list.append(self,element)

    def get_session_by_conv_id(self,conv_id):
        for e in self:
            try:
                session_info = e['session_info']
                conv = session_info['conv']
                id = conv.id
                if id == conv_id:
                    return session_info
            except KeyError as e:
                # entries without these infos are broken (unfinished). Ignoring.
                pass
        return None

class Application(object):
    def __init__(self, webenv):
        self.webenv = webenv
        self.session_store = SessionStore()

    def _static(self, path):
        if path in ["robots.txt", 'favicon.ico']:
            return "{}/robots.txt".format(self.webenv['static'])
        else:
            for p in ['acs/site/static/', 'site/static/', 'static/', 'export/']:
                if path.startswith(p):
                    return '{}/{}'.format(self.webenv['static'], path[len(p):])
        return ''

    def application(self, environ, start_response):
        LOGGER.info("Connection from: %s" % environ["REMOTE_ADDR"])
        session = environ['beaker.session']

        path = environ.get('PATH_INFO', '').lstrip('/')
        LOGGER.info("path: %s" % path)

        try:
            sh = session['session_info']
        except KeyError:
            sh = SessionHandler(**self.webenv)
            session_id = self.id_counter.next()
            sh.session_init()
            session['session_info'] = sh

        self.session_store.append(session)

        inut = WebIO(session=sh, **self.webenv)
        inut.environ = environ
        inut.start_response = start_response

        tester = Tester(inut, sh, **self.webenv)

        _path = self._static(path)
        if _path:
            return inut.static(_path)

        if path == "" or path == "/":  # list
            return tester.display_test_list()
        elif "flow_names" not in sh:
            sh.session_init()

        if path == "logs":
            return inut.display_log("log", issuer="", profile="", testid="")
        elif path.startswith("log"):
            if path == "log" or path == "log/":
                _cc = inut.conf.CLIENT
                try:
                    _iss = _cc["srv_discovery_url"]
                except KeyError:
                    _iss = _cc["provider_info"]["issuer"]
                parts = [quote_plus(_iss)]
            else:
                parts = []
                while path != "log":
                    head, tail = os.path.split(path)
                    # tail = tail.replace(":", "%3A")
                    # if tail.endswith("%2F"):
                    #     tail = tail[:-3]
                    parts.insert(0, tail)
                    path = head

            return inut.display_log("log", *parts)
        elif path.startswith("tar"):
            path = path.replace(":", "%3A")
            return inut.static(path)

        elif path.startswith("test_info"):
            p = path.split("/")
            try:
                return inut.test_info(p[1])
            except KeyError:
                return inut.not_found()
        elif path == "continue":
            return tester.cont(environ, self.webenv)
        elif path == 'reset':
            for param in ['flow', 'flow_names', 'index', 'node', 'profile',
                          'sequence', 'test_info', 'testid', 'tests']:
                try:
                    del sh[param]
                except KeyError:
                    pass
            return tester.display_test_list()
        elif path == "opresult":
            if tester.conv is None:
                return inut.sorry_response(self.webenv['base_url'],
                                           "No result to report")

            return inut.opresult(tester.conv, sh)
        # expected path format: /<testid>[/<endpoint>]
        elif path in sh["flow_names"]:
            resp = tester.run(path, **self.webenv)
            store_test_state(sh, sh['conv'].events)
            filename = self.webenv['profile_handler'](sh).log_path(path)
            if isinstance(resp, Response):
                res = Result(sh, self.webenv['profile_handler'])
                res.store_test_info()
                res.print_info(path, tester.fname(path))
                return inut.respond(resp)
            else:
                return inut.flow_list(filename)
        elif path == "acs/post":
            qs = get_post(environ).decode('utf8')
            resp = dict([(k, v[0]) for k, v in parse_qs(qs).items()])

            try:
                test_id = sh['conv'].test_id
            except KeyError as err:
                test_id = None

            if not test_id:
                """
                In other words: we've been contacted by robobrowser and are in a different environment now, than the
                code expects us to be. .... Hopefully, trickery and recreating of the environment will lead mostly
                to more intended effects than unintended ones.

                This is unfinished business: You can add other bindings here, to expand what RB can be used to test.
                """
                try:
                    txt = resp['SAMLResponse']
                    xmlstr = Entity.unravel(txt, BINDING_HTTP_POST)
                except Exception as e:
                    msg = 'Decoding not supported in the SP'
                    raise Exception(msg)

                rsp = samlp.any_response_from_string(xmlstr)
                original_request_id = rsp.in_response_to
                requester_session = self.session_store.get_session_by_conv_id(original_request_id)

                # recreating the environment. lets hope it is somewhat reentrant resistant
                sh = requester_session
                inut = WebIO(session=sh, **self.webenv)
                inut.environ = environ
                inut.start_response = start_response

                tester = Tester(inut, sh, **self.webenv)

            profile_handler = self.webenv['profile_handler']
            _sh = profile_handler(sh)
            #filename = self.webenv['profile_handler'](sh).log_path(test_id)
            #_sh.session.update({'conv': 'foozbar'})
            filename = _sh.log_path(test_id)

            html_page = do_next(tester, resp, sh, inut, filename, path)
            return html_page
        elif path == "acs/redirect":
            qs = environ['QUERY_STRING']
            resp = dict([(k, v[0]) for k, v in parse_qs(qs).items()])
            filename = self.webenv['profile_handler'](sh).log_path(
                sh['conv'].test_id)

            return do_next(tester, resp, sh, inut, filename, path)
        elif path == "acs/artifact":
            pass
        elif path == "ecp":
            pass
        elif path == "disco":
            qs = parse_qs(environ['QUERY_STRING'])
            resp = dict([(k, v[0]) for k, v in qs.items()])
            filename = self.webenv['profile_handler'](sh).log_path(
                sh['conv'].test_id)
            return do_next(tester, resp, sh, inut, filename, path=path)
        elif path == "slo":
            pass
        elif path == 'all':
            for test_id in sh['flow_names']:
                resp = tester.run(test_id, **self.webenv)
                store_test_state(sh, sh['conv'].events)
                if resp is True or resp is False:
                    continue
                elif resp:
                    return resp(environ, start_response)
                else:
                    resp = ServiceError('Unkown service error')
                    return resp(environ, start_response)

            filename = self.webenv['profile_handler'](sh).log_path(path)
            return inut.flow_list(filename)
        else:
            resp = BadRequest()
            return resp(environ, start_response)

if __name__ == '__main__':
    from beaker.middleware import SessionMiddleware
    from cherrypy import wsgiserver
    from mako.lookup import TemplateLookup

    try:
        cargs, kwargs, CONF = setup('wb')
    except ConfigError as e:
        str = e.error_details_as_string()
        print ('Error: {}'.format(e))
        if (str):
            print (str)
        os.sys.exit(-1)

    if CONF.config_infos:
        print ('Please notice these infos:')
        for info in CONF.config_infos:
            print (info)

    session_opts = {
        'session.type': 'memory',
        'session.cookie_expires': True,
        'session.auto': True,
        'session.timeout': 900
    }

    _tr = kwargs['template_root']
    LOOKUP = TemplateLookup(directories=[_tr + 'templates', _tr + 'htdocs'],
                            module_directory=_tr + 'modules',
                            input_encoding='utf-8',
                            output_encoding='utf-8')

    kwargs['lookup'] = LOOKUP
    _conf = kwargs['conf']

    _app = Application(webenv=kwargs)

    SRV = wsgiserver.CherryPyWSGIServer(('0.0.0.0', int(_conf.PORT)),
                                        SessionMiddleware(_app.application,
                                                          session_opts))

    if _conf.BASE.startswith("https"):
        from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter

        SRV.ssl_adapter = BuiltinSSLAdapter(_conf.SERVER_CERT, _conf.SERVER_KEY,
                                            _conf.CERT_CHAIN)
        extra = " using SSL/TLS"
    else:
        extra = ""

    txt = "SP listening on port:%s%s" % (_conf.PORT, extra)
    LOGGER.info(txt)
    print(txt)
    try:
        SRV.start()
    except KeyboardInterrupt:
        SRV.stop()
<commit_msg>again, no id_counter<commit_after>#!/usr/bin/env python

#import sys
#print('\n'.join(sys.path))

import os
import logging
from aatest.check import State, OK, ERROR
from aatest.events import EV_CONDITION, EV_PROTOCOL_RESPONSE, NoSuchEvent
from aatest.result import Result
from aatest.verify import Verify
from future.backports.urllib.parse import quote_plus
from future.backports.urllib.parse import parse_qs

from aatest.summation import store_test_state
from aatest.session import Done
#from aatest.session import SessionHandler

from saml2.httputil import BadRequest
from saml2.httputil import get_post
from saml2.httputil import Response
from saml2.httputil import ServiceError
from saml2.response import StatusError

from saml2test.idp_test.inut import WebIO
from saml2test.idp_test.setup import setup
from saml2test.idp_test.wb_tool import Tester
from saml2test.request import ServiceProviderRequestHandlerError
from saml2test.session import SessionHandler
from saml2test.checkedconfig import ConfigError

from saml2.entity import Entity
from saml2 import BINDING_HTTP_POST
from saml2 import samlp

import threading
SERVER_LOG_FOLDER = "server_log"
if not os.path.isdir(SERVER_LOG_FOLDER):
    os.makedirs(SERVER_LOG_FOLDER)

try:
    from mako.lookup import TemplateLookup
except Exception as ex:
    raise ex

LOGGER = logging.getLogger("")

def pick_args(args, kwargs):
    return dict([(k, kwargs[k]) for k in args])

def do_next(tester, resp, sh, inut, filename, path):
    tester.conv = tester.sh['conv']
    res = Result(tester.sh, tester.kwargs['profile_handler'])
    try:
        tester.handle_response(resp, {})
        # store_test_state(sh, sh['conv'].events)  this does actually nothing?
        res.store_test_info()

    except StatusError as err:
        # store event to be found in assertion test
        tester.conv.events.store(EV_PROTOCOL_RESPONSE,err)
        msg = "{}: {}".format(err.__class__.__name__, str(err))

    except ServiceProviderRequestHandlerError as err:
        msg = str(err)
        tester.conv.events.store(EV_CONDITION, State('SP Error', ERROR,  message=msg),
                                 sender='do_next')

    tester.conv.index += 1
    lix = len(tester.conv.sequence)
    while tester.conv.sequence[tester.conv.index] != Done:
        resp = tester.run_flow(tester.conv.test_id, index=tester.conv.index)
        store_test_state(sh, sh['conv'].events)
        if isinstance(resp, Response):
            res.store_test_info()
            return resp(inut.environ, inut.start_response)
        elif resp is False:
            break
        if tester.conv.index >= lix:
            break

    _done = False
    for _cond in tester.conv.events.get_data(EV_CONDITION):
        if _cond.test_id == 'Done' and _cond.status == OK:
            _done = True
            break

    if not _done:
        tester.conv.events.store(EV_CONDITION, State('Done', OK),
                                 sender='do_next')

        if 'assert' in tester.conv.flow:
            _ver = Verify(tester.chk_factory, tester.conv)
            try:
                _ver.test_sequence(tester.conv.flow["assert"])
            except NoSuchEvent as err:
                tester.conv.events.store(EV_CONDITION, State('Assertion Error', ERROR, message=msg),
                                         sender='idp_test')
            except Exception as err:
                msg = "ERROR Assertion verification had gone wrong."
                raise Exception(msg)

        store_test_state(sh, sh['conv'].events)
        res.store_test_info()

    html_page = inut.flow_list(filename)
    return html_page

class SessionStore(list):
    def append(self, element):
        key = hex(id(element['session_info']))
        for e in self:
            e_key = hex(id(e['session_info']))
            if e_key == key:
                return
        list.append(self,element)

    def get_session_by_conv_id(self,conv_id):
        for e in self:
            try:
                session_info = e['session_info']
                conv = session_info['conv']
                id = conv.id
                if id == conv_id:
                    return session_info
            except KeyError as e:
                # entries without these infos are broken (unfinished). Ignoring.
                pass
        return None

class Application(object):
    def __init__(self, webenv):
        self.webenv = webenv
        self.session_store = SessionStore()

    def _static(self, path):
        if path in ["robots.txt", 'favicon.ico']:
            return "{}/robots.txt".format(self.webenv['static'])
        else:
            for p in ['acs/site/static/', 'site/static/', 'static/', 'export/']:
                if path.startswith(p):
                    return '{}/{}'.format(self.webenv['static'], path[len(p):])
        return ''

    def application(self, environ, start_response):
        LOGGER.info("Connection from: %s" % environ["REMOTE_ADDR"])
        session = environ['beaker.session']

        path = environ.get('PATH_INFO', '').lstrip('/')
        LOGGER.info("path: %s" % path)

        try:
            sh = session['session_info']
        except KeyError:
            sh = SessionHandler(**self.webenv)
            sh.session_init()
            session['session_info'] = sh

        self.session_store.append(session)

        inut = WebIO(session=sh, **self.webenv)
        inut.environ = environ
        inut.start_response = start_response

        tester = Tester(inut, sh, **self.webenv)

        _path = self._static(path)
        if _path:
            return inut.static(_path)

        if path == "" or path == "/":  # list
            return tester.display_test_list()
        elif "flow_names" not in sh:
            sh.session_init()

        if path == "logs":
            return inut.display_log("log", issuer="", profile="", testid="")
        elif path.startswith("log"):
            if path == "log" or path == "log/":
                _cc = inut.conf.CLIENT
                try:
                    _iss = _cc["srv_discovery_url"]
                except KeyError:
                    _iss = _cc["provider_info"]["issuer"]
                parts = [quote_plus(_iss)]
            else:
                parts = []
                while path != "log":
                    head, tail = os.path.split(path)
                    # tail = tail.replace(":", "%3A")
                    # if tail.endswith("%2F"):
                    #     tail = tail[:-3]
                    parts.insert(0, tail)
                    path = head

            return inut.display_log("log", *parts)
        elif path.startswith("tar"):
            path = path.replace(":", "%3A")
            return inut.static(path)

        elif path.startswith("test_info"):
            p = path.split("/")
            try:
                return inut.test_info(p[1])
            except KeyError:
                return inut.not_found()
        elif path == "continue":
            return tester.cont(environ, self.webenv)
        elif path == 'reset':
            for param in ['flow', 'flow_names', 'index', 'node', 'profile',
                          'sequence', 'test_info', 'testid', 'tests']:
                try:
                    del sh[param]
                except KeyError:
                    pass
            return tester.display_test_list()
        elif path == "opresult":
            if tester.conv is None:
                return inut.sorry_response(self.webenv['base_url'],
                                           "No result to report")

            return inut.opresult(tester.conv, sh)
        # expected path format: /<testid>[/<endpoint>]
        elif path in sh["flow_names"]:
            resp = tester.run(path, **self.webenv)
            store_test_state(sh, sh['conv'].events)
            filename = self.webenv['profile_handler'](sh).log_path(path)
            if isinstance(resp, Response):
                res = Result(sh, self.webenv['profile_handler'])
                res.store_test_info()
                res.print_info(path, tester.fname(path))
                return inut.respond(resp)
            else:
                return inut.flow_list(filename)
        elif path == "acs/post":
            qs = get_post(environ).decode('utf8')
            resp = dict([(k, v[0]) for k, v in parse_qs(qs).items()])

            try:
                test_id = sh['conv'].test_id
            except KeyError as err:
                test_id = None

            if not test_id:
                """
                In other words: we've been contacted by robobrowser and are in a different environment now, than the
                code expects us to be. .... Hopefully, trickery and recreating of the environment will lead mostly
                to more intended effects than unintended ones.

                This is unfinished business: You can add other bindings here, to expand what RB can be used to test.
                """
                try:
                    txt = resp['SAMLResponse']
                    xmlstr = Entity.unravel(txt, BINDING_HTTP_POST)
                except Exception as e:
                    msg = 'Decoding not supported in the SP'
                    raise Exception(msg)

                rsp = samlp.any_response_from_string(xmlstr)
                original_request_id = rsp.in_response_to
                requester_session = self.session_store.get_session_by_conv_id(original_request_id)

                # recreating the environment. lets hope it is somewhat reentrant resistant
                sh = requester_session
                inut = WebIO(session=sh, **self.webenv)
                inut.environ = environ
                inut.start_response = start_response

                tester = Tester(inut, sh, **self.webenv)

            profile_handler = self.webenv['profile_handler']
            _sh = profile_handler(sh)
            #filename = self.webenv['profile_handler'](sh).log_path(test_id)
            #_sh.session.update({'conv': 'foozbar'})
            filename = _sh.log_path(test_id)

            html_page = do_next(tester, resp, sh, inut, filename, path)
            return html_page
        elif path == "acs/redirect":
            qs = environ['QUERY_STRING']
            resp = dict([(k, v[0]) for k, v in parse_qs(qs).items()])
            filename = self.webenv['profile_handler'](sh).log_path(
                sh['conv'].test_id)

            return do_next(tester, resp, sh, inut, filename, path)
        elif path == "acs/artifact":
            pass
        elif path == "ecp":
            pass
        elif path == "disco":
            qs = parse_qs(environ['QUERY_STRING'])
            resp = dict([(k, v[0]) for k, v in qs.items()])
            filename = self.webenv['profile_handler'](sh).log_path(
                sh['conv'].test_id)
            return do_next(tester, resp, sh, inut, filename, path=path)
        elif path == "slo":
            pass
        elif path == 'all':
            for test_id in sh['flow_names']:
                resp = tester.run(test_id, **self.webenv)
                store_test_state(sh, sh['conv'].events)
                if resp is True or resp is False:
                    continue
                elif resp:
                    return resp(environ, start_response)
                else:
                    resp = ServiceError('Unkown service error')
                    return resp(environ, start_response)

            filename = self.webenv['profile_handler'](sh).log_path(path)
            return inut.flow_list(filename)
        else:
            resp = BadRequest()
            return resp(environ, start_response)

if __name__ == '__main__':
    from beaker.middleware import SessionMiddleware
    from cherrypy import wsgiserver
    from mako.lookup import TemplateLookup

    try:
        cargs, kwargs, CONF = setup('wb')
    except ConfigError as e:
        str = e.error_details_as_string()
        print ('Error: {}'.format(e))
        if (str):
            print (str)
        os.sys.exit(-1)

    if CONF.config_infos:
        print ('Please notice these infos:')
        for info in CONF.config_infos:
            print (info)

    session_opts = {
        'session.type': 'memory',
        'session.cookie_expires': True,
        'session.auto': True,
        'session.timeout': 900
    }

    _tr = kwargs['template_root']
    LOOKUP = TemplateLookup(directories=[_tr + 'templates', _tr + 'htdocs'],
                            module_directory=_tr + 'modules',
                            input_encoding='utf-8',
                            output_encoding='utf-8')

    kwargs['lookup'] = LOOKUP
    _conf = kwargs['conf']

    _app = Application(webenv=kwargs)

    SRV = wsgiserver.CherryPyWSGIServer(('0.0.0.0', int(_conf.PORT)),
                                        SessionMiddleware(_app.application,
                                                          session_opts))

    if _conf.BASE.startswith("https"):
        from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter

        SRV.ssl_adapter = BuiltinSSLAdapter(_conf.SERVER_CERT, _conf.SERVER_KEY,
                                            _conf.CERT_CHAIN)
        extra = " using SSL/TLS"
    else:
        extra = ""

    txt = "SP listening on port:%s%s" % (_conf.PORT, extra)
    LOGGER.info(txt)
    print(txt)
    try:
        SRV.start()
    except KeyboardInterrupt:
        SRV.stop()
<|endoftext|>


<commit_before># pylint: disable=C0103
# pylint: disable=C0111
# pylint: disable=C0301

import sys
import os
import threading
import logging
from time import sleep

from globus_cli.services.transfer import get_client

from lib.events import EventList
from lib.initialize import initialize
from lib.finalize import finalize
from lib.filemanager import FileManager
from lib.runmanager import RunManager
from lib.display import start_display
from lib.util import (print_line,
                      path_exists,
                      write_human_state,
                      print_message,
                      print_debug,
                      transfer_directory)

# check for NCL
if not os.environ.get('NCARG_ROOT'):
    ncar_path = '/usr/local/src/NCL-6.3.0'
    if os.path.exists(ncar_path):
        os.environ['NCARG_ROOT'] = ncar_path
    else:
        print 'No NCARG_ROOT found in environment variables, make sure NCL installed on the machine and add its path to your ~/.bashrc'
        sys.exit()

# set variable to make vcs shut up
os.environ['UVCDAT_ANONYMOUS_LOG'] = 'False'

# create global EventList
event_list = EventList()

def main(test=False, **kwargs):
    # The master configuration object
    config = {}

    # A list of all the threads
    thread_list = []

    # An event to kill the threads on terminal exception
    thread_kill_event = threading.Event()
    mutex = threading.Lock()
    display_event = threading.Event()

    # A flag to tell if we have all the data locally
    all_data = False
    all_data_remote = False

    # get a globus client
    client = get_client()

    # Read in parameters from config
    args = kwargs['testargs'] if test else sys.argv[1:]
    config, filemanager, runmanager = initialize(
        argv=args,
        event_list=event_list,
        thread_list=thread_list,
        kill_event=thread_kill_event,
        mutex=mutex)

    if isinstance(config, int):
        print "Error in setup, exiting"
        return -1
    logging.info('Config setup complete')
    logging.info(str(config))

    # check that all netCDF files exist
    path_exists(config)
    # cleanup any temp directories from previous runs
    # cleanup(config)
    if not os.path.exists(config['global']['run_scripts_path']):
        os.makedirs(config['global']['run_scripts_path'])
    if not os.path.exists(config['global']['tmp_path']):
        os.makedirs(config['global']['tmp_path'])

    if config['global'].get('ui'):
        try:
            sys.stdout.write('Turning on the display')
            for _ in range(10):
                sys.stdout.write('.')
                sys.stdout.flush()
                sleep(0.1)
            print '\n'
            args = (display_event,
                    runmanager.job_sets,
                    filemanager,
                    event_list)
            diaplay_thread = threading.Thread(
                target=start_display,
                args=args)
            diaplay_thread.start()

        except KeyboardInterrupt as e:
            print 'keyboard interrupt'
            display_event.set()
            return -1

    state_path = os.path.join(
        config.get('global').get('output_path'),
        'run_state.txt')
    msg = "Updating local file status"
    print_line(
        ui=config['global']['ui'],
        line=msg,
        event_list=event_list)
    filemanager.update_local_status()
    all_data = filemanager.all_data_local()
    if not all_data:
        print_line(
            config['global']['ui'],
            "Updating remote file status",
            event_list,
            current_state=False)
        filemanager.update_remote_status(client)
        all_data_remote = filemanager.all_data_remote()
    msg = "Writing human readable state to file"
    print_line(
        ui=config['global']['ui'],
        line=msg,
        event_list=event_list,
        current_state=True)
    sleep(0.5)
    write_human_state(
        event_list=event_list,
        job_sets=runmanager.job_sets,
        state_path=state_path,
        print_file_list=config['global'].get('print_file_list'),
        mutex=mutex)

    dryrun = config['global'].get('dry_run')
    if dryrun:
        runmanager.dryrun(True)
        msg = 'Running in dry-run mode'
        print_line(
            ui=config['global']['ui'],
            line=msg,
            event_list=event_list,
            current_state=True)
        write_human_state(
            event_list=event_list,
            job_sets=runmanager.job_sets,
            state_path=state_path,
            print_file_list=config['global'].get('print_file_list'),
            mutex=mutex)
        if config['global'].get('ui'):
            sleep(50)
            display_event.set()
            for t in thread_list:
                thread_kill_event.set()
                t.join(timeout=1.0)
            return -1

    # check if the case_scripts directory is present
    # if its not, transfer it over
    case_scripts_dir = os.path.join(
        config['global']['input_path'],
        'case_scripts')

    if not os.path.exists(case_scripts_dir) \
       and not config['global']['no_monitor']:
        msg = 'case_scripts not local, transfering remote copy'
        print_line(
            ui=config['global']['ui'],
            line=msg,
            event_list=event_list)
        logging.info(msg)
        src_path = os.path.join(
            config['global']['source_path'], 'case_scripts')
        while True:
            try:
                args = {
                    'source_endpoint': config['transfer']['source_endpoint'],
                    'destination_endpoint': config['transfer']['destination_endpoint'],
                    'src_path': src_path,
                    'dst_path': case_scripts_dir,
                    'event_list': event_list,
                    'event': thread_kill_event
                }
                thread = threading.Thread(
                    target=transfer_directory,
                    name='transfer_directory',
                    kwargs=args)
            except:
                sleep(1)
            else:
                thread_list.append(thread)
                thread.start()
                break

    # Main loop
    remote_check_delay = 60
    local_check_delay = 2
    printed = False
    try:
        loop_count = remote_check_delay - 1
        if not config['global']['ui']:
            print "--------------------------"
            print " Entering Main Loop "
            print " Status file: {}".format(state_path)
            print "--------------------------"
        while True:
            # flush stdout to play nice with nohup
            sys.stdout.flush()
            # Check the remote status once every 5 minutes
            if loop_count == remote_check_delay:
                loop_count = 0
                if config.get('global').get('no_monitor', False):
                    loop_count += 1
                    continue
                if not all_data:
                    all_data = filemanager.all_data_local()
                if not all_data_remote and not all_data:
                    all_data_remote = filemanager.all_data_remote()
                if not all_data_remote and not all_data:
                    msg = 'Updating remote status'
                    print_line(
                        ui=config['global']['ui'],
                        line=msg,
                        event_list=event_list,
                        current_state=True)
                    filemanager.update_remote_status(client)
                if not all_data:
                    msg = 'Updating local status'
                    print_line(
                        ui=config['global']['ui'],
                        line=msg,
                        event_list=event_list,
                        current_state=True)
                    filemanager.update_local_status()
            # check the local status every 10 seconds
            if loop_count == local_check_delay:
                if not all_data:
                    all_data = filemanager.all_data_local()
                else:
                    if not printed:
                        msg = 'All data local, turning off remote checks'
                        print_line(
                            ui=config['global']['ui'],
                            line=msg,
                            event_list=event_list)
                        printed = True
                if not all_data \
                        and not config['global']['no_monitor']:
                    transfer_started = filemanager.transfer_needed(
                        event_list=event_list,
                        event=thread_kill_event,
                        remote_endpoint=config['transfer']['source_endpoint'],
                        ui=config['global']['ui'],
                        display_event=display_event,
                        emailaddr=config['global']['email'],
                        thread_list=thread_list)

            msg = "Checking for ready job sets"
            print_line(
                ui=config['global']['ui'],
                line=msg,
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            filemanager.check_year_sets(runmanager.job_sets)
            msg = "Starting ready jobs"
            print_line(
                ui=config['global']['ui'],
                line=msg,
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            sleep(0.5)
            runmanager.start_ready_job_sets()
            msg = "Checking running job status"
            print_line(
                ui=config['global']['ui'],
                line=msg,
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            sleep(0.5)
            runmanager.monitor_running_jobs()
            write_human_state(
                event_list=event_list,
                job_sets=runmanager.job_sets,
                state_path=state_path,
                print_file_list=config.get('global').get('print_file_list'),
                mutex=mutex)
            status = runmanager.is_all_done()
            if status >= 0:
                first_print = True
                while not filemanager.all_data_local():
                    if first_print:
                        msg = "All jobs complete, moving additional files"
                        print_line(
                            ui=config['global']['ui'],
                            line=msg,
                            event_list=event_list,
                            current_state=True)
                        first_print = False
                    started = filemanager.transfer_needed(
                        event_list=event_list,
                        event=thread_kill_event,
                        remote_endpoint=config['transfer']['source_endpoint'],
                        ui=config['global']['ui'],
                        display_event=display_event,
                        emailaddr=config['global']['email'],
                        thread_list=thread_list)
                    if not started:
                        sleep(5)
                    else:
                        msg = "Transfer started"
                        print_line(
                            ui=config['global']['ui'],
                            line=msg,
                            event_list=event_list)
                msg = "Finishing up run"
                print_line(
                    ui=config['global']['ui'],
                    line=msg,
                    event_list=event_list,
                    current_state=True)
                finalize(
                    config=config,
                    job_sets=runmanager.job_sets,
                    event_list=event_list,
                    status=status,
                    display_event=display_event,
                    thread_list=thread_list,
                    kill_event=thread_kill_event)
                # SUCCESS EXIT
                return 0
            print_line(
                ui=config['global']['ui'],
                line='sleeping',
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            sleep(5)
            loop_count += 1
    except KeyboardInterrupt as e:
        print_message('----- KEYBOARD INTERRUPT -----')
        print_message('----- cleaning up threads ---', 'ok')
        event_list.push(message="Exiting due to keyboard interrupt")
        write_human_state(
            event_list=event_list,
            job_sets=runmanager.job_sets,
            state_path=state_path,
            print_file_list=True,
            mutex=mutex)
        display_event.set()
        thread_kill_event.set()
        for thread in thread_list:
            thread.join(timeout=1.0)
    except Exception as e:
        display_event.set()
        thread_kill_event.set()
        for thread in thread_list:
            thread.join(timeout=1.0)
        sleep(1)
        print_message('----- UNEXPECTED EXCEPTION OCCURED -----')
        print_debug(e)

if __name__ == "__main__":
    if sys.argv[1] == 'test':
        config_path = os.path.join(os.getcwd(), 'tests', 'test_run_no_sta.cfg')
        testargs = ['-c', config_path, '-n', '-f']
        ret = main(test=True, testargs=testargs)
    else:
        ret = main()
    sys.exit(ret)
<commit_msg>The nohup fix didnt work<commit_after># pylint: disable=C0103
# pylint: disable=C0111
# pylint: disable=C0301

import sys
import os
import threading
import logging
from time import sleep

from globus_cli.services.transfer import get_client

from lib.events import EventList
from lib.initialize import initialize
from lib.finalize import finalize
from lib.filemanager import FileManager
from lib.runmanager import RunManager
from lib.display import start_display
from lib.util import (print_line,
                      path_exists,
                      write_human_state,
                      print_message,
                      print_debug,
                      transfer_directory)

# check for NCL
if not os.environ.get('NCARG_ROOT'):
    ncar_path = '/usr/local/src/NCL-6.3.0'
    if os.path.exists(ncar_path):
        os.environ['NCARG_ROOT'] = ncar_path
    else:
        print 'No NCARG_ROOT found in environment variables, make sure NCL installed on the machine and add its path to your ~/.bashrc'
        sys.exit()

# set variable to make vcs shut up
os.environ['UVCDAT_ANONYMOUS_LOG'] = 'False'

# create global EventList
event_list = EventList()

def main(test=False, **kwargs):
    # The master configuration object
    config = {}

    # A list of all the threads
    thread_list = []

    # An event to kill the threads on terminal exception
    thread_kill_event = threading.Event()
    mutex = threading.Lock()
    display_event = threading.Event()

    # A flag to tell if we have all the data locally
    all_data = False
    all_data_remote = False

    # get a globus client
    client = get_client()

    # Read in parameters from config
    args = kwargs['testargs'] if test else sys.argv[1:]
    config, filemanager, runmanager = initialize(
        argv=args,
        event_list=event_list,
        thread_list=thread_list,
        kill_event=thread_kill_event,
        mutex=mutex)

    if isinstance(config, int):
        print "Error in setup, exiting"
        return -1
    logging.info('Config setup complete')
    logging.info(str(config))

    # check that all netCDF files exist
    path_exists(config)
    # cleanup any temp directories from previous runs
    # cleanup(config)
    if not os.path.exists(config['global']['run_scripts_path']):
        os.makedirs(config['global']['run_scripts_path'])
    if not os.path.exists(config['global']['tmp_path']):
        os.makedirs(config['global']['tmp_path'])

    if config['global'].get('ui'):
        try:
            sys.stdout.write('Turning on the display')
            for _ in range(10):
                sys.stdout.write('.')
                sys.stdout.flush()
                sleep(0.1)
            print '\n'
            args = (display_event,
                    runmanager.job_sets,
                    filemanager,
                    event_list)
            diaplay_thread = threading.Thread(
                target=start_display,
                args=args)
            diaplay_thread.start()

        except KeyboardInterrupt as e:
            print 'keyboard interrupt'
            display_event.set()
            return -1

    state_path = os.path.join(
        config.get('global').get('output_path'),
        'run_state.txt')
    msg = "Updating local file status"
    print_line(
        ui=config['global']['ui'],
        line=msg,
        event_list=event_list)
    filemanager.update_local_status()
    all_data = filemanager.all_data_local()
    if not all_data:
        print_line(
            config['global']['ui'],
            "Updating remote file status",
            event_list,
            current_state=False)
        filemanager.update_remote_status(client)
        all_data_remote = filemanager.all_data_remote()
    msg = "Writing human readable state to file"
    print_line(
        ui=config['global']['ui'],
        line=msg,
        event_list=event_list,
        current_state=True)
    sleep(0.5)
    write_human_state(
        event_list=event_list,
        job_sets=runmanager.job_sets,
        state_path=state_path,
        print_file_list=config['global'].get('print_file_list'),
        mutex=mutex)

    dryrun = config['global'].get('dry_run')
    if dryrun:
        runmanager.dryrun(True)
        msg = 'Running in dry-run mode'
        print_line(
            ui=config['global']['ui'],
            line=msg,
            event_list=event_list,
            current_state=True)
        write_human_state(
            event_list=event_list,
            job_sets=runmanager.job_sets,
            state_path=state_path,
            print_file_list=config['global'].get('print_file_list'),
            mutex=mutex)
        if config['global'].get('ui'):
            sleep(50)
            display_event.set()
            for t in thread_list:
                thread_kill_event.set()
                t.join(timeout=1.0)
            return -1

    # check if the case_scripts directory is present
    # if its not, transfer it over
    case_scripts_dir = os.path.join(
        config['global']['input_path'],
        'case_scripts')

    if not os.path.exists(case_scripts_dir) \
       and not config['global']['no_monitor']:
        msg = 'case_scripts not local, transfering remote copy'
        print_line(
            ui=config['global']['ui'],
            line=msg,
            event_list=event_list)
        logging.info(msg)
        src_path = os.path.join(
            config['global']['source_path'], 'case_scripts')
        while True:
            try:
                args = {
                    'source_endpoint': config['transfer']['source_endpoint'],
                    'destination_endpoint': config['transfer']['destination_endpoint'],
                    'src_path': src_path,
                    'dst_path': case_scripts_dir,
                    'event_list': event_list,
                    'event': thread_kill_event
                }
                thread = threading.Thread(
                    target=transfer_directory,
                    name='transfer_directory',
                    kwargs=args)
            except:
                sleep(1)
            else:
                thread_list.append(thread)
                thread.start()
                break

    # Main loop
    remote_check_delay = 60
    local_check_delay = 2
    printed = False
    try:
        loop_count = remote_check_delay - 1
        if not config['global']['ui']:
            print "--------------------------"
            print " Entering Main Loop "
            print " Status file: {}".format(state_path)
            print "--------------------------"
        while True:
            # flush stdout to play nice with nohup
            # sys.stdout.flush()
            # Check the remote status once every 5 minutes
            if loop_count == remote_check_delay:
                loop_count = 0
                if config.get('global').get('no_monitor', False):
                    loop_count += 1
                    continue
                if not all_data:
                    all_data = filemanager.all_data_local()
                if not all_data_remote and not all_data:
                    all_data_remote = filemanager.all_data_remote()
                if not all_data_remote and not all_data:
                    msg = 'Updating remote status'
                    print_line(
                        ui=config['global']['ui'],
                        line=msg,
                        event_list=event_list,
                        current_state=True)
                    filemanager.update_remote_status(client)
                if not all_data:
                    msg = 'Updating local status'
                    print_line(
                        ui=config['global']['ui'],
                        line=msg,
                        event_list=event_list,
                        current_state=True)
                    filemanager.update_local_status()
            # check the local status every 10 seconds
            if loop_count == local_check_delay:
                if not all_data:
                    all_data = filemanager.all_data_local()
                else:
                    if not printed:
                        msg = 'All data local, turning off remote checks'
                        print_line(
                            ui=config['global']['ui'],
                            line=msg,
                            event_list=event_list)
                        printed = True
                if not all_data \
                        and not config['global']['no_monitor']:
                    transfer_started = filemanager.transfer_needed(
                        event_list=event_list,
                        event=thread_kill_event,
                        remote_endpoint=config['transfer']['source_endpoint'],
                        ui=config['global']['ui'],
                        display_event=display_event,
                        emailaddr=config['global']['email'],
                        thread_list=thread_list)

            msg = "Checking for ready job sets"
            print_line(
                ui=config['global']['ui'],
                line=msg,
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            filemanager.check_year_sets(runmanager.job_sets)
            msg = "Starting ready jobs"
            print_line(
                ui=config['global']['ui'],
                line=msg,
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            sleep(0.5)
            runmanager.start_ready_job_sets()
            msg = "Checking running job status"
            print_line(
                ui=config['global']['ui'],
                line=msg,
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            sleep(0.5)
            runmanager.monitor_running_jobs()
            write_human_state(
                event_list=event_list,
                job_sets=runmanager.job_sets,
                state_path=state_path,
                print_file_list=config.get('global').get('print_file_list'),
                mutex=mutex)
            status = runmanager.is_all_done()
            if status >= 0:
                first_print = True
                while not filemanager.all_data_local():
                    if first_print:
                        msg = "All jobs complete, moving additional files"
                        print_line(
                            ui=config['global']['ui'],
                            line=msg,
                            event_list=event_list,
                            current_state=True)
                        first_print = False
                    started = filemanager.transfer_needed(
                        event_list=event_list,
                        event=thread_kill_event,
                        remote_endpoint=config['transfer']['source_endpoint'],
                        ui=config['global']['ui'],
                        display_event=display_event,
                        emailaddr=config['global']['email'],
                        thread_list=thread_list)
                    if not started:
                        sleep(5)
                    else:
                        msg = "Transfer started"
                        print_line(
                            ui=config['global']['ui'],
                            line=msg,
                            event_list=event_list)
                msg = "Finishing up run"
                print_line(
                    ui=config['global']['ui'],
                    line=msg,
                    event_list=event_list,
                    current_state=True)
                finalize(
                    config=config,
                    job_sets=runmanager.job_sets,
                    event_list=event_list,
                    status=status,
                    display_event=display_event,
                    thread_list=thread_list,
                    kill_event=thread_kill_event)
                # SUCCESS EXIT
                return 0
            print_line(
                ui=config['global']['ui'],
                line='sleeping',
                event_list=event_list,
                current_state=True,
                ignore_text=True)
            sleep(5)
            loop_count += 1
    except KeyboardInterrupt as e:
        print_message('----- KEYBOARD INTERRUPT -----')
        print_message('----- cleaning up threads ---', 'ok')
        event_list.push(message="Exiting due to keyboard interrupt")
        write_human_state(
            event_list=event_list,
            job_sets=runmanager.job_sets,
            state_path=state_path,
            print_file_list=True,
            mutex=mutex)
        display_event.set()
        thread_kill_event.set()
        for thread in thread_list:
            thread.join(timeout=1.0)
    except Exception as e:
        display_event.set()
        thread_kill_event.set()
        for thread in thread_list:
            thread.join(timeout=1.0)
        sleep(1)
        print_message('----- UNEXPECTED EXCEPTION OCCURED -----')
        print_debug(e)

if __name__ == "__main__":
    if sys.argv[1] == 'test':
        config_path = os.path.join(os.getcwd(), 'tests', 'test_run_no_sta.cfg')
        testargs = ['-c', config_path, '-n', '-f']
        ret = main(test=True, testargs=testargs)
    else:
        ret = main()
    sys.exit(ret)
<|endoftext|>


<commit_before>from easyprocess import EasyProcess
from pyscreenshot.backendloader import BackendLoader
from PIL import Image
import logging
import tempfile
import sys

__version__ = '0.3.1'

log = logging.getLogger(__name__)
log.debug('version=' + __version__)

def _grab(to_file, childprocess=False, backend=None, bbox=None, filename=None):
    if childprocess:
        log.debug('running "%s" in child process' % backend)

        if not to_file:
            f = tempfile.NamedTemporaryFile(
                suffix='.png', prefix='pyscreenshot_childprocess_')
            filename = f.name

        params = ["'%s'" % (filename), 'childprocess=False']
        if backend:
            params += ["backend='%s'" % (backend)]
        params = ','.join(params)

        EasyProcess([sys.executable,
                     '-c',
                     "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % (
                     params),
                     ]).check()
        if not to_file:
            im = Image.open(filename)
            if bbox:
                im = im.crop(bbox)
            return im
    else:
        if backend:
            BackendLoader().force(backend)
        backend_obj = BackendLoader().selected()

        if to_file:
            return backend_obj.grab_to_file(filename)
        else:
            return backend_obj.grab(bbox)

def grab(bbox=None, childprocess=False, backend=None):
    '''Copy the contents of the screen to PIL image memory.

    :param bbox: optional bounding box
    :param childprocess: pyscreenshot can cause an error,
            if it is used on more different virtual displays
            and back-end is not in different process.
            Some back-ends are always different processes: scrot, imagemagick
    :param backend: back-end can be forced if set (examples:scrot, wx,..),
                    otherwise back-end is automatic
    '''
    return _grab(to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)

def grab_to_file(filename, childprocess=False, backend=None):
    '''Copy the contents of the screen to a file.

    :param filename: file for saving
    :param childprocess: see :py:func:`grab`
    :param backend: see :py:func:`grab`
    '''
    return _grab(to_file=True, childprocess=childprocess, backend=backend, filename=filename)
<commit_msg>version++<commit_after>from easyprocess import EasyProcess
from pyscreenshot.backendloader import BackendLoader
from PIL import Image
import logging
import tempfile
import sys

__version__ = '0.3.2'

log = logging.getLogger(__name__)
log.debug('version=' + __version__)

def _grab(to_file, childprocess=False, backend=None, bbox=None, filename=None):
    if childprocess:
        log.debug('running "%s" in child process' % backend)

        if not to_file:
            f = tempfile.NamedTemporaryFile(
                suffix='.png', prefix='pyscreenshot_childprocess_')
            filename = f.name

        params = ["'%s'" % (filename), 'childprocess=False']
        if backend:
            params += ["backend='%s'" % (backend)]
        params = ','.join(params)

        EasyProcess([sys.executable,
                     '-c',
                     "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % (
                     params),
                     ]).check()
        if not to_file:
            im = Image.open(filename)
            if bbox:
                im = im.crop(bbox)
            return im
    else:
        if backend:
            BackendLoader().force(backend)
        backend_obj = BackendLoader().selected()

        if to_file:
            return backend_obj.grab_to_file(filename)
        else:
            return backend_obj.grab(bbox)

def grab(bbox=None, childprocess=False, backend=None):
    '''Copy the contents of the screen to PIL image memory.

    :param bbox: optional bounding box
    :param childprocess: pyscreenshot can cause an error,
            if it is used on more different virtual displays
            and back-end is not in different process.
            Some back-ends are always different processes: scrot, imagemagick
    :param backend: back-end can be forced if set (examples:scrot, wx,..),
                    otherwise back-end is automatic
    '''
    return _grab(to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)

def grab_to_file(filename, childprocess=False, backend=None):
    '''Copy the contents of the screen to a file.

    :param filename: file for saving
    :param childprocess: see :py:func:`grab`
    :param backend: see :py:func:`grab`
    '''
    return _grab(to_file=True, childprocess=childprocess, backend=backend, filename=filename)
<|endoftext|>


<commit_before>import os,sys
try:
  import matplotlib
except:
  sys.exit("Module astropy missing")

matplotlib.use('Agg')
import matplotlib.pyplot as mpl

try:
  from astropy.io import fits
except:
  sys.exit("Module astropy missing")

from astropy.wcs import WCS

try:
  from aplpy import FITSFigure
except:
  print
  sys.exit("Module aplpy missing")

#enrico imports
try:
  from enrico.config import get_config
  from enrico.gtfunction import Observation
except:
  sys.exit("Enrico Module missing? Fermi ST installed?")

try :
  vmax = float(sys.argv[2])
  print "use user vmax for the color scale: "+sys.argv[2]
except:
  print "use default vmax for the color scale: 8"
  print "you can change it by typing python "+sys.argv[0]+" config [vmax]"
  vmax=8

def set_hgps_style(f):
    """Set HGPS style for a f = aplpy.FITSFigure"""
    #f.set_tick_labels_font(size='small')
    #f.set_axis_labels_font(size='small')
    f.ticks.set_xspacing(2)
    f.ticks.set_yspacing(2)
    f.ticks.set_linewidth(1.5)
    f.tick_labels.set_xformat('dd')
    f.tick_labels.set_yformat('dd')
    f.tick_labels.set_style('colons')
    #f.tick_labels.set_font(size='small')
    f.axis_labels.set_xtext('Right Ascension (deg)')
    f.axis_labels.set_ytext('Declination (deg)')

try:
  infile = sys.argv[1]
except:
  "Please provide a configuration file"
  sys.exit(1)

print
config = get_config(infile)
obs = Observation(config["out"], config, convtyp=config['analysis']['convtype'])

counts = dict(label='Counts', filename=obs.cmapfile)
model = dict(label='Model', filename=obs.ModelMap)
residuals = dict(label='Residuals', filename=config['out'] + "/" + config['target']['name']+'_Residual_Model_cmap.fits')
images = [counts, model, residuals][::-1]

# Determine image center and width / height
dpi = 2000
header = fits.getheader(images[0]['filename'])
wcs = WCS(header)
header['NAXIS1'] / dpi
header['NAXIS2'] / dpi
lon, lat = header['NAXIS1'] / 2., header['NAXIS2'] / 2.
x_center, y_center = wcs.wcs_pix2world(lon, lat, 0)
radius = header['CDELT2'] * header['NAXIS2'] / 2.

# Computing the sub-figure sizes is surprisingly hard
figsize=(5, 15)
figure = mpl.figure(figsize=figsize)
axis_ratio = figsize[0] / float(figsize[1])
edge_margin_x = 0.12
edge_margin_y = edge_margin_x * axis_ratio
edge_margin_x_up = 0.01
edge_margin_y_up = edge_margin_x_up * axis_ratio
inner_margin_x = 0.1
inner_margin_y = inner_margin_x * axis_ratio
size_x = (1 - edge_margin_x - edge_margin_x_up)
size_y = (1 - edge_margin_y - edge_margin_y_up - 2 * inner_margin_y) / 3

for ii, image in enumerate(images):
    subplot = [edge_margin_x, edge_margin_y + ii * (size_y + inner_margin_y), size_x, size_y]
    f = FITSFigure(image['filename'], figure=figure, subplot=subplot)
    f.recenter(x_center, y_center, 0.95 * radius)
    set_hgps_style(f)
    f.show_colorscale(vmin=-1, vmax=vmax, stretch='power', exponent=1, cmap='jet') #vmid=-3, stretch='log', )
    # TODO: overplot sources
#    f.show_regions("sources.reg")

filename = config["out"]+'/Maps.eps'
print('Writing {}'.format(filename))
figure.savefig(filename)
filename = config["out"]+'/Maps.png'
print('Writing {}'.format(filename))
figure.savefig(filename)
<commit_msg>bug in plotMaps.py<commit_after>import os,sys
try:
  import matplotlib
except:
  sys.exit("Module astropy missing")

matplotlib.use('Agg')
import matplotlib.pyplot as mpl

try:
  from astropy.io import fits
except:
  sys.exit("Module astropy missing")

from astropy.wcs import WCS

try:
  from aplpy import FITSFigure
except:
  print
  sys.exit("Module aplpy missing")

#enrico imports
try:
  from enrico.config import get_config
  from enrico.gtfunction import Observation
except:
  sys.exit("Enrico Module missing? Fermi ST installed?")

try :
  vmax = float(sys.argv[2])
  print "use user vmax for the color scale: "+sys.argv[2]
except:
  print "use default vmax for the color scale: 8"
  print "you can change it by typing python "+sys.argv[0]+" config [vmax]"
  vmax=8

def set_hgps_style(f):
    """Set HGPS style for a f = aplpy.FITSFigure"""
    #f.set_tick_labels_font(size='small')
    #f.set_axis_labels_font(size='small')
    f.ticks.set_xspacing(2)
    f.ticks.set_yspacing(2)
    f.ticks.set_linewidth(1.5)
    f.tick_labels.set_xformat('dd')
    f.tick_labels.set_yformat('dd')
    f.tick_labels.set_style('colons')
    #f.tick_labels.set_font(size='small')
    f.axis_labels.set_xtext('Right Ascension (deg)')
    f.axis_labels.set_ytext('Declination (deg)')

try:
  infile = sys.argv[1]
except:
  "Please provide a configuration file"
  sys.exit(1)

print
config = get_config(infile)
obs = Observation(config["out"], config)

counts = dict(label='Counts', filename=obs.cmapfile)
model = dict(label='Model', filename=obs.ModelMap)
residuals = dict(label='Residuals', filename=config['out'] + "/" + config['target']['name']+'_Residual_Model_cmap.fits')
images = [counts, model, residuals][::-1]

# Determine image center and width / height
dpi = 2000
header = fits.getheader(images[0]['filename'])
wcs = WCS(header)
header['NAXIS1'] / dpi
header['NAXIS2'] / dpi
lon, lat = header['NAXIS1'] / 2., header['NAXIS2'] / 2.
x_center, y_center = wcs.wcs_pix2world(lon, lat, 0)
radius = header['CDELT2'] * header['NAXIS2'] / 2.

# Computing the sub-figure sizes is surprisingly hard
figsize=(5, 15)
figure = mpl.figure(figsize=figsize)
axis_ratio = figsize[0] / float(figsize[1])
edge_margin_x = 0.12
edge_margin_y = edge_margin_x * axis_ratio
edge_margin_x_up = 0.01
edge_margin_y_up = edge_margin_x_up * axis_ratio
inner_margin_x = 0.1
inner_margin_y = inner_margin_x * axis_ratio
size_x = (1 - edge_margin_x - edge_margin_x_up)
size_y = (1 - edge_margin_y - edge_margin_y_up - 2 * inner_margin_y) / 3

for ii, image in enumerate(images):
    subplot = [edge_margin_x, edge_margin_y + ii * (size_y + inner_margin_y), size_x, size_y]
    f = FITSFigure(image['filename'], figure=figure, subplot=subplot)
    f.recenter(x_center, y_center, 0.95 * radius)
    set_hgps_style(f)
    f.show_colorscale(vmin=-1, vmax=vmax, stretch='power', exponent=1, cmap='jet') #vmid=-3, stretch='log', )
    # TODO: overplot sources
#    f.show_regions("sources.reg")

filename = config["out"]+'/Maps.eps'
print('Writing {}'.format(filename))
figure.savefig(filename)
filename = config["out"]+'/Maps.png'
print('Writing {}'.format(filename))
figure.savefig(filename)
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import sys, os, unittest, logging, inspect

# Extend PYTHONPATH with local 'lib' folder
if __name__ == "__main__":
    jasyroot = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir, os.pardir, os.pardir))
    sys.path.insert(0, jasyroot)
    print("Running from %s..." % jasyroot)

import jasy.style.Engine as Engine
import jasy.core.Permutation as Permutation

class Tests(unittest.TestCase):

    def process(self, code):
        callerName = inspect.stack()[1][3][5:]

        permutation = Permutation.Permutation({
            "jasy.engine" : "gecko",
            "jasy.debug" : True
        })

        tree = Engine.getTree(code, callerName)
        tree = Engine.permutateTree(tree, permutation)
        tree = Engine.reduceTree(tree)

        return Engine.compressTree(tree)

    def test_if(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if jasy.debug == true{
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            '''), 'h1{font-size:20px;outline:1px solid red;}p{color:red;}')

    def test_if_else_true(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if jasy.debug == true{
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:1px solid red;}p{color:red;}')

    def test_if_else_false(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if jasy.debug == false
            {
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:none;}p{color:black;}')

    def test_if_else_false_paren(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if (jasy.debug == false)
            {
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:none;}p{color:black;}')

    def test_if_else_false_paren_notoper(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if (jasy.debug == !false)
            {
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:1px solid red;}p{color:red;}')

    def test_field(self):
        self.assertEqual(self.process('''
            $engine = @field(jasy.engine);
            h2{
              content: $engine;
            }
            '''), 'h2{content:"gecko";}')

if __name__ == '__main__':
    logging.getLogger().setLevel(logging.ERROR)
    suite = unittest.TestLoader().loadTestsFromTestCase(Tests)
    unittest.TextTestRunner(verbosity=2).run(suite)

<commit_msg>Added another @field test<commit_after>#!/usr/bin/env python3

import sys, os, unittest, logging, inspect

# Extend PYTHONPATH with local 'lib' folder
if __name__ == "__main__":
    jasyroot = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir, os.pardir, os.pardir))
    sys.path.insert(0, jasyroot)
    print("Running from %s..." % jasyroot)

import jasy.style.Engine as Engine
import jasy.core.Permutation as Permutation

class Tests(unittest.TestCase):

    def process(self, code):
        callerName = inspect.stack()[1][3][5:]

        permutation = Permutation.Permutation({
            "jasy.engine" : "gecko",
            "jasy.debug" : True
        })

        tree = Engine.getTree(code, callerName)
        tree = Engine.permutateTree(tree, permutation)
        tree = Engine.reduceTree(tree)

        return Engine.compressTree(tree)

    def test_if(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if jasy.debug == true{
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            '''), 'h1{font-size:20px;outline:1px solid red;}p{color:red;}')

    def test_if_else_true(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if jasy.debug == true{
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:1px solid red;}p{color:red;}')

    def test_if_else_false(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if jasy.debug == false
            {
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:none;}p{color:black;}')

    def test_if_else_false_paren(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if (jasy.debug == false)
            {
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:none;}p{color:black;}')

    def test_if_else_false_paren_notoper(self):
        self.assertEqual(self.process('''
            h1{
              font-size: 20px;
            }

            @if (jasy.debug == !false)
            {
              h1{
                outline: 1px solid red;
              }

              p{
                color:red;
              }
            }
            @else
            {
              h1{
                outline: none;
              }

              p{
                color: black;
              }
            }
            '''), 'h1{font-size:20px;outline:1px solid red;}p{color:red;}')

    def test_field(self):
        self.assertEqual(self.process('''
            h2{
              content: @field(jasy.engine);
            }
            '''), 'h2{content:"gecko";}')

    def test_field_as_variable(self):
        self.assertEqual(self.process('''
            $engine = @field(jasy.engine);
            h2{
              content: $engine;
            }
            '''), 'h2{content:"gecko";}')

if __name__ == '__main__':
    logging.getLogger().setLevel(logging.ERROR)
    suite = unittest.TestLoader().loadTestsFromTestCase(Tests)
    unittest.TextTestRunner(verbosity=2).run(suite)

<|endoftext|>


<commit_before>from django.db import models
from rest_framework import serializers

class Choice(models.Model):
    text = models.CharField(max_length=255)
    version = models.CharField(max_length=4)
    created_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)

class Answer(models.Model):
    choice_id = models.ForeignKey(Choice, on_delete=models.CASCADE)
    user_id = models.CharField(max_length=255)
    created_on = models.DateTimeField(auto_now_add=True)

class ChoiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Choice
        fields = ('id', 'text', 'season', 'created_on', 'updated_on',)

class AnswerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Answer
        fields = ('id', 'choice_id', 'user_id', 'created_on',)
<commit_msg>Change choice_id field to choice<commit_after>from django.db import models
from rest_framework import serializers

class Choice(models.Model):
    text = models.CharField(max_length=255)
    version = models.CharField(max_length=4)
    created_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)

class Answer(models.Model):
    choice = models.ForeignKey(Choice, on_delete=models.CASCADE)
    user_id = models.CharField(max_length=255)
    created_on = models.DateTimeField(auto_now_add=True)

class ChoiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Choice
        fields = ('id', 'text', 'season', 'created_on', 'updated_on',)

class AnswerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Answer
        fields = ('id', 'choice_id', 'user_id', 'created_on',)
<|endoftext|>


<commit_before>import sublime
import sublime_plugin

from os.path import join
import os
import subprocess
import threading

from .lib.plat import is_windows
from .lib.plat import supress_window
from .lib.panels import OutputPanel
from . import PluginLogger
from .lib.sdk import SDK
from .lib.path import is_pubspec
from .lib.path import is_view_dart_script

_logger = PluginLogger(__name__)

class PubspecListener(sublime_plugin.EventListener):
    def __init__(self, *args, **kwargs):
        sublime_plugin.EventListener.__init__(self, *args, **kwargs)

    def on_activated(self, view):
        if not (is_pubspec(view) or is_view_dart_script(view)):
            return

        # TODO(guillermooo): I cannot get automatic detection of build sys
        # working. This code will make build systems change even if the user
        # has not enabled automatic detection of builds systems.
        if is_pubspec(view):
            _logger.debug('changing build system to "pubspec"')
            view.window().run_command('set_build_system', {
                "file": "Packages/Dart/Support/Dart - Pubspec.sublime-build"})
            return

        _logger.debug('changing build system to "dart"')
        view.window().run_command('set_build_system', {
            "file": "Packages/Dart/Support/Dart.sublime-build"})

    def on_post_save(self, view):
        if not is_pubspec(view):
            return

        _logger.debug("running pub with %s", view.file_name())
        RunPub(view, view.file_name())

def RunPub(view, file_name):
    # FIXME: Infefficient. We should store the path to the sdk away.
    dartsdk_path = SDK().path

    if not dartsdk_path:
        _logger.debug("`dartsdk_path` missing; aborting pub")
        return

    PubThread(view.window(), dartsdk_path, file_name).start()

# TODO(guillermooo): Perhaps we can replace this with Default.exec.AsyncProc.
class PubThread(threading.Thread):
    def __init__(self, window, dartsdk_path, file_name):
        super(PubThread, self).__init__()
        self.daemon = True
        self.window = window
        self.dartsdk_path = dartsdk_path
        self.file_name = file_name

    def run(self):
        working_directory = os.path.dirname(self.file_name)
        pub_path = join(self.dartsdk_path, 'bin', 'pub')

        if is_windows():
            pub_path += '.bat'

        print('pub get %s' % self.file_name)
        proc = subprocess.Popen([pub_path, 'get'],
                                cwd=working_directory,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT,
                                startupinfo=supress_window())

        out, errs = proc.communicate()
        data = out.decode('utf-8')

        if proc.returncode != 0:
            errs = errs.decode('utf-8')
            _logger.error("error running pub: %s\n%s", self.file_name, errs)
            data = 'error running pub: %s\n%s' % (self.file_name, errs)

        sublime.set_timeout(lambda: self.callback(data), 50)

    def format_data(self, data):
        return data.replace('\r', '')

    def callback(self, data):
        panel = OutputPanel('dart.pub')
        panel.write(self.format_data(data))
        panel.show()
<commit_msg>Don't change build systems automatically<commit_after>import sublime
import sublime_plugin

from os.path import join
import os
import subprocess
import threading

from .lib.plat import is_windows
from .lib.plat import supress_window
from .lib.panels import OutputPanel
from . import PluginLogger
from .lib.sdk import SDK
from .lib.path import is_pubspec
from .lib.path import is_view_dart_script

_logger = PluginLogger(__name__)

class PubspecListener(sublime_plugin.EventListener):
    def __init__(self, *args, **kwargs):
        sublime_plugin.EventListener.__init__(self, *args, **kwargs)

    def on_activated(self, view):
        if not (is_pubspec(view) or is_view_dart_script(view)):
            return

        # TODO(guillermooo): I cannot get automatic detection of build sys
        # working. This code will make build systems change even if the user
        # has not enabled automatic detection of builds systems.
        active_view = sublime.active_window().active_view()
        if is_pubspec(view) and (view.id() == active_view.id()):
            _logger.debug('changing build system to "pubspec"')
            view.window().run_command('set_build_system', {
                "file": "Packages/Dart/Support/Dart - Pubspec.sublime-build"})
            return

    def on_deactivated(self, view):
        if not (is_pubspec(view) or is_view_dart_script(view)):
            return

        # Activate 'automatic' build system detection.
        view.window().run_command('set_build_system', {'file': ''})

    def on_post_save(self, view):
        if not is_pubspec(view):
            return

        _logger.debug("running pub with %s", view.file_name())
        RunPub(view, view.file_name())

def RunPub(view, file_name):
    # FIXME: Infefficient. We should store the path to the sdk away.
    dartsdk_path = SDK().path

    if not dartsdk_path:
        _logger.debug("`dartsdk_path` missing; aborting pub")
        return

    PubThread(view.window(), dartsdk_path, file_name).start()

# TODO(guillermooo): Perhaps we can replace this with Default.exec.AsyncProc.
class PubThread(threading.Thread):
    def __init__(self, window, dartsdk_path, file_name):
        super(PubThread, self).__init__()
        self.daemon = True
        self.window = window
        self.dartsdk_path = dartsdk_path
        self.file_name = file_name

    def run(self):
        working_directory = os.path.dirname(self.file_name)
        pub_path = join(self.dartsdk_path, 'bin', 'pub')

        if is_windows():
            pub_path += '.bat'

        print('pub get %s' % self.file_name)
        proc = subprocess.Popen([pub_path, 'get'],
                                cwd=working_directory,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT,
                                startupinfo=supress_window())

        out, errs = proc.communicate()
        data = out.decode('utf-8')

        if proc.returncode != 0:
            errs = errs.decode('utf-8')
            _logger.error("error running pub: %s\n%s", self.file_name, errs)
            data = 'error running pub: %s\n%s' % (self.file_name, errs)

        sublime.set_timeout(lambda: self.callback(data), 50)

    def format_data(self, data):
        return data.replace('\r', '')

    def callback(self, data):
        panel = OutputPanel('dart.pub')
        panel.write(self.format_data(data))
        panel.show()
<|endoftext|>


<commit_before>from datetime import datetime

from mongoengine import *

from api import tv

def parse_date(date):
    """Parse YYYY-DD-MM date to a Python datetime.datetime() object

    :param date str - The string containing the date to parse.
    :return datetime.datetime
    """
    return datetime.strptime('2015-31-01', '%Y-%d-%m')

class Episode(EmbeddedDocument):
    id = IntField(unique=True)
    name = StringField(required=True, max_length=250)
    air_date = DateTimeField()

    @classmethod
    def from_api_object(cls, episode):
        """Construct an Episode object from a TVDb episode object

        :param episode tvdb_api.Episode - The object to construct
            an Episode object from.
        """
        # The id (order in the season) has to be added manually
        return cls(name=episode['episodename'],
                   air_date=parse_date(episode['firstaired']))

class Season(EmbeddedDocument):
    id = IntField(unique=True, required=True)
    episodes = EmbeddedDocumentListField(Episode)

    @classmethod
    def from_api_object(cls, season, season_id=0):
        """Construct a Season object from a TVDb season object

        :param season tvdb_api.Season - The object to construct a Season from.
        :param season_id int - The season order number in a Show
            (starting from 1, 0 is reserved for Show extras)
        """
        return cls(id=season_id, episodes=[
            Episode.from_api_object(e) for e in season.values()
        ])

class Show(Document):
    tvdb_id = IntField(unique=True, required=True, min_value=0)
    name = StringField(required=True, max_length=250)
    year = IntField(min_value=0)
    seasons = EmbeddedDocumentListField(Season)

    @classmethod
    def from_api(cls, tvdb_id):
        """Create a Show object from TVDb

        :param tvdb_id int - The show's id fetch data from The TV Database.
        """
        api_show = tv[tvdb_id]
        show = cls(tvdb_id=api_show['id'], name=api_show['seriesname'],
                   year=parse_date(api_show['firstaired']).year)
        for season_id, season in enumerate(api_show.values()):
            show.seasons.append(Season.from_api_object(season, season_id))

        return show

    def to_dict_short(self):
        """Custom method to return only selected elements of a Show object.

        Use Show.to_mongo() to get the whole Show's info

        :return dict
        """
        return {
            'tvdb_id': self.tvdb_id,
            'name': self.name,
            'year': self.year,
        }

class User(Document):
    email = EmailField(unique=True)
    display_name = StringField(max_length=250)
    google = StringField(max_length=120, unique=True)
    shows = ListField(ReferenceField(Show))

    def is_following(self, show):
        self.reload()
        return show in self.shows

    def follow_show(self, show):
        """Follow a Show

        :param show Show - The TV show to follow.
        """
        self.update(add_to_set__shows=[show])
<commit_msg>shows: Fix Season and Episode models<commit_after>from datetime import datetime

from mongoengine import *

from api import tv

def parse_date(date):
    """Parse YYYY-DD-MM date to a Python datetime.datetime() object

    :param date str - The string containing the date to parse.
    :return datetime.datetime
    """
    return datetime.strptime('2015-31-01', '%Y-%d-%m')

class Episode(EmbeddedDocument):
    id = IntField()
    name = StringField(required=True, max_length=250)
    air_date = DateTimeField()

    @classmethod
    def from_api_object(cls, episode):
        """Construct an Episode object from a TVDb episode object

        :param episode tvdb_api.Episode - The object to construct
            an Episode object from.
        """
        # The id (order in the season) has to be added manually
        return cls(name=episode['episodename'],
                   air_date=parse_date(episode['firstaired']))

class Season(EmbeddedDocument):
    id = IntField(required=True)
    episodes = EmbeddedDocumentListField(Episode)

    @classmethod
    def from_api_object(cls, season, season_id=0):
        """Construct a Season object from a TVDb season object

        :param season tvdb_api.Season - The object to construct a Season from.
        :param season_id int - The season order number in a Show
            (starting from 1, 0 is reserved for Show extras)
        """
        return cls(id=season_id, episodes=[
            Episode.from_api_object(e) for e in season.values()
        ])

class Show(Document):
    tvdb_id = IntField(unique=True, required=True, min_value=0)
    name = StringField(required=True, max_length=250)
    year = IntField(min_value=0)
    seasons = EmbeddedDocumentListField(Season)

    @classmethod
    def from_api(cls, tvdb_id):
        """Create a Show object from TVDb

        :param tvdb_id int - The show's id fetch data from The TV Database.
        """
        api_show = tv[tvdb_id]
        show = cls(tvdb_id=api_show['id'], name=api_show['seriesname'],
                   year=parse_date(api_show['firstaired']).year)
        for season_id, season in enumerate(api_show.values()):
            show.seasons.append(Season.from_api_object(season, season_id))

        return show

    def to_dict_short(self):
        """Custom method to return only selected elements of a Show object.

        Use Show.to_mongo() to get the whole Show's info

        :return dict
        """
        return {
            'tvdb_id': self.tvdb_id,
            'name': self.name,
            'year': self.year,
        }

class User(Document):
    email = EmailField(unique=True)
    display_name = StringField(max_length=250)
    google = StringField(max_length=120, unique=True)
    shows = ListField(ReferenceField(Show))

    def is_following(self, show):
        self.reload()
        return show in self.shows

    def follow_show(self, show):
        """Follow a Show

        :param show Show - The TV show to follow.
        """
        self.update(add_to_set__shows=[show])
<|endoftext|>


<commit_before># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

from __future__ import absolute_import, unicode_literals

import dataproperty
import logbook
import simplesqlite
from mbstrdecoder import MultiByteStrDecoder

logger = logbook.Logger("pytablewriter")
logger.disable()

def set_logger(is_enable):
    if is_enable != logger.disabled:
        return

    if is_enable:
        logger.enable()
    else:
        logger.disable()

    dataproperty.set_logger(is_enable)
    simplesqlite.set_logger(is_enable)
    try:
        import pytablereader
        pytablereader.set_logger(is_enable)
    except ImportError:
        pass

def set_log_level(log_level):
    """
    Set logging level of this module. Using
    `logbook <http://logbook.readthedocs.io/en/stable/>`__ module for logging.

    :param int log_level:
        One of the log level of
        `logbook <http://logbook.readthedocs.io/en/stable/api/base.html>`__.
        Disabled logging if ``log_level`` is ``logbook.NOTSET``.
    :raises LookupError: If ``log_level`` is an invalid value.
    """

    # validate log level
    logbook.get_level_name(log_level)

    if log_level == logger.level:
        return

    if log_level == logbook.NOTSET:
        set_logger(is_enable=False)
    else:
        set_logger(is_enable=True)

    logger.level = log_level
    dataproperty.set_log_level(log_level)
    simplesqlite.set_log_level(log_level)
    try:
        import pytablereader
        pytablereader.set_log_level(log_level)
    except ImportError:
        pass

class WriterLogger(object):

    @property
    def logger(self):
        return self.__logger

    def __init__(self, writer):
        self.__writer = writer
        self.__logger = logger

        self.logger.debug("created WriterLogger: format={}".format(writer.format_name))

    def logging_start_write(self, extra_message_list=None):
        log_entry_list = [
            self.__get_format_name_message(),
            self.__get_table_name_message(),
            "header={}".format(self.__writer.header_list),
        ]

        try:
            log_entry_list.append("rows={}".format(len(self.__writer.value_matrix)))
        except (TypeError, AttributeError):
            log_entry_list.append("rows=0")

        log_entry_list.append(self.__get_typehint_message())

        if extra_message_list:
            log_entry_list.extend(extra_message_list)

        logger.debug("write table: {}".format(", ".join(log_entry_list)))

    def logging_complete_write(self):
        log_entry_list = [
            self.__get_format_name_message(),
            self.__get_table_name_message(),
        ]

        logger.debug("complete write table: {}".format(", ".join(log_entry_list)))

    def __get_format_name_message(self):
        return "format={:s}".format(self.__writer.format_name)

    def __get_table_name_message(self):
        if self.__writer.table_name:
            table_name = MultiByteStrDecoder(self.__writer.table_name).unicode_str
        else:
            table_name = None

        return "table-name='{}'".format(table_name)

    def __get_typehint_message(self):
        try:
            return "type-hint={}".format([
                type_hint(None).typename for type_hint in self.__writer.type_hint_list
            ])
        except (TypeError, AttributeError):
            return "type-hint=[]"
<commit_msg>Modify a debug message<commit_after># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

from __future__ import absolute_import, unicode_literals

import dataproperty
import logbook
import simplesqlite
from mbstrdecoder import MultiByteStrDecoder

logger = logbook.Logger("pytablewriter")
logger.disable()

def set_logger(is_enable):
    if is_enable != logger.disabled:
        return

    if is_enable:
        logger.enable()
    else:
        logger.disable()

    dataproperty.set_logger(is_enable)
    simplesqlite.set_logger(is_enable)
    try:
        import pytablereader
        pytablereader.set_logger(is_enable)
    except ImportError:
        pass

def set_log_level(log_level):
    """
    Set logging level of this module. Using
    `logbook <http://logbook.readthedocs.io/en/stable/>`__ module for logging.

    :param int log_level:
        One of the log level of
        `logbook <http://logbook.readthedocs.io/en/stable/api/base.html>`__.
        Disabled logging if ``log_level`` is ``logbook.NOTSET``.
    :raises LookupError: If ``log_level`` is an invalid value.
    """

    # validate log level
    logbook.get_level_name(log_level)

    if log_level == logger.level:
        return

    if log_level == logbook.NOTSET:
        set_logger(is_enable=False)
    else:
        set_logger(is_enable=True)

    logger.level = log_level
    dataproperty.set_log_level(log_level)
    simplesqlite.set_log_level(log_level)
    try:
        import pytablereader
        pytablereader.set_log_level(log_level)
    except ImportError:
        pass

class WriterLogger(object):

    @property
    def logger(self):
        return self.__logger

    def __init__(self, writer):
        self.__writer = writer
        self.__logger = logger

        self.logger.debug("created WriterLogger: format={}".format(writer.format_name))

    def logging_start_write(self, extra_message_list=None):
        log_entry_list = [
            self.__get_format_name_message(),
            self.__get_table_name_message(),
            "header={}".format(self.__writer.header_list),
        ]

        try:
            log_entry_list.append("rows={}".format(len(self.__writer.value_matrix)))
        except (TypeError, AttributeError):
            log_entry_list.append("rows=0")

        log_entry_list.append(self.__get_typehint_message())

        if extra_message_list:
            log_entry_list.extend(extra_message_list)

        logger.debug("start write table: {}".format(", ".join(log_entry_list)))

    def logging_complete_write(self):
        log_entry_list = [
            self.__get_format_name_message(),
            self.__get_table_name_message(),
        ]

        logger.debug("complete write table: {}".format(", ".join(log_entry_list)))

    def __get_format_name_message(self):
        return "format={:s}".format(self.__writer.format_name)

    def __get_table_name_message(self):
        if self.__writer.table_name:
            table_name = MultiByteStrDecoder(self.__writer.table_name).unicode_str
        else:
            table_name = None

        return "table-name='{}'".format(table_name)

    def __get_typehint_message(self):
        try:
            return "type-hint={}".format([
                type_hint(None).typename for type_hint in self.__writer.type_hint_list
            ])
        except (TypeError, AttributeError):
            return "type-hint=[]"
<|endoftext|>