Esempio n. 1
0
    def add_order(self, orders):
        """
        Inserts the given order into the database.

        @type  orders: Order|list[Order]
        @param orders: The orders to be added.
        """
        if orders is None:
            raise AttributeError('order argument must not be None')
        with self.engine.contextual_connect(close_with_result=True).begin():
            for order in to_list(orders):
                self.__add_order(order)
Esempio n. 2
0
    def add_order(self, orders):
        """
        Inserts the given order into the database.

        @type  orders: Order|list[Order]
        @param orders: The orders to be added.
        """
        if orders is None:
            raise AttributeError('order argument must not be None')
        with self.engine.contextual_connect(close_with_result = True).begin():
            for order in to_list(orders):
                self.__add_order(order)
Esempio n. 3
0
    def __get_tasks_cond(self, **kwargs):
        tbl_t = self._table_map["task"]

        # Search conditions.
        where = None
        for field in ("id", "order_id", "job_id", "name", "status", "opened", "closed"):
            if field in kwargs:
                cond = None
                for value in to_list(kwargs.get(field)):
                    cond = sa.or_(cond, tbl_t.c[field] == value)
                where = sa.and_(where, cond)

        return where
Esempio n. 4
0
    def __get_conditions(self, **kwargs):
        tbl_h = self._table_map['host']
        where = None

        for field in ('address', 'name', 'protocol', 'tcp_port', 'path',
                      'country', 'city', 'os', 'duration', 'deleted'):
            if kwargs.has_key(field):
                cond = None
                for value in to_list(kwargs.get(field)):
                    cond = sa.or_(cond, tbl_h.c[field] == value)
                where = sa.and_(where, cond)

        return where
Esempio n. 5
0
    def __get_orders_cond(self, **kwargs):
        tbl_o = self._table_map["order"]

        # Search conditions.
        where = None
        for field in ("id", "service", "description", "status", "created_by"):
            values = kwargs.get(field)
            if values is not None:
                cond = None
                for value in to_list(values):
                    cond = sa.or_(cond, tbl_o.c[field].like(value))
                where = sa.and_(where, cond)

        return where
Esempio n. 6
0
    def add_account(self, accounts):
        """
        Adds one or more account instances to the pool.

        :type  accounts: Account|list[Account]
        :param accounts: The account to be added.
        """
        with self.unlock_cond:
            for account in to_list(accounts):
                account.acquired_event.listen(self._on_account_acquired)
                account.released_event.listen(self._on_account_released)
                self.accounts.add(account)
                self.unlocked_accounts.append(account)
            self.unlock_cond.notify_all()
Esempio n. 7
0
    def add_host(self, hosts):
        """
        Inserts the given hosts into the database.

        @type  orders: Host|list[Host]
        @param orders: The hosts to be added.
        @rtype:  Boolean
        @return: True on success, False otherwise.
        """
        if hosts is None:
            raise AttributeError('hosts argument must not be None')
        for host in to_list(hosts):
            self.__add_host(host)
        return True
Esempio n. 8
0
    def save_order(self, orders):
        """
        Updates the given orders in the database. Does nothing if
        the order doesn't exist.

        @type  orders: Order|list[Order]
        @param orders: The order to be saved.
        """
        if orders is None:
            raise AttributeError('order argument must not be None')

        with self.engine.contextual_connect(close_with_result = True).begin():
            for order in to_list(orders):
                self.__save_order(order)
Esempio n. 9
0
    def add_account(self, accounts):
        """
        Adds one or more account instances to the pool.

        @type  accounts: Account|list[Account]
        @param accounts: The account to be added.
        """
        with self.unlock_cond:
            for account in to_list(accounts):
                account.acquired_event.listen(self._on_account_acquired)
                account.released_event.listen(self._on_account_released)
                self.accounts.add(account)
                self.unlocked_accounts.append(account)
            self.unlock_cond.notify_all()
Esempio n. 10
0
    def __get_orders_cond(self, **kwargs):
        tbl_o = self._table_map['order']

        # Search conditions.
        where = None
        for field in ('id', 'service', 'description', 'status', 'created_by'):
            values = kwargs.get(field)
            if values is not None:
                cond = None
                for value in to_list(values):
                    cond = sa.or_(cond, tbl_o.c[field].like(value))
                where = sa.and_(where, cond)

        return where
