Example #1
0
 def addHandler(self, comparator, callback):
     """Adds a handler to the SerialListener
         
     Throws ValueError: on invalid arguments
      comparator is either True or a function. That function must return a 
      boolean value when passed the message in question.  callback is a 
      function that is called iff the comparator returns True and is also 
      passed the msg.
     """
     # Make sure that the comparator is either a function or a boolean
     if not (inspect.isfunction(comparator) or inspect.ismethod(comparator) or isinstance(comparator, bool)):
         raise ValueError("Invalid comparator passed to addHandler, must be a function or boolean")
     # If comparator is a function check to make sure it takes atleast 1 argument beyond self
     if inspect.isfunction(comparator) or inspect.ismethod(comparator):
         args = inspect.getargspec(comparator).args
         try:
             args.remove("self")
         except:
             pass
         if len(args) == 0:
             raise ValueError("Invalid comparator passed to addHandler, must take atleast one argument")
     # Make sure that the callback is a function
     if not (inspect.isfunction(callback) or inspect.ismethod(callback)):
         raise ValueError("Invalid callback passed to addHandler, must be a function")
     else:
         # Make sure it has atleast one argument for the msg
         args = inspect.getargspec(callback).args
         try:
             args.remove("self")
         except:
             pass
         if len(args) == 0:
             raise ValueError("Invalid callback passed to addHandler, must take atleast one argument")
     self.handlers.append((comparator, callback))
Example #2
0
    def testInit(self):
        """
        Tests __init__() behavior on invalid values
        """
        for name in (None, "", "name"):
            for value in (None, "", "value"):
                for comparator in (None, True, lambda x: True):
                    if not all((name, value, comparator)) \
                            or (not inspect.isfunction(comparator)
                                and not inspect.ismethod(comparator)):
                        # One value is None
                        self.assertRaises(ValueError,
                                          pelix.ldapfilter.LDAPCriteria, name,
                                          value, comparator)

                    else:
                        # All values are OK
                        criteria = pelix.ldapfilter.LDAPCriteria(name, value,
                                                                 comparator)
                        self.assertEqual(name, criteria.name,
                                         "Name modified")
                        self.assertEqual(value, criteria.value,
                                         "Value modified")
                        self.assertTrue(
                            inspect.ismethod(criteria.comparator)
                            or inspect.isfunction(criteria.comparator),
                            "Invalid comparator accepted")
Example #3
0
def print_classes():
    for name, obj in inspect.getmembers(jb):
        if inspect.isclass(obj):
            #f1.write('paichi_the_found'
            f1.write('\n')
            f1.write("Class      Name ->  "+name)
            f1.write('\n')
            for N, O in inspect.getmembers(obj):
            	#f1.write(' --- Extra --- '
            	if inspect.isclass(O):
            		f1.write("      "+"SubClass      Name ->  "+N)
            		f1.write('\n')
            	if inspect.ismethod(O):
            		f1.write("      "+"SubMethod     Name ->  "+N)
            		f1.write('\n')
            	if inspect.isfunction(O):
            		f1.write("      "+"SubFunction   Name ->  "+N)
            		f1.write('\n')
        if inspect.ismethod(obj):
            #f1.write('paichi_the_found'
            f1.write('')
            f1.write('\n')
            f1.write("Method     Name ->  "+name)
            f1.write('\n')
        if inspect.isfunction(obj):
            #f1.write('paichi_the_found'
            f1.write('')
            f1.write('\n')
            f1.write("Function   Name ->  "+name)
            f1.write('\n')
Example #4
0
    def __init__(self, name, func, args, kwargs):
        """
        Maintains node evaluation state for basic node types.

        :param name: Name of the node.
        :param func: A function, method or callable class instance.
        :param args: Positional arguments that will be passed to the function for evaluation.
        :param kwargs: Keyword arguments that will be passed to the function for evaluation.
        """
        super(NodeState, self).__init__(name, func)

        # If the func object is not a method or a function, assume it is a callable class
        if inspect.isclass(type(func)) and not inspect.ismethod(func) and not inspect.isfunction(func):
            self.func = func.__call__

        args_spec = inspect.getargspec(self.func)
        args_spec_args = args_spec.args

        # Exclude `self` for class methods
        if inspect.ismethod(self.func):
            args_spec_args = args_spec_args[1:]

        self._args_spec = NodeArgSpec(args_spec_args, args_spec.varargs, args_spec.keywords)

        self._args = args
        self._kwargs = kwargs
 def wrapper(self, *args, **kw):
     if hasattr(self, 'before') and inspect.ismethod(self.before):
         self.before()
     result = f(self, *args, **kw)
     if hasattr(self, 'after') and inspect.ismethod(self.after):
         self.after()
     return result
    def delete_card(self, data):
        profile_id = data.profile.id  
        if inspect.ismethod(profile_id):
            err_msg = "Profile Id not available"
            return (self.prepare_error(CustomerVault.Card.Card, 
                                    "400", 
                                    err_msg))         
        del data.profile
        card_id = data.id
        if inspect.ismethod(card_id):
            err_msg = "Card Id not available"
            return (self.prepare_error(CustomerVault.Card.Card, 
                                    "400", 
                                    err_msg))
        del data.id

        FULL_URL = self._prepare_uri(self._PROFILE_PATH + \
                                    profile_id + \
                                    self._CARD_PATH + \
                                    card_id)
        # print ("Communicating to ", FULL_URL)
        response = self.optimal_object.process_request(
                                                req_method="DELETE", 
                                                url=FULL_URL, 
                                                data=None) 
        return (self._process_response(response, 
                                    CustomerVault.Card.Card))
 def lookup_sepa_bank_account(self, data):
     profile_id = data.profile.id  
     if inspect.ismethod(profile_id):
         err_msg = "Profile Id not available"
         return (self.prepare_error(CustomerVault.SEPABankAccount.SEPABankAccount, 
                                 "400", 
                                 err_msg))         
     del data.profile
     
     sepa_bank_id = data.id
     if inspect.ismethod(sepa_bank_id):
         err_msg = "SEPA Bank Account Id not available"
         return (self.prepare_error(
                                 CustomerVault.SEPABankAccount.SEPABankAccount, 
                                 "400", 
                                 err_msg))
     del data.id
     
     FULL_URL = self._prepare_uri(self._PROFILE_PATH + \
                                 profile_id + \
                                 self._SEPA_BANK_PATH + \
                                 sepa_bank_id)
     # print ("Communicating to ", FULL_URL)
     response = self.optimal_object.process_request(
                                             req_method="GET", 
                                             url=FULL_URL, 
                                             data=None)                
     return (self._process_response(response, CustomerVault.SEPABankAccount.SEPABankAccount))
 def update_mandates(self, data):
     profile_id = data.profile.id
     mandates_id = data.id
     if inspect.ismethod(profile_id):
         err_msg = "Profile Id not available"
         return (self.prepare_error(
                                 CustomerVault.Mandates.Mandates, 
                                 "400", 
                                 err_msg))
     del data.profile
     
     if inspect.ismethod(mandates_id):
         err_msg = "Mandates Id not available"
         return (self.prepare_error(
                                 CustomerVault.Mandates.Mandates, 
                                 "400", 
                                 err_msg))
     del data.id
     
     FULL_URL = self._prepare_uri(self._PROFILE_PATH + \
                                 profile_id + \
                                 self._MANDATES_PATH +\
                                 self._URI_SEPARATOR +\
                                 mandates_id)
     # print ("Communicating to ", FULL_URL)
     response = self.optimal_object.process_request(
                                             req_method="PUT", 
                                             url=FULL_URL, 
                                             data=data)      
     return (self._process_response(response, CustomerVault.Mandates.Mandates))
    def delete_address(self, data):
        profile_id = data.profile.id 
        if inspect.ismethod(profile_id):
            err_msg = "Profile Id not available"
            return (self.prepare_error(CustomerVault.Addresses.Address, 
                                    "400", 
                                    err_msg))       
        del data.profile
        address_id = data.id
        if inspect.ismethod(address_id):
            err_msg = "Address Id not available"
            return (self.prepare_error(CustomerVault.Addresses.Address, 
                                    "400", 
                                    err_msg)) 
        del data.id

        FULL_URL = self._prepare_uri(self._PROFILE_PATH + \
                                    profile_id + \
                                    self._ADDRESS_PATH + \
                                    address_id)
        # print ("Communicating to ", FULL_URL)
        response = self.optimal_object.process_request(
                                                req_method="DELETE", 
                                                url=FULL_URL, 
                                                data=None)
        return (self._process_response(response, 
                                    CustomerVault.Addresses.Address))
