<commit_before>#
# Copyright (C) 2011 - 2015 Red Hat, Inc.
# Copyright (C) 2011 - 2013 Satoru SATOH <ssato redhat.com>
# License: MIT
#
"""Merge-able dict.

.. versionchanged: 0.5.0
   Convert collections.namedtuple objects to dicts recursively

.. versionadded: 0.3.1
   Added naive and partial implementation of JSON Pointer support

.. note::
   JSON Pointer: http://tools.ietf.org/html/rfc6901
"""
from __future__ import absolute_import

import collections
import functools
import operator
import re

from .compat import iteritems, UserDict, OrderedDict
from anyconfig.utils import is_iterable

MS_REPLACE = "replace"
MS_NO_REPLACE = "noreplace"
MS_DICTS = "merge_dicts"
MS_DICTS_AND_LISTS = "merge_dicts_and_lists"

MERGE_STRATEGIES = (MS_REPLACE, MS_NO_REPLACE, MS_DICTS, MS_DICTS_AND_LISTS)

PATH_SEPS = ('/', '.')

_JSNP_GET_ARRAY_IDX_REG = re.compile(r"(?:0|[1-9][0-9]*)")
_JSNP_SET_ARRAY_IDX = re.compile(r"(?:0|[1-9][0-9]*|-)")

NAMEDTUPLE_CLS_KEY = "_namedtuple_cls_"

def jsnp_unescape(jsn_s):
    """
    Parse and decode given encoded JSON Pointer expression, convert ~1 to
    / and ~0 to ~.

    >>> jsnp_unescape("/a~1b")
    '/a/b'
    >>> jsnp_unescape("~1aaa~1~0bbb")
    '/aaa/~bbb'
    """
    return jsn_s.replace('~1', '/').replace('~0', '~')

def parse_path(path, seps=PATH_SEPS):
    """
    Parse path expression and return list of path items.

    :param path: Path expression may contain separator chars.
    :param seps: Separator char candidates.

    :return: A list of keys to fetch object[s] later.

    >>> parse_path('')
    []
    >>> parse_path('/')  # JSON Pointer spec expects this behavior.
    ['']
    >>> parse_path('/a') == parse_path('.a') == ['a']
    True
    >>> parse_path('a') == parse_path('a.') == ['a']
    True
    >>> parse_path('/a/b/c') == parse_path('a.b.c') == ['a', 'b', 'c']
    True
    >>> parse_path('abc')
    ['abc']
    """
    if not path:
        return []

    for sep in seps:
        if sep in path:
            if path == sep:  # Special case, '/' or '.' only.
                return ['']
            return [x for x in path.split(sep) if x]

    return [path]

def get(dic, path, seps=PATH_SEPS):
    """
    getter for nested dicts.

    .. versionchanged: 0.3.1
       Added naive and partial implementation of JSON Pointer support

    :param dic: Dict or dict-like object
    :param path: Path expression to point object wanted
    :param seps: Separator char candidates

    :return: A tuple of (result_object, error_message)

    >>> d = {'a': {'b': {'c': 0, 'd': [1, 2]}}, '': 3}
    >>> get(d, '/')  # key becomes '' (empty string).
    (3, '')
    >>> get(d, "/a/b/c")
    (0, '')
    >>> sorted(get(d, "a.b")[0].items())
    [('c', 0), ('d', [1, 2])]
    >>> (get(d, "a.b.d"), get(d, "/a/b/d/1"))
    (([1, 2], ''), (2, ''))
    >>> get(d, "a.b.key_not_exist")  # doctest: +ELLIPSIS
    (None, "'...'")
    >>> get(d, "/a/b/d/2")
    (None, 'list index out of range')
    >>> get(d, "/a/b/d/-")  # doctest: +ELLIPSIS
    (None, 'list indices must be integers...')
    """
    items = [jsnp_unescape(p) for p in parse_path(path, seps)]
    if not items:
        return (dic, '')
    try:
        if len(items) == 1:
            return (dic[items[0]], '')

        parent = functools.reduce(operator.getitem, items[:-1], dic)

        if is_iterable(parent) and _JSNP_GET_ARRAY_IDX_REG.match(items[-1]):
            return (parent[int(items[-1])], '')
        else:
            return (parent[items[-1]], '')

    except (TypeError, KeyError, IndexError) as exc:
        return (None, str(exc))

def mk_nested_dic(path, val, seps=PATH_SEPS):
    """
    Make a nested dict iteratively.

    :param path: Path expression to make a nested dict
    :param val: Value to set
    :param seps: Separator char candidates

    >>> mk_nested_dic("a.b.c", 1)
    {'a': {'b': {'c': 1}}}
    >>> mk_nested_dic("/a/b/c", 1)
    {'a': {'b': {'c': 1}}}
    """
    ret = None
    for key in reversed(parse_path(path, seps)):
        ret = {key: val if ret is None else ret.copy()}

    return ret

def set_(dic, path, val, seps=PATH_SEPS, strategy=None):
    """
    setter for nested dicts.

    :param dic: MergeableDict instance or other dict-like objects support
        recursive merge operations.
    :param path: Path expression to point object wanted
    :param seps: Separator char candidates.

    >>> d = MergeableDict.create(dict(a=1, b=dict(c=2, )))
    >>> set_(d, 'a.b.d', 3)
    >>> d['a']['b']['d']
    3
    """
    diff = mk_nested_dic(path, val, seps)
    dic.update(diff, strategy)

def is_dict_like(obj):
    """
    :param obj: Any object may be an instance of MergeableDict or dict.

    >>> is_dict_like("a string")
    False
    >>> is_dict_like({})
    True
    >>> is_dict_like(create_from({}))
    True
    """
    return isinstance(obj, (dict, collections.Mapping, UserDict))

def is_namedtuple(obj):
    """
    >>> p0 = collections.namedtuple("Point", "x y")(1, 2)
    >>> is_namedtuple(p0)
    True
    >>> is_namedtuple(tuple(p0))
    False
    """
    return isinstance(obj, tuple) and hasattr(obj, "_asdict")

def convert_to(obj, to_namedtuple=False,
               _namedtuple_cls_key=NAMEDTUPLE_CLS_KEY):
    """
    Convert a OrderedMergeableDict or MergeableDict instances to a dict or
    namedtuple object recursively.

    Borrowed basic idea and implementation from bunch.unbunchify.
    (bunch is distributed under MIT license same as this module.)

    .. note::
       If `to_namedtuple` is True and given object `obj` is MergeableDict
       (! OrderedMergeableDict), then the order of fields of result namedtuple
       object may be random and not stable because the order of MergeableDict
       may not be kept and stable.

    .. note::
       namedtuple object cannot have fields start with '_' because it may be
       conflicts with special methods of object like '__class__'. So it'll fail
       if to convert dicts has such keys.

    :param obj: An [Ordered]MergeableDict instance or other object
    :param to_namedtuple:
        Convert `obj` to namedtuple object of which definition is created on
        the fly if True instead of dict.
    :param _namedtuple_cls_key:
        Special keyword to embedded the class name of namedtuple object to
        create.  See the comments in :func:`create_from` also.

    :return: A dict or namedtuple object if to_namedtuple is True
    """
    if is_dict_like(obj):
        if to_namedtuple:
            _name = obj.get(_namedtuple_cls_key, "NamedTuple")
            _keys = [k for k in obj.keys() if k != _namedtuple_cls_key]
            _vals = [convert_to(obj[k], to_namedtuple, _namedtuple_cls_key)
                     for k in _keys]
            return collections.namedtuple(_name, _keys)(*_vals)
        else:
            return dict((k, convert_to(v)) for k, v in iteritems(obj))
    elif is_namedtuple(obj):
        if to_namedtuple:
            return obj  # Nothing to do if it's nested n.t. (it should be).
        else:
            return dict((k, convert_to(getattr(obj, k))) for k in obj._fields)
    elif is_iterable(obj):
        return type(obj)(convert_to(v, to_namedtuple, _namedtuple_cls_key)
                         for v in obj)
    else:
        return obj

def create_from(obj, _namedtuple_cls_key=NAMEDTUPLE_CLS_KEY):
    """
    Try creating a MergeableDict instance[s] from a dict or any other objects.

    :param obj: A dict instance
    :param _namedtuple_cls_key:
        Special keyword to embedded the class name of namedtuple object to the
        MergeableDict object created. It's a hack and not elegant but I don't
        think there are another ways to make same namedtuple object from the
        MergeableDict object created from it.
    """
    if is_dict_like(obj):
        return MergeableDict((k, create_from(v)) for k, v in iteritems(obj))
    elif is_namedtuple(obj):
        mdict = OrderedMergeableDict((k, create_from(getattr(obj, k))) for k
                                     in obj._fields)
        mdict[_namedtuple_cls_key] = obj.__class__.__name__
        return mdict
    elif is_iterable(obj):
        return type(obj)(create_from(v) for v in obj)
    else:
        return obj

