예제 #1
0
    def test_injector_writes_expected_content_when_parsing_lock(self):
        # Copy some assets
        pipfile_path = TMP_DIR.joinpath('Pipfile.lock')
        pyproject_path = TMP_DIR.joinpath('pyproject.toml')
        pyproject_reference = ASSETS_DIR.joinpath('pyproject_from_lock.toml')

        copy(ASSETS_DIR.joinpath('Pipfile.lock'), pipfile_path)
        copy(ASSETS_DIR.joinpath('pyproject.toml'), pyproject_path)

        deps = Dependencies(prod=['argparse (==1.4.0)', 'toml (==0.9.4)'],
                            dev=[
                                'aspy.yaml (==1.1.1)', 'astroid (==1.6.3)',
                                'cached-property (==1.4.2)', 'cfgv (==1.0.0)',
                                'identify (==1.0.16)', 'isort (==4.3.4)',
                                'lazy-object-proxy (==1.3.1)',
                                'mccabe (==0.6.1)', 'nodeenv (==1.3.0)',
                                'pre-commit (==1.8.2)', 'pylint (==1.8.4)',
                                'pyyaml (==3.12)', 'six (==1.11.0)',
                                'virtualenv (==15.2.0)', 'wrapt (==1.10.11)'
                            ])
        inject(deps, pyproject_path)

        with pyproject_reference.open('r') as expected_file, \
                pyproject_path.open('r') as infile:
            expected = toml.load(expected_file)
            result = toml.load(infile)

        self.assertDictEqual(expected, result)
예제 #2
0
def mainCLI(args):
    payload = "<script>alert(1);</script>"  #TODO: Add more scripts to test with
    if args.payload != '':
        payload = args.payload
    if args.inject:
        injector.inject(args.url, payload, args.keyword, args.cookie)
    else:
        crawler.crawl(args.url, payload, args.keyword, args.cookie)
예제 #3
0
def mainVerbose(args):
    #payload = "<script>alert(1);</script>" TODO: Add more scripts to test with
    if args['payload'] != '':
        payload = args['payload']
    if args['inject']:
        injector.inject(args['url'], payload, args['keyword'], args['cookie'])
    else:
        crawler.crawl(args['url'], payload, args['keyword'], args['cookie'])
예제 #4
0
def test_dependency_with_free_parameters():
    def dep(_: int):
        pass

    with raises(Uncachable):
        inject(dep)

    def dep_with_default(_: int = 0):
        pass

    inject(dep_with_default)
예제 #5
0
def wrap_fun(fun: Callable, injector: Injector) -> Callable:

    if ismethod(fun):
        fun = instance_method_wrapper(fun)

    # This blockes needs to come before the block that checks for __call__
    # to prevent infinite recursion.
    if hasattr(fun, "__bindings__"):
        return wrap_function(fun, injector)

    if hasattr(fun, "__call__") and not isinstance(fun, type):
        try:
            type_hints = get_type_hints(fun)
        except (AttributeError, TypeError):
            # Some callables are not supported by get_type_hints:
            # https://github.com/alecthomas/flask_injector/blob/master/flask_injector/__init__.py#L75
            wrap_it = False
        else:
            type_hints.pop("return", None)
            wrap_it = type_hints != {}
        if wrap_it:
            return wrap_fun(inject(fun), injector)

    if hasattr(fun, "view_class"):
        return wrap_class_based_view(fun, injector)
    elif hasattr(fun, "cls"):
        # Django Rest Framework ViewSet's as_view returns a callable that
        # does NOT have a view_class attribute. Instead it has a cls attribute.
        return wrap_drf_view_set(fun, injector)

    return fun
