Best Python code snippet using localstack_python
test_wasm.py
Source:test_wasm.py  
1import re2from regexes import (3    CALL_REGEX, CALL_INDIRECT_REGEX,4    ELEM_REGEX, EXPORT_REGEX,5    FUNC_REGEX, GLOBAL_REGEX,6    IMPORT_REGEX, START_REGEX, TYPE_REGEX7)8from wasm import WASM9class TestWASM(WASM):10    def __init__(self):11        super(TestWASM, self).__init__()12    def shift_types(self, max_type):13        type_map = {}14        new_types = []15        for t in self.types:16            match = re.search(TYPE_REGEX, t)17            type_num = match.group(2)18            new_type_num = int(type_num) + int(max_type) + 119            type_map[type_num] = new_type_num20            new_type = re.sub(TYPE_REGEX, lambda x: f'{x.group(1)}{new_type_num}{x.group(3)}', t)21            new_types.append(new_type)22        self.types = new_types23        return type_map24    def shift_imports(self, type_map, max_import):25        new_imports = []26        for i in self.imports:27            m = re.search(IMPORT_REGEX, i)28            type_num = m.group(8)29            new_type_num = type_map[type_num]30            func_num = m.group(6)31            new_func_num = int(max_import) + 132            self.function_symbol_map[func_num] = new_func_num33            sub_func = lambda x: f'{m.group(1)}{m.group(2)}{m.group(3)}{m.group(4)}{m.group(5)}{new_func_num}{m.group(7)}{new_type_num}{m.group(9)}'34            new_import = re.sub(IMPORT_REGEX, sub_func, i)35            new_imports.append(new_import)36            max_import = int(max_import) + 137        self.imports = new_imports38        return max_import39    def shift_funcs(self, type_map, max_func_num):40        def get_type_num(f):41            return re.search(FUNC_REGEX, f).group(4)42        def get_func_num(f):43            return re.search(FUNC_REGEX, f).group(2)44        def inject_new_type_num(f, new_type_num):45            sub_func = lambda x: f'{x.group(1)}{x.group(2)}{x.group(3)}{new_type_num}{x.group(5)}'46            return re.sub(FUNC_REGEX, sub_func, f)47        def inject_new_func_num(f, new_func_num):48            sub_func = lambda x: f'{x.group(1)}{new_func_num}{x.group(3)}{x.group(4)}{x.group(5)}'49            return re.sub(FUNC_REGEX, sub_func, f)50        new_funcs = []51        for f in self.funcs:52            type_num = get_type_num(f)53            new_type_num = type_map[type_num]54            func_num = get_func_num(f)55            new_func_num = int(max_func_num) + 156            self.function_symbol_map[func_num] = new_func_num57            max_func_num = new_func_num58            new_func = inject_new_func_num(f, new_func_num)59            new_func = inject_new_type_num(new_func, new_type_num)60            new_funcs.append(new_func)61        self.funcs = new_funcs62        return max_func_num63    def shift_calls(self, type_map):64        new_funcs = []65        for f in self.funcs:66            func = ''67            for l in f.split('\n'):68                if re.search(CALL_INDIRECT_REGEX, l):69                    func += self.shift_call_indirect(l, type_map) + '\n'70                elif re.search(CALL_REGEX, l):71                    func += self.shift_call(l) + '\n'72                else:73                    func += l + '\n'74            new_funcs.append(func)75        self.funcs = new_funcs76    def shift_call(self, line):77        match = re.search(CALL_REGEX, line)78        func_num = match.group(2)79        new_num = self.function_symbol_map[func_num]80        new_call = re.sub(CALL_REGEX, lambda x: f'{x.group(1)}{new_num}{x.group(3)}', line)81        return new_call82    def shift_call_indirect(self, line, type_map):83        match = re.search(CALL_INDIRECT_REGEX, line)84        type_num = match.group(2)85        new_num = type_map[type_num]86        new_call = re.sub(CALL_INDIRECT_REGEX, lambda x: f'{x.group(1)}{new_num}{x.group(3)}', line)87        return new_call88    def shift_start(self):89        if self.start:90            match = re.search(START_REGEX, self.start)91            func_num = match.group(2)92            new_num = self.function_symbol_map[func_num]93            new_start = re.sub(START_REGEX, lambda x: f'{x.group(1)}{new_num}{x.group(3)}', self.start)94            self.start = new_start95    def shift_exports(self):96        def get_func_num(f):97            return re.search(EXPORT_REGEX, f).group(4)98        def inject_new_func_num(f, new_func_num):99            return re.sub(EXPORT_REGEX, lambda x: f'{x.group(1)}{x.group(2)}{x.group(3)}{new_func_num}{x.group(5)}', f)100        def normalize(val):101            ret_val = '_'102            for i in range(0, len(val)):103                ret_val += '_' if val[i] == '-' or val[i] == '.' else val[i]104            return ret_val105        new_exports = []106        exports_map = {}107        for e in self.exports:108            if e.find('(func ') > -1:109                func_num = get_func_num(e)110                new_func_num = self.function_symbol_map[func_num]111                new_export = inject_new_func_num(e, new_func_num)112                new_exports.append(new_export)113            else:114                new_exports.append(e)115            exports_map[normalize(re.search(EXPORT_REGEX, e).group(2))] = new_func_num116        self.exports = new_exports117        return exports_map118    def shift_elems(self):119        new_elems = []120        for e in self.elems:121            match = re.search(ELEM_REGEX, e)122            new_numbers = []123            if match:124                numbers = match.group(2)125                for n in numbers.split():126                    shifted_num = self.function_symbol_map[n]127                    new_numbers.append(str(shifted_num))128            new_numbers_str = ' '.join(new_numbers)129            sub_func = lambda x: f'{x.group(1)}{new_numbers_str}{x.group(3)}'130            new_elems.append(re.sub(ELEM_REGEX, sub_func, e))131        self.elems = new_elems132    def get_max_global(self):133        max_global_var = -1134        for g in self.global_vars:135            match = re.search(GLOBAL_REGEX, g)136            max_global_var = match.group(2)...jinja_utils.py
Source:jinja_utils.py  
1#!/usr/bin/env python2# Copyright (c) 2007 fuel-ccp3# Licensed under the Apache License Version 2.0.4# https://github.com/openstack/fuel-ccp5import os6import re7import jinja28from six.moves.urllib import parse as urlparse9# pylint: disable=E021110class SilentUndefined(jinja2.Undefined):11    def _fail_with_undefined_error(self, *args, **kwargs):12        return ''13    def _new(*args, **kwargs):14        return SilentUndefined()15    __call__ = __getitem__ = __getattr__ = _new16    __add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \17        __truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \18        __mod__ = __rmod__ = __pos__ = __neg__ = __lt__ = __le__ = \19        __gt__ = __ge__ = __int__ = __float__ = __complex__ = __pow__ = \20        __rpow__ = _fail_with_undefined_error21def get_host(path):22    return urlparse.urlsplit(path).netloc23def j2raise(msg):24    raise AssertionError(msg)25def jinja_render(path, context, functions=(), ignore_undefined=False):26    kwargs = {}27    if ignore_undefined:28        kwargs['undefined'] = SilentUndefined29    else:30        kwargs['undefined'] = jinja2.StrictUndefined31    env = jinja2.Environment(loader=jinja2.FileSystemLoader(32        os.path.dirname(path)), **kwargs)33    env.filters['host'] = get_host34    # FIXME: gethostbyname should be only used during config files render35    env.filters['gethostbyname'] = lambda x: x36    for func in functions:37        env.globals[func.__name__] = func38    env.globals['raise_exception'] = j2raise39    content = env.get_template(os.path.basename(path)).render(context)40    return content41def generate_jinja_imports(exports_map):42    """Generate a files header of jinja imports from exports map"""43    imports = []  # list of j2 imports: "{% import 'msg.j2' as msg %}"44    for export_key in exports_map:45        name = exports_map[export_key]['name']  # real filename46        import_as, extension = os.path.splitext(name)  # remove file extension47        if not re.match('[a-zA-Z_][a-zA-Z0-9_]*', import_as):48            raise RuntimeError('Wrong templates file naming: the %s cannot be '49                               'imported by jinja with %s name. Please use '50                               'python compatible naming' % (name, import_as))51        imports.append(52            "{% import '" + name + "' as " + import_as + " with context %}")...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