class MergeableDict(dict):
    """
    Dict based object supports 'merge' operation.
    """

    strategy = MS_DICTS

    @classmethod
    def create(cls, obj):
        """Create an instance from any object."""
        return create_from(obj)

    @classmethod
    def convert_to(cls, mdict):
        """Create an object from MergeableDict instances"""
        return convert_to(mdict)

    def get_strategy(self):
        """Merge strategy"""
        return self.strategy

    def update(self, other, strategy=None, keep=False):
        """Update members recursively based on given strategy.

        :param other: Other MergeableDict instance to merge
        :param strategy: Merge strategy
        :param keep:
            Keep original value if type of original value is not a dict nor
            list.  It will be simply replaced with new value by default.
        """
        if strategy is None:
            strategy = self.get_strategy()

        if strategy == MS_REPLACE:
            self.update_w_replace(other)
        elif strategy == MS_NO_REPLACE:
            self.update_wo_replace(other)
        elif strategy == MS_DICTS_AND_LISTS:
            self.update_w_merge(other, merge_lists=True, keep=keep)
        else:
            self.update_w_merge(other, merge_lists=False, keep=keep)

    def update_w_replace(self, other):
        """Update and replace self w/ other if both has same keys.

        :param other: object of which type is same as self's.

        >>> md0 = MergeableDict.create(dict(a=1, b=[1, 3], c="abc"))
        >>> md1 = MergeableDict.create(dict(a=2, b=[0, 1], c="xyz"))
        >>> md0.update_w_replace(md1)
        >>> all(md0[k] == md1[k] for k in ("a", "b", "c"))
        True
        """
        if is_dict_like(other):
            for key, val in iteritems(other):
                self[key] = val

    def update_wo_replace(self, other):
        """Update self w/ other but never replace self w/ other.

        :param other: object of which type is same as self's.

        >>> md0 = md1 = MergeableDict.create(dict(a=1, b=[1, 3], c="abc"))
        >>> md2 = MergeableDict.create(dict(a=2, b=[0, 1], c="xyz", d=None))
        >>> md0.update_wo_replace(md2)
        >>> all(md0[k] != md2[k] for k in ("a", "b", "c"))
        True
        >>> all(md0[k] == md1[k] for k in ("a", "b", "c"))
        True
        >>> md0["d"] == md2["d"]
        True
        """
        if is_dict_like(other):
            for key, val in iteritems(other):
                if key not in self:
                    self[key] = val

    def update_w_merge(self, other, merge_lists=False, keep=False):
        """
        Merge members recursively. Behavior of merge will be vary depends on
        types of original and new values.

        - dict vs. dict -> merge recursively
        - list vs. list -> vary depends on `merge_lists`. see its description.
        - other objects vs. any -> vary depends on `keep`. see its description.

        :param merge_lists: Merge not only dicts but also lists. For example,

            [1, 2, 3], [3, 4] ==> [1, 2, 3, 4]
            [1, 2, 2], [2, 4] ==> [1, 2, 2, 4]

        :param keep:
            Keep original value if type of original value is not a dict nor
            list.  It will be simply replaced with new value by default.

        >>> md0 = md1 = MergeableDict.create(dict(a=1, b=dict(c=2, d=3),
        ...                                       e=[1, 2, 2]))
        >>> md2 = MergeableDict.create(dict(a=2, b=dict(d=4, f=5),
        ...                                 e=[2, 3, 4]))
        >>> md0.update_w_merge(md2, False)
        >>> md0["a"] == md2["a"]
        True
        >>> md0["b"]["d"] == md2["b"]["d"] and md0["b"]["f"] == md2["b"]["f"]
        True
        >>> md0["e"] == md2["e"]
        True

        >>> md3 = MergeableDict.create(dict(aaa=[1, 2, 3], ))
        >>> md4 = MergeableDict.create(dict(aaa=[4, 4, 5], ))
        >>> md3.update_w_merge(md4, True)
        >>> md3["aaa"] == [1, 2, 3, 4, 4, 5]
        True
        """
        if not is_dict_like(other):
            return

        for key, val in iteritems(other):
            val0 = self.get(key, None)  # Original value

            if val0 is None:
                self[key] = val
                continue

            if is_dict_like(val0):  # It needs recursive updates.
                self[key].update_w_merge(val, merge_lists, keep)
            elif merge_lists and is_iterable(val) and is_iterable(val0):
                self[key] += [x for x in list(val) if x not in val0]
            elif not keep:
                self[key] = val  # Overwrite it.

class OrderedMergeableDict(OrderedDict, MergeableDict):
    """MergeableDict keeps order of keys.
    """
    pass

# vim:sw=4:ts=4:et:
<commit_msg>fix: correct version in the changelog as 0.5.0 is not released<commit_after>#
# Copyright (C) 2011 - 2015 Red Hat, Inc.
# Copyright (C) 2011 - 2013 Satoru SATOH <ssato redhat.com>
# License: MIT
#
"""Merge-able dict.

.. versionchanged: 0.4.99
   Convert collections.namedtuple objects to dicts recursively

.. versionadded: 0.3.1
   Added naive and partial implementation of JSON Pointer support

.. note::
   JSON Pointer: http://tools.ietf.org/html/rfc6901
"""
from __future__ import absolute_import

import collections
import functools
import operator
import re

from .compat import iteritems, UserDict, OrderedDict
from anyconfig.utils import is_iterable

MS_REPLACE = "replace"
MS_NO_REPLACE = "noreplace"
MS_DICTS = "merge_dicts"
MS_DICTS_AND_LISTS = "merge_dicts_and_lists"

MERGE_STRATEGIES = (MS_REPLACE, MS_NO_REPLACE, MS_DICTS, MS_DICTS_AND_LISTS)

PATH_SEPS = ('/', '.')

_JSNP_GET_ARRAY_IDX_REG = re.compile(r"(?:0|[1-9][0-9]*)")
_JSNP_SET_ARRAY_IDX = re.compile(r"(?:0|[1-9][0-9]*|-)")

NAMEDTUPLE_CLS_KEY = "_namedtuple_cls_"

def jsnp_unescape(jsn_s):
    """
    Parse and decode given encoded JSON Pointer expression, convert ~1 to
    / and ~0 to ~.

    >>> jsnp_unescape("/a~1b")
    '/a/b'
    >>> jsnp_unescape("~1aaa~1~0bbb")
    '/aaa/~bbb'
    """
    return jsn_s.replace('~1', '/').replace('~0', '~')

def parse_path(path, seps=PATH_SEPS):
    """
    Parse path expression and return list of path items.

    :param path: Path expression may contain separator chars.
    :param seps: Separator char candidates.

    :return: A list of keys to fetch object[s] later.

    >>> parse_path('')
    []
    >>> parse_path('/')  # JSON Pointer spec expects this behavior.
    ['']
    >>> parse_path('/a') == parse_path('.a') == ['a']
    True
    >>> parse_path('a') == parse_path('a.') == ['a']
    True
    >>> parse_path('/a/b/c') == parse_path('a.b.c') == ['a', 'b', 'c']
    True
    >>> parse_path('abc')
    ['abc']
    """
    if not path:
        return []

    for sep in seps:
        if sep in path:
            if path == sep:  # Special case, '/' or '.' only.
                return ['']
            return [x for x in path.split(sep) if x]

    return [path]

def get(dic, path, seps=PATH_SEPS):
    """
    getter for nested dicts.

    .. versionchanged: 0.3.1
       Added naive and partial implementation of JSON Pointer support

    :param dic: Dict or dict-like object
    :param path: Path expression to point object wanted
    :param seps: Separator char candidates

    :return: A tuple of (result_object, error_message)

    >>> d = {'a': {'b': {'c': 0, 'd': [1, 2]}}, '': 3}
    >>> get(d, '/')  # key becomes '' (empty string).
    (3, '')
    >>> get(d, "/a/b/c")
    (0, '')
    >>> sorted(get(d, "a.b")[0].items())
    [('c', 0), ('d', [1, 2])]
    >>> (get(d, "a.b.d"), get(d, "/a/b/d/1"))
    (([1, 2], ''), (2, ''))
    >>> get(d, "a.b.key_not_exist")  # doctest: +ELLIPSIS
    (None, "'...'")
    >>> get(d, "/a/b/d/2")
    (None, 'list index out of range')
    >>> get(d, "/a/b/d/-")  # doctest: +ELLIPSIS
    (None, 'list indices must be integers...')
    """
    items = [jsnp_unescape(p) for p in parse_path(path, seps)]
    if not items:
        return (dic, '')
    try:
        if len(items) == 1:
            return (dic[items[0]], '')

        parent = functools.reduce(operator.getitem, items[:-1], dic)

        if is_iterable(parent) and _JSNP_GET_ARRAY_IDX_REG.match(items[-1]):
            return (parent[int(items[-1])], '')
        else:
            return (parent[items[-1]], '')

    except (TypeError, KeyError, IndexError) as exc:
        return (None, str(exc))

def mk_nested_dic(path, val, seps=PATH_SEPS):
    """
    Make a nested dict iteratively.

    :param path: Path expression to make a nested dict
    :param val: Value to set
    :param seps: Separator char candidates

    >>> mk_nested_dic("a.b.c", 1)
    {'a': {'b': {'c': 1}}}
    >>> mk_nested_dic("/a/b/c", 1)
    {'a': {'b': {'c': 1}}}
    """
    ret = None
    for key in reversed(parse_path(path, seps)):
        ret = {key: val if ret is None else ret.copy()}

    return ret