Esempio n. 11
0
    def save_order(self, orders):
        """
        Updates the given orders in the database. Does nothing if
        the order doesn't exist.

        @type  orders: Order|list[Order]
        @param orders: The order to be saved.
        """
        if orders is None:
            raise AttributeError('order argument must not be None')

        with self.engine.contextual_connect(close_with_result=True).begin():
            for order in to_list(orders):
                self.__save_order(order)
Esempio n. 12
0
    def __get_tasks_cond(self, **kwargs):
        tbl_t = self._table_map['task']

        # Search conditions.
        where = None
        for field in ('id', 'order_id', 'job_id', 'name', 'status', 'opened',
                      'closed'):
            if field in kwargs:
                cond = None
                for value in to_list(kwargs.get(field)):
                    cond = sa.or_(cond, tbl_t.c[field] == value)
                where = sa.and_(where, cond)

        return where
Esempio n. 13
0
    def __get_orders_cond(self, **kwargs):
        tbl_o = self._table_map['order']

        # Search conditions.
        where = None
        for field in ('id', 'service', 'description', 'status', 'created_by'):
            values = kwargs.get(field)
            if values is not None:
                cond = None
                for value in to_list(values):
                    cond = sa.or_(cond, tbl_o.c[field].like(value))
                where = sa.and_(where, cond)

        return where
Esempio n. 14
0
    def add_host(self, hosts):
        """
        Inserts the given hosts into the database.

        @type  orders: Host|list[Host]
        @param orders: The hosts to be added.
        @rtype:  Boolean
        @return: True on success, False otherwise.
        """
        if hosts is None:
            raise AttributeError('hosts argument must not be None')
        for host in to_list(hosts):
            self.__add_host(host)
        return True
Esempio n. 15
0
 def _remove_account(self, accounts):
     """
     :type  accounts: Account|list[Account]
     :param accounts: The accounts to be removed.
     """
     for account in to_list(accounts):
         if account not in self.accounts:
             msg = 'attempt to remove unknown account %s' % account
             raise Exception(msg)
         if account not in self.unlocked_accounts:
             raise Exception('account %s should be unlocked' % account)
         account.acquired_event.disconnect(self._on_account_acquired)
         account.released_event.disconnect(self._on_account_released)
         self.accounts.remove(account)
         self.unlocked_accounts.remove(account)
Esempio n. 16
0
 def _remove_account(self, accounts):
     """
     @type  accounts: Account|list[Account]
     @param accounts: The accounts to be removed.
     """
     for account in to_list(accounts):
         if account not in self.accounts:
             msg = "attempt to remove unknown account %s" % account
             raise Exception(msg)
         if account not in self.unlocked_accounts:
             raise Exception("account %s should be unlocked" % account)
         account.acquired_event.disconnect(self._on_account_acquired)
         account.released_event.disconnect(self._on_account_released)
         self.accounts.remove(account)
         self.unlocked_accounts.remove(account)
Esempio n. 17
0
    def save_host(self, hosts, fields=None):
        """
        Updates the given hosts in the database. Does nothing if
        the host doesn't exist.
        If fields is a tuple of column names, only the fields with
        the given names are updated.

        @type  hosts: Host|list[Host]
        @param hosts: The host to be saved.
        @rtype:  Boolean
        @return: True on success, False otherwise.
        """
        if hosts is None:
            raise AttributeError('hosts argument must not be None')
        for host in to_list(hosts):
            self.__save_host(host, fields, None)
        return True
Esempio n. 18
0
    def save_host(self, hosts, fields = None):
        """
        Updates the given hosts in the database. Does nothing if
        the host doesn't exist.
        If fields is a tuple of column names, only the fields with
        the given names are updated.

        @type  hosts: Host|list[Host]
        @param hosts: The host to be saved.
        @rtype:  Boolean
        @return: True on success, False otherwise.
        """
        if hosts is None:
            raise AttributeError('hosts argument must not be None')
        for host in to_list(hosts):
            self.__save_host(host, fields, None)
        return True
