Пример #1
0
    def Insert(self, table, values, id_column='id'):
        columns = ','.join(values.keys())
        placeholder = ','.join('?' * len(values))
        statement = 'INSERT INTO {0} ({1}) VALUES ({2})'.format(table, columns, placeholder)

        try:
            self.cursor.execute(statement, values.values())
        except sqlite3.ProgrammingError as e:
            raise pyaas.error('Problem executing statement: %s', e)
        except sqlite3.IntegrityError as e:
            raise pyaas.error('Integrity error: %s', e)

        if id_column not in values:
            values[id_column] = self.cursor.lastrowid
Пример #2
0
    def Initialize(self):
        # Check if the db exists before we make a connection.  The connection
        # will create the file but it will not yet have a schema.
        exists = os.path.isfile(self.path)

        try:
            self.conn = sqlite3.connect(self.path)
            self.conn.text_factory = str
            self.conn.row_factory  = sqlite3.Row
            self.cursor = self.conn.cursor()
        except sqlite3.OperationalError:
            raise pyaas.error('Unable to open database: %s', self.path)

        if self.schema and not exists:
            schema = self.schema

            # Attempt to find the schema.  This is kind of dumb but it
            # prevents some old code from breaking.  At some point we
            # might could remove this first condition
            if not os.path.isfile(schema):
                schema = os.path.join(pyaas.prefix, self.schema)
            if not os.path.isfile(schema):
                schema = pyaas.paths('etc', self.schema)

            if os.path.isfile(schema):
                logging.debug('Attempt to load schema: %s', schema)
                schema = open(schema, 'rb').read()
                self.cursor.executescript(schema)
                self.conn.commit()
            else:
                logging.debug('Schema not found: %s', schema)
Пример #3
0
    def __init__(self, section='server'):
        self.websockets = set()

        section = pyaas.args.name or section

        if not pyaas.config.has_section(section):
            raise pyaas.error('Invalid instance name: %s', section)

        # get the interface and port to listen on
        self.addr = pyaas.args.address or pyaas.config.get(section, 'address')
        self.port = pyaas.args.port or pyaas.config.getint(section, 'port')

        # load the cookie secret used to encrypt cookies
        cookie_path = pyaas.paths('etc', 'cookie.secret')

        if pyaas.args.newcookie:
            cookie_secret = pyaas.util.generateCookieSecret(cookie_path)
        else:
            try:
                with open(cookie_path, 'rb') as fp:
                    cookie_secret = fp.read(44)
            except IOError:
                cookie_secret = pyaas.util.generateCookieSecret(cookie_path)

        # URI patterns
        if not hasattr(self, 'ssl_options'):
            self.ssl_options = None

        # URI patterns
        if not hasattr(self, 'patterns'):
            self.patterns = []

        # Tornado settings
        self.settings = dict(
            static_path   = pyaas.paths('share', 'static'),
            template_path = pyaas.paths('share', 'templates'),
            cookie_secret = cookie_secret,
            debug         = False
        )

        # useful during development
        if pyaas.args.debug:
            self.settings['debug'] = True

            self.patterns.append(
                ( r'/src/(.*)', pyaas.web.handlers.Source ),
                )

        authModules = pyaas.module.PyaasModule.CLASSES.get('AuthModule', None)
        if authModules:
            for (name,authModule) in authModules.items():
                # extend the patterns and settings accordingly
                self.patterns.extend([
                    ( authModule.URL, authModule ),
                    ( r'/logout', pyaas.web.auth.Logout ),
                    ])

            logging.debug('Setting default login URL to: %s', authModule.URL)
            self.settings['login_url'] = authModule.URL
Пример #4
0
 def Update(self, table, values, id_column='id'):
     _id = values[id_column]
     columns = ','.join(s + '=?' for s in values.keys())
     statement = 'UPDATE {0} SET {1} WHERE id=?'.format(table, columns, _id)
     try:
         self.cursor.execute(statement, values.values() + [_id])
     except sqlite3.ProgrammingError:
         raise pyaas.error('Problem executing statement')