def set_(dic, path, val, seps=PATH_SEPS, strategy=None):
    """
    setter for nested dicts.

    :param dic: MergeableDict instance or other dict-like objects support
        recursive merge operations.
    :param path: Path expression to point object wanted
    :param seps: Separator char candidates.

    >>> d = MergeableDict.create(dict(a=1, b=dict(c=2, )))
    >>> set_(d, 'a.b.d', 3)
    >>> d['a']['b']['d']
    3
    """
    diff = mk_nested_dic(path, val, seps)
    dic.update(diff, strategy)

def is_dict_like(obj):
    """
    :param obj: Any object may be an instance of MergeableDict or dict.

    >>> is_dict_like("a string")
    False
    >>> is_dict_like({})
    True
    >>> is_dict_like(create_from({}))
    True
    """
    return isinstance(obj, (dict, collections.Mapping, UserDict))

def is_namedtuple(obj):
    """
    >>> p0 = collections.namedtuple("Point", "x y")(1, 2)
    >>> is_namedtuple(p0)
    True
    >>> is_namedtuple(tuple(p0))
    False
    """
    return isinstance(obj, tuple) and hasattr(obj, "_asdict")

def convert_to(obj, to_namedtuple=False,
               _namedtuple_cls_key=NAMEDTUPLE_CLS_KEY):
    """
    Convert a OrderedMergeableDict or MergeableDict instances to a dict or
    namedtuple object recursively.

    Borrowed basic idea and implementation from bunch.unbunchify.
    (bunch is distributed under MIT license same as this module.)

    .. note::
       If `to_namedtuple` is True and given object `obj` is MergeableDict
       (! OrderedMergeableDict), then the order of fields of result namedtuple
       object may be random and not stable because the order of MergeableDict
       may not be kept and stable.

    .. note::
       namedtuple object cannot have fields start with '_' because it may be
       conflicts with special methods of object like '__class__'. So it'll fail
       if to convert dicts has such keys.

    :param obj: An [Ordered]MergeableDict instance or other object
    :param to_namedtuple:
        Convert `obj` to namedtuple object of which definition is created on
        the fly if True instead of dict.
    :param _namedtuple_cls_key:
        Special keyword to embedded the class name of namedtuple object to
        create.  See the comments in :func:`create_from` also.

    :return: A dict or namedtuple object if to_namedtuple is True
    """
    if is_dict_like(obj):
        if to_namedtuple:
            _name = obj.get(_namedtuple_cls_key, "NamedTuple")
            _keys = [k for k in obj.keys() if k != _namedtuple_cls_key]
            _vals = [convert_to(obj[k], to_namedtuple, _namedtuple_cls_key)
                     for k in _keys]
            return collections.namedtuple(_name, _keys)(*_vals)
        else:
            return dict((k, convert_to(v)) for k, v in iteritems(obj))
    elif is_namedtuple(obj):
        if to_namedtuple:
            return obj  # Nothing to do if it's nested n.t. (it should be).
        else:
            return dict((k, convert_to(getattr(obj, k))) for k in obj._fields)
    elif is_iterable(obj):
        return type(obj)(convert_to(v, to_namedtuple, _namedtuple_cls_key)
                         for v in obj)
    else:
        return obj

def create_from(obj, _namedtuple_cls_key=NAMEDTUPLE_CLS_KEY):
    """
    Try creating a MergeableDict instance[s] from a dict or any other objects.

    :param obj: A dict instance
    :param _namedtuple_cls_key:
        Special keyword to embedded the class name of namedtuple object to the
        MergeableDict object created. It's a hack and not elegant but I don't
        think there are another ways to make same namedtuple object from the
        MergeableDict object created from it.
    """
    if is_dict_like(obj):
        return MergeableDict((k, create_from(v)) for k, v in iteritems(obj))
    elif is_namedtuple(obj):
        mdict = OrderedMergeableDict((k, create_from(getattr(obj, k))) for k
                                     in obj._fields)
        mdict[_namedtuple_cls_key] = obj.__class__.__name__
        return mdict
    elif is_iterable(obj):
        return type(obj)(create_from(v) for v in obj)
    else:
        return obj

class MergeableDict(dict):
    """
    Dict based object supports 'merge' operation.
    """

    strategy = MS_DICTS

    @classmethod
    def create(cls, obj):
        """Create an instance from any object."""
        return create_from(obj)

    @classmethod
    def convert_to(cls, mdict):
        """Create an object from MergeableDict instances"""
        return convert_to(mdict)

    def get_strategy(self):
        """Merge strategy"""
        return self.strategy

    def update(self, other, strategy=None, keep=False):
        """Update members recursively based on given strategy.

        :param other: Other MergeableDict instance to merge
        :param strategy: Merge strategy
        :param keep:
            Keep original value if type of original value is not a dict nor
            list.  It will be simply replaced with new value by default.
        """
        if strategy is None:
            strategy = self.get_strategy()

        if strategy == MS_REPLACE:
            self.update_w_replace(other)
        elif strategy == MS_NO_REPLACE:
            self.update_wo_replace(other)
        elif strategy == MS_DICTS_AND_LISTS:
            self.update_w_merge(other, merge_lists=True, keep=keep)
        else:
            self.update_w_merge(other, merge_lists=False, keep=keep)

    def update_w_replace(self, other):
        """Update and replace self w/ other if both has same keys.

        :param other: object of which type is same as self's.

        >>> md0 = MergeableDict.create(dict(a=1, b=[1, 3], c="abc"))
        >>> md1 = MergeableDict.create(dict(a=2, b=[0, 1], c="xyz"))
        >>> md0.update_w_replace(md1)
        >>> all(md0[k] == md1[k] for k in ("a", "b", "c"))
        True
        """
        if is_dict_like(other):
            for key, val in iteritems(other):
                self[key] = val

    def update_wo_replace(self, other):
        """Update self w/ other but never replace self w/ other.

        :param other: object of which type is same as self's.

        >>> md0 = md1 = MergeableDict.create(dict(a=1, b=[1, 3], c="abc"))
        >>> md2 = MergeableDict.create(dict(a=2, b=[0, 1], c="xyz", d=None))
        >>> md0.update_wo_replace(md2)
        >>> all(md0[k] != md2[k] for k in ("a", "b", "c"))
        True
        >>> all(md0[k] == md1[k] for k in ("a", "b", "c"))
        True
        >>> md0["d"] == md2["d"]
        True
        """
        if is_dict_like(other):
            for key, val in iteritems(other):
                if key not in self:
                    self[key] = val

    def update_w_merge(self, other, merge_lists=False, keep=False):
        """
        Merge members recursively. Behavior of merge will be vary depends on
        types of original and new values.

        - dict vs. dict -> merge recursively
        - list vs. list -> vary depends on `merge_lists`. see its description.
        - other objects vs. any -> vary depends on `keep`. see its description.

        :param merge_lists: Merge not only dicts but also lists. For example,

            [1, 2, 3], [3, 4] ==> [1, 2, 3, 4]
            [1, 2, 2], [2, 4] ==> [1, 2, 2, 4]

        :param keep:
            Keep original value if type of original value is not a dict nor
            list.  It will be simply replaced with new value by default.

        >>> md0 = md1 = MergeableDict.create(dict(a=1, b=dict(c=2, d=3),
        ...                                       e=[1, 2, 2]))
        >>> md2 = MergeableDict.create(dict(a=2, b=dict(d=4, f=5),
        ...                                 e=[2, 3, 4]))
        >>> md0.update_w_merge(md2, False)
        >>> md0["a"] == md2["a"]
        True
        >>> md0["b"]["d"] == md2["b"]["d"] and md0["b"]["f"] == md2["b"]["f"]
        True
        >>> md0["e"] == md2["e"]
        True

        >>> md3 = MergeableDict.create(dict(aaa=[1, 2, 3], ))
        >>> md4 = MergeableDict.create(dict(aaa=[4, 4, 5], ))
        >>> md3.update_w_merge(md4, True)
        >>> md3["aaa"] == [1, 2, 3, 4, 4, 5]
        True
        """
        if not is_dict_like(other):
            return

        for key, val in iteritems(other):
            val0 = self.get(key, None)  # Original value

            if val0 is None:
                self[key] = val
                continue

            if is_dict_like(val0):  # It needs recursive updates.
                self[key].update_w_merge(val, merge_lists, keep)
            elif merge_lists and is_iterable(val) and is_iterable(val0):
                self[key] += [x for x in list(val) if x not in val0]
            elif not keep:
                self[key] = val  # Overwrite it.

class OrderedMergeableDict(OrderedDict, MergeableDict):
    """MergeableDict keeps order of keys.
    """
    pass

# vim:sw=4:ts=4:et:
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from bottle import Bottle

class BottleCL(object):
    def __init__(self):
        self._app = Bottle()
        self.init_routes()

    def __call__(self, **kwargs):
        self._app(**kwargs)

    def init_routes(self):
        pass

    def add_route(self, route_path, handler):
        self._app.route(route_path, callback=handler)

    def add_error(self, error_code, handler):
        if not self._app.error_handler:
            self._app.error_handler = {error_code: handler}
        else:
            self._app.error_handler[error_code] = handler

    def start(self, **kwargs):
        self._app.run(**kwargs)
<commit_msg>Fix callable<commit_after># -*- coding: utf-8 -*-

from bottle import Bottle