예제 #6
0
def wrap_fun(fun: T, injector: Injector) -> T:
    if isinstance(fun, LocalProxy):
        return fun  # type: ignore

    if ismethod(fun):
        fun = instance_method_wrapper(fun)

    # Important: this block needs to stay here so it's executed *before* the
    # hasattr(fun, '__call__') block below - otherwise things may crash.
    if hasattr(fun, '__bindings__'):
        return wrap_function(fun, injector)

    if hasattr(fun, '__call__') and not isinstance(fun, type):
        try:
            type_hints = get_type_hints(fun)
        except (AttributeError, TypeError):
            # Some callables aren't introspectable with get_type_hints,
            # let's assume they don't have anything to inject. The exception
            # types handled here are what I encountered so far.
            # It used to be AttributeError, then https://github.com/python/typing/pull/314
            # changed it to TypeError.
            wrap_it = False
        except NameError:
            wrap_it = True
        else:
            type_hints.pop('return', None)
            wrap_it = type_hints != {}
        if wrap_it:
            return wrap_fun(inject(fun), injector)

    if hasattr(fun, 'view_class'):
        return wrap_class_based_view(fun, injector)

    return fun
예제 #7
0
def wrap_fun(fun: T, injector: Injector) -> T:
    if isinstance(fun, LocalProxy):
        return fun  # type: ignore

    # Important: this block needs to stay here so it's executed *before* the
    # hasattr(fun, '__call__') block below - otherwise things may crash.
    if hasattr(fun, '__bindings__'):
        return wrap_function(fun, injector)

    if hasattr(fun, '__call__') and not isinstance(fun, type):
        try:
            type_hints = get_type_hints(fun)
        except (AttributeError, TypeError):
            # Some callables aren't introspectable with get_type_hints,
            # let's assume they don't have anything to inject. The exception
            # types handled here are what I encountered so far.
            # It used to be AttributeError, then https://github.com/python/typing/pull/314
            # changed it to TypeError.
            wrap_it = False
        except NameError:
            wrap_it = True
        else:
            type_hints.pop('return', None)
            wrap_it = type_hints != {}
        if wrap_it:
            return wrap_fun(inject(fun), injector)

    if hasattr(fun, 'view_class'):
        return wrap_class_based_view(fun, injector)

    return fun
예제 #8
0
            async def wrapped(_: Request):
                response = self.injector.call_with_injection(inject(func))
                if inspect.iscoroutine(response):
                    response = await response

                if isinstance(response, Response):
                    return response
                return self.response_class(content=response)
예제 #9
0
    def test_injector_writes_expected_content_when_parsing_Pipfile(self):
        # Copy some assets
        pipfile_path = TMP_DIR.joinpath('Pipfile')
        pyproject_path = TMP_DIR.joinpath('pyproject.toml')
        pyproject_reference = ASSETS_DIR.joinpath(
            'pyproject_from_pipfile.toml')

        copy(ASSETS_DIR.joinpath('Pipfile'), pipfile_path)
        copy(ASSETS_DIR.joinpath('pyproject.toml'), pyproject_path)

        deps = Dependencies(prod=['toml (~=0.9.4)', 'argparse (~=1.4.0)'],
                            dev=['pylint (~=1.8.4)', 'pre-commit (~=1.8.2)'])
        inject(deps, pyproject_path)

        with pyproject_reference.open('r') as expected_file, \
                pyproject_path.open('r') as infile:
            expected = toml.load(expected_file)
            result = toml.load(infile)

        self.assertDictEqual(expected, result)
예제 #10
0
def wrap_fun(fun, injector):
    if isinstance(fun, LocalProxy):
        return fun

    # Important: this block needs to stay here so it's executed *before* the
    # hasattr(fun, '__call__') block below - otherwise things may crash.
    if hasattr(fun, '__bindings__'):
        return wrap_function(fun, injector)

    if hasattr(fun, '__call__') and not isinstance(fun, type):
        bindings_from_annotations = injector._infer_injected_bindings(fun)
        if bindings_from_annotations:
            return wrap_fun(inject(**bindings_from_annotations)(fun), injector)

    if hasattr(fun, 'view_class'):
        return wrap_class_based_view(fun, injector)

    return fun