Example #10
0
 def _ListAllSubprocesses():
   try:
     import psutil
   except ImportError:
     logging.warning(
         'psutil is not installed on the system. Not listing possible '
         'leaked processes. To install psutil, see: '
         'https://pypi.python.org/pypi/psutil')
     return
   telemetry_pid = os.getpid()
   parent = psutil.Process(telemetry_pid)
   if hasattr(parent, 'children'):
     children = parent.children(recursive=True)
   else:  # Some old version of psutil use get_children instead children.
     children = parent.get_children()
   if children:
     leak_processes_info = []
     for p in children:
       if inspect.ismethod(p.name):
         name = p.name()
       else:  # Process.name is a property in old versions of psutil.
         name = p.name
       process_info = '%s (%s)' % (name, p.pid)
       try:
         if inspect.ismethod(p.cmdline):
           cmdline = p.cmdline()
         else:
           cmdline = p.cmdline
         process_info += ' - %s' % cmdline
       except Exception as e:
         logging.warning(str(e))
       leak_processes_info.append(process_info)
     logging.error('Telemetry leaks these processes: %s',
                   ', '.join(leak_processes_info))
 def create_mandates_sepa_bank(self, data):
     profile_id = data.profile.id
     sepa_bank_id = data.sepa.id
     
     if inspect.ismethod(profile_id):
         err_msg = "Profile Id not available"
         return (self.prepare_error(
                                 CustomerVault.Mandates.Mandates, 
                                 "400", 
                                 err_msg))
     del data.profile
     
     if inspect.ismethod(sepa_bank_id):
         err_msg = "SEPA Bank Account Id not available"
         return (self.prepare_error(
                                 CustomerVault.Mandates.Mandates, 
                                 "400", 
                                 err_msg))
     del data.sepa
     
     FULL_URL = self._prepare_uri(self._PROFILE_PATH +\
                                 profile_id +\
                                 self._SEPA_BANK_PATH +\
                                 sepa_bank_id +\
                                 self._MANDATES_PATH)
     # print ("Communicating to ", FULL_URL)
     response = self.optimal_object.process_request(
                                             req_method="POST",
                                             url=FULL_URL,
                                             data=data)  
     
     return (self._process_response(response, CustomerVault.Mandates.Mandates))
Example #12
0
    def __init__(self, *check, **kwargs):
        """        

        @keyword default: value/ function that will be
        assigned/called if prop value is requested but is None.
        
        @keyword reset: value/function that will be assigned/called on
        initialization
        
        @keyword blurb: Short description.
        @keyword doc: Longer description.
        """

        self.blurb = kwargs.pop('blurb', None)
        self.doc = kwargs.pop('doc', None)

        self.check = CheckAll(check or [])

        default = kwargs.pop('default', None)
        if inspect.isfunction(default) or inspect.ismethod(default):
            self.on_default = default
        else:
            if default is not None:
                default = self.check(default)
            self.on_default = (lambda: default)

        reset = kwargs.pop('reset', None)
        if inspect.isfunction(reset) or inspect.ismethod(reset):
            self.on_reset = reset
        else:
            if reset is not None:
                reset = self.check(reset)
            self.on_reset = (lambda: reset)

        self.name = None # set by the owning HasProps class
Example #13
0
    def _get_all_props(self):
        'return all properties accessible through get'
        all_props = []

        # release properties
        rel_props = getmembers(self.release,
                               predicate=lambda p: (not ismethod(p)))
        rel_props = [a[0] for a in rel_props if not a[0].startswith('_')]

        all_props.extend(rel_props)

        # element_type properties
        et_props = getmembers(self.element_type,
                              predicate=lambda p: (not ismethod(p)))
        'remove _state - update this after we change _state to _state'
        et_props = [a[0] for a in et_props
                    if not a[0].startswith('_') and a[0] != '_state']

        all_props.extend(et_props)

        # properties for each of the initializer objects
        i_props = []
        for val in self.element_type.initializers:
            toadd = getmembers(val, lambda p: (not ismethod(p)))
            i_props.extend([a[0] for a in toadd
                            if not a[0].startswith('_') and a[0] != '_state'])

            all_props.extend(i_props)
        return all_props
def isstaticmethod(obj):
    # python2:
    # >>> str(object)
    # <function staticmethod at ...>
    # python3
    # >>> str(object)
    # <function CLS.staticmethod at ...>
    if not obj_check(obj):
        return False
    module = inspect.getmodule(obj)
    if not module:
        return False
    try:
        lines, _ = inspect.getsourcelines(obj)
        if lines:
            if "staticmethod" not in "\n".join(lines):
                return False
            if lines[0].lstrip() == "@staticmethod":
                return True
    except Exception:
        pass
    # python 2: <function staticmethod at ...>
    if "<function staticmethod at" in str(obj):
        return True
    # python 3: <function CLS.staticmethod at ...>
    if "<function " in str(obj) and "." in str(obj):
        return True
    for _, cls in inspect.getmembers(module, inspect.isclass):
        for _, member in inspect.getmembers(cls, inspect.isroutine):
            if inspect.ismethod(member):
                continue
            if member == obj:
                # python 2 only
                return not inspect.ismethod(obj)
    return False
Example #15
0
def _call_partial(function, args, kwargs, require_event=False):
    """
    Add args and kwargs when calling function (similar to functools.partial).
    Also, allow omission of the first argument (event) if require_event
    is False.
    """
    try:
        argspec = _inspect.getargspec(function)
        ismethod = _inspect.ismethod(function)
    except TypeError:
        # support callable objects
        argspec = _inspect.getargspec(function.__call__)
        ismethod = _inspect.ismethod(function.__call__)

    if (not require_event and argspec[1] == None
                and len(argspec[0]) - int(ismethod) == 0):
        # omit event argument when function has no positional arguments
        if len(kwargs):
            return lambda ev: function(**kwargs)
        else:
            return lambda ev: function()
    if len(args) or len(kwargs):
        # return a function with args and kwargs applied
        return lambda ev: function(ev, *args, **kwargs)
    else:
        # no additional arguments, no wrapper needed
        return function
Example #16
0
def varnames(func):
    """ return argument name tuple for a function, method, class or callable.

    In case of a class, its "__init__" method is considered.
    For methods the "self" parameter is not included unless you are passing
    an unbound method with Python3 (which has no supports for unbound methods)
    """
    cache = getattr(func, "__dict__", {})
    try:
        return cache["_varnames"]
    except KeyError:
        pass
    if inspect.isclass(func):
        try:
            func = func.__init__
        except AttributeError:
            return ()
        ismethod = True
    else:
        if not inspect.isfunction(func) and not inspect.ismethod(func):
            func = getattr(func, '__call__', func)
        ismethod = inspect.ismethod(func)
    rawcode = py.code.getrawcode(func)
    try:
        x = rawcode.co_varnames[ismethod:rawcode.co_argcount]
    except AttributeError:
        x = ()
    try:
        cache["_varnames"] = x
    except TypeError:
        pass
    return x
