Best Python code snippet using tox_python
path_normalizer.py
Source:path_normalizer.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3"""4Page normalizer will modify uri_stem according the configuration.5"""6from complexconfig.configcontainer import configcontainer7import logging8logger = logging.getLogger("sniffer.pathnormalizer")9sniffer_config = configcontainer.get_config("sniffer")10class PathAdder(object):11 """12 Add some query values into page.13 The config should like "/testapi/index.html+user+password", when we meet the request on "/testapi/index.html", we14 will add query keys into path; for example, when we meet "/testapi/index.html?user=123&password=abc", we can get15 page "/testapi/index.html/user@123/password@abc", if the key has no value in query, we will use "____" instead16 """17 def __init__(self, config):18 self._query_key_list = list()19 self._prefix = ""20 parts = config.split("+")21 if not parts:22 return23 self._prefix = parts[0]24 self._query_key_list = parts[1:]25 def get_page(self, uri_stem, query_dict=None):26 """27 Add some values in the query_dict into the page, return the page.28 @:return [success, new_path]: the first param means if the path adder matches successfully, the second one29 returns the new path30 """31 if query_dict is None:32 query_dict = dict()33 if not self._query_key_list:34 return False, uri_stem35 if uri_stem != self._prefix:36 return False, uri_stem37 result_list = list()38 result_list.append(uri_stem)39 for key in self._query_key_list:40 value = query_dict.get(key, "____")41 result_list.append("{}@{}".format(key, value))42 result = "/".join(result_list)43 logger.debug("path adder change path %s to %s", uri_stem, result)44 return True, result45class PathRemover(object):46 """47 Remove some parts of path into query.48 The config should like "/testapi/index.html/user@****/password@****", when we meet the request on49 "/testapi/index.html/123/abc", we will get page "/testapi/index.html" and the query will have 2 more keys: user=12350 and password=abc51 """52 def __init__(self, config):53 self._path_parts = config.split("/")54 self._flags = [None] * len(self._path_parts)55 for idx, part in enumerate(self._path_parts):56 if part.endswith("@****"):57 key = part[:-5]58 if not key:59 logger.error("invalid config for path remover: %s", config)60 self._path_parts = []61 raise RuntimeError("invalid config")62 self._flags[idx] = key63 def get_page(self, uri_stem):64 """65 Remove some parts of uri into query dict.66 @:return [success, new_path, new_query_objects]: the first param means if the path adder matches successfully,67 the second one returns the new path, the third one means the new query objects from path.68 """69 if not self._path_parts:70 return False, uri_stem, {}71 uri_stem_parts = uri_stem.split("/")72 if len(uri_stem_parts) != len(self._path_parts):73 # length not match74 return False, uri_stem, {}75 result_parts = []76 new_query_dict = {}77 match = False78 for idx in range(len(uri_stem_parts)):79 uri_stem_part = uri_stem_parts[idx]80 match_part = self._path_parts[idx]81 flag = self._flags[idx]82 if not flag:83 if uri_stem_part != match_part:84 break85 else:86 result_parts.append(uri_stem_part)87 else:88 result_parts.append(flag)89 new_query_dict[flag] = uri_stem_part90 else:91 match = True92 if not match:93 return False, uri_stem, {}94 else:95 result_page = "/".join(result_parts)96 logger.debug("path remover change path from %s to %s", uri_stem, result_page)97 return True, result_page, new_query_dict98def gen_path_adders(config_str):99 """100 generate path adders from the config.101 :param config_str:102 :return: the list of the adders103 """104 items = config_str.split(",")105 new_adders = list()106 for item in items:107 adder = PathAdder(item)108 new_adders.append(adder)109 return new_adders110def gen_path_removers(config_str):111 """112 generate path removers from the config113 :param config_str:114 :return: the list of the removers115 """116 items = config_str.split(",")117 new_removers = list()118 for item in items:119 try:120 remover = PathRemover(item)121 new_removers.append(remover)122 except:123 # ignore124 pass125 return new_removers126adders_config = sniffer_config.item(key="filter.log.added_suffixes", caching=60, default=list(),127 cb_load=gen_path_adders)128removers_config = sniffer_config.item(key="filter.log.ignored_suffixes", caching=60, default=list(),129 cb_load=gen_path_removers)130def normalize_path(uri_stem, query_dict):131 adders = adders_config.get()132 removers = removers_config.get()133 for adder in adders:134 succ, path = adder.get_page(uri_stem, query_dict)135 if succ:136 return True, path, {}137 for remover in removers:138 succ, path, new_dict = remover.get_page(uri_stem)139 if succ:140 return True, path, new_dict141 # not match...
_uri.py
Source:_uri.py
1"""2Copyright (c) 2015 Contributors as noted in the AUTHORS file3This file is part of scylla.4scylla is free software: you can redistribute it and/or modify5it under the terms of the GNU Lesser General Public License as published by6the Free Software Foundation, either version 3 of the License, or7(at your option) any later version.8scylla is distributed in the hope that it will be useful,9but WITHOUT ANY WARRANTY; without even the implied warranty of10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11GNU Lesser General Public License for more details.12You should have received a copy of the GNU Lesser General Public License13along with scylla. If not, see <http://www.gnu.org/licenses/>.14"""15import re16import uuid17_URI_SCHEME_PATTERN = r'(?P<SCHEME>.*)://'18_URI_HOST_PATTERN = r'(?P<HOST>[\d\w\.\:\-]*)/?'19_URI_PATH_PATTERNS = [r'(?P<GRAPHS>(?<=/)graphs/?)',20 r'((?P<GRAPH>(?<=/graphs/)[\w-]*)/?)',21 r'(?P<NODES>(?<=/)nodes/?)',22 r'((?P<NODE>(?<=/nodes/)[\w-]*)/?)',23 r'(?P<SLOTS>(?<=/)slots/?)',24 r'((?P<SLOT>(?<=/slots/)[\w-]*)/?)',25 r'(?P<CONNECTIONS>(?<=/)connections/?)',26 r'((?P<CONNECTION>(?<=/connections/)[\w-]*)/?)']27_URI_PATTERN = '{0}{1}(?P<PATH>{2}?)?'.format(_URI_SCHEME_PATTERN,28 _URI_HOST_PATTERN,29 '?'.join(_URI_PATH_PATTERNS))30_URI_PARTS_RE = re.compile(_URI_PATTERN)31class URI(object):32 @property33 def scheme(self):34 return self._path_parts['SCHEME']35 @property36 def host(self):37 return self._path_parts['HOST']38 @property39 def has_graphs(self):40 return bool(self._path_parts['GRAPHS'])41 @property42 def graph(self):43 result = None44 if self.has_graphs:45 result = self._path_parts['GRAPH']46 if result:47 result = uuid.UUID(result)48 return result49 @property50 def has_nodes(self):51 return bool(self._path_parts['NODES'])52 @property53 def node(self):54 result = self._path_parts['NODE']55 if result:56 result = uuid.UUID(result)57 return result58 @property59 def has_slots(self):60 return bool(self._path_parts['SLOTS'])61 @property62 def slot(self):63 result = self._path_parts['SLOT']64 if result:65 result = uuid.UUID(result)66 return result67 @property68 def has_connections(self):69 if self.slot:70 return bool(self._path_parts['CONNECTIONS'])71 return False72 @property73 def connection(self):74 result = self._path_parts['CONNECTION']75 if result:76 result = uuid.UUID(result)77 return result78 @property79 def path(self):80 return self._path_parts['PATH']81 def __init__(self, uri):82 self._uri = str(uri)83 self._path_parts = _URI_PARTS_RE.match(self._uri).groupdict()84 def __str__(self):85 return self._uri86 def __repr__(self):...
TestHelpers.py
Source:TestHelpers.py
1import os2import shutil3from helpers.StaticMethods import get_bq_path4class TempFile:5 def __init__(self, file_path: str, content: str):6 self._file_path = file_path 7 self._deepest_existing = None8 if os.path.exists(self._file_path):9 raise Exception("Path specified for TempFile already exists, use a non-existent path.")10 self._path_parts = file_path.split('/')11 self._directory = '/'.join(self._path_parts[:-1]) 12 if not os.path.exists(self._directory):13 # Resolve current deepest folder that exists so that we can restore that state later14 parts_stripped = -215 test_path = '/'.join(self._path_parts[:parts_stripped])16 # The len comparison is a safeguard against wiping out too much17 while test_path and (len(self._path_parts) - abs(parts_stripped)) > len(get_bq_path().split('/')):18 if os.path.exists(test_path):19 self._deepest_existing = test_path20 break21 parts_stripped -= 122 test_path = '/'.join(self._path_parts[:parts_stripped])23 os.makedirs(self._directory)24 with open(self._file_path, 'w') as f:25 f.write(content)26 def __enter__(self):27 return self28 def __exit__(self, exc_type, exc_val, exc_tb):29 if self._deepest_existing and self._directory != self._deepest_existing:30 to_delete = '/'.join(self._path_parts[:len(self._deepest_existing.split('/'))+1])31 shutil.rmtree(to_delete)32 else:...
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!!