예제 #11
0
def flask_inject(func: Callable) -> Callable:
    """
    Inject dependencies using FlaskInjector at runtime.

    Only useful when running inside the Flask application context.
    """

    # Call the original @inject decorator to create the `__bindings__` map.
    injected_func = inject(func)

    # Wrap the function to fetch the Injector instance from the current Flask
    # application and use it to inject arguments when called.
    @functools.wraps(injected_func)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        i = flask.current_app.extensions['flask_injector'].injector
        return i.call_with_injection(injected_func, args=args, kwargs=kwargs)

    return wrapper
예제 #12
0
파일: test_io.py 프로젝트: xihe/runko
    def test_write_Mesh3V(self):
        ##################################################
        # write
        conf = Conf()
        conf.Nx = 2
        conf.Ny = 1
        conf.Nz = 1
        conf.NxMesh = 3
        conf.NyMesh = 1
        conf.NzMesh = 1 
        conf.Nspecies = 2
        conf.outdir = "io_test_mesh/"

        conf.dx  = 1.0
        conf.dy  = 1.0
        conf.dz  = 1.0
        conf.Nvx = 3
        conf.Nvy = 5
        conf.Nvz = 8

        conf.vxmin =  -1.0
        conf.vymin =  -2.0
        conf.vzmin =  -3.0
        conf.vxmax =   4.0
        conf.vymax =   5.0
        conf.vzmax =   6.0
        conf.refinement_level= 0
        conf.clip= True
        conf.clipThreshold= 1.0e-5

        if not os.path.exists( conf.outdir ):
            os.makedirs(conf.outdir)

        grid = pycorgi.oneD.Grid(conf.Nx, conf.Ny)
        grid.set_grid_lims(conf.xmin, conf.xmax, conf.ymin, conf.ymax)

        for i in range(grid.get_Nx()):
            for j in range(grid.get_Ny()):
                #if n.get_mpi_grid(i) == n.rank:
                c = pyrunko.vlv.oneD.Tile(conf.NxMesh, conf.NyMesh, conf.NzMesh)
                grid.add_tile(c, (i,) ) 
        injector.inject(grid, filler, conf ) #injecting plasma

        pyrunko.vlv.oneD.write_mesh(grid, 0, conf.outdir)

        ##################################################
        # read using analysis tools
        fname = conf.outdir + "meshes-0_0.h5"
        f = h5py.File(fname,'r')

        for i in range(grid.get_Nx()):
            for j in range(grid.get_Ny()):
                for k in range(grid.get_Nz()):
                    c = grid.get_tile(i,j,k)

                    #if n.get_mpi_grid(i,j) == n.rank:
                    if True:
                        for ispcs in range(conf.Nspecies):
                            block = c.get_plasma_species(0, ispcs)

                            for q in range(conf.NxMesh):
                                for r in range(conf.NyMesh):
                                    for s in range(conf.NzMesh):
                                        tinfo = TileInfo()
                                        tinfo.i = i
                                        tinfo.j = j
                                        tinfo.k = k
                                        tinfo.q = q
                                        tinfo.r = r
                                        tinfo.s = s
                                        tinfo.ispcs = ispcs

                                        #now assert 
                                        vm = get_mesh(f, tinfo)
                                        ref = block[q,r,s]
                                        self.compareMeshes(vm, ref)



        ##################################################
        # read back

        node2 = pycorgi.oneD.Grid(conf.Nx, conf.Ny)
        node2.set_grid_lims(conf.xmin, conf.xmax, conf.ymin, conf.ymax)

        for i in range(node2.get_Nx()):
            for j in range(node2.get_Ny()):
                #if n.get_mpi_grid(i) == n.rank:
                c = pyrunko.vlv.oneD.Tile(conf.NxMesh, conf.NyMesh, conf.NzMesh)
                node2.add_tile(c, (i,) ) 
        injector.inject(node2, injector.empty_filler, conf, empty=True) #injecting empty meshes

        #pyrunko.vlv.oneD.write_mesh(node2, 1, conf.outdir)
        pyrunko.vlv.oneD.read_mesh(node2,  0, "io_test_mesh")
        #pyrunko.vlv.oneD.write_mesh(node2, 2, conf.outdir)

        for i in range(node2.get_Nx()):
            for j in range(node2.get_Ny()):
                for k in range(node2.get_Nz()):
                    c1 = grid.get_tile(i,j,k)
                    c2 = node2.get_tile(i,j,k)

                    #if n.get_mpi_grid(i,j) == n.rank:
                    if True:
                        for ispcs in range(conf.Nspecies):
                            block1 = c1.get_plasma_species(0, ispcs)
                            block2 = c2.get_plasma_species(0, ispcs)

                            for q in range(conf.NxMesh):
                                for r in range(conf.NyMesh):
                                    for s in range(conf.NzMesh):
                                        tinfo = TileInfo()
                                        tinfo.i = i
                                        tinfo.j = j
                                        tinfo.k = k
                                        tinfo.q = q
                                        tinfo.r = r
                                        tinfo.s = s
                                        tinfo.ispcs = ispcs

                                        #now assert 
                                        vm1 = block1[q,r,s]
                                        vm2 = block2[q,r,s]
                                        self.compareMeshes(vm1, vm2)
