Skip to content

Commit

Permalink
Complex PEP8ization
Browse files Browse the repository at this point in the history
* star imports in rope/base/pynamesdef.py and rope/base/ast.py
* many too complex methods/functions

Fixes python-rope#46
  • Loading branch information
Matěj Cepl committed May 22, 2014
1 parent 25424f8 commit 909f1e0
Show file tree
Hide file tree
Showing 81 changed files with 1,456 additions and 1,097 deletions.
2 changes: 2 additions & 0 deletions rope/base/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def get_pynames(self, parameters):

def get_instance_pyname(self):
return self.pynames[0]


class MixedArguments(object):

def __init__(self, pyname, arguments, scope):
Expand Down
67 changes: 45 additions & 22 deletions rope/base/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ def _get_builtin(*args):
return cls._generated[args]
return _get_builtin


def _create_builtin_getter(cls):
type_getter = _create_builtin_type_getter(cls)

def _get_builtin(*args):
return pyobjects.PyObject(type_getter(*args))
return _get_builtin
Expand Down Expand Up @@ -233,7 +235,7 @@ def __call__(self, name, returned=None, function=None,
except AttributeError:
if check_existence:
raise
builtin=None
builtin = None
self.attributes[name] = BuiltinName(
BuiltinFunction(returned=returned, function=function,
argnames=argnames, builtin=builtin))
Expand All @@ -252,7 +254,8 @@ def __init__(self, holding=None):
collector('__new__', function=self._new_list)

# Adding methods
collector('append', function=self._list_add, argnames=['self', 'value'])
collector('append', function=self._list_add,
argnames=['self', 'value'])
collector('__setitem__', function=self._list_add,
argnames=['self', 'index', 'value'])
collector('insert', function=self._list_add,
Expand Down Expand Up @@ -306,7 +309,6 @@ class Dict(BuiltinClass):
def __init__(self, keys=None, values=None):
self.keys = keys
self.values = values
item = get_tuple(self.keys, self.values)
collector = _AttributeCollector(dict)
collector('__new__', function=self._new_dict)
collector('__setitem__', function=self._dict_add)
Expand All @@ -327,7 +329,8 @@ def do_create(holding=None):
if holding is None:
return get_dict()
type = holding.get_type()
if isinstance(type, Tuple) and len(type.get_holding_objects()) == 2:
if isinstance(type, Tuple) and \
len(type.get_holding_objects()) == 2:
return get_dict(*type.get_holding_objects())
return _create_builtin(args, do_create)

Expand Down Expand Up @@ -384,7 +387,7 @@ def _self_set(self, context):
if new_dict and isinstance(new_dict.get_object().get_type(), Dict):
args = arguments.ObjectArguments([new_dict])
items = new_dict.get_object()['popitem'].\
get_object().get_returned_object(args)
get_object().get_returned_object(args)
context.save_per_name(items)
else:
holding = _infer_sequence_for_pyname(new_dict)
Expand All @@ -405,7 +408,8 @@ def __init__(self, *objects):
first = objects[0]
attributes = {
'__getitem__': BuiltinName(BuiltinFunction(first)),
'__getslice__': BuiltinName(BuiltinFunction(pyobjects.PyObject(self))),
'__getslice__':
BuiltinName(BuiltinFunction(pyobjects.PyObject(self))),
'__new__': BuiltinName(BuiltinFunction(function=self._new_tuple)),
'__iter__': BuiltinName(BuiltinFunction(get_iterator(first)))}
super(Tuple, self).__init__(tuple, attributes)
Expand Down Expand Up @@ -485,8 +489,9 @@ def __init__(self):

self_methods = ['__getitem__', '__getslice__', 'capitalize', 'center',
'decode', 'encode', 'expandtabs', 'join', 'ljust',
'lower', 'lstrip', 'replace', 'rjust', 'rstrip', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']
'lower', 'lstrip', 'replace', 'rjust', 'rstrip',
'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
for method in self_methods:
collector(method, self_object)

Expand Down Expand Up @@ -514,6 +519,7 @@ def get_object(self):
def get_definition_location(self):
return (None, None)


class Iterator(pyobjects.AbstractClass):

def __init__(self, holding=None):
Expand All @@ -539,7 +545,8 @@ def __init__(self, holding=None):
self.holding = holding
self.attributes = {
'next': BuiltinName(BuiltinFunction(self.holding)),
'__iter__': BuiltinName(BuiltinFunction(get_iterator(self.holding))),
'__iter__': BuiltinName(BuiltinFunction(
get_iterator(self.holding))),
'close': BuiltinName(BuiltinFunction()),
'send': BuiltinName(BuiltinFunction()),
'throw': BuiltinName(BuiltinFunction())}
Expand All @@ -556,10 +563,10 @@ def get_returned_object(self, args):
class File(BuiltinClass):

def __init__(self):
self_object = pyobjects.PyObject(self)
str_object = get_str()
str_list = get_list(get_str())
attributes = {}

def add(name, returned=None, function=None):
builtin = getattr(file, name, None)
attributes[name] = BuiltinName(
Expand Down Expand Up @@ -587,7 +594,8 @@ def __init__(self, fget=None, fset=None, fdel=None, fdoc=None):
'fget': BuiltinName(BuiltinFunction()),
'fset': BuiltinName(pynames.UnboundName()),
'fdel': BuiltinName(pynames.UnboundName()),
'__new__': BuiltinName(BuiltinFunction(function=_property_function))}
'__new__': BuiltinName(
BuiltinFunction(function=_property_function))}
super(Property, self).__init__(property, attributes)

def get_property_object(self, args):
Expand Down Expand Up @@ -631,7 +639,7 @@ def get_attributes(self):
return {}

def get_name(self):
return 'lambda'
return 'lambda'

def get_param_names(self, special_args=True):
result = [node.id for node in self.arguments.args
Expand Down Expand Up @@ -671,7 +679,7 @@ def _infer_sequence_for_pyname(pyname):
iter = obj.get_returned_object(args)
if iter is not None and 'next' in iter:
holding = iter['next'].get_object().\
get_returned_object(args)
get_returned_object(args)
return holding


Expand All @@ -690,12 +698,15 @@ def _create_builtin(args, creator):
def _range_function(args):
return get_list()


def _reversed_function(args):
return _create_builtin(args, get_iterator)


def _sorted_function(args):
return _create_builtin(args, get_list)


def _super_function(args):
passed_class, passed_self = args.get_arguments(['type', 'self'])
if passed_self is None:
Expand All @@ -709,6 +720,7 @@ def _super_function(args):
return pyobjects.PyObject(supers[0])
return passed_self


def _zip_function(args):
args = args.get_pynames(['sequence'])
objects = []
Expand All @@ -721,6 +733,7 @@ def _zip_function(args):
tuple = get_tuple(*objects)
return get_list(tuple)


def _enumerate_function(args):
passed = args.get_pynames(['sequence'])[0]
if passed is None:
Expand All @@ -730,6 +743,7 @@ def _enumerate_function(args):
tuple = get_tuple(None, holding)
return get_iterator(tuple)


def _iter_function(args):
passed = args.get_pynames(['sequence'])[0]
if passed is None:
Expand All @@ -738,6 +752,7 @@ def _iter_function(args):
holding = _infer_sequence_for_pyname(passed)
return get_iterator(holding)


def _input_function(args):
return get_str()

Expand All @@ -751,17 +766,25 @@ def _input_function(args):
'file': BuiltinName(get_file_type()),
'open': BuiltinName(get_file_type()),
'unicode': BuiltinName(get_str_type()),
'range': BuiltinName(BuiltinFunction(function=_range_function, builtin=range)),
'reversed': BuiltinName(BuiltinFunction(function=_reversed_function, builtin=reversed)),
'sorted': BuiltinName(BuiltinFunction(function=_sorted_function, builtin=sorted)),
'super': BuiltinName(BuiltinFunction(function=_super_function, builtin=super)),
'property': BuiltinName(BuiltinFunction(function=_property_function, builtin=property)),
'range': BuiltinName(BuiltinFunction(function=_range_function,
builtin=range)),
'reversed': BuiltinName(BuiltinFunction(function=_reversed_function,
builtin=reversed)),
'sorted': BuiltinName(BuiltinFunction(function=_sorted_function,
builtin=sorted)),
'super': BuiltinName(BuiltinFunction(function=_super_function,
builtin=super)),
'property': BuiltinName(BuiltinFunction(function=_property_function,
builtin=property)),
'zip': BuiltinName(BuiltinFunction(function=_zip_function, builtin=zip)),
'enumerate': BuiltinName(BuiltinFunction(function=_enumerate_function, builtin=enumerate)),
'enumerate': BuiltinName(BuiltinFunction(function=_enumerate_function,
builtin=enumerate)),
'object': BuiltinName(BuiltinObject()),
'type': BuiltinName(BuiltinType()),
'iter': BuiltinName(BuiltinFunction(function=_iter_function, builtin=iter)),
'raw_input': BuiltinName(BuiltinFunction(function=_input_function, builtin=raw_input)),
}
'iter': BuiltinName(BuiltinFunction(function=_iter_function,
builtin=iter)),
'raw_input': BuiltinName(BuiltinFunction(function=_input_function,
builtin=raw_input)),
}

builtins = BuiltinModule('__builtin__', initial=_initial_builtins)
12 changes: 7 additions & 5 deletions rope/base/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import difflib
import os
import time
import warnings

import rope.base.fscommands
from rope.base import taskhandle, exceptions, utils
Expand All @@ -17,13 +16,13 @@ class Change(object):

def do(self, job_set=None):
"""Perform the change
.. note:: Do use this directly. Use `Project.do()` instead.
"""

def undo(self, job_set=None):
"""Perform the change
.. note:: Do use this directly. Use `History.undo()` instead.
"""

Expand Down Expand Up @@ -97,7 +96,8 @@ def __str__(self):
date = datetime.datetime.fromtimestamp(self.time)
if date.date() == datetime.date.today():
string_date = 'today'
elif date.date() == (datetime.date.today() - datetime.timedelta(1)):
elif date.date() == (datetime.date.today() -
datetime.timedelta(1)):
string_date = 'yesterday'
elif date.year == datetime.date.today().year:
string_date = date.strftime('%b %d')
Expand Down Expand Up @@ -257,7 +257,8 @@ class CreateFolder(CreateResource):
"""

def __init__(self, parent, name):
resource = parent.project.get_folder(self._get_child_path(parent, name))
resource = parent.project.get_folder(
self._get_child_path(parent, name))
super(CreateFolder, self).__init__(resource)


Expand Down Expand Up @@ -309,6 +310,7 @@ def count_changes(change):
return result
return 1


def create_job_set(task_handle, change):
return task_handle.create_jobset(str(change), count_changes(change))

Expand Down
14 changes: 10 additions & 4 deletions rope/base/codeanalyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def add_change(self, start, end, new_text=None):
def get_changed(self):
if not self.changes:
return None

def compare_changes(change1, change2):
return cmp(change1[:2], change2[:2])
self.changes.sort(compare_changes)
Expand Down Expand Up @@ -131,6 +132,7 @@ def __call__(self):
return result

_main_chars = re.compile(r'[\'|"|#|\\|\[|\]|\{|\}|\(|\)]')

def _analyze_line(self, line):
char = None
for match in self._main_chars.finditer(line):
Expand All @@ -142,8 +144,8 @@ def _analyze_line(self, line):
if char * 3 == line[i:i + 3]:
self.in_string = char * 3
elif self.in_string == line[i:i + len(self.in_string)] and \
not (i > 0 and line[i - 1] == '\\' and
not (i > 1 and line[i - 2] == '\\')):
not (i > 0 and line[i - 1] == '\\' and
not (i > 1 and line[i - 2] == '\\')):
self.in_string = ''
if self.in_string:
continue
Expand All @@ -158,6 +160,7 @@ def _analyze_line(self, line):
else:
self.continuation = False


def custom_generator(lines):
return _CustomGenerator(lines)()

Expand Down Expand Up @@ -189,7 +192,6 @@ def generate_regions(self, start_line=1, end_line=None):
# XXX: `block_start` should be at a better position!
block_start = 1
readline = LinesToReadline(self.lines, block_start)
shifted = start_line - block_start + 1
try:
for start, end in self._logical_lines(readline):
real_start = start + block_start - 1
Expand All @@ -199,7 +201,7 @@ def generate_regions(self, start_line=1, end_line=None):
real_end = end + block_start - 1
if real_start >= start_line:
yield (real_start, real_end)
except tokenize.TokenError, e:
except tokenize.TokenError:
pass

def _block_logical_line(self, block_start, line_number):
Expand Down Expand Up @@ -254,13 +256,15 @@ def __init__(self, lines, generate=custom_generator):
self._generate = generate

_starts = None

@property
def starts(self):
if self._starts is None:
self._init_logicals()
return self._starts

_ends = None

@property
def ends(self):
if self._ends is None:
Expand Down Expand Up @@ -326,6 +330,7 @@ def get_block_start(lines, lineno, maximum_indents=80):

_block_start_pattern = None


def get_block_start_patterns():
global _block_start_pattern
if not _block_start_pattern:
Expand Down Expand Up @@ -354,5 +359,6 @@ def get_string_pattern():
return '|'.join([longstr, longstr.replace('"', "'"),
shortstr, shortstr.replace('"', "'")])


def get_comment_pattern():
return r'#[^\n]*'
Loading

0 comments on commit 909f1e0

Please sign in to comment.