Ejemplo n.º 1
0
def convert_to_unicode(object):
    '''
        Converts given object to its unicode string representation like \
        python's native "builtins.str()" method.
    '''
    if builtins.isinstance(object, builtins.Exception):
        if builtins.hasattr(object, '__unicode__'):
            return object.__unicode__()
        if builtins.hasattr(object, 'message'):
            object = object.message
    if builtins.isinstance(object, builtins.unicode):
        '''
            NOTE: To force specified encoding we should encode and than \
            decode here.
        '''
        return object
    if builtins.isinstance(object, builtins.str):
        return builtins.unicode(object, ENCODING)
    if not builtins.isinstance(object, builtins.type):
        return convert_type_to_unicode(object)
    '''
        NOTE: We have to avoid using an explicit encoding to mimic python \
        native "builtins.str()" method behavior for getting a string \
        representation.
    '''
    return builtins.unicode(object)
Ejemplo n.º 2
0
    def fit(self, X, y, sample_weight=None):
        if self.verbose >= 1: print '>>>>> fitting by category'

        for val in np.unique(X[:, self.pivotColInd]):

            curClf = clone(self.regressor)
            curInd = X[:, self.pivotColInd]==val
            curX = X[curInd, :]
            curY = y[curInd]

            if self.verbose >= 2: print '\n-----', val, curX.shape, len(curY)

            if self.useWeights:
                curWeights = sample_weight[curInd]
                curClf.fit(curX, curY, sample_weight=curWeights)
            else:
                curClf.fit(curX, curY)

            self.regressors[val] = curClf

            if self.verbose >= 1:
                if hasattr(curClf, 'intercept_'):
                    print 'intercept =', curClf.intercept_

                if hasattr(curClf, 'coef_'):
                    pprint(dict(zip(range(X.shape[1]), curClf.coef_)))

        if self.verbose >= 1: print '>>>>> fitting everything'
        if self.useWeights:
            self.overallRegressor.fit(X, y, sample_weight=sample_weight)
        else:
            self.overallRegressor.fit(X, y)

        return self
Ejemplo n.º 3
0
def convert_type_to_unicode(object):
    '''Converts a generic string representable object to unicode.'''
    if builtins.hasattr(object, '__unicode__'):
        return object.__unicode__()
    elif builtins.hasattr(object, '__str__'):
        try:
            object = object.__str__()
        except builtins.UnicodeEncodeError:
            if builtins.isinstance(object, Iterable):
                for index, item in builtins.enumerate(object):
                    object[index] = convert_to_unicode(item)
                object = object.__str__()
            else:
                raise
        if builtins.isinstance(object, builtins.unicode):
            return object
        return builtins.unicode(object, ENCODING)
    return builtins.unicode(object)
Ejemplo n.º 4
0
 def hasattr(cls, obj, name):
     """ Overrides the builtin `hasattr` function within LimitedExec
         scripts.  This version checks that the given attribute is
         permissible.
     """
     if name.startswith(config.names.LTDEXEC_PRIVATE_PREFIX):
         m = 'Cannot access attribute "{0}".'.format(name)
         raise exceptions.LXPrivateAttrError(m)
     elif name in cls.forbidden_attrs_set:
         m = 'Cannot access attribute "{0}".'.format(name)
         raise exceptions.ForbiddenAttrError(m)
     return __builtin__.hasattr(obj, name)
Ejemplo n.º 5
0
 def return_handler(advice):
     '''
         Supports classes, simple functions or methods as triggered \
         return handler.
     '''
     if 'return' == advice['event']:
         self.return_value = advice['callback'](
             self.class_object, self.object,
             self.__func__, self.arguments, self.keywords,
             return_value)
         if(builtins.hasattr(advice['callback'], 'aspect') and
            builtins.callable(
                builtins.getattr(advice['callback'], 'aspect'))):
             self.return_value = self.return_value.aspect()