Пример #5
0
    def Listen(self, **kwds):
        # initialize here so patterns and settings can be extended by plugins
        tornado.web.Application.__init__(self, self.patterns, **self.settings)

        if 'ssl_options' not in kwds:
            kwds['ssl_options'] = self.ssl_options

        if 'xheaders' not in kwds:
            kwds['xheaders'] = True

        try:
            self.listen(self.port, self.addr, **kwds)
        except socket.gaierror as e:
            if 8 == e.errno:
                raise pyaas.error('Invalid address specified "%s"' % self.addr)
            raise pyaas.error('Could not listen: %s' % e)
        except socket.error as e:
            raise pyaas.error('Could not listen: %s' % e)

        logging.info('Listening on %s:%d', self.addr, self.port)

        pyaas.ioloop.start()
Пример #6
0
    def loadModule(cls, moduleName):
        classes = cls.CLASSES[cls.__name__]

        try:
            return classes[moduleName]
        except KeyError:
            # try to load the module
            pass

        # then try loading a pyaas module first
        try:
            path = cls.PKG_PATH + '.' + moduleName
            module = __import__(path)
        except ImportError:
            # try loading a user-supplied module next
            try:
                path = moduleName
                module = __import__(path)
            except ImportError:
                raise pyaas.error('Unknown module: %s', moduleName)

        subPackageName = path
        for subPackageName in subPackageName.split('.')[1:]:
            module = getattr(module, subPackageName)

        classname = subPackageName.capitalize()

        moduleClass = getattr(module, classname, None)
        if moduleClass is None:
            try:
                moduleClass = getattr(module, 'Database')
            except AttributeError:
                raise pyaas.error('Bad module: %s', moduleName)

        classes[moduleName] = moduleClass

        return moduleClass
Пример #7
0
    def loadModule(cls, moduleName):
        classes = cls.CLASSES[cls.__name__]

        try:
            return classes[moduleName]
        except KeyError:
            # try to load the module
            pass

        # then try loading a pyaas module first
        try:
            path = cls.PKG_PATH + '.' + moduleName
            module = __import__(path)
        except ImportError:
            # try loading a user-supplied module next
            try:
                path = moduleName
                module = __import__(path)
            except ImportError:
                raise pyaas.error('Unknown module: %s', moduleName)

        subPackageName = path
        for subPackageName in subPackageName.split('.')[1:]:
            module = getattr(module, subPackageName)

        classname = subPackageName.capitalize()

        moduleClass = getattr(module, classname, None)
        if moduleClass is None:
            try:
                moduleClass = getattr(module, 'Database')
            except AttributeError:
                raise pyaas.error('Bad module: %s', moduleName)

        classes[moduleName] = moduleClass

        return moduleClass
Пример #8
0
    def __init__(self, **kwds):
        psycopg2.extensions.string_types.pop(
            psycopg2.extensions.JSON.values[0], None)
        psycopg2.extensions.string_types.pop(
            psycopg2.extensions.JSONARRAY.values[0], None)

        self.schema = kwds.get('schema', None)
        if self.schema is not None:
            del kwds['schema']

        try:
            self.conn = psycopg2.connect(**kwds)
        except psycopg2.OperationalError as e:
            raise pyaas.error('Could not connect to database: %s', e)

        self.cursor = self.conn.cursor(
            cursor_factory=psycopg2.extras.DictCursor)
Пример #9
0
    def __init__(self, entry, *args, **kwds):
        self.entry = entry
        self.args = args
        self.kwds = kwds

        script = pyaas.util.getParent()
        # get the filename of the caller
        script = os.path.basename(script)

        instance = pyaas.args.instance or 'server'

        self.pidfile = '/tmp/pyaas-{}-{}-{}.pid'.format(
            script, entry.func_name, instance)

        if 'start' == pyaas.args.daemon:
            self.start()
        elif 'stop' == pyaas.args.daemon:
            self.stop()
        elif 'restart' == pyaas.args.daemon:
            self.restart()
        else:
            raise pyaas.error('Unknown daemon option')