Example #17
0
    def _finddoc(obj):
        # type: (Any) -> unicode
        if inspect.isclass(obj):
            for base in obj.__mro__:
                if base is not object:
                    try:
                        doc = base.__doc__
                    except AttributeError:
                        continue
                    if doc is not None:
                        return doc
            return None

        if inspect.ismethod(obj) and getattr(obj, '__self__', None):
            name = obj.__func__.__name__
            self = obj.__self__
            if (inspect.isclass(self) and
                    getattr(getattr(self, name, None), '__func__')
                    is obj.__func__):
                # classmethod
                cls = self
            else:
                cls = self.__class__
        elif inspect.isfunction(obj) or inspect.ismethod(obj):
            name = obj.__name__
            cls = _findclass(obj)
            if cls is None or getattr(cls, name) != obj:
                return None
        elif inspect.isbuiltin(obj):
            name = obj.__name__
            self = obj.__self__
            if (inspect.isclass(self) and
                    self.__qualname__ + '.' + name == obj.__qualname__):
                # classmethod
                cls = self
            else:
                cls = self.__class__
        # Should be tested before isdatadescriptor().
        elif isinstance(obj, property):
            func = obj.fget
            name = func.__name__
            cls = _findclass(func)
            if cls is None or getattr(cls, name) is not obj:
                return None
        elif inspect.ismethoddescriptor(obj) or inspect.isdatadescriptor(obj):
            name = obj.__name__
            cls = obj.__objclass__
            if getattr(cls, name) is not obj:
                return None
        else:
            return None

        for base in cls.__mro__:
            try:
                doc = getattr(base, name).__doc__
            except AttributeError:
                continue
            if doc is not None:
                return doc
        return None
Example #18
0
 def _func_name(func):
     """Return name of a callable (function, class, partial, etc.)"""
     module = ''
     if hasattr(func,'__module__'):
         module = (func.__module__ if func.__module__ else '__main__')
     # Return a human readable name associated with a function
     if inspect.ismethod(func):
         nme = '.'.join([module,func.im_class.__name__,func.__name__])
     elif inspect.isfunction(func):
         nme =  '.'.join([module,func.__name__])
     elif inspect.isbuiltin(func):
         return  '.'.join([module,func.__name__])
     elif isinstance(func,partial):
         return 'partial_of_' + JobModule._func_name(func.func)
     elif inspect.isclass(func):
         nme = '.'.join([module,func.__name__])
         if hasattr(func, '__init__') and inspect.ismethod(func.__init__):            
             func = func.__init__
         else:
             return nme
     else:
         nme = 'type %s' % type(func)
         if hasattr(func, '__name__'):
             nme = '%s of %s' % (func.__name__, type(func))
         return nme
     nme +=  ' at ' + ':'.join([func.func_code.co_filename,
                                str(func.func_code.co_firstlineno)])
     return nme
def parseCallable(targetCall, name, user_args, user_kwargs):
    '''
    '''
    #
    # Check callable given
    #
    import inspect
    if not (inspect.ismethod(targetCall) or inspect.isfunction(targetCall)):
        raise GraphError("Callable must be a function or method.")

    # Common args
    callableArgs = {}
    callableArgs['sysPath'] = sys.path
    callableArgs['moduleName'] = targetCall.__module__

    # Ensure params can be serialized i.e. not object, instance or function
    try:
        callableArgs['user_args'] = json.dumps(user_args)
        callableArgs['user_kwargs'] = json.dumps(user_kwargs)
    except TypeError:
        raise GraphError("Error: invalid parameters (not JSON serializable): %s" % params)

    # Specific args
    if inspect.isfunction(targetCall):
        callableArgs['execType'] = 'function'
        callableArgs['funcName'] = targetCall.__name__
        taskName = name if name != "" else callableArgs['funcName']

    if inspect.ismethod(targetCall):
        callableArgs['execType'] = 'method'
        callableArgs['className'] = inspect.getmro(targetCall.im_class)[0].__name__
        callableArgs['methodName'] = targetCall.__name__
        taskName = name if name != "" else callableArgs['className'] + "." + callableArgs['methodName']

    return (taskName, callableArgs)
Example #20
0
    def __call__(self, *args, **kwargs):
        transaction = newrelic.api.transaction.current_transaction()
        if not transaction:
            return self._nr_next_object(*args, **kwargs)

        if callable(self._nr_library):
            if self._nr_instance and inspect.ismethod(self._nr_next_object):
                library = self._nr_library(self._nr_instance, *args,
                                           **kwargs)
            else:
                library = self._nr_library(*args, **kwargs)
        else:
            library = self._nr_library

        if callable(self._nr_command):
            if self._nr_instance and inspect.ismethod(self._nr_next_object):
                command = self._nr_command(self._nr_instance, *args,
                                           **kwargs)
            else:
                command = self._nr_command(*args, **kwargs)
        else:
            command = self._nr_command

        with SolrTrace(transaction, library, command):
            return self._nr_next_object(*args, **kwargs)
Example #21
0
        def wrapper(*args, **kwargs):
            name, my_args = function.__name__, args

            if inspect.ismethod(function):
                name = function.__self__.__class__.__name__ + '.' + function.__name__
            elif len(args):
                members = dict(inspect.getmembers(args[0], predicate = lambda _: inspect.ismethod(_) and _.__name__ == function.__name__))
                logger.debug('members.keys(): %s', members.keys())

                if len(members):
                    name, my_args = args[0].__class__.__name__ + '.' + function.__name__, args[1:]

            format_args = (
                prefix + name,
                ', '.join(list(map(str, my_args)) + [ ' = '.join(map(str, item)) for item in kwargs.items() ]),
            )

            logger.info('STARTING: %s(%s)', *format_args)

            try:
                return function(*args, **kwargs)
            except:
                logger.exception('EXCEPTION: %s(%s)', *format_args)
                raise
            finally:
                logger.info('STOPPING: %s(%s)', *format_args)
    def getRulesAndTokens(parsercls):
        """
        build the tokens and precedence tuples from inherited declarations.
        gather tokens and rules definition from Parser class members (own and inherited)
        """

        tokensDict = {}
        rulesDict = {}
        for name, obj in inspect.getmembers(parsercls):
            if name.startswith("t_") and name != "t_error":

                if isinstance(obj, basestring):
                    v = obj
                elif inspect.isfunction(obj) or inspect.ismethod(obj):
                    v = obj.__doc__
                else:
                    raise SyntaxError, "Token definition %s defines neither a string nor a function, unable to parse" % m[
                        0
                    ]
                k = name[2:]
                tokensDict[k] = obj
            elif name.startswith("p_") and inspect.ismethod(obj) and name != "p_error":
                k = name[2:]
                rulesDict[k] = obj
        #            elif name == '_reserved' and isinstance(obj,dict):
        #                # reserved attribute holds a list of reserved token keywords.
        #                # these should only exist on
        #                print "FOUND reserved!"
        #                for k,v in obj.items():
        #                    tokensDict[v]=None

        return rulesDict, tokensDict