Ejemplo n.º 6
0
            def wrapper_function(*arguments, **keywords):
                '''
                    Wrapper function for doing the aspect orientated stuff \
                    before and after a function call.

                    Arguments and keywords are forwarded to wrapped function.
                '''
                '''Unpack wrapper methods.'''
                while builtins.hasattr(self.__func__, '__func__'):
                    self.__func__ = self.__func__.__func__
                arguments = self._determine_arguments(arguments)
                point_cut = PointCut(
                    self.class_object, self.object, function=self.__func__,
                    arguments=arguments, keywords=keywords)
                if point_cut.handle_call():
                    self.return_value = point_cut.handle_return(
                        return_value=self.__func__(*arguments, **keywords))
                return self.return_value
Ejemplo n.º 7
0
        def call_handler(advice):
            '''
                Supports classes, simple functions or methods as triggered \
                call handler.

                **advice** - Dictionary saving the advice properties.

                Returns "True" if we have a "call" event and the functions \
                return value if we have "return" event.
            '''
            if 'call' == advice['event']:
                result = advice['callback'](
                    self.class_object, self.object, self.__func__,
                    self.arguments, self.keywords)
                if(builtins.hasattr(advice['callback'], 'aspect') and
                   builtins.isinstance(
                       builtins.getattr(advice['callback'], 'aspect'),
                       (Method, Function))):
                    result = result.aspect()
                return result is not False
            return True
Ejemplo n.º 8
0
def application(environ, start_response):
    """
    The main WSGI application. Dispatch the current request to
    the functions from above and store the regular expression
    captures in the WSGI environment as  `oic.url_args` so that
    the functions from above can access the url placeholders.

    If nothing matches call the `not_found` function.

    :param environ: The HTTP application environment
    :param start_response: The application to run when the handling of the
        request is done
    :return: The response as a list of lines
    """
    global LOOKUP
    global OAS

    #user = environ.get("REMOTE_USER", "")
    path = environ.get('PATH_INFO', '').lstrip('/')

    logger = logging.getLogger('oicServer')

    if path == "robots.txt":
        return static(environ, start_response, logger, "static/robots.txt")

    environ["oic.oas"] = OAS
    environ["mako.lookup"] = LOOKUP

    remote = environ.get("REMOTE_ADDR")

    kaka = environ.get("HTTP_COOKIE", '')
    a1 = None
    handle = ""
    key = ""
    if kaka:
        try:
            handle = parse_cookie(OAS.cookie_name, OAS.seed, kaka)
            try:
                key = handle[0]
            except TypeError:
                key = ""

            if hasattr(OAS, "trace_log"):
                try:
                    _log = OAS.trace_log[key]
                except KeyError:
                    _log = create_session_logger(key)
                    OAS.trace_log[key] = _log
            else:
                _log = replace_format_handler(logger)

            a1 = logging.LoggerAdapter(_log,
                                       {'path': path,
                                        'client': remote,
                                        "cid": key})

        except ValueError:
            pass

    if not a1:
        key = STR + rndstr() + STR
        handle = (key, 0)

        if hasattr(OAS, "trace_log"):
            try:
                _log = OAS.trace_log[key]
            except KeyError:
                _log = OAS.new_trace_log(key)
        else:
            _log = replace_format_handler(logger)

        a1 = logging.LoggerAdapter(_log, {'path': path, 'client': remote,
                                          "cid": key})

    #logger.info("handle:%s [%s]" % (handle, a1))
    #a1.info(40*"-")
    if path.startswith("static/"):
        return static(environ, start_response, a1, path)
    #    elif path.startswith("oc3_keys/"):
    #        return static(environ, start_response, a1, path)

    for regex, callback in URLS:
        match = re.search(regex, path)
        if match is not None:
            try:
                environ['oic.url_args'] = match.groups()[0]
            except IndexError:
                environ['oic.url_args'] = path

            a1.info("callback: %s" % callback)
            try:
                return callback(environ, start_response, a1, handle)
            except Exception, err:
                print >> sys.stderr, "%s" % err
                message = traceback.format_exception(*sys.exc_info())
                print >> sys.stderr, message
                a1.exception("%s" % err)
                if key and hasattr(OAS, "trace_log"):
                    _txt = OAS.dump_tracelog(key)
                    _txt += "\n" + "%s" % err
                    resp = ServiceError(_txt)
                else:
                    resp = ServiceError("%s" % err)
                return resp(environ, start_response)
