Example #1
0
import logging
import projex.rest
import projex.security
import projex.text

from collections import defaultdict
from projex.locks import ReadLocker, ReadWriteLock, WriteLocker
from projex.lazymodule import lazy_import
from projex import funcutil

from .metamodel import MetaModel
from .search import SearchEngine


log = logging.getLogger(__name__)
orb = lazy_import('orb')
errors = lazy_import('orb.errors')


class Model(object):
    """
    Defines the base class type that all database records should inherit from.
    """
    # define the table meta class
    __metaclass__ = MetaModel
    __model__ = False
    __search_engine__ = 'basic'
    __auth__ = None

    def __len__(self):
        return len(self.schema().columns())
Example #2
0
"""
Defines a searching algorithm for searching across multiple tables.
"""
from projex.lazymodule import lazy_import

orb = lazy_import('orb')
pyparsing = lazy_import('pyparsing')


class SearchJoiner(object):
    def __init__(self, text):
        self._text = text

    def __str__(self):
        return self.toString()

    def toString(self):
        return self._text


# ---------------------------------------------------------------------


class SearchTerm(object):
    def __init__(self, engine, text, column=''):
        self._engine = engine
        self._text = text
        self._column = column

    def __str__(self):
        return self.toString()
Example #3
0
import datetime
import logging
import time

from projex.lazymodule import lazy_import
from ..column import Column

# optional imports
try:
    from dateutil import parser as dateutil_parser
except ImportError:
    dateutil_parser = None


log = logging.getLogger(__name__)
orb = lazy_import('orb')
pytz = lazy_import('pytz')


class AbstractDatetimeColumn(Column):

    def __init__(self, defaultFormat='%Y-%m-%d %H:%M:%S', **kwds):
        super(AbstractDatetimeColumn, self).__init__(**kwds)

        self.__defaultFormat = defaultFormat

    def defaultFormat(self):
        """
        Returns the default format for this datetime column.

        :return: <str>
Example #4
0
""" Defines the base database class. """

# ------------------------------------------------------------------------------

import logging

from collections import defaultdict
from multiprocessing.util import register_after_fork
from projex.lazymodule import lazy_import
from projex.text import nativestring as nstr

log = logging.getLogger(__name__)
orb = lazy_import('orb')
errors = lazy_import('orb.errors')


class Database(object):
    """ Contains all the database connectivity information. """
    def __init__(self,
                 connectionType,
                 code='',
                 username='',
                 password='',
                 host=None,
                 port=None,
                 name=None,
                 timeout=20000,
                 credentials=None):

        # define custom properties
        conn = orb.Connection.byName(connectionType)
Example #5
0
""" Defines an indexing system to use when looking up records. """

import logging
import projex.text

from xml.etree import ElementTree
from projex.lazymodule import lazy_import
from projex.text import nativestring as nstr


log = logging.getLogger(__name__)
orb = lazy_import("orb")
errors = lazy_import("orb.errors")


class Index(object):
    """ 
    Defines an indexed way to lookup information from a database.
    Creating an Index generates an object that works like a method, however
    has a preset query built into it, along with caching options.
    """

    def __init__(self, name="", columns=None, unique=False, order=None, cached=False, referenced=False):

        if columns is None:
            columns = []

        self.__name__ = name
        self._schema = None
        self._columnNames = columns
        self._unique = unique
Example #6
0
import datetime
import glob
import logging
import os
import projex.security
import projex.text

from projex.lazymodule import lazy_import
from projex.text import nativestring as nstr
from xml.etree import ElementTree
from xml.parsers.expat import ExpatError

from .settings import Settings

log = logging.getLogger(__name__)
orb = lazy_import('orb')
pytz = lazy_import('pytz')
tzlocal = lazy_import('tzlocal')


class Manager(object):
    _instance = None

    def __init__(self):
        # system wide options
        self._environment = None  # current environment
        self._database = None  # current database
        self._tableclass = None  # base table class (default: orb.Table)
        self._namespace = ''  # current namespace
        self._token = None  # security token -- CREATE NEW TOKEN!!!!
Example #7
0
import logging
import os
import projex.text
import random
import yaml

from projex.lazymodule import lazy_import
from projex import rest

from ..column import Column

log = logging.getLogger(__name__)
pickle = lazy_import('cPickle')
orb = lazy_import('orb')


class BinaryColumn(Column):
    TypeMap = {
        'Postgres': 'TEXT',
        'SQLite': 'BLOB',
        'MySQL': 'TEXT'
    }

    def random(self):
        """
        Returns a random value that fits this column's parameters.

        :return: <variant>
        """
        return os.urandom(16).encode('hex')
Example #8
0
import cPickle

from projex.lazymodule import lazy_import
from orb.caching.datacache import DataCache

redis = lazy_import('redis')
orb = lazy_import('orb')


# noinspection PyAbstractClass
class RedisCache(DataCache):
    """ Base caching object for tracking data caches """
    def __init__(self, host='localhost', port=6379, timeout=0):
        super(RedisCache, self).__init__(timeout)

        # define custom properties
        self._client = redis.StrictRedis(host=host, port=port)

    @staticmethod
    def __key(key):
        return 'ORB({0})'.format(key or '*')

    def expire(self, key=None):
        """
        Expires out all the ORB related keys from the redis cache.

        :param      key | <hashable>
        """
        if key:
            key = self.__key(key)
            self._client.delete(self.__key(key))
Example #9
0
"""
Defines a searching algorithm for searching across multiple tables.
"""
from projex.lazymodule import lazy_import

orb = lazy_import('orb')
pyparsing = lazy_import('pyparsing')


class SearchJoiner(object):
    def __init__(self, text):
        self._text = text

    def __str__(self):
        return self.toString()

    def toString(self):
        return self._text

# ---------------------------------------------------------------------


class SearchTerm(object):
    def __init__(self, engine, text, column=''):
        self._engine = engine
        self._text = text
        self._column = column

    def __str__(self):
        return self.toString()
Example #10
0
"""

