Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions business_logic/blockly/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)}'
Copy link
Author

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:

method = getattr(self, method_name, None)

if not method:
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlBuilder.visit_reference_constant refactored with the following changes:


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)
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlBuilder._get_variable_block_type refactored with the following changes:


def visit_variable(self, node, parent_xml):
block_type = self._get_variable_block_type(node, 'get')
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlBuilder.visit_binary_operator refactored with the following changes:


block = etree.SubElement(parent_xml, 'block', type=block_type)
field_element = etree.SubElement(block, 'field', name='OP')
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlBuilder.visit_if_statement refactored with the following changes:

mutation.set('elseif', str(int(elifs)))

for i, pair in enumerate(pairs(children)):
Expand All @@ -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
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlBuilder.visit_function refactored with the following changes:

self.visit(child_node, value)

return block
Expand Down
4 changes: 2 additions & 2 deletions business_logic/blockly/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function NodeTreeCreator.create_content_object refactored with the following changes:

del data[kwarg]

return data
Expand Down
34 changes: 18 additions & 16 deletions business_logic/blockly/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlParser.visit refactored with the following changes:

This removes the following comments ( why? ):

# start of code block


def _process_next(self, node):
children = []
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlParser._process_next refactored with the following changes:


_node = _children[0]
children.append(_node)
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlParser._call_method refactored with the following changes:


def _visit_children(self, node, data, children=None):
if 'children' not in data:
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlParser._visit_single_child refactored with the following changes:


return self.visit(children[0])

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlParser.visit_block refactored with the following changes:

method = getattr(self, method_name)
return method(node)

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BlocklyXmlParser.visit_field refactored with the following changes:

method = getattr(self, method_name)
return method(node)

Expand Down
2 changes: 1 addition & 1 deletion business_logic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ContextConfig.__init__ refactored with the following changes:


for k, v in self.defaults.items():
kwargs.setdefault(k, v)
Expand Down
Loading