Ejemplo n.º 9
0
def hasattr(thing, attr):
    """does *thing* have an attribute named *attr*?"""
    return __builtin__.hasattr(thing, attr)
Ejemplo n.º 10
0
    def __getattr__(self, name):
        # #
        """
            Is triggered if a property was tried to be read but is \
            inaccessible.

            **name** - is the inaccessible property name.

            Should return the current value of the given property name \
            depends on its getter method. Returns "None" if no getter method \
            is accessible.

            Examples:

            >>> class TestA(Class):
            ...     _a = 'hans'
            ...     _b = 5
            ...     def __init__(self, c=2): self._c = c
            ...     def get_a(self): return self._a
            ...     def get_c(self): return 2 * self._c
            >>> TestA().a
            'hans'
            >>> TestA().b # doctest: +ELLIPSIS
            Traceback (most recent call last):
            ...
            AttributeError: Property "b" doesn't exist in given instance of ...
            >>> TestA().c
            4

            >>> class TestB(Class):
            ...     _a = ''
            ...     def __init__(self):
            ...         self._a = 'hans'
            ...         self._b = 'also hans'
            ...     def get(self, name):
            ...         if name in ('_a', '_b'):
            ...             return self.__dict__[name]
            ...         return None
            >>> TestB().b
            'also hans'
            >>> TestB().c # doctest: +ELLIPSIS
            Traceback (most recent call last):
            ...
            AttributeError: Property "c" doesn't exist in given instance of ...
        """
        internal_name = "_%s" % name
        getter_name = "get%s" % internal_name
        special_method = None
        if self.is_method(name=getter_name):
            special_method = builtins.getattr(self, getter_name)
        if (
            self.is_property(internal_name)
            or special_method is not None
            and builtins.hasattr(special_method, "pseudo_property")
        ):
            if special_method is not None:
                return special_method()
            elif self.is_method(name="get"):
                return self.get(internal_name)
        raise builtins.AttributeError(
            'Property "%s" doesn\'t exist in given instance of "%s".' % (name, self.__class__.__name__)
        )