예제 #13
0
 async def wrapped():
     handler = self.injector.call_with_injection(inject(func))
     if inspect.iscoroutine(handler):
         return await handler
예제 #14
0
 async def wrapped(request: Request, exc: Exception):
     return await self.injector.call_with_injection(
         inject(func), args=(request, exc)
     )
예제 #15
0
 def dep(_: 'Local' = inject()):
     pass
예제 #16
0
@contextmanager
def ctx_mgr() -> Iterator[str]:
    yield ""


async def afunc() -> str:
    return ""


@asynccontextmanager
async def actx_mgr() -> AsyncIterator[str]:
    yield ""


# check editor and mypy inference in these tests
test0 = inject(User)
test0 = 0
test1 = inject(func)
test1 = 0
test2 = inject(ctx_mgr)
test2 = 0
test3 = inject(afunc)
test3 = 0
test4 = inject(actx_mgr)
test4 = 0
test5: int = inject()
test5 = 0
# We can see that MyPy is able to infer type of injected func without
# annotation, but Pycharm isn't
예제 #17
0
 def func(i: int = inject(dep)) -> int:
     nonlocal count
     count += 1
     return i
예제 #18
0
 def run():
     return self.injector.call_with_injection(
         inject(func), args=args, kwargs=kwargs
     )
예제 #19
0
파일: dev4.py 프로젝트: Krissmedt/imprunko
    # node configuration
    conf = Conf()

    node = plasma.Grid(conf.Nx, conf.Ny)
    xmin = 0.0
    xmax = conf.dx * conf.Nx * conf.NxMesh
    ymin = 0.0
    ymax = conf.dy * conf.Ny * conf.NyMesh

    node.setGridLims(xmin, xmax, ymin, ymax)

    init.loadCells(node, conf)

    ##################################################
    # load values into cells
    injector.inject(node, filler, conf)

    insert_em(node, conf)

    #visualize initial condition
    plotAll(axs, node, conf, 0)

    vsol = pdev.AmrMomentumLagrangianSolver()

    print("solving momentum space push")
    cid = node.cellId(0, 0)
    cell = node.getCellPtr(cid)

    for lap in range(1, 20):
        print("-------lap {} -------".format(lap))