Пример #10
0
import pyaas

try:
    import redis
except ImportError:
    raise pyaas.error('Missing redis module')


class CacheTx(object):

    def __init__(self, redis):
        self._pipeline = redis.pipeline()

    def __enter__(self):
        return self

    def __exit__(self):
        self.end()

    def put(self, key, field, value):
        self._pipeline.hset(key, field, value)
        return self

    def remove(self, key, field):
        self._pipeline.hdel(key, field)
        return self

    def end(self):
        self._pipeline.execute()
Пример #11
0
import sys
import logging

import pyaas

try:
    import pymongo
    import gridfs
    import bson
    import motor
except ImportError:
    raise pyaas.error('Missing motor (mongodb) module')


class Mongo:
    def __init__(self, server, database, store=None):
        self.mongo = pymongo.Connection(server)
        self.motor = motor.MotorClient(server)

        self.mongo.document_class = bson.SON
        self.motor.document_class = bson.SON

        self.db  = self.mongo[database]
        self.mdb = self.motor[database]
        if store:
            self.fs  = gridfs.GridFS(self.mongo[store])
            self.mfs = motor.MotorGridFS(self.motor[store])
        else:
            self.fs  = None
            self.mfs = None
Пример #12
0
def load(settings=None, namespace=None, prefix=None):
    """
    Call this guy to init pyaas stuffs
    :param settings: Alternative name of ini file to load
    :param namespace: Namespace is used to derive paths, pass '' for an empty namespace
    :param prefix: The root path of the app
    :return: None
    """

    parent = pyaas.util.getParent()
    script_name = os.path.basename(parent)
    script_name = script_name.rsplit('.', 1)[0]

    if prefix is None:
        # get the filename of the caller
        # get the directory name of the file
        prefix = os.path.dirname(parent)

        if prefix.endswith(os.path.sep + 'bin'):
            prefix = os.path.join(prefix, '..')
            prefix = os.path.abspath(prefix)

    prefix = os.path.abspath(prefix)
    if pyaas.prefix != prefix:
        pyaas.prefix = prefix
        logging.debug('Setting prefix to "%s"', pyaas.prefix)

    if namespace is None:
        namespace = script_name

    if namespace != pyaas.namespace:
        pyaas.namespace = namespace
        logging.debug('Setting namespace to "%s"', pyaas.namespace)

    # if settings is not passed in use the supplied or derived namespace
    settings = settings or namespace

    pyaas.args = pyaas.argparser.parse_args()

    pyaas.config = configparser.SafeConfigParser(dict_type=collections.OrderedDict)
    pyaas.config.optionxform = str

    ini_files = [
        pyaas.paths('etc', settings + '.ini'),
        pyaas.paths('etc', settings + '.ini.local')
    ]

    if pyaas.args.ini:
        ini_files.append(pyaas.args.ini)

    try:
        ok = pyaas.config.read(ini_files)
    except configparser.ParsingError as e:
        raise pyaas.error('Unable to parse file: %s', e)

    if not ok:
        raise pyaas.error('Unable to read config file(s): %s', ini_files)

    # setup file log
    file_name = '%s_%s.log' % (script_name, time.strftime('%Y%m%d_%H%M%S'))

    # hack back in single log file option without breaking existing code
    if pyaas.config.has_section('logging'):
        if pyaas.config.has_option('logging', 'append'):
            append = pyaas.config.getboolean('logging', 'append')
            if append:
                file_name = script_name + '.log'

    full_path = pyaas.paths('var', file_name)
    logfile = logging.FileHandler(full_path)
    logfile.setLevel(logging.INFO)

    logfile.setFormatter(
        logging.Formatter(
            fmt = '%(asctime)s %(levelname)-8s %(message)s',
            datefmt = '%Y-%m-%d %H:%M:%S',
            )
        )

    # add the handlers to the logger
    root = logging.getLogger()
    root.addHandler(logfile)

    if pyaas.args.debug:
        root.setLevel(logging.DEBUG)
        logfile.setLevel(logging.DEBUG)

    # call this here if there is no daemon option
    if not hasattr(pyaas.args, 'daemon'):
        pyaas.module.load()

    return