Ejemplo n.º 11
0
def check_semantics(model, metamodel):
    parser = metamodel.parser
    # Check config model
    if not model.config:
        raise TextXSemanticError('Config is required')

    # If app label does not exist, put app name instead.
    if not model.config.app_label:
        model.config.app_label = model.config.app_name
    model.config.qname = model.config.namespace + '.' + camel_to_under(
        model.config.app_name)
    if model.config.platforms.android and not model.config.android_specs:
        raise TextXSemanticError(
            'Android specs are required if the android platform is specified')
    android_specs = model.config.android_specs
    if not android_specs:
        android_specs.min_version = DEF_ANDROID_MIN_VER
        android_specs.target_version = DEF_ANDROID_TARGET_VER
        model.config.android_specs = android_specs
    else:
        if not android_specs.min_version:
            android_specs.min_version = DEF_ANDROID_MIN_VER
        if not android_specs.target_version:
            android_specs.target_version = DEF_ANDROID_TARGET_VER
        if android_specs.min_version > android_specs.target_version:
            raise TextXSemanticError(
                'In Android Specs: Minimum version cannot be greater than the target version, {}'
                .format(print_position(parser, android_specs._position)))
    if model.config.start_screen:
        if not model.config.start_screen.operations.listall:
            raise TextXSemanticError(
                "Start screen must be an entity that has the 'listall' operation, "
                "start screen entity = '{}', {}".format(
                    model.config.start_screen.name,
                    print_position(parser, model.config._position)))

    if not model.entities:
        raise TextXSemanticError('At least one entity is required')
    if not [x for x in model.entities if x.operations.listall]:
        raise TextXSemanticError(
            "At least one entity with the 'listAll' operation is required")
    #Checking duplicate entity names
    entity_names = [x.name for x in model.entities]
    if len(entity_names) != len(set(entity_names)):
        raise TextXSemanticError('Duplicate entity names detected!')

    for entity in model.entities:
        # If entity label does not exist, put entity name instead.
        if not entity.label:
            entity.label = entity.name

        attribute_names = [x.name for x in entity.attributes]
        if len(attribute_names) != len(set(attribute_names)):
            raise TextXSemanticError(
                'Duplicate attribute names in entity "{}" detected, {}'.format(
                    entity.name, print_position(parser, entity._position)))

        for attr in entity.attributes:
            # If attribute label does not exist, put attribute name instead.
            if not attr.label:
                attr.label = attr.name.title()
            if attr.view_from_container and not attr.reference_type:
                raise TextXSemanticError(
                    "Cannot have 'viewFromContainer' on an non-reference type, in attribute '{}', entity '{}', {}"
                    .format(attr.name, entity.name,
                            print_position(parser, attr._position)))
            #Setting toString of the entity
            if attr.to_string:
                if not attr.primitive_type or attr.primitive_type == 'image':
                    raise TextXSemanticError(
                        "toString attribute must be a non-image primitive type, in attribute '{}', entity '{}', {}"
                        .format(attr.name, entity.name,
                                print_position(parser, attr._position)))
                if not hasattr(entity, 'to_string'):
                    entity.to_string = attr
                else:
                    raise TextXSemanticError(
                        "Entity cannot have more than one toString attribute, in attribute '{}'"
                        ", toString attribute '{}' entity '{}', {}".format(
                            attr.name, entity.to_string.name, entity.name,
                            print_position(parser, attr._position)))
            if attr.searchable and attr.reference_type:
                if not hasattr(attr.reference_type, 'to_string'):
                    if len([
                            ref_attr
                            for ref_attr in attr.reference_type.attributes
                            if ref_attr.to_string
                    ]) == 0:
                        raise TextXSemanticError(
                            "The entity that is reference by a searchable attribute must have a 'toString' attribute, "
                            "\nfrom searchable attribute '{}' in entity '{}', {}"
                            .format(attr.name, entity.name,
                                    print_position(parser, attr._position)))
        # Unique_set check
        for unique_set in entity.unique_sets:
            if len(unique_set.attributes) == 1:
                raise TextXSemanticError(
                    "Unique set must contain at least two attributes, in entity {}, {}"
                    .format(entity.name,
                            print_position(parser, unique_set._position)))
            for attr in unique_set.attributes:
                if attr.name not in [x.name for x in entity.attributes]:
                    raise TextXSemanticError(
                        "Unique set must only contain attributes from the parent entity {}. "
                        "Attribute {} does not belong here, {}".format(
                            entity.name, attr.name,
                            print_position(parser, unique_set._position)))

    return model