class BottleCL(object):
    def __init__(self):
        self._app = Bottle()
        self.init_routes()

    def __call__(self, *args):
        self._app(*args)

    def init_routes(self):
        pass

    def add_route(self, route_path, handler):
        self._app.route(route_path, callback=handler)

    def add_error(self, error_code, handler):
        if not self._app.error_handler:
            self._app.error_handler = {error_code: handler}
        else:
            self._app.error_handler[error_code] = handler

    def start(self, **kwargs):
        self._app.run(**kwargs)
<|endoftext|>


<commit_before>
import collections
import struct
import sys

PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

#
# Compatibility helpers
#

if sys.version_info[:2] <= (2, 6):
    # struct.unpack() does not handle bytearray() in Python < 2.7
    def unpack(fmt, value):
        return struct.unpack(fmt, buffer(value))
else:
    unpack = struct.unpack

if PY2:
    def to_bytes(value, size):
        if size <= 8:
            return struct.pack('>Q', value)[-size:]

        if size <= 10:
            return struct.pack(
                '>HQ',
                (value >> 64) & 0xffff,
                value & 0xffffffffffffffff)[-size:]

        # Temporenc values are always 3-10 bytes.
        assert False, "value too large"

else:
    def to_bytes(value, size):
        return value.to_bytes(size, 'big')

#
# Components and types
#
# Composite components like date and time are split to make the
# implementation simpler. Each component is a tuple with these
# components:
#
#   (name, size, mask, min_value, max_value, empty)
#

COMPONENT_YEAR = ('year', 12, 0xfff, 0, 4094, 4095)
COMPONENT_MONTH = ('month', 4, 0xf, 0, 11, 15)
COMPONENT_DAY = ('day', 5, 0x1f, 0, 30, 31)
COMPONENT_HOUR = ('hour', 5, 0x1f, 0, 23, 31)
COMPONENT_MINUTE = ('minute', 6, 0x3f, 0, 59, 63)
COMPONENT_SECOND = ('second', 6, 0x3f, 0, 60, 63)
COMPONENT_MILLISECOND = ('millisecond', 10, 0x3ff, 0, 999, None)
COMPONENT_MICROSECOND = ('microsecond', 20, 0xfffff, 0, 999999, None)
COMPONENT_NANOSECOND = ('nanosecond', 30, 0x3fffffff, 0, 999999999, None)
COMPONENT_PADDING_2 = ('padding', 2, 0x2, 0, 0, None)
COMPONENT_PADDING_4 = ('padding', 4, 0x4, 0, 0, None)
COMPONENT_PADDING_6 = ('padding', 6, 0x6, 0, 0, None)

SUPPORTED_TYPES = set(['D', 'T', 'DT', 'DTZ', 'DTS', 'DTSZ'])

TYPE_D = (COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY)
TYPE_T = (COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND)
TYPE_DT = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND)
TYPE_DTZ = ()  # TODO
TYPE_DTS_MS = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_MILLISECOND, COMPONENT_PADDING_4)
TYPE_DTS_US = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_MICROSECOND, COMPONENT_PADDING_2)
TYPE_DTS_NS = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_NANOSECOND)
TYPE_DTS_NONE = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_PADDING_6)
TYPE_DTSZ = ()  # TODO,

TYPE_DETECTION_MAP = {
    # This maps (D, T, S) tuples of booleans to the most suitable type
    (False, False, False): 'D',
    (False, False, True): 'DTS',
    (False, True, False): 'T',
    (False, True, True): 'DTS',
    (True, False, False): 'D',
    (True, False, True): 'DTS',
    (True, True, False): 'DT',
    (True, True, True): 'DTS',
}

Value = collections.namedtuple('Value', [
    'year', 'month', 'day', 'hour', 'minute', 'second'])

def packb(
        type=None,
        year=None, month=None, day=None,
        hour=None, minute=None, second=None,
        millisecond=None, microsecond=None, nanosecond=None):
    """
    Pack date and time information into a byte string.

    :return: encoded temporenc value
    :rtype: bytes
    """

    if type is None:
        has_d = not (year is None and month is None and day is None)
        has_t = not (hour is None and minute is None and second is None)
        has_s = not (millisecond is None and microsecond is None
                     and nanosecond is None)
        type = TYPE_DETECTION_MAP[has_d, has_t, has_s]
    elif type not in SUPPORTED_TYPES:
        raise ValueError("invalid temporenc type: {0!r}".format(type))

    # Month and day are stored off-by-one.
    if month is not None:
        month -= 1
    if day is not None:
        day -= 1

    padding = 0
    kwargs = locals()  # ugly, but it works :)

    # Byte packing
    if type == 'D':
        typespec = TYPE_D
        n = 0b100
        bits_used = 3

    elif type == 'T':
        typespec = TYPE_T
        n = 0b1010000
        bits_used = 7

    elif type == 'DT':
        typespec = TYPE_DT
        n = 0b00
        bits_used = 2

    elif type == 'DTS':
        bits_used = 4  # combined type tag and precision tag
        if nanosecond is not None:
            n = 0b0110
            typespec = TYPE_DTS_NS
        elif microsecond is not None:
            n = 0b0101
            typespec = TYPE_DTS_US
        elif millisecond is not None:
            n = 0b0100
            typespec = TYPE_DTS_MS
        else:
            n = 0b0111
            typespec = TYPE_DTS_NONE

    else:
        raise NotImplementedError()

    # Pack the components
    for name, size, mask, min_value, max_value, empty in typespec:
        value = kwargs[name]

        if value is None:
            value = empty
        elif not min_value <= value <= max_value:
            raise ValueError(
                "{0} {1:d} not in range [{2:d}, {3:d}]".format(
                    name, value, min_value, max_value))

        bits_used += size
        n <<= size
        n |= value

    assert bits_used % 8 == 0  # FIXME remove once all types are implemented
    return to_bytes(n, bits_used // 8)

def unpackb(value):
    if not 3 <= len(value) <= 10:
        raise ValueError("value must be between 3 and 10 bytes")

    # Individual bytes should be integers.
    if PY2:
        value = bytearray(value)

    # Detect the type and convert the value into a number
    first = value[0]

    if first <= 0b00111111:  # tag 00
        typespec = TYPE_DT
        value = value.rjust(8, b'\x00')
        (n,) = unpack('>Q', value.rjust(8, b'\x00'))

    elif first <= 0b01111111:  # tag 01
        raise NotImplementedError("DTS")

    elif first <= 0b10011111:  # tag 100
        typespec = TYPE_D
        (n,) = unpack('>L', bytes(value.rjust(4, b'\x00')))

    elif first <= 0b10100001:  # tag 1010000
        typespec = TYPE_T
        (n,) = unpack('>L', bytes(value.rjust(4, b'\x00')))

    elif first <= 0b10111111:
        raise ValueError("first byte does not contain a valid tag")

    elif first <= 0b11011111:  # tag 110
        raise NotImplementedError("DTZ")

    elif first <= 0b11111111:  # tag 111
        raise NotImplementedError("DTSZ")

    # Iteratively shift off components from the numerical value
    kwargs = dict.fromkeys(Value._fields)
    for name, size, mask, min_value, max_value, empty in reversed(typespec):
        decoded = n & mask

        if decoded == empty:
            continue

        if not min_value <= decoded <= max_value:
            raise ValueError(
                "{0} {1:d} not in range [{2:d}, {3:d}]".format(
                    name, decoded, min_value, max_value))

        kwargs[name] = decoded
        n >>= size

    # Both month and day are stored off-by-one.
    if kwargs['month'] is not None:
        kwargs['month'] += 1
    if kwargs['day'] is not None:
        kwargs['day'] += 1

    return Value(**kwargs)
<commit_msg>Drop duplicate compat code<commit_after>
import collections
import struct
import sys

PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

#
# Compatibility helpers
#

if sys.version_info[:2] <= (2, 6):
    # struct.unpack() does not handle bytearray() in Python < 2.7
    def unpack(fmt, value):
        return struct.unpack(fmt, buffer(value))
else:
    unpack = struct.unpack

if PY2:
    def to_bytes(value, size):
        if size <= 8:
            return struct.pack('>Q', value)[-size:]

        if size <= 10:
            return struct.pack(
                '>HQ',
                (value >> 64) & 0xffff,
                value & 0xffffffffffffffff)[-size:]

        # Temporenc values are always 3-10 bytes.
        assert False, "value too large"

else:
    def to_bytes(value, size):
        return value.to_bytes(size, 'big')

#
# Components and types
#
# Composite components like date and time are split to make the
# implementation simpler. Each component is a tuple with these
# components:
#
#   (name, size, mask, min_value, max_value, empty)
#

COMPONENT_YEAR = ('year', 12, 0xfff, 0, 4094, 4095)
COMPONENT_MONTH = ('month', 4, 0xf, 0, 11, 15)
COMPONENT_DAY = ('day', 5, 0x1f, 0, 30, 31)
COMPONENT_HOUR = ('hour', 5, 0x1f, 0, 23, 31)
COMPONENT_MINUTE = ('minute', 6, 0x3f, 0, 59, 63)
COMPONENT_SECOND = ('second', 6, 0x3f, 0, 60, 63)
COMPONENT_MILLISECOND = ('millisecond', 10, 0x3ff, 0, 999, None)
COMPONENT_MICROSECOND = ('microsecond', 20, 0xfffff, 0, 999999, None)
COMPONENT_NANOSECOND = ('nanosecond', 30, 0x3fffffff, 0, 999999999, None)
COMPONENT_PADDING_2 = ('padding', 2, 0x2, 0, 0, None)
COMPONENT_PADDING_4 = ('padding', 4, 0x4, 0, 0, None)
COMPONENT_PADDING_6 = ('padding', 6, 0x6, 0, 0, None)

SUPPORTED_TYPES = set(['D', 'T', 'DT', 'DTZ', 'DTS', 'DTSZ'])

TYPE_D = (COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY)
TYPE_T = (COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND)
TYPE_DT = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND)
TYPE_DTZ = ()  # TODO
TYPE_DTS_MS = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_MILLISECOND, COMPONENT_PADDING_4)
TYPE_DTS_US = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_MICROSECOND, COMPONENT_PADDING_2)
TYPE_DTS_NS = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_NANOSECOND)
TYPE_DTS_NONE = (
    COMPONENT_YEAR, COMPONENT_MONTH, COMPONENT_DAY,
    COMPONENT_HOUR, COMPONENT_MINUTE, COMPONENT_SECOND,
    COMPONENT_PADDING_6)