Example #23
0
def detectCapabilities():
    body = {}
    precon = {}
    try:
        import capability
        from inspect import getmembers, isclass, ismethod

        for m in filter( lambda x: not x.startswith('__'), dir(capability) ):
            for c in getmembers( eval('capability.' + m), isclass ):
                d_b = dict(getmembers(c[1],lambda x: ismethod(x) and 'SDMethodType' in dir(x) and x.SDMethodType == 'body'))
                d_p = dict(getmembers(c[1],lambda x: ismethod(x) and 'SDMethodType' in dir(x) and x.SDMethodType == 'pre'))
                if d_b:
                    if '__init__' in d_b.keys():
                        del d_b['__init__']
                    body[c[0]] = d_b
                if d_p:
                    if '__init__' in d_p.keys():
                        del d_p['__init__']
                    precon[c[0]] = d_p

        #del getmembers, isclass, ismethod

    except Exception, x:
        print "Error detecting capabilities: "
        print str(x)
Example #24
0
  def test_model_schema(self):

    """ Test that there's a proper schema spec on `Model` """

    # check lookup
    self.assertTrue(hasattr(TestPerson, '__lookup__'))
    self.assertIsInstance(TestPerson.__lookup__, frozenset)

    # check property descriptors
    self.assertTrue(hasattr(TestPerson, 'firstname'))
    self.assertTrue(hasattr(TestPerson, 'lastname'))
    self.assertTrue(hasattr(TestPerson, 'cars'))

    # check kind
    self.assertTrue(hasattr(TestPerson, 'kind'))
    self.assertIsInstance(TestPerson.kind(), basestring)

    # check set/get
    self.assertTrue(hasattr(TestPerson, '_get_value'))
    self.assertTrue(hasattr(TestPerson, '_set_value'))
    self.assertTrue(inspect.ismethod(TestPerson._get_value))
    self.assertTrue(inspect.ismethod(TestPerson._set_value))

    # check key
    self.assertTrue(hasattr(TestPerson, 'key'))
    self.assertTrue(hasattr(TestPerson(), '__key__'))

    # should not have key until instantiation
    self.assertTrue((not hasattr(TestPerson, '__key__')))
Example #25
0
    def objects_link(name='', description=None, model=None):
        """
        Create a function that can be attached to a ModelAdmin to use
        as a list_display field
        """
        related_names = name.split('__')

        def getter(self, obj):
            if not related_names[0]:
                return obj.link()

            for related_name in related_names:
                obj = getattr(obj, related_name)

            if inspect.ismethod(obj):  # Is a method
                obj = obj()

            return render_to_string(
                'includes/objects_link.html',
                {
                    'objects': obj.all()
                }
            )

        for related_name in related_names:
            field = model
            if hasattr(model, "_meta"):
                related_name = related_name.replace("_set", "")
                if related_name in model._meta.fields_map:
                    model = model._meta.fields_map[related_name]
                elif inspect.ismethod(model):  # Is a method
                    getter.short_description = description \
                        or getattr(model, related_name).short_description
                    return getter
                else:

                    if inspect.ismethod(getattr(model, related_name)):
                        getter.short_description = description \
                            or getattr(model, related_name).short_description
                        return getter

                    else:
                        model = getattr(model, related_name).field
                        getter.short_description = description \
                            or _(model.verbose_name.title())
                        return getter

                if model.get_internal_type() in [
                    "ForeignKey", "ManyToManyField"
                ]:
                    model = model.related_model
                    getter.short_description = description \
                        or _(model._meta.verbose_name_plural.title())
                else:
                    getter.short_description = description \
                        or _(field._meta.verbose_name_plural.title())

        getter.allow_tags = True

        return getter
Example #26
0
    def getMethods(self):
        # remove the security proxy, so that `attr` is not proxied. We could
        # unproxy `attr` for each turn, but that would be less efficient.
        #
        # `getPermissionIds()` also expects the class's security checker not
        # to be proxied.
        klass = zope.security.proxy.removeSecurityProxy(self.klassView.context)
        obj = zope.security.proxy.removeSecurityProxy(self.context)

        for name in apidoc.utilities.getPublicAttributes(obj):
            val = getattr(obj, name)
            if not (inspect.ismethod(val) or inspect.ismethoddescriptor(val)):
                continue
            if inspect.ismethod(val):
                signature = apidoc.utilities.getFunctionSignature(val)
            else:
                signature = '(...)'

            entry = {
                'name': name,
                'signature': signature,
                'doc': apidoc.utilities.renderText(
                     val.__doc__ or '',
                     zapi.getParent(self.klassView.context).getPath()),
                'interface': apidoc.utilities.getInterfaceForAttribute(
                     name, klass._Class__all_ifaces)}

            entry.update(apidoc.utilities.getPermissionIds(
                name, klass.getSecurityChecker()))

            yield entry
Example #27
0
    def _find_sub_controllers(self, remainder):
        """
        Identifies the correct controller to route to by analyzing the
        request URI.
        """
        # need either a get_one or get to parse args
        method = None
        for name in ("get_one", "get"):
            if hasattr(self, name):
                method = name
                break
        if not method:
            return

        # get the args to figure out how much to chop off
        args = getargspec(getattr(self, method))
        fixed_args = len(args[0][1:]) - len(request.pecan.get("routing_args", []))
        var_args = args[1]

        # attempt to locate a sub-controller
        if var_args:
            for i, item in enumerate(remainder):
                controller = getattr(self, item, None)
                if controller and not ismethod(controller):
                    self._set_routing_args(remainder[:i])
                    return lookup_controller(controller, remainder[i + 1 :])
        elif fixed_args < len(remainder) and hasattr(self, remainder[fixed_args]):
            controller = getattr(self, remainder[fixed_args])
            if not ismethod(controller):
                self._set_routing_args(remainder[:fixed_args])
                return lookup_controller(controller, remainder[fixed_args + 1 :])
Example #28
0
    def dynamic_wrapper(wrapped, instance, args, kwargs):
        transaction = current_transaction()

        if transaction is None:
            return wrapped(*args, **kwargs)

        if callable(url):
            if instance and inspect.ismethod(wrapped):
                _url = url(instance, *args, **kwargs)
            else:
                _url = url(*args, **kwargs)

        else:
            _url = url

        if callable(method):
            if instance and inspect.ismethod(wrapped):
                _method = method(instance, *args, **kwargs)
            else:
                _method = method(*args, **kwargs)

        else:
            _method = method

        with ExternalTrace(transaction, library, _url, _method):
            return wrapped(*args, **kwargs)
Example #29
0
def flexapply(handler, args, kwargs):
    if inspect.isfunction(handler) or inspect.ismethod(handler):
        if inspect.ismethod(handler) and handler.im_self is not None:
            args = (handler.im_self, ) + args   
            handler = handler.im_func
        args, kwargs = buildargs(handler, args, kwargs)
    return handler(*args, **kwargs)
Example #30
0
def getexpected(func):
    """
    Returns as a 2-tuple the passed in object's expected arguments and
    whether or not it accepts variable keywords.
    """
    assert callable(func), 'object not callable'

    if not inspect.isclass(func):
        # At this point, we assume func is either a function, method, or
        # callable instance.
        if not inspect.isfunction(func) and not inspect.ismethod(func):
            func = getattr(func, '__call__') # When would this fail?

        argspec = inspect.getargspec(func)
        expected, varkw = argspec[0], argspec[2] is not None
        if inspect.ismethod(func):
            expected = expected[1:]
    else:
        # A class. Try to figure out the calling conventions of the
        # constructor.
        init = getattr(func, '__init__', None)
        # Sigh, this is getting into the realm of black magic...
        if init is not None and inspect.ismethod(init):
            argspec = inspect.getargspec(init)
            expected, varkw = argspec[0], argspec[2] is not None
            expected = expected[1:]
        else:
            expected, varkw = [], False

    return expected, varkw
 def get_public_fields(obj):
     return [
         attr for attr in dir(obj)
         if not (attr.startswith("_") or inspect.isbuiltin(attr)
                 or inspect.isfunction(attr) or inspect.ismethod(attr))
     ]
