def obf_var(self, var: cst.Name, updated_node: cst.Attribute): name = var.value if self.change_fields and self.can_rename(name, 'cv'): attr = self.get_new_cst_name(name) updated_node = updated_node.with_changes(attr=attr) elif self.change_variables and self.can_rename(name, 'v'): updated_node = updated_node.with_changes() return updated_node
def leave_Attribute(self, original_node: cst.Attribute, updated_node: cst.Attribute) -> cst.Attribute: for matcher, renamed in self.matchers_map.items(): if matchers.matches(updated_node, matcher): return updated_node.with_changes(attr=cst.Name(value=renamed)) return updated_node
def leave_Attribute( self, original_node: cst.Attribute, updated_node: cst.Attribute) -> Union[cst.Name, cst.Attribute]: full_name_for_node = get_full_name_for_node(original_node) if full_name_for_node is None: raise Exception("Could not parse full name for Attribute node.") full_replacement_name = self.gen_replacement(full_name_for_node) # If a node has no associated QualifiedName, we are still inside an import statement. inside_import_statement: bool = not self.get_metadata( QualifiedNameProvider, original_node, set()) if (QualifiedNameProvider.has_name( self, original_node, self.old_name, ) or (inside_import_statement and full_replacement_name == self.new_name)): new_value, new_attr = self.new_module, self.new_mod_or_obj if not inside_import_statement: self.scheduled_removals.add(original_node.value) if full_replacement_name == self.new_name: return updated_node.with_changes( value=cst.parse_expression(new_value), attr=cst.Name(value=new_attr.rstrip(".")), ) return self.gen_name_or_attr_node(new_attr) return updated_node
def _get_async_attr_replacement( self, node: cst.Attribute) -> Optional[cst.CSTNode]: value = node.value if m.matches(value, m.Call()): value = cast(cst.Call, value) value_replacement = self._get_async_call_replacement(value) if value_replacement is not None: return node.with_changes(value=value_replacement) return self._get_awaitable_replacement(node)
def leave_Attribute(self, original_node: Attribute, updated_node: Attribute) -> BaseExpression: matcher = self.name_matcher if (matcher and m.matches(updated_node, matcher) and not self.is_wrapped_in_call(original_node) and self.matches_import_scope(original_node)): return updated_node.with_changes( value=Name(self.new_parent_name), attr=Name(self.new_name), ) return super().leave_Attribute(original_node, updated_node)
def leave_Attribute(self, original_node: cst.Attribute, updated_node: cst.Attribute) -> cst.Attribute: for matcher in self.matchers: if matchers.matches(updated_node, matcher): # Ensure that wx.adv is imported AddImportsVisitor.add_needed_import(self.context, "wx.adv") # Return modified node return updated_node.with_changes(value=cst.Attribute( value=cst.Name(value="wx"), attr=cst.Name(value="adv"))) return updated_node
def leave_Attribute(self, original_node: cst.Attribute, updated_node: cst.Attribute) -> cst.CSTNode: """Replace requests attrs with httpx attrs""" if (isinstance(original_node.value, cst.Name) and original_node.value.value == "requests"): mapping = {"Session": "AsyncClient"} return updated_node.with_changes( value=cst.Name("httpx"), attr=cst.Name(mapping[original_node.attr.value]), ) return updated_node
def visit_Attribute(self, node: cst.Attribute) -> None: rule_config = self.context.config.rule_config parenthesize_attribute_config = rule_config.get( self.__class__.__name__, {}) if isinstance(parenthesize_attribute_config, dict) and parenthesize_attribute_config.get( "disabled", False): return if len(node.lpar) == 0: new_node = node.with_changes(lpar=[cst.LeftParen()], rpar=[cst.RightParen()]) self.report( node, "All attributes should be parenthesized.", replacement=new_node, )
def leave_Attribute(self, original_node: cst.Attribute, updated_node: cst.Attribute): self.attribute_stack.pop() # x.y. z tail = updated_node.value head = updated_node.attr attrs = split_attribute(tail) # Обфускация метода/поля if m.matches(head, m.Name()): head = cst.ensure_type(head, cst.Name) updated_node = self.obf_var(head, updated_node) elif m.matches(head, m.Call()): head = cst.ensure_type(head, cst.Call) updated_node = self.obf_function_name(head, updated_node) else: pass # Обфускация имени if m.matches(tail, m.Name()): tail = cst.ensure_type(tail, cst.Name) if self.can_rename(tail.value, 'v', 'a', 'ca'): updated_node = updated_node.with_changes( value=self.get_new_cst_name(tail.value)) elif m.matches(tail, m.Subscript()): tail = cst.ensure_type(tail, cst.Subscript) else: pass return updated_node
def leave_Attribute(self, original_node: cst.Attribute, updated_node: cst.Attribute) -> cst.Attribute: if matchers.matches(updated_node, self.matcher): return updated_node.with_changes(attr=cst.Name(value="Colour")) return updated_node
def leave_Attribute(self, original_node: cst.Attribute, updated_node: cst.Attribute) -> cst.Attribute: new_value = updated_node.value.visit(self) final_node = updated_node.with_changes(value=new_value) return final_node