import time
import cPickle
import datetime
import decimal
import orb
import orb.errors
import projex.rest

from projex.addon import AddonManager
from projex.lazymodule import lazy_import
from projex.text import nativestring as nstr
from .converter import DataConverter

yaml = lazy_import('yaml')
pytz = lazy_import('pytz')


class DataStore(AddonManager):
    def restore(self, column, db_value):
        """
        Restores the inputted value from the database to a Python value.
        
        :param      column   | <orb.Column>
                    db_value | <variant>
        
        :return     <variant> | python value
        """
        if not column:
            return db_value
Example #11
0
import datetime
import logging
import time

from projex.lazymodule import lazy_import
from ..column import Column

# optional imports
try:
    from dateutil import parser as dateutil_parser
except ImportError:
    dateutil_parser = None

log = logging.getLogger(__name__)
orb = lazy_import('orb')
pytz = lazy_import('pytz')


class AbstractDatetimeColumn(Column):
    def __init__(self, defaultFormat='%Y-%m-%d %H:%M:%S', **kwds):
        super(AbstractDatetimeColumn, self).__init__(**kwds)

        self.__defaultFormat = defaultFormat

    def defaultFormat(self):
        """
        Returns the default format for this datetime column.

        :return: <str>
        """
        return self.__defaultFormat
Example #12
0
"""
Defines methods for aggregation within the database system.
"""

from projex.lazymodule import lazy_import

orb = lazy_import("orb")