Example #32
0
 def check_validity_of_input(self, method):
     if not inspect.ismethod(method):
         raise Exception("Whoops, looks like you accidentally invoked "
                         "the method you want to animate")
     assert (isinstance(method.__self__, Mobject))
Example #33
0
def _get_handler_methods(lib):
    attrs = [getattr(lib, a) for a in dir(lib) if not a.startswith('_')]
    return [a for a in attrs if inspect.ismethod(a)]
Example #34
0
    def _generate_blobs(self):
        """
        Create a list of blobs, one per blob group.
        
        ..note:: This should only be run for individual simulations,
            not in the analysis of MCMC data.
        
        Returns
        -------
        List, where each element has shape (ivar x blobs). Each element of 
        this corresponds to the blobs for one blob group, which is defined by
        either its dimensionality, its independent variables, or both.
        
        For example, for 1-D blobs, self.blobs[i][j][k] would mean
            i = blob group
            j = index corresponding to elements of self.blob_names
            k = index corresponding to elements of self.blob_ivars[i]
        """

        self._blobs = []
        for i, element in enumerate(self.blob_names):

            this_group = []
            for j, key in enumerate(element):

                # 0-D blobs. Need to know name of attribute where stored!
                if self.blob_nd[i] == 0:
                    if self.blob_funcs[i][j] is None:
                        # Assume blob name is the attribute
                        #blob = self.__getattribute__(key)
                        blob = parse_attribute(key, self)

                    else:
                        fname = self.blob_funcs[i][j]

                        # In this case, the return of parse_attribute is
                        # a value, not a function to be applied to ivars.
                        blob = parse_attribute(fname, self)

                # 1-D blobs. Assume the independent variable is redshift
                # unless a function is provided
                elif self.blob_nd[i] == 1:
                    # The 0 index is because ivars are kept in a list no
                    # matter what
                    x = np.array(self.blob_ivars[i][0]).squeeze()
                    if (self.blob_funcs[i][j] is None) and (key
                                                            in self.history):
                        blob = np.interp(x, self.history['z'][-1::-1],
                                         self.history[key][-1::-1])
                    elif self.blob_funcs[i][j] is None:
                        raise KeyError('Blob {!s} not in history!'.format(key))
                    else:
                        fname = self.blob_funcs[i][j]

                        # Name of independent variable
                        xn = self.blob_ivarn[i][0]

                        if isinstance(fname, basestring):
                            func = parse_attribute(fname, self)
                        else:

                            print('hey {!s}'.format(fname))
                            raise ValueError('pretty sure this is broken!')

                            # fname is a slice, like ('igm_k_heat', 0)
                            # to retrieve heating rate from H ionizations
                            _xx = self.history['z'][-1::-1]
                            _yy = self.history[fname[0]][-1::-1, fname[1]]

                            func = (_xx, _yy)

                        if ismethod(func) or isinstance(func, interp1d) or \
                            (type(func) == FunctionType) \
                            or hasattr(func, '__call__'):

                            try:
                                if self.blob_kwargs[i] is not None:
                                    kw = self.blob_kwargs[i][j]
                                else:
                                    kw = {}

                                def func_kw(xx):
                                    _kw = kw.copy()
                                    _kw.update({xn: xx})
                                    return func(**_kw)

                                blob = np.array([func_kw(xx) for xx in x])

                            except TypeError:
                                blob = np.array(list(map(func, x)))

                        else:
                            blob = np.interp(x, func[0], func[1])

                else:

                    # Must have blob_funcs for this case
                    fname = self.blob_funcs[i][j]
                    tmp_f = parse_attribute(fname, self)

                    xarr, yarr = list(map(np.array, self.blob_ivars[i]))

                    if (type(tmp_f) is FunctionType) or ismethod(tmp_f) \
                        or hasattr(func, '__call__'):
                        func = tmp_f
                    elif type(tmp_f) is tuple:
                        z, E, flux = tmp_f
                        func = RectBivariateSpline(z, E, flux)
                    else:
                        raise TypeError(
                            'Sorry: don\'t understand blob {!s}'.format(key))

                    xn, yn = self.blob_ivarn[i]

                    blob = []
                    # We're assuming that the functions are vectorized.
                    # Didn't used to, but it speeds things up (a lot).
                    for x in xarr:
                        tmp = []

                        if self.blob_kwargs[i] is not None:
                            kw = self.blob_kwargs[i][j]
                        else:
                            kw = {}

                        kw.update({xn: x, yn: yarr})
                        result = func(**kw)

                        # Happens when we save a blob that isn't actually
                        # a PQ (i.e., just a constant). Need to kludge so it
                        # doesn't crash.
                        if type(result) in [int, float, np.float64]:
                            result = result * np.ones_like(yarr)

                        tmp.extend(result)
                        blob.append(tmp)

                this_group.append(np.array(blob))

            self._blobs.append(np.array(this_group))
Example #35
0
    def trace_call(self, frame, event, arg):
        log.info('Trace call (co_name=%s)' % get_co_name(frame))

        # retrieve the object for the function being called
        func_obj = get_function_obj(frame,
                                    src_lines=self.src_lines,
                                    filename=self.file_path)

        # either not a function call (e.g. entered code block)
        # or couldn't retrieve function source to get object
        if func_obj is None:
            log.info('Function object was None for source: %s' %
                     self._getsource(frame))
            if self._defined_by_user(frame):
                log.info('Tracing as defined by user')
                return self.trace
            else:
                log.info(
                    'Ignoring and treating as external since not defined by user'
                )
                log.info('Function from frame: %s' % get_co_name(frame))
                return None

        if is_stub_call(func_obj):
            log.info('Function object is a stub: %s' % get_co_name(frame))
            return self.trace_stub(func_obj, frame, event, arg)

        log.info('Collecting call made by user for: %s' % func_obj)
        # # increase the depth of the traced stack
        self.traced_stack_depth += 1
        caller_frame = get_caller_frame(frame)
        call_site_lineno = inspect.getlineno(caller_frame)
        call_site_line = self._getsource(caller_frame)

        # call details
        is_method = inspect.ismethod(func_obj)
        co_name = get_co_name(frame)
        qualname = get_function_qual_name(func_obj)
        module = get_function_module(func_obj)
        call_args = inspect.getargvalues(frame)
        abstract_call_args = get_abstract_vals(call_args)
        # we keep track of the memory location of the function object
        # because it can allow us to establish a link between a line that calls
        # an function and the actual function call entry
        mem_loc_func = safe_id(func_obj)
        details = dict(
            is_method=is_method,
            co_name=co_name,
            qualname=qualname,
            module=module,
            abstract_call_args=abstract_call_args,
            mem_loc_func=mem_loc_func,
            called_by_user=self._called_by_user(frame),
            defined_by_user=self._defined_by_user(frame),
        )
        event_id = self._allocate_event_id()
        trace_event = EnterCall(event_id, call_site_lineno, call_site_line,
                                details)
        log.info('Appending trace event: %s' % trace_event)
        self.push_trace_event(trace_event)

        return self.trace