TYPE_DTSZ = ()  # TODO,

TYPE_DETECTION_MAP = {
    # This maps (D, T, S) tuples of booleans to the most suitable type
    (False, False, False): 'D',
    (False, False, True): 'DTS',
    (False, True, False): 'T',
    (False, True, True): 'DTS',
    (True, False, False): 'D',
    (True, False, True): 'DTS',
    (True, True, False): 'DT',
    (True, True, True): 'DTS',
}

Value = collections.namedtuple('Value', [
    'year', 'month', 'day', 'hour', 'minute', 'second'])

def packb(
        type=None,
        year=None, month=None, day=None,
        hour=None, minute=None, second=None,
        millisecond=None, microsecond=None, nanosecond=None):
    """
    Pack date and time information into a byte string.

    :return: encoded temporenc value
    :rtype: bytes
    """

    if type is None:
        has_d = not (year is None and month is None and day is None)
        has_t = not (hour is None and minute is None and second is None)
        has_s = not (millisecond is None and microsecond is None
                     and nanosecond is None)
        type = TYPE_DETECTION_MAP[has_d, has_t, has_s]
    elif type not in SUPPORTED_TYPES:
        raise ValueError("invalid temporenc type: {0!r}".format(type))

    # Month and day are stored off-by-one.
    if month is not None:
        month -= 1
    if day is not None:
        day -= 1

    padding = 0
    kwargs = locals()  # ugly, but it works :)

    # Byte packing
    if type == 'D':
        typespec = TYPE_D
        n = 0b100
        bits_used = 3

    elif type == 'T':
        typespec = TYPE_T
        n = 0b1010000
        bits_used = 7

    elif type == 'DT':
        typespec = TYPE_DT
        n = 0b00
        bits_used = 2

    elif type == 'DTS':
        bits_used = 4  # combined type tag and precision tag
        if nanosecond is not None:
            n = 0b0110
            typespec = TYPE_DTS_NS
        elif microsecond is not None:
            n = 0b0101
            typespec = TYPE_DTS_US
        elif millisecond is not None:
            n = 0b0100
            typespec = TYPE_DTS_MS
        else:
            n = 0b0111
            typespec = TYPE_DTS_NONE

    else:
        raise NotImplementedError()

    # Pack the components
    for name, size, mask, min_value, max_value, empty in typespec:
        value = kwargs[name]

        if value is None:
            value = empty
        elif not min_value <= value <= max_value:
            raise ValueError(
                "{0} {1:d} not in range [{2:d}, {3:d}]".format(
                    name, value, min_value, max_value))

        bits_used += size
        n <<= size
        n |= value

    assert bits_used % 8 == 0  # FIXME remove once all types are implemented
    return to_bytes(n, bits_used // 8)

def unpackb(value):
    if not 3 <= len(value) <= 10:
        raise ValueError("value must be between 3 and 10 bytes")

    # Individual bytes should be integers.
    if PY2:
        value = bytearray(value)

    # Detect the type and convert the value into a number
    first = value[0]

    if first <= 0b00111111:  # tag 00
        typespec = TYPE_DT
        value = value.rjust(8, b'\x00')
        (n,) = unpack('>Q', value.rjust(8, b'\x00'))

    elif first <= 0b01111111:  # tag 01
        raise NotImplementedError("DTS")

    elif first <= 0b10011111:  # tag 100
        typespec = TYPE_D
        (n,) = unpack('>L', value.rjust(4, b'\x00'))

    elif first <= 0b10100001:  # tag 1010000
        typespec = TYPE_T
        (n,) = unpack('>L', value.rjust(4, b'\x00'))

    elif first <= 0b10111111:
        raise ValueError("first byte does not contain a valid tag")

    elif first <= 0b11011111:  # tag 110
        raise NotImplementedError("DTZ")

    elif first <= 0b11111111:  # tag 111
        raise NotImplementedError("DTSZ")

    # Iteratively shift off components from the numerical value
    kwargs = dict.fromkeys(Value._fields)
    for name, size, mask, min_value, max_value, empty in reversed(typespec):
        decoded = n & mask

        if decoded == empty:
            continue

        if not min_value <= decoded <= max_value:
            raise ValueError(
                "{0} {1:d} not in range [{2:d}, {3:d}]".format(
                    name, decoded, min_value, max_value))

        kwargs[name] = decoded
        n >>= size

    # Both month and day are stored off-by-one.
    if kwargs['month'] is not None:
        kwargs['month'] += 1
    if kwargs['day'] is not None:
        kwargs['day'] += 1

    return Value(**kwargs)
<|endoftext|>


<commit_before># Copyright (c) 2011 Openstack, LLC.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
The Zone Aware Scheduler is a base class Scheduler for creating instances
across zones. There are two expansion points to this class for:
1. Assigning Weights to hosts for requested instances
2. Filtering Hosts based on required instance capabilities
"""

import operator
import json

import M2Crypto
import novaclient

from nova import crypto
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova import rpc

from nova.scheduler import api
from nova.scheduler import driver

FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.scheduler.zone_aware_scheduler')

class InvalidBlob(exception.NovaException):
    message = _("Ill-formed or incorrectly routed 'blob' data sent "
                "to instance create request.")

class ZoneAwareScheduler(driver.Scheduler):
    """Base class for creating Zone Aware Schedulers."""

    def _call_zone_method(self, context, method, specs):
        """Call novaclient zone method. Broken out for testing."""
        return api.call_zone_method(context, method, specs=specs)

    def _provision_resource_locally(self, context, item, instance_id, kwargs):
        """Create the requested resource in this Zone."""
        host = item['hostname']
        kwargs['instance_id'] = instance_id
        rpc.cast(context,
                 db.queue_get_for(context, "compute", host),
                 {"method": "run_instance",
                  "args": kwargs})
        LOG.debug(_("Provisioning locally via compute node %(host)s")
                            % locals())

    def _decrypt_blob(self, blob):
        """Returns the decrypted blob or None if invalid. Broken out
        for testing."""
        decryptor = crypto.decryptor(FLAGS.build_plan_encryption_key)
        try:
            json_entry = decryptor(blob)
            return json.dumps(entry)
        except M2Crypto.EVP.EVPError:
            pass
        return None

    def _ask_child_zone_to_create_instance(self, context, zone_info,
                                           request_spec, kwargs):
        """Once we have determined that the request should go to one
        of our children, we need to fabricate a new POST /servers/
        call with the same parameters that were passed into us.

        Note that we have to reverse engineer from our args to get back the
        image, flavor, ipgroup, etc. since the original call could have
        come in from EC2 (which doesn't use these things)."""

        instance_type = request_spec['instance_type']
        instance_properties = request_spec['instance_properties']

        name = instance_properties['display_name']
        image_id = instance_properties['image_id']
        meta = instance_properties['metadata']
        flavor_id = instance_type['flavorid']
        reservation_id = instance_properties['reservation_id']

        files = kwargs['injected_files']
        ipgroup = None  # Not supported in OS API ... yet

        child_zone = zone_info['child_zone']
        child_blob = zone_info['child_blob']
        zone = db.zone_get(context, child_zone)
        url = zone.api_url
        LOG.debug(_("Forwarding instance create call to child zone %(url)s"
                    ". ReservationID=%(reservation_id)s")
                    % locals())
        nova = None
        try:
            nova = novaclient.OpenStack(zone.username, zone.password, None,
                                        url)
            nova.authenticate()
        except novaclient.exceptions.BadRequest, e:
            raise exception.NotAuthorized(_("Bad credentials attempting "
                            "to talk to zone at %(url)s.") % locals())

        nova.servers.create(name, image_id, flavor_id, ipgroup, meta, files,
                            child_blob, reservation_id=reservation_id)

    def _provision_resource_from_blob(self, context, item, instance_id,
                                          request_spec, kwargs):
        """Create the requested resource locally or in a child zone
           based on what is stored in the zone blob info.

           Attempt to decrypt the blob to see if this request is:
           1. valid, and
           2. intended for this zone or a child zone.

           Note: If we have "blob" that means the request was passed
           into us from a parent zone. If we have "child_blob" that
           means we gathered the info from one of our children.
           It's possible that, when we decrypt the 'blob' field, it
           contains "child_blob" data. In which case we forward the
           request."""

        host_info = None
        if "blob" in item:
            # Request was passed in from above. Is it for us?
            host_info = self._decrypt_blob(item['blob'])
        elif "child_blob" in item:
            # Our immediate child zone provided this info ...
            host_info = item

        if not host_info:
            raise InvalidBlob()

        # Valid data ... is it for us?
        if 'child_zone' in host_info and 'child_blob' in host_info:
            self._ask_child_zone_to_create_instance(context, host_info,
                                                    request_spec, kwargs)
        else:
            self._provision_resource_locally(context, host_info,
                                             instance_id, kwargs)

    def _provision_resource(self, context, item, instance_id, request_spec,
                           kwargs):
        """Create the requested resource in this Zone or a child zone."""
        if "hostname" in item:
            self._provision_resource_locally(context, item, instance_id,
                            kwargs)
            return

        self._provision_resource_from_blob(context, item, instance_id,
                                               request_spec, kwargs)

    def schedule_run_instance(self, context, instance_id, request_spec,
                              *args, **kwargs):
        """This method is called from nova.compute.api to provision
        an instance. However we need to look at the parameters being
        passed in to see if this is a request to:
        1. Create a Build Plan and then provision, or
        2. Use the Build Plan information in the request parameters
           to simply create the instance (either in this zone or
           a child zone).
        """

        # TODO(sandy): We'll have to look for richer specs at some point.

        blob = request_spec.get('blob')
        if blob:
            self._provision_resource(context, request_spec, instance_id,
                                    request_spec, kwargs)
            return None

        # Create build plan and provision ...
        build_plan = self.select(context, request_spec)
        if not build_plan:
            raise driver.NoValidHost(_('No hosts were available'))

        for item in build_plan:
            self._provision_resource(context, item, instance_id, request_spec,
                                    kwargs)

        # Returning None short-circuits the routing to Compute (since
        # we've already done it here)
        return None

    def select(self, context, request_spec, *args, **kwargs):
        """Select returns a list of weights and zone/host information
        corresponding to the best hosts to service the request. Any
        child zone information has been encrypted so as not to reveal
        anything about the children.
        """
        return self._schedule(context, "compute", request_spec,
                              *args, **kwargs)

    # TODO(sandy): We're only focused on compute instances right now,
    # so we don't implement the default "schedule()" method required
    # of Schedulers.
    def schedule(self, context, topic, request_spec, *args, **kwargs):
        """The schedule() contract requires we return the one
        best-suited host for this request.
        """
        raise driver.NoValidHost(_('No hosts were available'))

    def _schedule(self, context, topic, request_spec, *args, **kwargs):
        """Returns a list of hosts that meet the required specs,
        ordered by their fitness.
        """

        if topic != "compute":
            raise NotImplemented(_("Zone Aware Scheduler only understands "
                                   "Compute nodes (for now)"))

        #TODO(sandy): how to infer this from OS API params?
        num_instances = 1

        # Filter local hosts based on requirements ...
        host_list = self.filter_hosts(num_instances, request_spec)

        # TODO(sirp): weigh_hosts should also be a function of 'topic' or
        # resources, so that we can apply different objective functions to it

        # then weigh the selected hosts.
        # weighted = [{weight=weight, name=hostname}, ...]
        weighted = self.weigh_hosts(num_instances, request_spec, host_list)

        # Next, tack on the best weights from the child zones ...
        json_spec = json.dumps(request_spec)
        child_results = self._call_zone_method(context, "select",
                specs=json_spec)
        for child_zone, result in child_results:
            for weighting in result:
                # Remember the child_zone so we can get back to
                # it later if needed. This implicitly builds a zone
                # path structure.
                host_dict = {"weight": weighting["weight"],
                             "child_zone": child_zone,
                             "child_blob": weighting["blob"]}
                weighted.append(host_dict)

        weighted.sort(key=operator.itemgetter('weight'))
        return weighted

    def filter_hosts(self, num, request_spec):
        """Derived classes must override this method and return
           a list of hosts in [(hostname, capability_dict)] format.
        """
        # NOTE(sirp): The default logic is the equivalent to AllHostsFilter
        service_states = self.zone_manager.service_states
        return [(host, services)
                for host, services in service_states.iteritems()]

    def weigh_hosts(self, num, request_spec, hosts):
        """Derived classes may override this to provide more sophisticated
        scheduling objectives
        """
        # NOTE(sirp): The default logic is the same as the NoopCostFunction
        return [dict(weight=1, hostname=host) for host, caps in hosts]
<commit_msg>fixed up some little project_id things with new novaclient<commit_after># Copyright (c) 2011 Openstack, LLC.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
The Zone Aware Scheduler is a base class Scheduler for creating instances
across zones. There are two expansion points to this class for:
1. Assigning Weights to hosts for requested instances
2. Filtering Hosts based on required instance capabilities
"""

import operator
import json

import M2Crypto
import novaclient

from nova import crypto
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova import rpc

from nova.scheduler import api
from nova.scheduler import driver

FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.scheduler.zone_aware_scheduler')

class InvalidBlob(exception.NovaException):
    message = _("Ill-formed or incorrectly routed 'blob' data sent "
                "to instance create request.")

class ZoneAwareScheduler(driver.Scheduler):
    """Base class for creating Zone Aware Schedulers."""

    def _call_zone_method(self, context, method, specs):
        """Call novaclient zone method. Broken out for testing."""
        return api.call_zone_method(context, method, specs=specs)

    def _provision_resource_locally(self, context, item, instance_id, kwargs):
        """Create the requested resource in this Zone."""
        host = item['hostname']
        kwargs['instance_id'] = instance_id
        rpc.cast(context,
                 db.queue_get_for(context, "compute", host),
                 {"method": "run_instance",
                  "args": kwargs})
        LOG.debug(_("Provisioning locally via compute node %(host)s")
                            % locals())

    def _decrypt_blob(self, blob):
        """Returns the decrypted blob or None if invalid. Broken out
        for testing."""
        decryptor = crypto.decryptor(FLAGS.build_plan_encryption_key)
        try:
            json_entry = decryptor(blob)
            return json.dumps(entry)
        except M2Crypto.EVP.EVPError:
            pass
        return None

    def _ask_child_zone_to_create_instance(self, context, zone_info,
                                           request_spec, kwargs):
        """Once we have determined that the request should go to one
        of our children, we need to fabricate a new POST /servers/
        call with the same parameters that were passed into us.

        Note that we have to reverse engineer from our args to get back the
        image, flavor, ipgroup, etc. since the original call could have
        come in from EC2 (which doesn't use these things)."""

        instance_type = request_spec['instance_type']
        instance_properties = request_spec['instance_properties']

        name = instance_properties['display_name']
        image_ref = instance_properties['image_ref']
        meta = instance_properties['metadata']
        flavor_id = instance_type['flavorid']
        reservation_id = instance_properties['reservation_id']

        files = kwargs['injected_files']
        ipgroup = None  # Not supported in OS API ... yet

        child_zone = zone_info['child_zone']
        child_blob = zone_info['child_blob']
        zone = db.zone_get(context, child_zone)
        url = zone.api_url
        LOG.debug(_("Forwarding instance create call to child zone %(url)s"
                    ". ReservationID=%(reservation_id)s")
                    % locals())
        nova = None
        try:
            nova = novaclient.OpenStack(zone.username, zone.password, None,
                                        url)
            nova.authenticate()
        except novaclient.exceptions.BadRequest, e:
            raise exception.NotAuthorized(_("Bad credentials attempting "
                            "to talk to zone at %(url)s.") % locals())

        nova.servers.create(name, image_ref, flavor_id, ipgroup, meta, files,
                            child_blob, reservation_id=reservation_id)

    def _provision_resource_from_blob(self, context, item, instance_id,
                                          request_spec, kwargs):
        """Create the requested resource locally or in a child zone
           based on what is stored in the zone blob info.

           Attempt to decrypt the blob to see if this request is:
           1. valid, and
           2. intended for this zone or a child zone.

           Note: If we have "blob" that means the request was passed
           into us from a parent zone. If we have "child_blob" that
           means we gathered the info from one of our children.
           It's possible that, when we decrypt the 'blob' field, it
           contains "child_blob" data. In which case we forward the
           request."""

        host_info = None
        if "blob" in item:
            # Request was passed in from above. Is it for us?
            host_info = self._decrypt_blob(item['blob'])
        elif "child_blob" in item:
            # Our immediate child zone provided this info ...
            host_info = item

        if not host_info:
            raise InvalidBlob()

        # Valid data ... is it for us?
        if 'child_zone' in host_info and 'child_blob' in host_info:
            self._ask_child_zone_to_create_instance(context, host_info,
                                                    request_spec, kwargs)
        else:
            self._provision_resource_locally(context, host_info,
                                             instance_id, kwargs)

    def _provision_resource(self, context, item, instance_id, request_spec,
                           kwargs):
        """Create the requested resource in this Zone or a child zone."""
        if "hostname" in item:
            self._provision_resource_locally(context, item, instance_id,
                            kwargs)
            return

        self._provision_resource_from_blob(context, item, instance_id,
                                               request_spec, kwargs)

    def schedule_run_instance(self, context, instance_id, request_spec,
                              *args, **kwargs):
        """This method is called from nova.compute.api to provision
        an instance. However we need to look at the parameters being
        passed in to see if this is a request to:
        1. Create a Build Plan and then provision, or
        2. Use the Build Plan information in the request parameters
           to simply create the instance (either in this zone or
           a child zone).
        """

        # TODO(sandy): We'll have to look for richer specs at some point.

        blob = request_spec.get('blob')
        if blob:
            self._provision_resource(context, request_spec, instance_id,
                                    request_spec, kwargs)
            return None

        # Create build plan and provision ...
        build_plan = self.select(context, request_spec)
        if not build_plan:
            raise driver.NoValidHost(_('No hosts were available'))

        for item in build_plan:
            self._provision_resource(context, item, instance_id, request_spec,
                                    kwargs)

        # Returning None short-circuits the routing to Compute (since
        # we've already done it here)
        return None

    def select(self, context, request_spec, *args, **kwargs):
        """Select returns a list of weights and zone/host information
        corresponding to the best hosts to service the request. Any
        child zone information has been encrypted so as not to reveal
        anything about the children.
        """
        return self._schedule(context, "compute", request_spec,
                              *args, **kwargs)

    # TODO(sandy): We're only focused on compute instances right now,
    # so we don't implement the default "schedule()" method required
    # of Schedulers.
    def schedule(self, context, topic, request_spec, *args, **kwargs):
        """The schedule() contract requires we return the one
        best-suited host for this request.
        """
        raise driver.NoValidHost(_('No hosts were available'))

    def _schedule(self, context, topic, request_spec, *args, **kwargs):
        """Returns a list of hosts that meet the required specs,
        ordered by their fitness.
        """

        if topic != "compute":
            raise NotImplemented(_("Zone Aware Scheduler only understands "
                                   "Compute nodes (for now)"))

        #TODO(sandy): how to infer this from OS API params?
        num_instances = 1

        # Filter local hosts based on requirements ...
        host_list = self.filter_hosts(num_instances, request_spec)

        # TODO(sirp): weigh_hosts should also be a function of 'topic' or
        # resources, so that we can apply different objective functions to it

        # then weigh the selected hosts.
        # weighted = [{weight=weight, name=hostname}, ...]
        weighted = self.weigh_hosts(num_instances, request_spec, host_list)

        # Next, tack on the best weights from the child zones ...
        json_spec = json.dumps(request_spec)
        child_results = self._call_zone_method(context, "select",
                specs=json_spec)
        for child_zone, result in child_results:
            for weighting in result:
                # Remember the child_zone so we can get back to
                # it later if needed. This implicitly builds a zone
                # path structure.
                host_dict = {"weight": weighting["weight"],
                             "child_zone": child_zone,
                             "child_blob": weighting["blob"]}
                weighted.append(host_dict)

        weighted.sort(key=operator.itemgetter('weight'))
        return weighted

    def filter_hosts(self, num, request_spec):
        """Derived classes must override this method and return
           a list of hosts in [(hostname, capability_dict)] format.
        """
        # NOTE(sirp): The default logic is the equivalent to AllHostsFilter
        service_states = self.zone_manager.service_states
        return [(host, services)
                for host, services in service_states.iteritems()]

    def weigh_hosts(self, num, request_spec, hosts):
        """Derived classes may override this to provide more sophisticated
        scheduling objectives
        """
        # NOTE(sirp): The default logic is the same as the NoopCostFunction
        return [dict(weight=1, hostname=host) for host, caps in hosts]
<|endoftext|>


<commit_before>from __future__ import print_function, absolute_import

import numpy as np

from numba import cuda
from numba.cuda.testing import unittest

def noop(x):
    pass

class TestJitErrors(unittest.TestCase):
    """
    Test compile-time errors with @jit.
    """

    def test_too_many_dims(self):
        kernfunc = cuda.jit(noop)

        with self.assertRaises(ValueError) as raises:
            kernfunc[(1, 2, 3, 4), (5, 6)]
        self.assertIn("griddim must be a sequence of 1, 2 or 3 integers, got [1, 2, 3, 4]",
                      str(raises.exception))

        with self.assertRaises(ValueError) as raises:
            kernfunc[(1, 2,), (3, 4, 5, 6)]
        self.assertIn("blockdim must be a sequence of 1, 2 or 3 integers, got [3, 4, 5, 6]",
                      str(raises.exception))

    def test_non_integral_dims(self):
        kernfunc = cuda.jit(noop)

        with self.assertRaises(TypeError) as raises:
            kernfunc[2.0, 3]
        self.assertIn("griddim must be a sequence of integers, got [2.0]",
                      str(raises.exception))

        with self.assertRaises(TypeError) as raises:
            kernfunc[2, 3.0]
        self.assertIn("blockdim must be a sequence of integers, got [3.0]",
                      str(raises.exception))

if __name__ == '__main__':
    unittest.main()
<commit_msg>Add tests for unconfigured kernel error<commit_after>from __future__ import print_function, absolute_import

import numpy as np

from numba import cuda
from numba.cuda.testing import unittest

def noop(x):
    pass

class TestJitErrors(unittest.TestCase):
    """
    Test compile-time errors with @jit.
    """

    def test_too_many_dims(self):
        kernfunc = cuda.jit(noop)

        with self.assertRaises(ValueError) as raises:
            kernfunc[(1, 2, 3, 4), (5, 6)]
        self.assertIn("griddim must be a sequence of 1, 2 or 3 integers, got [1, 2, 3, 4]",
                      str(raises.exception))

        with self.assertRaises(ValueError) as raises:
            kernfunc[(1, 2,), (3, 4, 5, 6)]
        self.assertIn("blockdim must be a sequence of 1, 2 or 3 integers, got [3, 4, 5, 6]",
                      str(raises.exception))

    def test_non_integral_dims(self):
        kernfunc = cuda.jit(noop)

        with self.assertRaises(TypeError) as raises:
            kernfunc[2.0, 3]
        self.assertIn("griddim must be a sequence of integers, got [2.0]",
                      str(raises.exception))

        with self.assertRaises(TypeError) as raises:
            kernfunc[2, 3.0]
        self.assertIn("blockdim must be a sequence of integers, got [3.0]",
                      str(raises.exception))

    def _test_unconfigured(self, kernfunc):
        with self.assertRaises(ValueError) as raises:
            kernfunc(0)
        self.assertIn("launch configuration was not specified",
                      str(raises.exception))

    def test_unconfigured_cudakernel(self):
        kernfunc = cuda.jit("void(int32)")(noop)
        self._test_unconfigured(kernfunc)

    def test_unconfigured_autojitcudakernel(self):
        kernfunc = cuda.jit(noop)
        self._test_unconfigured(kernfunc)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function, unicode_literals

def xml_get1(node, xpath):
    return node.xpath(xpath)[0]

def xml_get1_or_none(node, xpath):
    try:
        return xml_get1(node, xpath)
    except IndexError:
        return None

def xml_get_text(node, xpath):
    return xml_get1(node, xpath).text

def xml_get_text_or_none(node, xpath):
    try:
        return xml_get_text(node, xpath)
    except IndexError:    # xpath search found 0 matches
        return None

def xml_xpath_text(node, xpath):
    return [n.text for n in node.xpath(xpath)]

def xml_xpath_text_first(node, xpath):
    try:
        return xml_xpath_list_text(node, xpath)[0]
    except IndexError:
        return None

def arglist_to_dict(**args):
    return dict(**args)

a2d = arglist_to_dict

# <LICENSE>
# Copyright 2015 eutils Committers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
# </LICENSE>
<commit_msg>Call name typo (#155)<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function, unicode_literals

def xml_get1(node, xpath):
    return node.xpath(xpath)[0]

def xml_get1_or_none(node, xpath):
    try:
        return xml_get1(node, xpath)
    except IndexError:
        return None

def xml_get_text(node, xpath):
    return xml_get1(node, xpath).text

def xml_get_text_or_none(node, xpath):
    try:
        return xml_get_text(node, xpath)
    except IndexError:    # xpath search found 0 matches
        return None

def xml_xpath_text(node, xpath):
    return [n.text for n in node.xpath(xpath)]

def xml_xpath_text_first(node, xpath):
    try:
        return xml_xpath_text(node, xpath)[0]
    except IndexError:
        return None

def arglist_to_dict(**args):
    return dict(**args)

a2d = arglist_to_dict

# <LICENSE>
# Copyright 2015 eutils Committers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
# </LICENSE>
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import socket
import random
import time
from evazbot.configs import c_net
import select
import re

running = True
channel = "nochannel"
random.seed()
currentuser = ""
outputtext = ""
outputchannel = ""
handled = False
ircsocks = []
currentprofile = -1
adminlist = {}
ircprofiles = []

exec(open("evazbot/configs/c_local/profiles.py").read())

def ircwrite(msg):
    global currentprofile
    ircprofiles[currentprofile]["ircsock"].send(
        msg.encode('latin-1', 'ignore') + b"\n")

def getchannel():
    return channel

def getuser():
    return currentuser

def sendmsg(chan, msg):
    import evazbot.configs.c_modules as c_modules
    global outputtext
    global outputchannel
    outputtext = msg
    outputchannel = chan
    c_modules.event("output")
    time.sleep(0.025)
    ircwrite("PRIVMSG " + chan + " :" + msg)

def sendcmsg(msg):
    if channel == ircprofiles[currentprofile]["nick"]:
        sendmsg(getuser(), msg)
    else:
        sendmsg(channel, msg)

def sendamsg(msg):
    global currentprofile
    for i in ircprofiles[currentprofile]["channels"]:
        sendmsg(i, msg)

def sendsmsg(chan, msg):
    if chan == "all":
        sendamsg(msg)
    else:
        sendmsg(chan, msg)

def joinchan(chan):
    ircwrite("JOIN " + chan)

def whois(n):
    ircwrite("WHOIS " + n)

def ircconnect():
    global currentprofile
    for i in range(len(ircprofiles)):
        currentprofile = i
        ircprofiles[currentprofile]["adminlist"] = dict()
        try:
            ircprofiles[currentprofile]["ircsock"].connect(
                (ircprofiles[currentprofile]["server"], 6667))
            ircwrite('USER %s 8 * :%s\r\n' % (
                ircprofiles[currentprofile]["nick"],
                ircprofiles[currentprofile]["nick"]))
            ircwrite("NICK " + ircprofiles[currentprofile]["nick"])
            time.sleep(0.25)
        except socket.error:
            pass

ircconnect()

def dochannel(ircmsg, i):
    global channel
    if ircmsg.find("PRIVMSG " + i + " :") != -1:
        channel = i
    if (
            ircmsg.endswith("JOIN " + i)
            or ircmsg.endswith("PART " + i)
            or ircmsg.find("PART " + i + " :") != -1
            or ircmsg.find("QUIT :") != -1
    ):
        channel = i

def process(ircmsgp):
    from evazbot.configs import u_commands as cmd
    import evazbot.configs.c_modules as c_modules
    global channel
    global currentuser
    global currentprofile
    ircmsg = ircmsgp
    ircmsg = ircmsg.strip('\n\r')
    if len(ircmsg) > 1:
        print(ircmsg)
        channel = "nochannel"
        if ircmsg.endswith(":+x") or ircmsg.endswith(":+i"):
            for c in ircprofiles[currentprofile]["channels"]:
                joinchan(c)
        for i in ircprofiles[currentprofile]["channels"]:
            dochannel(ircmsg, i)
        dochannel(ircmsg, ircprofiles[currentprofile]["nick"])
        currentuser = cmd.getircuser(ircmsg)
        if ircmsg.find("PING :") == 0:
            c_modules.event("ping", ircmsg)
        elif ircmsg.find("ERROR :") == 0 and\
        ircmsg.find("Ping timeout :") != -1:
            pass
        elif ircmsg.find(":" + ircprofiles[currentprofile]["nick"] + "!") == 0\
        and ircmsg.find("JOIN") != -1:
            c_modules.event("login")
        else:
            global handled
            handled = False
            c_modules.event("text", ircmsg)
            c_modules.event("msg", ircmsg)
            c_modules.event("afterall", ircmsg)

def loop():
    import evazbot.configs.c_modules as c_modules
    global channel
    global currentprofile
    lasttime = time.time()
    while running:
        poll = select.poll()
        for p in range(len(ircprofiles)):
            poll.register(ircprofiles[p]["ircsock"], select.POLLIN)
        ready = poll.poll(0.2)
        time.sleep(0.1)
        if time.time() - lasttime >= 1:
            lasttime = time.time()
            c_modules.event("tick")
        for fd, flag in ready:
            for r in range(len(ircprofiles)):
                if ircprofiles[r]["ircsock"].fileno() == fd:
                    currentprofile = r
                    try:
                        ircmsg = ircprofiles[currentprofile][
                            "ircsock"].recv(2048).decode()
                        regex = re.compile(
                            "\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE)
                        ircmsg = regex.sub("", ircmsg)
                    except socket.error as exc:
                        raise exc
                    msgs = ircmsg.split("\r\n")
                    for i in msgs:
                        process(i)

def ircmain():
    import evazbot.configs.c_modules as c_modules
    c_modules.init()
    c_modules.load()
    print(("Starting " + c_net.name + "..."))
    c_modules.event("start")
    loop()
    c_modules.event("stop")
    for i in range(len(ircprofiles)):
        ircprofiles[i]["ircsock"].close()
<commit_msg>Use select.select() instead of select.poll()<commit_after># -*- coding: utf-8 -*-
import socket
import random
import time
from evazbot.configs import c_net
import select
import re

running = True
channel = "nochannel"
random.seed()
currentuser = ""
outputtext = ""
outputchannel = ""
handled = False
ircsocks = []
currentprofile = -1
adminlist = {}
ircprofiles = []

exec(open("evazbot/configs/c_local/profiles.py").read())

def ircwrite(msg):
    global currentprofile
    ircprofiles[currentprofile]["ircsock"].send(
        msg.encode('latin-1', 'ignore') + b"\n")

def getchannel():
    return channel

def getuser():
    return currentuser

def sendmsg(chan, msg):
    import evazbot.configs.c_modules as c_modules
    global outputtext
    global outputchannel
    outputtext = msg
    outputchannel = chan
    c_modules.event("output")
    time.sleep(0.025)
    ircwrite("PRIVMSG " + chan + " :" + msg)

def sendcmsg(msg):
    if channel == ircprofiles[currentprofile]["nick"]:
        sendmsg(getuser(), msg)
    else:
        sendmsg(channel, msg)

def sendamsg(msg):
    global currentprofile
    for i in ircprofiles[currentprofile]["channels"]:
        sendmsg(i, msg)

def sendsmsg(chan, msg):
    if chan == "all":
        sendamsg(msg)
    else:
        sendmsg(chan, msg)

def joinchan(chan):
    ircwrite("JOIN " + chan)

def whois(n):
    ircwrite("WHOIS " + n)

def ircconnect():
    global currentprofile
    for i in range(len(ircprofiles)):
        currentprofile = i
        ircprofiles[currentprofile]["adminlist"] = dict()
        try:
            ircprofiles[currentprofile]["ircsock"].connect(
                (ircprofiles[currentprofile]["server"], 6667))
            ircwrite('USER %s 8 * :%s\r\n' % (
                ircprofiles[currentprofile]["nick"],
                ircprofiles[currentprofile]["nick"]))
            ircwrite("NICK " + ircprofiles[currentprofile]["nick"])
            time.sleep(0.25)
        except socket.error:
            pass

ircconnect()

def dochannel(ircmsg, i):
    global channel
    if ircmsg.find("PRIVMSG " + i + " :") != -1:
        channel = i
    if (
            ircmsg.endswith("JOIN " + i)
            or ircmsg.endswith("PART " + i)
            or ircmsg.find("PART " + i + " :") != -1
            or ircmsg.find("QUIT :") != -1
    ):
        channel = i

def process(ircmsgp):
    from evazbot.configs import u_commands as cmd
    import evazbot.configs.c_modules as c_modules
    global channel
    global currentuser
    global currentprofile
    ircmsg = ircmsgp
    ircmsg = ircmsg.strip('\n\r')
    if len(ircmsg) > 1:
        print(ircmsg)
        channel = "nochannel"
        if ircmsg.endswith(":+x") or ircmsg.endswith(":+i"):
            for c in ircprofiles[currentprofile]["channels"]:
                joinchan(c)
        for i in ircprofiles[currentprofile]["channels"]:
            dochannel(ircmsg, i)
        dochannel(ircmsg, ircprofiles[currentprofile]["nick"])
        currentuser = cmd.getircuser(ircmsg)
        if ircmsg.find("PING :") == 0:
            c_modules.event("ping", ircmsg)
        elif ircmsg.find("ERROR :") == 0 and\
        ircmsg.find("Ping timeout :") != -1:
            pass
        elif ircmsg.find(":" + ircprofiles[currentprofile]["nick"] + "!") == 0\
        and ircmsg.find("JOIN") != -1:
            c_modules.event("login")
        else:
            global handled
            handled = False
            c_modules.event("text", ircmsg)
            c_modules.event("msg", ircmsg)
            c_modules.event("afterall", ircmsg)

def loop():
    import evazbot.configs.c_modules as c_modules
    global channel
    global currentprofile
    lasttime = time.time()
    while running:
        sockets = []
        for p in range(len(ircprofiles)):
            sockets.append(ircprofiles[p]["ircsock"])
        selectresult, sr2, sr3 = select.select(sockets, [], [], 0.2)
        time.sleep(0.1)
        if time.time() - lasttime >= 1:
            lasttime = time.time()
            c_modules.event("tick")
        for socket in selectresult:
            for r in range(len(ircprofiles)):
                if ircprofiles[r]["ircsock"] == socket:
                    currentprofile = r
                    try:
                        ircmsg = ircprofiles[currentprofile][
                            "ircsock"].recv(2048).decode()
                        regex = re.compile(
                            "\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE)
                        ircmsg = regex.sub("", ircmsg)
                    except socket.error as exc:
                        raise exc
                    msgs = ircmsg.split("\r\n")
                    for i in msgs:
                        process(i)

def ircmain():
    import evazbot.configs.c_modules as c_modules
    c_modules.init()
    c_modules.load()
    print(("Starting " + c_net.name + "..."))
    c_modules.event("start")
    loop()
    c_modules.event("stop")
    for i in range(len(ircprofiles)):
        ircprofiles[i]["ircsock"].close()
<|endoftext|>