Best Python code snippet using localstack_python
scaffold.py
Source:scaffold.py
...35 sanitized += "_"36 if sanitized.startswith("__"):37 sanitized = sanitized[1:]38 return sanitized39def html_to_rst(html: str):40 import pypandoc41 doc = pypandoc.convert_text(html, "rst", format="html")42 doc = doc.replace("\_", "_") # noqa: W60543 doc = doc.replace("\|", "|") # noqa: W60544 doc = doc.replace("\ ", " ") # noqa: W60545 rst = doc.strip()46 return rst47class ShapeNode:48 service: ServiceModel49 shape: Shape50 def __init__(self, service: ServiceModel, shape: Shape) -> None:51 super().__init__()52 self.service = service53 self.shape = shape54 @property55 def is_request(self):56 for operation_name in self.service.operation_names:57 operation = self.service.operation_model(operation_name)58 if operation.input_shape is None:59 continue60 if to_valid_python_name(self.shape.name) == to_valid_python_name(61 operation.input_shape.name62 ):63 return True64 return False65 @property66 def name(self) -> str:67 return to_valid_python_name(self.shape.name)68 @property69 def is_exception(self):70 metadata = self.shape.metadata71 return metadata.get("error") or metadata.get("exception")72 @property73 def is_primitive(self):74 return self.shape.type_name in ["integer", "boolean", "float", "double", "string"]75 @property76 def is_enum(self):77 return isinstance(self.shape, StringShape) and self.shape.enum78 @property79 def dependencies(self) -> List[str]:80 shape = self.shape81 if isinstance(shape, StructureShape):82 return [to_valid_python_name(v.name) for v in shape.members.values()]83 if isinstance(shape, ListShape):84 return [to_valid_python_name(shape.member.name)]85 if isinstance(shape, MapShape):86 return [to_valid_python_name(shape.key.name), to_valid_python_name(shape.value.name)]87 return []88 def _print_structure_declaration(self, output, doc=True, quote_types=False):89 if self.is_exception:90 self._print_as_class(output, "ServiceException", doc)91 return92 if any(map(is_keyword, self.shape.members.keys())):93 self._print_as_typed_dict(output)94 return95 if self.is_request:96 base = "ServiceRequest"97 else:98 base = "TypedDict, total=False"99 self._print_as_class(output, base, doc, quote_types)100 def _print_as_class(self, output, base: str, doc=True, quote_types=False):101 output.write(f"class {to_valid_python_name(self.shape.name)}({base}):\n")102 q = '"' if quote_types else ""103 if doc:104 self.print_shape_doc(output, self.shape)105 if not self.shape.members:106 output.write(" pass\n")107 for k, v in self.shape.members.items():108 if k in self.shape.required_members:109 output.write(f" {k}: {q}{to_valid_python_name(v.name)}{q}\n")110 else:111 output.write(f" {k}: Optional[{q}{to_valid_python_name(v.name)}{q}]\n")112 def _print_as_typed_dict(self, output, doc=True, quote_types=False):113 name = to_valid_python_name(self.shape.name)114 q = '"' if quote_types else ""115 output.write('%s = TypedDict("%s", {\n' % (name, name))116 for k, v in self.shape.members.items():117 if k in self.shape.required_members:118 output.write(f' "{k}": {q}{to_valid_python_name(v.name)}{q},\n')119 else:120 output.write(f' "{k}": Optional[{q}{to_valid_python_name(v.name)}{q}],\n')121 output.write("}, total=False)")122 def print_shape_doc(self, output, shape):123 html = shape.documentation124 rst = html_to_rst(html)125 if rst:126 output.write(' """')127 output.write(f"{rst}\n")128 output.write(' """\n')129 def print_declaration(self, output, doc=True, quote_types=False):130 shape = self.shape131 q = '"' if quote_types else ""132 if isinstance(shape, StructureShape):133 self._print_structure_declaration(output, doc, quote_types)134 elif isinstance(shape, ListShape):135 output.write(136 f"{to_valid_python_name(shape.name)} = List[{q}{to_valid_python_name(shape.member.name)}{q}]"137 )138 elif isinstance(shape, MapShape):139 output.write(140 f"{to_valid_python_name(shape.name)} = Dict[{q}{to_valid_python_name(shape.key.name)}{q}, {q}{to_valid_python_name(shape.value.name)}{q}]"141 )142 elif isinstance(shape, StringShape):143 if shape.enum:144 output.write(f"class {to_valid_python_name(shape.name)}(str):\n")145 for value in shape.enum:146 name = to_valid_python_name(value)147 output.write(f' {name} = "{value}"\n')148 else:149 output.write(f"{to_valid_python_name(shape.name)} = str")150 elif shape.type_name == "string":151 output.write(f"{to_valid_python_name(shape.name)} = str")152 elif shape.type_name == "integer":153 output.write(f"{to_valid_python_name(shape.name)} = int")154 elif shape.type_name == "long":155 output.write(f"{to_valid_python_name(shape.name)} = int")156 elif shape.type_name == "double":157 output.write(f"{to_valid_python_name(shape.name)} = float")158 elif shape.type_name == "float":159 output.write(f"{to_valid_python_name(shape.name)} = float")160 elif shape.type_name == "boolean":161 output.write(f"{to_valid_python_name(shape.name)} = bool")162 elif shape.type_name == "blob":163 output.write(164 f"{to_valid_python_name(shape.name)} = bytes"165 ) # FIXME check what type blob really is166 elif shape.type_name == "timestamp":167 output.write(f"{to_valid_python_name(shape.name)} = datetime")168 else:169 output.write(170 f"# unknown shape type for {to_valid_python_name(shape.name)}: {shape.type_name}"171 )172 # TODO: BoxedInteger?173 output.write("\n")174 def get_order(self):175 """176 Defines a basic order in which to sort the stack of shape nodes before printing.177 First all non-enum primitives are printed, then enums, then exceptions, then all other types.178 """179 if self.is_primitive:180 if self.is_enum:181 return 1182 else:183 return 0184 if self.is_exception:185 return 2186 return 3187def generate_service_types(output, service: ServiceModel, doc=True):188 output.write("import sys\n")189 output.write("from typing import Dict, List, Optional\n")190 output.write("from datetime import datetime\n")191 output.write("if sys.version_info >= (3, 8):\n")192 output.write(" from typing import TypedDict\n")193 output.write("else:\n")194 output.write(" from typing_extensions import TypedDict\n")195 output.write("\n")196 output.write(197 "from localstack.aws.api import handler, RequestContext, ServiceException, ServiceRequest"198 )199 output.write("\n")200 # ==================================== print type declarations201 nodes: Dict[str, ShapeNode] = {}202 for shape_name in service.shape_names:203 shape = service.shape_for(shape_name)204 nodes[to_valid_python_name(shape_name)] = ShapeNode(service, shape)205 # output.write("__all__ = [\n")206 # for name in nodes.keys():207 # output.write(f' "{name}",\n')208 # output.write("]\n")209 printed: Set[str] = set()210 visited: Set[str] = set()211 stack: List[str] = list(nodes.keys())212 stack = sorted(stack, key=lambda name: nodes[name].get_order())213 stack.reverse()214 while stack:215 name = stack.pop()216 if name in printed:217 continue218 node = nodes[name]219 dependencies = [dep for dep in node.dependencies if dep not in printed]220 if not dependencies:221 node.print_declaration(output, doc=doc)222 printed.add(name)223 elif name in visited:224 # break out of circular dependencies225 node.print_declaration(output, doc=doc, quote_types=True)226 printed.add(name)227 else:228 stack.append(name)229 stack.extend(dependencies)230 visited.add(name)231def generate_service_api(output, service: ServiceModel, doc=True):232 service_name = service.service_name.replace("-", "_")233 class_name = service_name + "_api"234 class_name = snake_to_camel_case(class_name)235 output.write(f"class {class_name}:\n")236 output.write("\n")237 output.write(f' service = "{service.service_name}"\n')238 output.write(f' version = "{service.api_version}"\n')239 for op_name in service.operation_names:240 operation: OperationModel = service.operation_model(op_name)241 fn_name = camel_to_snake_case(op_name)242 if operation.output_shape:243 output_shape = to_valid_python_name(operation.output_shape.name)244 else:245 output_shape = "None"246 output.write("\n")247 parameters = OrderedDict()248 param_shapes = OrderedDict()249 input_shape = operation.input_shape250 if input_shape is not None:251 members = list(input_shape.members)252 for m in input_shape.required_members:253 members.remove(m)254 m_shape = input_shape.members[m]255 parameters[xform_name(m)] = to_valid_python_name(m_shape.name)256 param_shapes[xform_name(m)] = m_shape257 for m in members:258 m_shape = input_shape.members[m]259 param_shapes[xform_name(m)] = m_shape260 parameters[xform_name(m)] = f"{to_valid_python_name(m_shape.name)} = None"261 if any(map(is_bad_param_name, parameters.keys())):262 # if we cannot render the parameter name, don't expand the parameters in the handler263 param_list = f"request: {to_valid_python_name(input_shape.name)}" if input_shape else ""264 output.write(f' @handler("{operation.name}", expand=False)\n')265 else:266 param_list = ", ".join([f"{k}: {v}" for k, v in parameters.items()])267 output.write(f' @handler("{operation.name}")\n')268 output.write(269 f" def {fn_name}(self, context: RequestContext, {param_list}) -> {output_shape}:\n"270 )271 # convert html documentation to rst and print it into to the signature272 if doc:273 html = operation.documentation274 rst = html_to_rst(html)275 output.write(' """')276 output.write(f"{rst}\n")277 output.write("\n")278 # parameters279 for param_name, shape in param_shapes.items():280 # FIXME: this doesn't work properly281 rst = html_to_rst(shape.documentation)282 rst = rst.strip().split(".")[0] + "."283 output.write(f":param {param_name}: {rst}\n")284 # return value285 if operation.output_shape:286 output.write(f":returns: {to_valid_python_name(operation.output_shape.name)}\n")287 # errors288 for error in operation.error_shapes:289 output.write(f":raises {to_valid_python_name(error.name)}:\n")290 output.write(' """\n')291 output.write(" raise NotImplementedError\n")292@click.group()293def scaffold():294 pass295@scaffold.command(name="generate")...
introspection.py
Source:introspection.py
...108 :type html: string109 :returns: The converted text110 :rtype: string111 """112 return html_to_rst(html)113 def introspect_operation(self, operation):114 """115 Introspects an entire operation, returning::116 * the method name (to expose to the user)117 * the API name (used server-side)118 * docs119 * introspected information about the parameters120 * information about the output121 :param operation: The operation to introspect122 :type operation: A <botocore.operation.Operation> object123 :returns: A dict of information124 """125 return {126 'method_name': operation.py_name,...
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!!