Example #36
0
def use_np_array(func):
    """A decorator wrapping Gluon `Block`s and all its methods, properties, and static functions
    with the semantics of NumPy-array, which means that where ndarrays are created,
    `mxnet.numpy.ndarray`s should be created, instead of legacy ndarrays of type `mx.nd.NDArray`.
    For example, at the time when a parameter is created in a `Block`, an `mxnet.numpy.ndarray`
    is created if it's decorated with this decorator.

    Example::
        import mxnet as mx
        from mxnet import gluon, np


        class TestHybridBlock1(gluon.HybridBlock):
            def __init__(self):
                super(TestHybridBlock1, self).__init__()
                self.w = self.params.get('w', shape=(2, 2))

            def hybrid_forward(self, F, x, w):
                return F.dot(x, w)


        x = mx.nd.ones((2, 2))
        net1 = TestHybridBlock1()
        net1.initialize()
        out = net1.forward(x)
        for _, v in net1.collect_params().items():
            assert type(v.data()) is mx.nd.NDArray
        assert type(out) is mx.nd.NDArray


        @np.use_np_array
        class TestHybridBlock2(gluon.HybridBlock):
            def __init__(self):
                super(TestHybridBlock2, self).__init__()
                self.w = self.params.get('w', shape=(2, 2))

            def hybrid_forward(self, F, x, w):
                return F.np.dot(x, w)


        x = np.ones((2, 2))
        net2 = TestHybridBlock2()
        net2.initialize()
        out = net2.forward(x)
        for _, v in net2.collect_params().items():
            print(type(v.data()))
            assert type(v.data()) is np.ndarray
        assert type(out) is np.ndarray

    Parameters
    ----------
    func : a user-provided callable function or class to be scoped by the NumPy-array semantics.

    Returns
    -------
    Function or class
        A function or class wrapped in the NumPy-array scope.
    """
    if inspect.isclass(func):
        for name, method in inspect.getmembers(
                func,
                predicate=lambda f: inspect.isfunction(f) or inspect.ismethod(
                    f) or isinstance(f, property)):
            if isinstance(method, property):
                setattr(
                    func, name,
                    property(use_np_array(method.__get__), method.__set__,
                             method.__delattr__, method.__doc__))
            else:
                setattr(func, name, use_np_array(method))
        return func
    elif callable(func):

        @functools.wraps(func)
        def _with_np_array(*args, **kwargs):
            with np_array(active=True):
                return func(*args, **kwargs)

        return _with_np_array
    else:
        raise TypeError(
            'use_np_array can only decorate classes and callable objects, '
            'while received a {}'.format(str(type(func))))
Example #37
0
def use_np_shape(func):
    """A decorator wrapping a function or class with activated NumPy-shape semantics.
    When `func` is a function, this ensures that the execution of the function is scoped with NumPy
    shape semantics, such as the support for zero-dim and zero size tensors. When
    `func` is a class, it ensures that all the methods, static functions, and properties
    of the class are executed with the NumPy shape semantics.

    Example::
        import mxnet as mx
        @mx.use_np_shape
        def scalar_one():
            return mx.nd.ones(())
        print(scalar_one())

        @np.use_np_shape
        class ScalarTensor(object):
            def __init__(self, val=None):
                if val is None:
                    val = ScalarTensor.random().value
                self._scalar = mx.nd.ones(()) * val

            def __repr__(self):
                print("Is __repr__ in np_shape semantics? {}!".format(str(np.is_np_shape())))
                return str(self._scalar.asnumpy())

            @staticmethod
            def random():
                val = mx.nd.random.uniform().asnumpy().item()
                return ScalarTensor(val)

            @property
            def value(self):
                print("Is value property in np_shape semantics? {}!".format(str(np.is_np_shape())))
                return self._scalar.asnumpy().item()


        print("Is global scope of np_shape activated? {}!".format(str(np.is_np_shape())))
        scalar_tensor = ScalarTensor()
        print(scalar_tensor)

    Parameters
    ----------
    func : a user-provided callable function or class to be scoped by the NumPy-shape semantics.

    Returns
    -------
    Function or class
        A function or class wrapped in the NumPy-shape scope.
    """

    if inspect.isclass(func):
        for name, method in inspect.getmembers(
                func,
                predicate=lambda f: inspect.isfunction(f) or inspect.ismethod(
                    f) or isinstance(f, property)):
            if isinstance(method, property):
                setattr(
                    func, name,
                    property(use_np_shape(method.__get__), method.__set__,
                             method.__delattr__, method.__doc__))
            else:
                setattr(func, name, use_np_shape(method))
        return func
    elif callable(func):

        @functools.wraps(func)
        def _with_np_shape(*args, **kwargs):
            with np_shape(active=True):
                return func(*args, **kwargs)

        return _with_np_shape
    else:
        raise TypeError(
            'use_np_shape can only decorate classes and callable objects, '
            'while received a {}'.format(str(type(func))))
Example #38
0
 def _is_public_attribute(self, member):
     return not ismethod(member[1]) and not isinstance(
         member[1], Model) and member[0][0] != "_"
Example #39
0
    def invoke(self, model, prefix_args=None, varargs=False):
        '''
        Invoke a callable `model` as if it was a native function. If `varargs`
        is true, model receives a single argument that is a generator for
        function arguments. Pass a tuple of arguments for `prefix_args` you'd
        like to precede the actual arguments.

        :param callable model: Python model of the function
        :param tuple prefix_args: Parameters to pass to model before actual ones
        :param bool varargs: Whether the function expects a variable number of arguments
        :return: The result of calling `model`
        '''
        prefix_args = prefix_args or ()

        spec = inspect.getargspec(model)

        if spec.varargs:
            logger.warning("ABI: A vararg model must be a unary function.")

        nargs = len(spec.args) - len(prefix_args)

        # If the model is a method, we need to account for `self`
        if inspect.ismethod(model):
            nargs -= 1

        def resolve_argument(arg):
            if isinstance(arg, str):
                return self._cpu.read_register(arg)
            else:
                return self._cpu.read_int(arg)

        # Create a stream of resolved arguments from argument descriptors
        descriptors = self.get_arguments()
        argument_iter = imap(resolve_argument, descriptors)

        try:
            if varargs:
                result = model(*(prefix_args + (argument_iter, )))
            else:
                argument_tuple = prefix_args + tuple(
                    islice(argument_iter, nargs))
                result = model(*argument_tuple)
        except ConcretizeArgument as e:
            assert e.argnum >= len(
                prefix_args), "Can't concretize a constant arg"
            idx = e.argnum - len(prefix_args)

            # Arguments were lazily computed in case of varargs, so recompute here
            descriptors = self.get_arguments()
            src = next(islice(descriptors, idx, idx + 1))

            msg = 'Concretizing due to model invocation'
            if isinstance(src, str):
                raise ConcretizeRegister(src, msg)
            else:
                raise ConcretizeMemory(src, self._cpu.address_bit_size, msg)
        else:
            if result is not None:
                self.write_result(result)

            self.ret()

        return result
Example #40
0
 def is_user_method(m):
     return ((inspect.ismethod(m) or inspect.isfunction(m))
             and not inspect.isbuiltin(m)
             and not ClientProxyBuilder._method_name(m).startswith('__')
             and not ClientProxyBuilder._method_name(m).endswith('__'))