예제 #20
0
파일: server.py 프로젝트: solute/hrt
    def do_GET(self):
        """ Handle a GET-Request which was redirected here by injecting this servers URL """

        origin_url, url = injector.parse_path(
            self.path)  # reconstruct the originial url

        if self.path.startswith("/stop/"):
            return

        logging.info("reading url " + repr(url))

        # append missing parts of the original-url
        if url.startswith("//"):
            url = "http:" + url
        elif not url.startswith("http"):
            url = base_href(origin_url) + url.lstrip("/")

        parsed_url = urlparse.urlparse(url)

        if url.endswith(".jpg") or url.endswith(".gif") or url.endswith(
                ".png"):
            # images? - we do not need no stinkin images!
            content = u""
            info = {"Content-Type": "image/gif"}

        else:
            # create the request to the original URL and read it's content...
            headers = {
                "User-agent":
                "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11",
                "Accept": "*/*",
                "Accept-encoding": "identity",
                "Referer": origin_url,
                "Host": parsed_url.netloc
            }

            # ... using the original cookies ...
            for copy_header in ["Cookie", "X-Requested-With"]:
                if copy_header in self.headers:
                    headers[copy_header] = self.headers[copy_header]

            # ... fire ...
            req = urllib2.Request(url=url, headers=headers)

            # ... slurp ...
            try:
                opener = urllib2.build_opener(
                )  #urllib2.HTTPRedirectHandler, urllib2.HTTPCookieProcessor, urllib2.ProxyHandler)
                f = opener.open(req)
                info = f.info()
                content = ""
                while True:
                    time.sleep(0.1)  # dirty but effective
                    chunk = f.read()
                    if not chunk:
                        break
                    content += chunk
                f.close()
            except urllib2.HTTPError:
                content = ""
                info = {"Content-Type": "none"}

            # ... if its compressed decompress it.
            if info.get("Content-Encoding") == "gzip":
                content = zlib.decompress(content, 16 + zlib.MAX_WBITS)

            # ... decode encoding ...
            if "charset=" in info["Content-Type"]:
                encoding = info["Content-Type"].split("charset=")[-1]
                if encoding[0] in "'\"":
                    encoding = encoding[1:-1]
                content = unicode(content, encoding)
            else:
                # ...or guess it!
                try:
                    content = unicode(content, "utf-8")
                except:
                    try:
                        content = unicode(content, "latin-1")
                    except:
                        print "F****d up encoding in:", url

            # the current url is now the origin_url for injection
            # this will cause the downloads initiated by this document to have it self as origin_url
            origin_string = config.get("hrt_url") % base64.b64encode(url)
            content = injector.inject(content, info["Content-Type"],
                                      origin_string)

        self.send_response(200)

        # create the response for the original request...
        response_headers = {
            "Content-Length": str(len(content)),
            "Cache-Control": "nocache",  # disable caching
            "Content-Type": info["Content-Type"]
        }

        # copy the headers
        for key, value in response_headers.items():
            self.send_header(key, value)
        self.end_headers()

        # send the response
        if type(content) is unicode:
            content = content.encode("utf-8")
        self.wfile.write(content)

        ##        self.write_to_disk(url, content)

        if url != config.get("url"):  # omit self reference to root
            self.server.visualizer.add_relation(url=url, origin_url=origin_url)
예제 #21
0
 def dep(_=inject(sub_dep)):
     pass
예제 #22
0
    ##################################################
    # Path to be created
    #if grid.master:
    if True:
        if not os.path.exists(conf.outdir):
            os.makedirs(conf.outdir)

    ##################################################
    # initialize
    Nx = conf.Nx * conf.NxMesh
    modes = np.arange(Nx)
    #modes        = np.array([2])
    random_phase = np.random.rand(len(modes))

    injector.inject(grid, filler, conf)  #injecting plasma

    #insert initial electric field
    #insert_em(grid, conf)

    lap = 0
    plasma.write_yee(grid, lap)
    plasma.write_analysis(grid, lap)
    plasma.write_mesh(grid, lap)

    #Initial step backwards for velocity
    for j in range(grid.get_Ny()):
        for i in range(grid.get_Nx()):
            cell = grid.get_tileptr(i, j)
            cell.update_boundaries(grid)
    plasma.initial_step_1d(grid)
예제 #23
0
 def read(self, path, length, offset, fh):
     os.lseek(fh, offset, os.SEEK_SET)
     orig_data = os.read(fh, length)
     new_data = inject(path, length, offset, orig_data)
     return new_data
예제 #24
0
 async def wrapped(request: Request, call_next: Callable):
     return await self.injector.call_with_injection(
         inject(func), args=(request, call_next)
     )
