Beispiel #1
0
 def __init__(self,
     title,
     handler,
     image=None,
     attributes=None,
     state=False,
     discoverability_title=None,
 ):
     self._menu = None
     self._handler = handler
     self._title = title
     self._image = image
     self._attributes = attributes
     self._state = state
     self._discoverability_title = discoverability_title
     
     def _action_handler(_cmd):
         self._handler(
             self._menu.button,
             self
         )
 
     _action_handler_block = objc_util.ObjCBlock(
         _action_handler,
         restype=None, 
         argtypes=[ctypes.c_void_p])
     objc_util.retain_global(_action_handler_block)
     
     self._objc_action = UIAction.actionWithHandler_(_action_handler_block)
     
     self._update_objc_action()
Beispiel #2
0
def swizzle(cls_name, selector_name, fn):
    cls = ObjCClass(cls_name).ptr

    new_selector_name = SWIZZLED_SELECTOR_PREFIX + selector_name
    new_selector = sel(new_selector_name)

    if c.class_getInstanceMethod(cls, new_selector):
        error('Skipping swizzling, already responds to {} selector'.format(
            new_selector_name))
        return

    selector = sel(selector_name)
    method = c.class_getInstanceMethod(cls, selector)
    if not method:
        error('Failed to get {} instance method'.format(selector_name))
        return

    type_encoding = c.method_getTypeEncoding(method)
    parsed_types = parse_types(type_encoding)
    restype = parsed_types[0]
    argtypes = parsed_types[1]

    IMPTYPE = CFUNCTYPE(restype, *argtypes)
    imp = IMPTYPE(fn)
    retain_global(imp)

    did_add = c.class_addMethod(cls, new_selector, imp, type_encoding)

    if not did_add:
        error('Failed to add {} method'.format(new_selector_name))
        return

    new_method = c.class_getInstanceMethod(cls, new_selector)
    method_exchangeImplementations(method, new_method)
Beispiel #3
0
def add_method(cls_name, selector_name, fn, type_encoding):
    cls = ObjCClass(cls_name).ptr

    selector = sel(selector_name)

    if c.class_getInstanceMethod(cls, selector):
        error(
            'Failed to add method, class {} already provides method {}'.format(
                cls_name, selector_name))
        return

    parsed_types = parse_types(type_encoding)
    restype = parsed_types[0]
    argtypes = parsed_types[1]

    IMPTYPE = CFUNCTYPE(restype, *argtypes)
    imp = IMPTYPE(fn)
    retain_global(imp)

    did_add = c.class_addMethod(cls, selector, imp,
                                c_char_p(type_encoding.encode('utf-8')))
    if not did_add:
        error('Failed to add class method')

    return did_add
Beispiel #4
0
    def get(self, url=None, auth=None, headers=None, params=None):
        # Make url
        if params:
            params_encoded = urlencode(params)
        else:
            params_encoded = ""

        url = objc_util.nsurl("{}?{}".format(url, params_encoded))

        #request = objc_util.ObjCClass("NSURLRequest").request(URL=url)
        request = objc_util.ObjCClass(
            'NSMutableURLRequest').alloc().initWithURL_(url)

        # Make headers
        if headers:
            for key in headers:
                request.setValue_forHTTPHeaderField_(headers[key], key)

        if auth:
            userName, password = auth
            authStr = "%s:%s" % (userName, password)
            authencode = base64.b64encode(bytes(authStr))
            request.addValue_forHTTPHeaderField_("Basic %s" % authencode,
                                                 "Authorization")

        configuration = objc_util.ObjCClass(
            "NSURLSessionConfiguration").defaultSessionConfiguration()
        session = objc_util.ObjCClass(
            "NSURLSession").sessionWithConfiguration_(configuration)

        completionHandler = objc_util.ObjCBlock(
            self.responseHandlerBlock,
            restype=None,
            argtypes=[c_void_p, c_void_p, c_void_p, c_void_p])
        objc_util.retain_global(completionHandler)

        #dataTask = session.dataTask(Request=request, completionHandler=completionHandler)
        dataTask = session.dataTaskForRequest_completion_(
            request, completionHandler)
        dataTask.resume()

        # Wait for completions
        wait = True
        while wait:
            if self.data != None:
                wait = False
                return json.loads(self.data)
            elif self.error != None:
                wait = False
                raise RequestsException(["Error in request", self.error])
