-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored dgk/django-business-logic #42
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ def visit(self, node, parent_xml): | |
if cls == Model: | ||
break | ||
|
||
method_name = 'visit_{}'.format(camel_case_to_snake_case(cls.__name__)) | ||
method_name = f'visit_{camel_case_to_snake_case(cls.__name__)}' | ||
method = getattr(self, method_name, None) | ||
|
||
if not method: | ||
|
@@ -62,7 +62,7 @@ def visit(self, node, parent_xml): | |
return node_xml | ||
|
||
if content_object.__class__ not in (VariableDefinition,): | ||
logger.debug('Unsupported content_object: {}'.format(content_object.__class__)) | ||
logger.debug(f'Unsupported content_object: {content_object.__class__}') | ||
|
||
def visit_constant(self, node, parent_xml): | ||
block_type = { | ||
|
@@ -91,16 +91,21 @@ def visit_reference_constant(self, node, parent_xml): | |
children = self.get_children(node) | ||
|
||
if len(children) != 1: | ||
raise BlocklyXmlBuilderException('Incorrect number of ReferenceConstant node children: {}'.format( | ||
len(children))) | ||
raise BlocklyXmlBuilderException( | ||
f'Incorrect number of ReferenceConstant node children: {len(children)}' | ||
) | ||
|
||
Comment on lines
-94
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
value_object_node = children[0] | ||
content_type = value_object_node.content_type | ||
|
||
block = etree.SubElement(parent_xml, 'block', type='business_logic_reference') | ||
|
||
type_field = etree.SubElement(block, 'field', name='TYPE') | ||
type_field.text = '{}.{}'.format(content_type.app_label, content_type.model_class().__name__) | ||
type_field.text = ( | ||
f'{content_type.app_label}.{content_type.model_class().__name__}' | ||
) | ||
|
||
|
||
value_field = etree.SubElement(block, 'field', name='VALUE') | ||
value_field.text = str(value_object_node.object_id) | ||
|
@@ -112,8 +117,8 @@ def _get_variable_block_type(self, node, action): | |
assert action in ('get', 'set') | ||
|
||
if node.content_object.definition.name.find('.') != -1: | ||
return 'business_logic_argument_field_{}'.format(action) | ||
return 'variables_{}'.format(action) | ||
return f'business_logic_argument_field_{action}' | ||
return f'variables_{action}' | ||
Comment on lines
-115
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def visit_variable(self, node, parent_xml): | ||
block_type = self._get_variable_block_type(node, 'get') | ||
|
@@ -148,7 +153,7 @@ def visit_binary_operator(self, node, parent_xml): | |
if operator_value in table: | ||
break | ||
else: | ||
raise BlocklyXmlBuilderException('Invalid Operator: {}'.format(operator_value)) | ||
raise BlocklyXmlBuilderException(f'Invalid Operator: {operator_value}') | ||
Comment on lines
-151
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
block = etree.SubElement(parent_xml, 'block', type=block_type) | ||
field_element = etree.SubElement(block, 'field', name='OP') | ||
|
@@ -171,8 +176,7 @@ def visit_if_statement(self, node, parent_xml): | |
mutation = etree.SubElement(block, 'mutation') | ||
if len(children) % 2: | ||
mutation.set('else', '1') | ||
elifs = (len(children) - 2 - len(children) % 2) / 2 | ||
if elifs: | ||
if elifs := (len(children) - 2 - len(children) % 2) / 2: | ||
Comment on lines
-174
to
+179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
mutation.set('elseif', str(int(elifs))) | ||
|
||
for i, pair in enumerate(pairs(children)): | ||
|
@@ -183,10 +187,10 @@ def visit_if_statement(self, node, parent_xml): | |
break | ||
|
||
if_condition = pair[0] | ||
if_value = etree.SubElement(block, 'value', name='IF{}'.format(i)) | ||
if_value = etree.SubElement(block, 'value', name=f'IF{i}') | ||
self.visit(if_condition, if_value) | ||
|
||
statement = etree.SubElement(block, 'statement', name='DO{}'.format(i)) | ||
statement = etree.SubElement(block, 'statement', name=f'DO{i}') | ||
self.visit(pair[1], statement) | ||
|
||
return block | ||
|
@@ -204,7 +208,7 @@ def visit_function(self, node, parent_xml): | |
field_element.text = function_definition.title | ||
|
||
for i, child_node in enumerate(children): | ||
value = etree.SubElement(block, 'value', name='ARG{}'.format(i)) | ||
value = etree.SubElement(block, 'value', name=f'ARG{i}') | ||
Comment on lines
-207
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.visit(child_node, value) | ||
|
||
return block | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,11 +78,11 @@ def create_content_object(self, data): | |
return | ||
|
||
node_kwargs = [x.name for x in Node._meta.get_fields()] | ||
content_object_kwargs = dict(((k, v) for k, v in data.items() if k not in node_kwargs)) | ||
content_object_kwargs = {k: v for k, v in data.items() if k not in node_kwargs} | ||
|
||
content_object = model_class.objects.create(**content_object_kwargs) | ||
data['object_id'] = content_object.id | ||
for kwarg in content_object_kwargs.keys(): | ||
for kwarg in content_object_kwargs: | ||
Comment on lines
-81
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
del data[kwarg] | ||
|
||
return data | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,13 +53,13 @@ def transform_shadow(root): | |
def visit(self, node): | ||
parent = node.getparent() | ||
|
||
if parent is not None and parent.tag != 'next' and node.find('next') is not None: | ||
# start of code block | ||
data = self._process_next(node) | ||
else: | ||
data = self._call_method(node) | ||
|
||
return data | ||
return ( | ||
self._process_next(node) | ||
if parent is not None | ||
and parent.tag != 'next' | ||
and node.find('next') is not None | ||
else self._call_method(node) | ||
) | ||
Comment on lines
-56
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
|
||
def _process_next(self, node): | ||
children = [] | ||
|
@@ -74,7 +74,9 @@ def _process_next(self, node): | |
|
||
if len(_children) != 1: | ||
raise BlocklyXmlParserException( | ||
'Incorrect number of children ({}) for BlocklyXmlParser._process_next()'.format(len(_children))) | ||
f'Incorrect number of children ({len(_children)}) for BlocklyXmlParser._process_next()' | ||
) | ||
|
||
Comment on lines
-77
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
_node = _children[0] | ||
children.append(_node) | ||
|
@@ -89,13 +91,11 @@ def _call_method(self, node): | |
if node.tag in ('next', 'mutation'): | ||
return | ||
|
||
method_name = 'visit_{}'.format(node.tag) | ||
method = getattr(self, method_name, None) | ||
|
||
if method: | ||
method_name = f'visit_{node.tag}' | ||
if method := getattr(self, method_name, None): | ||
return method(node) | ||
|
||
raise BlocklyXmlParserException('Unknown tag: {}'.format(node.tag)) | ||
raise BlocklyXmlParserException(f'Unknown tag: {node.tag}') | ||
Comment on lines
-92
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def _visit_children(self, node, data, children=None): | ||
if 'children' not in data: | ||
|
@@ -110,7 +110,9 @@ def _visit_single_child(self, node): | |
|
||
if len(children) != 1: | ||
raise BlocklyXmlParserException( | ||
'Incorrect number of children ({}) for BlocklyXmlParser._visit_single_child()'.format(len(children))) | ||
f'Incorrect number of children ({len(children)}) for BlocklyXmlParser._visit_single_child()' | ||
) | ||
|
||
Comment on lines
-113
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
return self.visit(children[0]) | ||
|
||
|
@@ -161,7 +163,7 @@ def visit_xml(self, node): | |
return self._visit_single_child(node) | ||
|
||
def visit_block(self, node): | ||
method_name = 'visit_block_{}'.format(node.get('type')) | ||
method_name = f"visit_block_{node.get('type')}" | ||
Comment on lines
-164
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
method = getattr(self, method_name) | ||
return method(node) | ||
|
||
|
@@ -193,7 +195,7 @@ def visit_block_controls_if(self, node): | |
return self._visit_simple_container(node, IfStatement) | ||
|
||
def visit_field(self, node): | ||
method_name = 'visit_field_{}'.format(node.get('name').lower()) | ||
method_name = f"visit_field_{node.get('name').lower()}" | ||
Comment on lines
-196
to
+198
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
method = getattr(self, method_name) | ||
return method(node) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ class ContextConfig(object): | |
def __init__(self, **kwargs): | ||
for k in kwargs.keys(): | ||
if k not in self.defaults: | ||
raise TypeError('Incorrect kwarg {}'.format(k)) | ||
raise TypeError(f'Incorrect kwarg {k}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
for k, v in self.defaults.items(): | ||
kwargs.setdefault(k, v) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
BlocklyXmlBuilder.visit
refactored with the following changes:use-fstring-for-formatting
)