Ejemplo n.º 12
0
def check_semantics(model, metamodel):
    parser = metamodel.parser
    # Check config model
    if not model.config:
        raise TextXSemanticError('Config is required')

    # If app label does not exist, put app name instead.
    if not model.config.app_label:
        model.config.app_label = model.config.app_name
    model.config.qname = model.config.namespace + '.' + camel_to_under(model.config.app_name)
    if model.config.platforms.android and not model.config.android_specs:
        raise TextXSemanticError('Android specs are required if the android platform is specified')
    android_specs = model.config.android_specs
    if not android_specs:
        android_specs.min_version = DEF_ANDROID_MIN_VER
        android_specs.target_version = DEF_ANDROID_TARGET_VER
        model.config.android_specs = android_specs
    else:
        if not android_specs.min_version:
            android_specs.min_version = DEF_ANDROID_MIN_VER
        if not android_specs.target_version:
            android_specs.target_version = DEF_ANDROID_TARGET_VER
        if android_specs.min_version > android_specs.target_version:
            raise TextXSemanticError('In Android Specs: Minimum version cannot be greater than the target version, {}'
                                     .format(print_position(parser, android_specs._position)))
    if model.config.start_screen:
        if not model.config.start_screen.operations.listall:
            raise TextXSemanticError("Start screen must be an entity that has the 'listall' operation, "
                                     "start screen entity = '{}', {}"
                                     .format(model.config.start_screen.name,
                                             print_position(parser, model.config._position)))

    if not model.entities:
        raise TextXSemanticError('At least one entity is required')
    if not [x for x in model.entities if x.operations.listall]:
        raise TextXSemanticError("At least one entity with the 'listAll' operation is required")
    #Checking duplicate entity names
    entity_names = [x.name for x in model.entities]
    if len(entity_names) != len(set(entity_names)):
        raise TextXSemanticError('Duplicate entity names detected!')

    for entity in model.entities:
        # If entity label does not exist, put entity name instead.
        if not entity.label:
            entity.label = entity.name

        attribute_names = [x.name for x in entity.attributes]
        if len(attribute_names) != len(set(attribute_names)):
            raise TextXSemanticError('Duplicate attribute names in entity "{}" detected, {}'.format(entity.name,
                                             print_position(parser, entity._position)))

        for attr in entity.attributes:
            # If attribute label does not exist, put attribute name instead.
            if not attr.label:
                attr.label = attr.name.title()
            if attr.view_from_container and not attr.reference_type:
                raise TextXSemanticError(
                    "Cannot have 'viewFromContainer' on an non-reference type, in attribute '{}', entity '{}', {}"
                     .format(attr.name, entity.name, print_position(parser, attr._position)))
            #Setting toString of the entity
            if attr.to_string:
                if not attr.primitive_type or attr.primitive_type == 'image':
                    raise TextXSemanticError(
                        "toString attribute must be a non-image primitive type, in attribute '{}', entity '{}', {}"
                        .format(attr.name, entity.name, print_position(parser, attr._position)))
                if not hasattr(entity, 'to_string'):
                    entity.to_string = attr
                else:
                    raise TextXSemanticError(
                        "Entity cannot have more than one toString attribute, in attribute '{}'"
                        ", toString attribute '{}' entity '{}', {}"
                        .format(attr.name, entity.to_string.name, entity.name,
                                print_position(parser, attr._position)))
            if attr.searchable and attr.reference_type:
                if not hasattr(attr.reference_type, 'to_string'):
                    if len([ref_attr for ref_attr in attr.reference_type.attributes if ref_attr.to_string]) == 0:
                        raise TextXSemanticError(
                            "The entity that is reference by a searchable attribute must have a 'toString' attribute, "
                            "\nfrom searchable attribute '{}' in entity '{}', {}"
                            .format(attr.name, entity.name, print_position(parser, attr._position)))
        # Unique_set check
        for unique_set in entity.unique_sets:
            if len(unique_set.attributes) == 1:
                raise TextXSemanticError("Unique set must contain at least two attributes, in entity {}, {}"
                                         .format(entity.name, print_position(parser, unique_set._position)))
            for attr in unique_set.attributes:
                if attr.name not in [x.name for x in entity.attributes]:
                    raise TextXSemanticError("Unique set must only contain attributes from the parent entity {}. "
                                             "Attribute {} does not belong here, {}"
                                             .format(entity.name, attr.name,
                                                     print_position(parser, unique_set._position)))

    return model
Ejemplo n.º 13
0
def hasattr(thing, attr):
    """does *thing* have an attribute named *attr*?"""
    return __builtin__.hasattr(thing, attr)