Beispiel #5
0
def _register_key_command(input, modifier_flags, function, title=None):
    if not UIApplication.sharedApplication().respondsToSelector_(
            sel('originalkeyCommands')):
        swizzle('UIApplication', 'keyCommands', _blackmamba_keyCommands)

    selector_name = _key_command_selector_name(input, modifier_flags)
    selector = sel(selector_name)
    obj = UIApplication.sharedApplication()

    info('Registering key command "{}" ({})'.format(
        _shortcut_name(input, modifier_flags), title
        or 'No discoverability title'))

    if not callable(function):
        error('Skipping, provided function is not callable')
        return False

    if obj.respondsToSelector_(selector):
        error('Skipping, method {} already registered'.format(selector_name))
        return False

    def key_command_action(_sel, _cmd, sender):
        function()

    IMPTYPE = CFUNCTYPE(None, c_void_p, c_void_p, c_void_p)
    imp = IMPTYPE(key_command_action)
    retain_global(imp)

    cls = c.object_getClass(obj.ptr)
    type_encoding = c_char_p('v@:@'.encode('utf-8'))
    did_add = c.class_addMethod(cls, selector, imp, type_encoding)
    if not did_add:
        error('Failed to add key command method {}'.format(selector_name))
        return False

    if isinstance(modifier_flags, UIKeyModifier):
        modifier_flags = modifier_flags.value

    if title:
        kc = UIKeyCommand.keyCommandWithInput_modifierFlags_action_discoverabilityTitle_(
            ns(input), modifier_flags, selector, ns(title))
    else:
        kc = UIKeyCommand.keyCommandWithInput_modifierFlags_action_(
            ns(input), modifier_flags, selector)

    _key_commands.append(kc)
    return True
Beispiel #6
0
def _add_method(cls, func):
    # void, object, selector
    type_encoding = "v@:"
    sel_name = str(uuid.uuid4())
    sel = objc_util.sel(sel_name)
    class_ptr = objc_util.object_getClass(cls.ptr)

    # ----------------- Modified from objc_util.add_method ------------------ #
    parsed_types = objc_util.parse_types(type_encoding)
    restype, argtypes, _ = parsed_types
    imp = ctypes.CFUNCTYPE(restype, *argtypes)(func)
    objc_util.retain_global(imp)
    if isinstance(type_encoding, str):
        type_encoding = type_encoding.encode('ascii')
    objc_util.class_addMethod(class_ptr, sel, imp, type_encoding)
    # ----------------------------------------------------------------------- #
    return sel
Beispiel #7
0
def add_method(method, cls):
	guard_objc_util()
	import ctypes
	import objc_util
	import uuid
	
	encoding = "v@:"
	
	# this code is borrowed from `objc_util._add_method`
	parsed_types = objc_util.parse_types(encoding)
	restype = parsed_types[0]
	argtypes = parsed_types[1]
	IMPTYPE = ctypes.CFUNCTYPE(restype, *argtypes)
	imp = IMPTYPE(method)
	objc_util.retain_global(imp)
	
	selector = objc_util.sel(str(uuid.uuid1()).replace("-", ""))
	objc_util.class_addMethod(objc_util.object_getClass(cls.ptr), selector, imp, encoding.encode("ascii"))
	return selector
Beispiel #8
0
def validate(url, params, responseHandler):
    if params:
        params_encoded = urllib.parse.urlencode(params)
    else:
        params_encoded = ""
    url = objc_util.nsurl("{}?{}".format(url, params_encoded))
    request = NSURLRequest.request(URL=url)
    configuration = NSURLSessionConfiguration.defaultSessionConfiguration()

    session = NSURLSession.session(Configuration=configuration)

    completionHandler = objc_util.ObjCBlock(
        responseHandler,
        restype=None,
        argtypes=[c_void_p, c_void_p, c_void_p, c_void_p])
    objc_util.retain_global(completionHandler)

    dataTask = session.dataTask(Request=request,
                                completionHandler=completionHandler)
    dataTask.resume()
Beispiel #9
0
    def __init__(self, url=None, params=None):
        self.data = None

        if params:
            params_encoded = urlencode(params)
        else:
            params_encoded = ""
        url = objc_util.nsurl("{}?{}".format(url, params_encoded))
        request = objc_util.ObjCClass("NSURLRequest").request(URL=url)
        configuration = objc_util.ObjCClass(
            "NSURLSessionConfiguration").defaultSessionConfiguration()

        session = objc_util.ObjCClass("NSURLSession").session(
            Configuration=configuration)

        completionHandler = objc_util.ObjCBlock(
            self.responseHandlerBlock,
            restype=None,
            argtypes=[c_void_p, c_void_p, c_void_p, c_void_p])
        objc_util.retain_global(completionHandler)

        dataTask = session.dataTask(Request=request,
                                    completionHandler=completionHandler)
        dataTask.resume()