class ColumnAggregator(object):
    def __init__(self, type, reference, referenceColumn=None, targetColumn=None, where=None):
        """
        Defines a new column aggregation mechanism.  This class allows
        defining a column on a schema that generates an <orb.QueryAggregate>
        rather than an actual column in the backend database.
        
        :param      type            | <orb.QueryAggregate.Type>
                    reference       | <str>
                    referenceColumn | <str>
                    targetColumn    | <str>
                    where           | <orb.Query> or None
        """
        self._aggregateType = type
        self._reference = reference
        self._referenceColumn = referenceColumn
        self._targetColumn = targetColumn
        self._where = where

    def aggregateType(self):
        """
        Returns the aggregation type for this instance.
Example #13
0
import logging
import os
import projex.text
import random

from ..column import Column
from projex.lazymodule import lazy_import
from projex import rest

log = logging.getLogger(__name__)
pickle = lazy_import('cPickle')
orb = lazy_import('orb')
yaml = lazy_import('yaml')


class BinaryColumn(Column):
    TypeMap = {'Postgres': 'TEXT', 'SQLite': 'BLOB', 'MySQL': 'TEXT'}

    def random(self):
        """
        Returns a random value that fits this column's parameters.

        :return: <variant>
        """
        return os.urandom(16).encode('hex')

    def dbRestore(self, db_value, context=None):
        """
        Converts a stored database value to Python.

        :param py_value: <variant>
Example #14
0
"""
Defines methods for joining columns within the database system.
"""

from projex.lazymodule import lazy_import

orb = lazy_import('orb')


class ColumnJoiner(object):
    def __init__(self,
                 reference,
                 referenceColumn=None,
                 targetColumn=None,
                 where=None):
        self._reference = reference
        self._referenceColumn = referenceColumn
        self._targetColumn = targetColumn
        self._where = where

    def query(self, column):
        """
        Generates the where query for this joiner for the given column.
        
        :param      column | <orb.Column>
        
        :return     <orb.Query>
        """
        where = self.where(column)
        out = where if where is not None else orb.Query()
        out &= orb.Query(self.referenceColumn()) == orb.Query(
Example #15
0
File: qt.py Project: SPRIME01/orb
""" Defines a mapper for converting Qt information to and from the database. """

import binascii
import logging

from projex.text import nativestring as nstr
from projex.lazymodule import lazy_import
from ..converter import DataConverter

log = logging.getLogger(__name__)
QtCore = lazy_import('xqt.QtCore')


class QDataConverter(DataConverter):
    """
    Maps Qt values to standard python values.
    """
    def convert(self, value):
        """
        Converts the inputted value to a Python value.
        
        :param      value | <variant>
        
        :return     <variant>
        """
        val_name = type(value).__name__
        
        if val_name == 'QString':
            return nstr(value.toUtf8())
        elif val_name == 'QVariant':
            return value.toPyObject()
Example #16
0
from projex.lazymodule import lazy_import
from ..sqliteconnection import SQLiteStatement

orb = lazy_import('orb')


class CREATE(SQLiteStatement):
    def __call__(self, model, owner='', includeReferences=True):
        if issubclass(model, orb.Table):
            return self._createTable(model, owner, includeReferences)
        elif issubclass(model, orb.View):
            return self._createView(model, owner, includeReferences)
        else:
            raise orb.errors.OrbError('Cannot create model for type: '.format(type(model)))

    def _createTable(self, model, owner, includeReferences):
        ADD_COLUMN = self.byName('ADD COLUMN')

        add_i18n = []
        add_standard = []

        # divide columns between standard and translatable
        for col in model.schema().columns(recurse=False).values():
            if col.testFlag(col.Flags.Virtual):
                continue

            if not includeReferences and isinstance(col, orb.ReferenceColumn):
                continue

            if col.testFlag(col.Flags.I18n):
                add_i18n.append(col)
Example #17
0
import cPickle

from projex.lazymodule import lazy_import
from orb.caching.datacache import DataCache

redis = lazy_import('redis')
orb = lazy_import('orb')


# noinspection PyAbstractClass
class RedisCache(DataCache):
    """ Base caching object for tracking data caches """
    def __init__(self, host='localhost', port=6379, timeout=0):
        super(RedisCache, self).__init__(timeout)

        # define custom properties
        self._client = redis.StrictRedis(host=host, port=port)

    @staticmethod
    def __key(key):
        return 'ORB({0})'.format(key or '*')

    def expire(self, key=None):
        """
        Expires out all the ORB related keys from the redis cache.

        :param      key | <hashable>
        """
        if key:
            key = self.__key(key)
            self._client.delete(self.__key(key))