Пример #13
0
import sys
import logging

import pyaas

try:
    import pymongo
    import gridfs
    import bson
    import motor
except ImportError:
    raise pyaas.error('Missing motor (mongodb) module')


class Mongo:
    def __init__(self, server, database, store=None):
        self.mongo = pymongo.Connection(server)
        self.motor = motor.MotorClient(server)

        self.mongo.document_class = bson.SON
        self.motor.document_class = bson.SON

        self.db = self.mongo[database]
        self.mdb = self.motor[database]
        if store:
            self.fs = gridfs.GridFS(self.mongo[store])
            self.mfs = motor.MotorGridFS(self.motor[store])
        else:
            self.fs = None
            self.mfs = None
Пример #14
0
import time
import logging

import pyaas

import tornado.web

try:
    import ldap
except ImportError:
    raise pyaas.error('Missing LDAP module')


class Slap(tornado.web.RequestHandler):
    def get(self):
        next = self.get_argument('next', '/')
        self.render('login.html', next=next)

    def post(self):
        username = self.get_argument('username', '')
        username = tornado.escape.xhtml_escape(username)
        password = self.get_argument('password', '')

        ldap_dn  = pyaas.config.get('slap', 'dn')
        ldap_uri = pyaas.config.get('slap', 'uri')

        try:
            dn = ldap_dn.format(username)
            ldap_server = ldap.initialize(ldap_uri)
            ldap_server.bind_s(dn, password)
Пример #15
0
import time
import logging

import pyaas

import tornado.web

try:
    import ldap
except ImportError:
    raise pyaas.error('Missing LDAP module')


class Slap(tornado.web.RequestHandler):
    def get(self):
        next = self.get_argument('next', '/')
        self.render('login.html', next=next)

    def post(self):
        username = self.get_argument('username', '')
        username = tornado.escape.xhtml_escape(username)
        password = self.get_argument('password', '')

        ldap_dn = pyaas.config.get('slap', 'dn')
        ldap_uri = pyaas.config.get('slap', 'uri')

        try:
            dn = ldap_dn.format(username)
            ldap_server = ldap.initialize(ldap_uri)
            ldap_server.bind_s(dn, password)
Пример #16
0
import os

import pyaas
import logging

try:
    import sqlite3
except ImportError:
    raise pyaas.error('Missing sqlite3 module')


class Sqlite:
    def __init__(self, path=None, schema=None):
        if path is None:
            path = ':memory:'
        else:
            path = pyaas.paths('var', path)
            # TODO: make any missing directories

        self.path = path
        self.schema = schema


    def Initialize(self):
        # Check if the db exists before we make a connection.  The connection
        # will create the file but it will not yet have a schema.
        exists = os.path.isfile(self.path)

        try:
            self.conn = sqlite3.connect(self.path)
Пример #17
0
import os

import pyaas

try:
    import psycopg2
    import psycopg2.extensions
    import psycopg2.extras
except ImportError:
    raise pyaas.error('Missing psycopg2 (Postgresql) module')


class Pgsql:
    def __init__(self, **kwds):
        psycopg2.extensions.string_types.pop(
            psycopg2.extensions.JSON.values[0], None)
        psycopg2.extensions.string_types.pop(
            psycopg2.extensions.JSONARRAY.values[0], None)

        self.schema = kwds.get('schema', None)
        if self.schema is not None:
            del kwds['schema']

        try:
            self.conn = psycopg2.connect(**kwds)
        except psycopg2.OperationalError as e:
            raise pyaas.error('Could not connect to database: %s', e)

        self.cursor = self.conn.cursor(
            cursor_factory=psycopg2.extras.DictCursor)