Esempio n. 19
0
    def __get_tasks_cond(self, **kwargs):
        tbl_t = self._table_map['task']

        # Search conditions.
        where = None
        for field in ('id',
                      'order_id',
                      'job_id',
                      'name',
                      'status',
                      'opened',
                      'closed'):
            if field in kwargs:
                cond = None
                for value in to_list(kwargs.get(field)):
                    cond = sa.or_(cond, tbl_t.c[field] == value)
                where = sa.and_(where, cond)

        return where
Esempio n. 20
0
    def get_orders(self, offset = 0, limit = None, **kwargs):
        """
        Returns all orders that match the given criteria.

        @type  offset: int
        @param offset: The offset of the first item to be returned.
        @type  limit: int
        @param limit: The maximum number of items that is returned.
        @type  kwargs: dict
        @param kwargs: The following keys may be used:
                         - id - the id of the order (str)
                         - service - the service name (str)
                         - status - the status (str)
                       All values may also be lists (logical OR).
        @rtype:  list[Order]
        @return: The list of orders.
        """
        tbl_o = self._table_map['order']
        tbl_t = self._table_map['task']

        # Search conditions.
        where = None
        for field in ('id', 'service', 'status'):
            if field in kwargs:
                cond = None
                for value in to_list(kwargs.get(field)):
                    cond = sa.or_(cond, tbl_o.c[field] == value)
                where = sa.and_(where, cond)

        table  = tbl_o.outerjoin(tbl_t, tbl_t.c.order_id == tbl_o.c.id)
        fields = list(tbl_o.c)
        fields.append(sa.func.avg(tbl_t.c.progress).label('avg_progress'))
        select = sa.select(fields,
                           where,
                           from_obj = [table],
                           group_by = [tbl_o.c.id],
                           order_by = [sa.desc(tbl_o.c.id)],
                           offset   = offset,
                           limit    = limit)

        return self.__get_orders_from_query(select)
Esempio n. 21
0
    def __get_conditions(self, **kwargs):
        tbl_h = self._table_map['host']
        where = None

        for field in ('address',
                      'name',
                      'protocol',
                      'tcp_port',
                      'path',
                      'country',
                      'city',
                      'os',
                      'duration',
                      'deleted'):
            if kwargs.has_key(field):
                cond = None
                for value in to_list(kwargs.get(field)):
                    cond = sa.or_(cond, tbl_h.c[field] == value)
                where = sa.and_(where, cond)

        return where
Esempio n. 22
0
    def get_uri(self):
        """
        Returns a URI formatted representation of the host, including all
        of it's attributes except for the name. Uses the
        address, not the name of the host to build the URI.

        @rtype:  str
        @return: A URI.
        """
        url = Url()
        url.protocol = self.get_protocol()
        url.hostname = self.get_address()
        url.port = self.get_tcp_port()
        url.vars = dict((k, to_list(v))
                        for (k, v) in self.get_all().iteritems()
                        if isinstance(v, str) or isinstance(v, list))

        if self.account:
            url.username = self.account.get_name()
            url.password1 = self.account.get_password()
            url.password2 = self.account.authorization_password

        return str(url)