Example #41
0
def as_stub_func_if_any(func0,
                        decorated_func=None,
                        func_class=None,
                        nesting=None):
    # Check for stubfile
    # Todo: Compactify
    module = get_stub_module(func0)
    if not module is None:
        if hasattr(module, func0.__name__):
            res = getattr(module, func0.__name__)
            if inspect.isfunction(res) or inspect.ismethod(res) \
                    or inspect.ismethoddescriptor(res):
                return getattr(module, func0.__name__)
        if not decorated_func is None and ismethod(decorated_func):
            cls = util.get_class_that_defined_method(decorated_func)
            if hasattr(module, cls.__name__):
                cls2 = getattr(module, cls.__name__)
                if hasattr(cls2, func0.__name__):
                    return getattr(cls2, func0.__name__)
            else:
                if nesting is None:
                    nesting = util._get_class_nesting_list(
                        cls, sys.modules[cls.__module__])
                else:
                    nesting = nesting[:-1]
                if not nesting is None:
                    mcls = module
                    try:
                        for cl in nesting:
                            mcls = getattr(mcls, cl.__name__)
                        mcls = getattr(mcls, cls.__name__)
                        return getattr(mcls, func0.__name__)
                    except AttributeError:
                        pass
        elif not func_class is None:
            if hasattr(module, func_class.__name__):
                cls2 = getattr(module, func_class.__name__)
                if hasattr(cls2, func0.__name__):
                    return getattr(cls2, func0.__name__)
            else:
                if nesting is None:
                    nesting = util._get_class_nesting_list(
                        func_class, sys.modules[func_class.__module__])
                if not nesting is None:
                    mcls = module
                    try:
                        for cl in nesting:
                            mcls = getattr(mcls, cl.__name__)
                        mcls = getattr(mcls, func_class.__name__)
                        return getattr(mcls, func0.__name__)
                    except AttributeError:
                        pass

        if nesting is None:
            nesting = util._get_class_nesting_list_for_staticmethod(
                decorated_func, sys.modules[func0.__module__], [], set())
        if not nesting is None:
            mcls = module
            try:
                for cl in nesting:
                    mcls = getattr(mcls, cl.__name__)
                return getattr(mcls, func0.__name__)
            except AttributeError:
                pass
    return func0
Example #42
0
def _get_methods(cls):
    import inspect
    # In Python 3 unbound methods are functions, but in Python 2 they are methods
    return inspect.getmembers(
        cls, predicate=lambda x: inspect.isfunction(x) or inspect.ismethod(x))
Example #43
0
def validate_class(val):
    if inspect.isfunction(val) or inspect.ismethod(val):
        val = val()
    if inspect.isclass(val):
        return val
    return validate_string(val)
Example #44
0
 def is_procedure(obj):
     return (
         inspect.ismethod(obj) or
         inspect.isfunction(obj)
     )
Example #45
0
 def ismethod(self):
     """Return true if the object is a method.
     """
     return inspect.ismethod(self.obj)
Example #46
0
def is_method(func):
    # Under Python 3, there are no unbound methods
    return isfunction(func) or ismethod(func)
Example #47
0
 def predicate(o):
     if inspect.ismethod(o):
         return hasattr(o, "__route_method__") and hasattr(
             o, "__route_path__")
     return False
Example #48
0
 def findmethods(object):
     return inspect.ismethod(object) or inspect.isfunction(object)
Example #49
0
def _add_locals_data(data, exc_info):
    if not SETTINGS['locals']['enabled']:
        return

    frames = data['body']['trace']['frames']

    cur_tb = exc_info[2]
    frame_num = 0
    num_frames = len(frames)
    while cur_tb:
        cur_frame = frames[frame_num]
        tb_frame = cur_tb.tb_frame
        cur_tb = cur_tb.tb_next

        if not isinstance(tb_frame, types.FrameType):
            # this can happen if the traceback or frame is wrapped in some way,
            # for example by `ExceptionInfo` in
            # https://github.com/celery/billiard/blob/master/billiard/einfo.py
            log.warning('Traceback frame not a types.FrameType. Ignoring.')
            frame_num += 1
            continue

        # Create placeholders for args/kwargs/locals
        args = []
        kw = {}
        _locals = {}

        try:
            arginfo = inspect.getargvalues(tb_frame)
            local_vars = arginfo.locals
            argspec = None

            func = _get_func_from_frame(tb_frame)
            if func:
                if inspect.isfunction(func) or inspect.ismethod(func):
                    argspec = inspect.getargspec(func)
                elif inspect.isclass(func):
                    init_func = getattr(func, '__init__', None)
                    if init_func:
                        argspec = inspect.getargspec(init_func)

            # Fill in all of the named args
            for named_arg in arginfo.args:
                args.append(_local_repr(local_vars[named_arg]))

            # Add any varargs
            if arginfo.varargs is not None:
                args.extend(map(_local_repr, local_vars[arginfo.varargs]))

            # Fill in all of the kwargs
            if arginfo.keywords is not None:
                kw.update(
                    dict((k, _local_repr(v))
                         for k, v in local_vars[arginfo.keywords].items()))

            if argspec and argspec.defaults:
                # Put any of the args that have defaults into kwargs
                num_defaults = len(argspec.defaults)
                if num_defaults:
                    # The last len(argspec.defaults) args in arginfo.args should be added
                    # to kwargs and removed from args
                    kw.update(
                        dict(
                            zip(arginfo.args[-num_defaults:],
                                args[-num_defaults:])))
                    args = args[:-num_defaults]

            # Optionally fill in locals for this frame
            if local_vars and _check_add_locals(cur_frame, frame_num,
                                                num_frames):
                _locals.update(
                    dict((k, _local_repr(v)) for k, v in local_vars.items()))

            args = _scrub_obj(args)
            kw = _scrub_obj(kw)
            _locals = _scrub_obj(_locals)

        except Exception as e:
            log.exception(
                'Error while extracting arguments from frame. Ignoring.')

        if args:
            cur_frame['args'] = args
        if kw:
            cur_frame['kwargs'] = kw
        if _locals:
            cur_frame['locals'] = _locals

        frame_num += 1
Example #50
0
 def getAllMethod(self, module_str):
     module = __import__(module_str, fromlist=[''])
     method_list = [
         o for o in inspect.getmembers(module) if inspect.ismethod(o[1])
     ]
     return method_list
Example #51
0
 def outer(new_func):
     def wrapper(*args, **kwargs):
         return new_func(orig_func, *args, **kwargs)
     if inspect.ismethod(orig_func):
         setattr(orig_func.im_class, orig_func.__name__, wrapper)
     return wrapper
Example #52
0
    def wrapper(wrapped, instance, args, kwargs):
        transaction = current_transaction()

        if transaction is None:
            return wrapped(*args, **kwargs)

        if callable(name):
            if instance and inspect.ismethod(wrapped):
                _name = name(instance, *args, **kwargs)
            else:
                _name = name(*args, **kwargs)

        elif name is None:
            _name = callable_name(wrapped)

        else:
            _name = name

        if callable(group):
            if instance and inspect.ismethod(wrapped):
                _group = group(instance, *args, **kwargs)
            else:
                _group = group(*args, **kwargs)

        else:
            _group = group

        if callable(label):
            if instance and inspect.ismethod(wrapped):
                _label = label(instance, *args, **kwargs)
            else:
                _label = label(*args, **kwargs)

        else:
            _label = label

        if callable(params):
            if instance and inspect.ismethod(wrapped):
                _params = params(instance, *args, **kwargs)
            else:
                _params = params(*args, **kwargs)

        else:
            _params = params

        def _generator(generator):
            _gname = '%s (generator)' % _name

            try:
                value = None
                exc = None

                while True:
                    transaction = current_transaction()

                    params = {}

                    frame = generator.gi_frame

                    params['filename'] = frame.f_code.co_filename
                    params['lineno'] = frame.f_lineno

                    with FunctionTrace(transaction,
                                       _gname,
                                       _group,
                                       params=params):
                        try:
                            if exc is not None:
                                yielded = generator.throw(*exc)
                                exc = None
                            else:
                                yielded = generator.send(value)

                        except StopIteration:
                            raise

                        except Exception:
                            raise

                    try:
                        value = yield yielded

                    except Exception:
                        exc = sys.exc_info()

            finally:
                generator.close()

        with FunctionTrace(transaction, _name, _group, _label, _params):
            try:
                result = wrapped(*args, **kwargs)

            except Exception:
                raise

            else:
                if isinstance(result, types.GeneratorType):
                    return _generator(result)

                else:
                    return result