예제 #25
0
 def wrapper(*args: Any, **kwargs: Any) -> Any:
     return injector.call_with_injection(callable=inject(fun),
                                         args=args,
                                         kwargs=kwargs)
예제 #26
0
def test_empty_dependency():
    def dep(_=inject()):
        pass

    with raises(EmptyDependency):
        inject(dep)
예제 #27
0
def inject_init(cls):
    cls.__init__ = inject(cls.__init__)

    return cls
예제 #28
0
 def dep(_=inject()):
     pass
예제 #29
0
파일: server.py 프로젝트: solute/hrt
    def do_GET(self):
        """ Handle a GET-Request which was redirected here by injecting this servers URL """

        origin_url, url = injector.parse_path(self.path) # reconstruct the originial url

        if self.path.startswith("/stop/"):
            return

        logging.info("reading url " + repr(url))

        # append missing parts of the original-url
        if url.startswith("//"):
            url = "http:" + url
        elif not url.startswith("http"):
            url = base_href(origin_url) + url.lstrip("/")

        parsed_url = urlparse.urlparse(url)

        if url.endswith(".jpg") or url.endswith(".gif") or url.endswith(".png"):
            # images? - we do not need no stinkin images!
            content = u""
            info = {"Content-Type": "image/gif"}

        else:
            # create the request to the original URL and read it's content...
            headers = {"User-agent": "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11",
                       "Accept": "*/*",
                       "Accept-encoding": "identity",
                       "Referer": origin_url,
                       "Host": parsed_url.netloc}

            # ... using the original cookies ...
            for copy_header in ["Cookie", "X-Requested-With"]:
                if copy_header in self.headers:
                    headers[copy_header] = self.headers[copy_header]

            # ... fire ...
            req = urllib2.Request(url = url,
                                  headers = headers)

            # ... slurp ...
            try:
                opener = urllib2.build_opener()#urllib2.HTTPRedirectHandler, urllib2.HTTPCookieProcessor, urllib2.ProxyHandler)
                f = opener.open(req)
                info = f.info()
                content = ""
                while True:
                    time.sleep(0.1) # dirty but effective
                    chunk = f.read()
                    if not chunk:
                        break
                    content += chunk
                f.close()
            except urllib2.HTTPError:
                content = ""
                info = {"Content-Type": "none"}

            # ... if its compressed decompress it.
            if info.get("Content-Encoding") == "gzip":
                content = zlib.decompress(content, 16 + zlib.MAX_WBITS)

            # ... decode encoding ...
            if "charset=" in info["Content-Type"]:
                encoding = info["Content-Type"].split("charset=")[-1]
                if encoding[0] in "'\"":
                    encoding = encoding[1:-1]
                content = unicode(content, encoding)
            else:
                # ...or guess it!
                try:
                    content = unicode(content, "utf-8")
                except:
                    try:
                        content = unicode(content, "latin-1")
                    except:
                        print "F****d up encoding in:", url

            # the current url is now the origin_url for injection
            # this will cause the downloads initiated by this document to have it self as origin_url
            origin_string = config.get("hrt_url") % base64.b64encode(url)
            content = injector.inject(content, info["Content-Type"], origin_string)


        self.send_response(200)

        # create the response for the original request...
        response_headers = {"Content-Length": str(len(content)),
                            "Cache-Control": "nocache", # disable caching
                            "Content-Type": info["Content-Type"]}

        # copy the headers
        for key, value in response_headers.items():
            self.send_header(key, value)
        self.end_headers()

        # send the response
        if type(content) is unicode:
            content = content.encode("utf-8")
        self.wfile.write(content)

##        self.write_to_disk(url, content)


        if url != config.get("url"): # omit self reference to root
            self.server.visualizer.add_relation(url = url, origin_url = origin_url)
예제 #30
0
 def dep1(_: Global = inject()):
     pass
예제 #31
0
 def dep2(_: 'Global' = inject()):
     pass
예제 #32
0
 def decorator(cls):
     setattr(cls, ROUTER_KEY, arguments)
     return singleton(inject(cls))