Esempio n. 23
0
def prompt(key,
           message,
           default = None,
           strip   = True,
           check   = None,
           history = None):
    """
    Prompt the user for input. This function is similar to Python's built
    in raw_input, with the following differences:

        - You may specify a default value that is returned if the user
          presses "enter" without entering anything.
        - The user's input is recorded in a config file, and offered
          as the default value the next time this function is used
          (based on the key argument).

    The config file is based around the L{InputHistory}. If a history object
    is not passed in the history argument, a new one will be created.

    The key argument specifies under which name the input is saved in the
    config file.

    The given default value is only used if a default was not found in the
    history.

    The strip argument specifies that the returned value should be stripped
    of whitespace (default).

    The check argument allows for validating the input; if the validation
    fails, the user is prompted again before the value is stored in the
    InputHistory. Example usage::

        def validate(input):
            if len(input) < 4:
                return 'Please enter at least 4 characters!'
        value = prompt('test', 'Enter a value', 'My Default', check = validate)
        print 'You entered:', value

    This leads to the following output::

        Please enter a value [My Default]: abc
        Please enter at least 4 characters!
        Please enter a value [My Default]: Foobar
        You entered: Foobar

    The next time the same code is started, the input 'Foobar' is remembered::

        Please enter a value [Foobar]:        (enters nothing)
        You entered: Foobar

    @type  key: str
    @param key: The key under which to store the input in the L{InputHistory}.
    @type  message: str
    @param message: The user prompt.
    @type  default: str|None
    @param default: The offered default if none was found in the history.
    @type  strip: bool
    @param strip: Whether to remove whitespace from the input.
    @type  check: callable
    @param check: A function that is called for validating the input.
    @type  history: L{InputHistory}|None
    @param history: The history used for recording default values, or None.
    """
    if history is None:
        history = InputHistory()
    default = history.get(key, str(default))
    while True:
        if default is None:
            value = raw_input('%s: ' % message)
        else:
            value = raw_input('%s [%s]: ' % (message, default)) or default
        if strip:
            value = value.strip()
        if not check:
            break
        errors = check(value)
        if errors:
            print '\n'.join(to_list(errors))
        else:
            break
    history.set(key, value)
    return value
Esempio n. 24
0
def prompt(key, message, default=None, strip=True, check=None, history=None):
    """
    Prompt the user for input. This function is similar to Python's built
    in raw_input, with the following differences:

        - You may specify a default value that is returned if the user
          presses "enter" without entering anything.
        - The user's input is recorded in a config file, and offered
          as the default value the next time this function is used
          (based on the key argument).

    The config file is based around the L{InputHistory}. If a history object
    is not passed in the history argument, a new one will be created.

    The key argument specifies under which name the input is saved in the
    config file.

    The given default value is only used if a default was not found in the
    history.

    The strip argument specifies that the returned value should be stripped
    of whitespace (default).

    The check argument allows for validating the input; if the validation
    fails, the user is prompted again before the value is stored in the
    InputHistory. Example usage::

        def validate(input):
            if len(input) < 4:
                return 'Please enter at least 4 characters!'
        value = prompt('test', 'Enter a value', 'My Default', check = validate)
        print 'You entered:', value

    This leads to the following output::

        Please enter a value [My Default]: abc
        Please enter at least 4 characters!
        Please enter a value [My Default]: Foobar
        You entered: Foobar

    The next time the same code is started, the input 'Foobar' is remembered::

        Please enter a value [Foobar]:        (enters nothing)
        You entered: Foobar

    @type  key: str
    @param key: The key under which to store the input in the L{InputHistory}.
    @type  message: str
    @param message: The user prompt.
    @type  default: str|None
    @param default: The offered default if none was found in the history.
    @type  strip: bool
    @param strip: Whether to remove whitespace from the input.
    @type  check: callable
    @param check: A function that is called for validating the input.
    @type  history: L{InputHistory}|None
    @param history: The history used for recording default values, or None.
    """
    if history is None:
        history = InputHistory()
    default = history.get(key, str(default))
    while True:
        if default is None:
            value = raw_input('%s: ' % message)
        else:
            value = raw_input('%s [%s]: ' % (message, default)) or default
        if strip:
            value = value.strip()
        if not check:
            break
        errors = check(value)
        if errors:
            print '\n'.join(to_list(errors))
        else:
            break
    history.set(key, value)
    return value
Esempio n. 25
0
 def testToList(self):
     from Exscript.util.cast import to_list
     self.assertEqual(to_list(None), [None])
     self.assertEqual(to_list([]), [])
     self.assertEqual(to_list('test'), ['test'])
     self.assertEqual(to_list(['test']), ['test'])
Esempio n. 26
0
 def testToList(self):
     from Exscript.util.cast import to_list
     self.assertEqual(to_list(None),     [None])
     self.assertEqual(to_list([]),       [])
     self.assertEqual(to_list('test'),   ['test'])
     self.assertEqual(to_list(['test']), ['test'])