Example #53
0
    def create(cls,
               func,
               args=None,
               kwargs=None,
               connection=None,
               result_ttl=None,
               ttl=None,
               status=None,
               description=None,
               depends_on=None,
               timeout=None,
               id=None,
               origin=None,
               meta=None,
               failure_ttl=None,
               serializer=None):
        """Creates a new Job instance for the given function, arguments, and
        keyword arguments.
        """
        if args is None:
            args = ()
        if kwargs is None:
            kwargs = {}

        if not isinstance(args, (tuple, list)):
            raise TypeError('{0!r} is not a valid args list'.format(args))
        if not isinstance(kwargs, dict):
            raise TypeError('{0!r} is not a valid kwargs dict'.format(kwargs))

        job = cls(connection=connection, serializer=serializer)
        if id is not None:
            job.set_id(id)

        if origin is not None:
            job.origin = origin

        # Set the core job tuple properties
        job._instance = None
        if inspect.ismethod(func):
            job._instance = func.__self__
            job._func_name = func.__name__
        elif inspect.isfunction(func) or inspect.isbuiltin(func):
            job._func_name = '{0}.{1}'.format(func.__module__, func.__name__)
        elif isinstance(func, string_types):
            job._func_name = as_text(func)
        elif not inspect.isclass(func) and hasattr(
                func, '__call__'):  # a callable class instance
            job._instance = func
            job._func_name = '__call__'
        else:
            raise TypeError(
                'Expected a callable or a string, but got: {0}'.format(func))
        job._args = args
        job._kwargs = kwargs

        # Extra meta data
        job.description = description or job.get_call_string()
        job.result_ttl = parse_timeout(result_ttl)
        job.failure_ttl = parse_timeout(failure_ttl)
        job.ttl = parse_timeout(ttl)
        job.timeout = parse_timeout(timeout)
        job._status = status
        job.meta = meta or {}

        # dependency could be job instance or id
        if depends_on is not None:
            job._dependency_ids = [
                depends_on.id if isinstance(depends_on, Job) else depends_on
            ]
        return job
Example #54
0
 def isTest(attr):
     return inspect.ismethod(attr) and "EdscTest.setUp " not in str(attr)
Example #55
0
def main():
    p = optparse.OptionParser(__doc__.strip())
    p.add_option("-p",
                 "--phantom",
                 action="store",
                 type="string",
                 dest="phantom",
                 default=None,
                 help="Phantom import modules from a file")
    p.add_option(
        "-o",
        "--output-dir",
        action="store",
        type="string",
        dest="output_dir",
        default=None,
        help=("Write all output files to the given directory (instead "
              "of writing them as specified in the autosummary:: "
              "directives)"))
    options, args = p.parse_args()

    if len(args) == 0:
        p.error("wrong number of arguments")

    if options.phantom and os.path.isfile(options.phantom):
        import_phantom_module(options.phantom)

    # read
    names = {}
    for name, loc in get_documented(args).items():
        for (filename, sec_title, keyword, toctree) in loc:
            if toctree is not None:
                path = os.path.join(os.path.dirname(filename), toctree)
                names[name] = os.path.abspath(path)

    # write
    for name, path in sorted(names.items()):
        if options.output_dir is not None:
            path = options.output_dir

        if not os.path.isdir(path):
            os.makedirs(path)

        try:
            obj, name = import_by_name(name)
        except ImportError, e:
            print "Failed to import '%s': %s" % (name, e)
            continue

        fn = os.path.join(path, '%s.rst' % name)

        if os.path.exists(fn):
            # skip
            continue

        f = open(fn, 'w')

        try:
            f.write('%s\n%s\n\n' % (name, '=' * len(name)))

            if inspect.isclass(obj):
                if issubclass(obj, Exception):
                    f.write(format_modulemember(name, 'autoexception'))
                else:
                    f.write(format_modulemember(name, 'autoclass'))
            elif inspect.ismodule(obj):
                f.write(format_modulemember(name, 'automodule'))
            elif inspect.ismethod(obj) or inspect.ismethoddescriptor(obj):
                f.write(format_classmember(name, 'automethod'))
            elif callable(obj):
                f.write(format_modulemember(name, 'autofunction'))
            elif hasattr(obj, '__get__'):
                f.write(format_classmember(name, 'autoattribute'))
            else:
                f.write(format_modulemember(name, 'autofunction'))
        finally:
            f.close()
Example #56
0
 def pred(x):
     return (inspect.isfunction(x) or inspect.ismethod(x)
             or is_cython(x))
Example #57
0
def pyhelp(s, imports=None):
    """Return object description"""
    _import_modules(imports, globals(), None)
    return _getdoc(s)


def pysignature(s):
    """Return info about function parameters"""
    f = None
    try:
        f = eval(s)
    except Exception, ex:
        return "%s" % ex

    if inspect.ismethod(f):
        f = f.im_func
    if not inspect.isfunction(f):
        return ''
    (args, varargs, varkw, defaults) = inspect.getargspec(f)
    return (
        '%s: %s' %
        (f.__name__, inspect.formatargspec(args, varargs, varkw, defaults)))


def _getdoc(s):
    """Return string printed by `help` function"""
    obj = None
    try:
        obj = eval(s)
    except Exception, ex:
Example #58
0
def get_items(cls):
    """ get key/value items from an enum-like class (members represent enumeration key/value) """

    res = {k: v for k, v in getmembers(cls) if not (k.startswith("_") or ismethod(v))}
    return res
Example #59
0
    def build_py_coverage(self):
        objects = self.env.domaindata['py']['objects']
        modules = self.env.domaindata['py']['modules']

        skip_undoc = self.config.coverage_skip_undoc_in_source

        for mod_name in modules:
            ignore = False
            for exp in self.mod_ignorexps:
                if exp.match(mod_name):
                    ignore = True
                    break
            if ignore:
                continue

            try:
                mod = __import__(mod_name, fromlist=['foo'])
            except ImportError as err:
                self.warn('module %s could not be imported: %s' %
                          (mod_name, err))
                self.py_undoc[mod_name] = {'error': err}
                continue

            funcs = []
            classes = {}

            for name, obj in inspect.getmembers(mod):
                # diverse module attributes are ignored:
                if name[0] == '_':
                    # begins in an underscore
                    continue
                if not hasattr(obj, '__module__'):
                    # cannot be attributed to a module
                    continue
                if obj.__module__ != mod_name:
                    # is not defined in this module
                    continue

                full_name = '%s.%s' % (mod_name, name)

                if inspect.isfunction(obj):
                    if full_name not in objects:
                        for exp in self.fun_ignorexps:
                            if exp.match(name):
                                break
                        else:
                            if skip_undoc and not obj.__doc__:
                                continue
                            funcs.append(name)
                elif inspect.isclass(obj):
                    for exp in self.cls_ignorexps:
                        if exp.match(name):
                            break
                    else:
                        if full_name not in objects:
                            if skip_undoc and not obj.__doc__:
                                continue
                            # not documented at all
                            classes[name] = []
                            continue

                        attrs = []

                        for attr_name in dir(obj):
                            if attr_name not in obj.__dict__:
                                continue
                            attr = getattr(obj, attr_name)
                            if not (inspect.ismethod(attr)
                                    or inspect.isfunction(attr)):
                                continue
                            if attr_name[0] == '_':
                                # starts with an underscore, ignore it
                                continue
                            if skip_undoc and not attr.__doc__:
                                # skip methods without docstring if wished
                                continue

                            full_attr_name = '%s.%s' % (full_name, attr_name)
                            if full_attr_name not in objects:
                                attrs.append(attr_name)

                        if attrs:
                            # some attributes are undocumented
                            classes[name] = attrs

            self.py_undoc[mod_name] = {'funcs': funcs, 'classes': classes}
Example #60
0
def SUBRP(x):
    return inspect.isbuiltin(x) or inspect.isfunction(x) or inspect.ismethod(
        x) or inspect.isclass(x)