X Tutup
Skip to content

Commit 26c147d

Browse files
committed
Make pep8 happy
… and use ifilter Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent 4773da6 commit 26c147d

File tree

3 files changed

+54
-65
lines changed

3 files changed

+54
-65
lines changed

bpython/config.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@
1010

1111
import bpython.autocomplete
1212

13+
1314
class Struct(object):
1415
"""Simple class for instantiating objects we can add arbitrary attributes
1516
to and use for various arbitrary things."""
1617

18+
1719
def get_config_home():
1820
"""Returns the base directory for bpython's configuration files."""
1921
xdg_config_home = os.environ.get('XDG_CONFIG_HOME', '~/.config')
2022
return os.path.join(xdg_config_home, 'bpython')
2123

24+
2225
def default_config_path():
2326
"""Returns bpython's default configuration file path."""
2427
return os.path.join(get_config_home(), 'config')
2528

29+
2630
def fill_config_with_default_values(config, default_values):
2731
for section in default_values.iterkeys():
2832
if not config.has_section(section):
@@ -117,7 +121,6 @@ def loadini(struct, configfile):
117121
"%s\n" % default_config_path())
118122
sys.exit(1)
119123

120-
121124
def get_key_no_doublebind(command):
122125
default_commands_to_keys = defaults['keyboard']
123126
requested_key = config.get('keyboard', command)
@@ -126,7 +129,7 @@ def get_key_no_doublebind(command):
126129
default_command = default_keys_to_commands[requested_key]
127130

128131
if default_commands_to_keys[default_command] == \
129-
config.get('keyboard', default_command):
132+
config.get('keyboard', default_command):
130133
setattr(struct, '%s_key' % default_command, '')
131134
except KeyError:
132135
pass
@@ -194,37 +197,37 @@ def get_key_no_doublebind(command):
194197
struct.cli_suggestion_width = config.getfloat('cli',
195198
'suggestion_width')
196199
struct.cli_trim_prompts = config.getboolean('cli',
197-
'trim_prompts')
200+
'trim_prompts')
198201

199202
struct.complete_magic_methods = config.getboolean('general',
200203
'complete_magic_methods')
201204
struct.autocomplete_mode = config.get('general', 'autocomplete_mode')
202205
struct.save_append_py = config.getboolean('general', 'save_append_py')
203206

204207
struct.curtsies_list_above = config.getboolean('curtsies', 'list_above')
205-
struct.curtsies_right_arrow_completion = config.getboolean('curtsies',
206-
'right_arrow_completion')
208+
struct.curtsies_right_arrow_completion = \
209+
config.getboolean('curtsies', 'right_arrow_completion')
207210

208211
color_scheme_name = config.get('general', 'color_scheme')
209212

210213
default_colors = {
211-
'keyword': 'y',
212-
'name': 'c',
213-
'comment': 'b',
214-
'string': 'm',
215-
'error': 'r',
216-
'number': 'G',
217-
'operator': 'Y',
218-
'punctuation': 'y',
219-
'token': 'C',
220-
'background': 'd',
221-
'output': 'w',
222-
'main': 'c',
223-
'paren': 'R',
224-
'prompt': 'c',
225-
'prompt_more': 'g',
226-
'right_arrow_suggestion': 'K',
227-
}
214+
'keyword': 'y',
215+
'name': 'c',
216+
'comment': 'b',
217+
'string': 'm',
218+
'error': 'r',
219+
'number': 'G',
220+
'operator': 'Y',
221+
'punctuation': 'y',
222+
'token': 'C',
223+
'background': 'd',
224+
'output': 'w',
225+
'main': 'c',
226+
'paren': 'R',
227+
'prompt': 'c',
228+
'prompt_more': 'g',
229+
'right_arrow_suggestion': 'K',
230+
}
228231

229232
if color_scheme_name == 'default':
230233
struct.color_scheme = default_colors
@@ -238,7 +241,7 @@ def get_key_no_doublebind(command):
238241
load_theme(struct, path, struct.color_scheme, default_colors)
239242
except EnvironmentError:
240243
sys.stderr.write("Could not load theme '%s'.\n" %
241-
(color_scheme_name, ))
244+
(color_scheme_name, ))
242245
sys.exit(1)
243246

244247
# checks for valid key configuration this part still sucks

bpython/history.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,17 @@ def __init__(self, entries=None, duplicates=True, hist_size=100):
3737
self.entries = ['']
3838
else:
3939
self.entries = list(entries)
40-
self.index = 0 # how many lines back in history is currently selected
41-
# where 0 is the saved typed line, 1 the prev entered
42-
# line
43-
self.saved_line = '' # what was on the prompt before using history
40+
# how many lines back in history is currently selected where 0 is the
41+
# saved typed line, 1 the prev entered line
42+
self.index = 0
43+
# what was on the prompt before using history
44+
self.saved_line = ''
4445
self.duplicates = duplicates
4546
self.hist_size = hist_size
4647

47-
4848
def append(self, line):
4949
self.append_to(self.entries, line)
5050

51-
5251
def append_to(self, entries, line):
5352
line = line.rstrip('\n')
5453
if line:
@@ -61,14 +60,12 @@ def append_to(self, entries, line):
6160
pass
6261
entries.append(line)
6362

64-
6563
def first(self):
6664
"""Move back to the beginning of the history."""
6765
if not self.is_at_end:
6866
self.index = len(self.entries)
6967
return self.entries[-self.index]
7068

71-
7269
def back(self, start=True, search=False, target=None,
7370
include_current=False):
7471
"""Move one step back in the history."""
@@ -84,18 +81,15 @@ def back(self, start=True, search=False, target=None,
8481
self.index += 1
8582
return self.entry
8683

87-
8884
@property
8985
def entry(self):
9086
"""The current entry, which may be the saved line"""
9187
return self.entries[-self.index] if self.index else self.saved_line
9288

93-
9489
@property
9590
def entries_by_index(self):
9691
return list(reversed(self.entries + [self.saved_line]))
9792

98-
9993
def find_match_backward(self, search_term, include_current=False):
10094
add = 0 if include_current else 1
10195
start = self.index + add
@@ -104,7 +98,6 @@ def find_match_backward(self, search_term, include_current=False):
10498
return idx + add
10599
return 0
106100

107-
108101
def find_partial_match_backward(self, search_term, include_current=False):
109102
add = 0 if include_current else 1
110103
start = self.index + add
@@ -113,7 +106,6 @@ def find_partial_match_backward(self, search_term, include_current=False):
113106
return idx + add
114107
return 0
115108

116-
117109
def forward(self, start=True, search=False, target=None,
118110
include_current=False):
119111
"""Move one step forward in the history."""
@@ -132,7 +124,6 @@ def forward(self, start=True, search=False, target=None,
132124
self.index = 0
133125
return self.saved_line
134126

135-
136127
def find_match_forward(self, search_term, include_current=False):
137128
add = 0 if include_current else 1
138129
end = max(0, self.index - (1 - add))
@@ -142,7 +133,6 @@ def find_match_forward(self, search_term, include_current=False):
142133
return idx + (0 if include_current else 1)
143134
return self.index
144135

145-
146136
def find_partial_match_forward(self, search_term, include_current=False):
147137
add = 0 if include_current else 1
148138
end = max(0, self.index - (1 - add))
@@ -152,68 +142,57 @@ def find_partial_match_forward(self, search_term, include_current=False):
152142
return idx + add
153143
return self.index
154144

155-
156145
def last(self):
157146
"""Move forward to the end of the history."""
158147
if not self.is_at_start:
159148
self.index = 0
160149
return self.entries[0]
161150

162-
163151
@property
164152
def is_at_end(self):
165153
return self.index >= len(self.entries) or self.index == -1
166154

167-
168155
@property
169156
def is_at_start(self):
170157
return self.index == 0
171158

172-
173159
def enter(self, line):
174160
if self.index == 0:
175161
self.saved_line = line
176162

177-
178163
@classmethod
179164
def from_filename(cls, filename):
180165
history = cls()
181166
history.load(filename)
182167
return history
183168

184-
185169
def reset(self):
186170
self.index = 0
187171
self.saved_line = ''
188172

189-
190173
def load(self, filename, encoding):
191174
with codecs.open(filename, 'r', encoding, 'ignore') as hfile:
192175
with FileLock(hfile):
193176
self.entries = self.load_from(hfile)
194177

195-
196178
def load_from(self, fd):
197179
entries = []
198180
for line in fd:
199181
self.append_to(entries, line)
200182
return entries
201183

202-
203184
def save(self, filename, encoding, lines=0):
204185
with codecs.open(filename, 'w', encoding, 'ignore') as hfile:
205186
with FileLock(hfile):
206187
self.save_to(hfile, self.entries, lines)
207188

208-
209189
def save_to(self, fd, entries=None, lines=0):
210190
if entries is None:
211191
entries = self.entries
212192
for line in entries[-lines:]:
213193
fd.write(line)
214194
fd.write('\n')
215195

216-
217196
def append_reload_and_write(self, s, filename, encoding):
218197
if not self.hist_size:
219198
return self.append(s)
@@ -233,8 +212,9 @@ def append_reload_and_write(self, s, filename, encoding):
233212

234213
self.entries = entries
235214
except EnvironmentError as err:
236-
raise RuntimeError(_('Error occurred while writing to file %s (%s)')
237-
% (filename, err.strerror))
215+
raise RuntimeError(
216+
_('Error occurred while writing to file %s (%s)')
217+
% (filename, err.strerror))
238218
else:
239219
if len(self.entries) == 0:
240220
# Make sure that entries contains at least one element. If the

bpython/importcompletion.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
# THE SOFTWARE.
2222

2323
from bpython._py3compat import py3
24-
from bpython import line as lineparts
24+
from bpython.line import current_word, current_import, \
25+
current_from_import_from, current_from_import_import
26+
2527
import imp
2628
import os
2729
import sys
2830
import warnings
2931
from warnings import catch_warnings
32+
from itertools import ifilter
3033

3134
if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
3235
import importlib.machinery
@@ -61,6 +64,7 @@ def module_matches(cw, prefix=''):
6164
else:
6265
return set(matches)
6366

67+
6468
def attr_matches(cw, prefix='', only_modules=False):
6569
"""Attributes to replace name with"""
6670
full = '%s.%s' % (prefix, cw) if prefix else cw
@@ -79,47 +83,49 @@ def attr_matches(cw, prefix='', only_modules=False):
7983
if module_part:
8084
matches = ('%s.%s' % (module_part, m) for m in matches)
8185

82-
return set(filter(lambda x: x is not None,
83-
(try_decode_module(match, 'ascii') for match in matches)))
86+
generator = (try_decode_module(match, 'ascii') for match in matches)
87+
return set(ifilter(lambda x: x is not None, generator))
88+
8489

8590
def module_attr_matches(name):
8691
"""Only attributes which are modules to replace name with"""
8792
return attr_matches(name, prefix='', only_modules=True)
8893

94+
8995
def complete(cursor_offset, line):
9096
"""Construct a full list of possibly completions for imports."""
9197
tokens = line.split()
9298
if 'from' not in tokens and 'import' not in tokens:
9399
return None
94100

95-
result = lineparts.current_word(cursor_offset, line)
101+
result = current_word(cursor_offset, line)
96102
if result is None:
97103
return None
98104

99-
from_import_from = lineparts.current_from_import_from(cursor_offset, line)
105+
from_import_from = current_from_import_from(cursor_offset, line)
100106
if from_import_from is not None:
101-
from_import_import = lineparts.current_from_import_import(cursor_offset,
102-
line)
103-
if from_import_import is not None:
107+
import_import = current_from_import_import(cursor_offset, line)
108+
if import_import is not None:
104109
# `from a import <b|>` completion
105-
matches = module_matches(from_import_import[2], from_import_from[2])
106-
matches.update(attr_matches(from_import_import[2],
110+
matches = module_matches(import_import[2], from_import_from[2])
111+
matches.update(attr_matches(import_import[2],
107112
from_import_from[2]))
108113
else:
109114
# `from <a|>` completion
110115
matches = module_attr_matches(from_import_from[2])
111116
matches.update(module_matches(from_import_from[2]))
112117
return matches
113118

114-
current_import = lineparts.current_import(cursor_offset, line)
115-
if current_import is not None:
119+
cur_import = current_import(cursor_offset, line)
120+
if cur_import is not None:
116121
# `import <a|>` completion
117-
matches = module_matches(current_import[2])
118-
matches.update(module_attr_matches(current_import[2]))
122+
matches = module_matches(cur_import[2])
123+
matches.update(module_attr_matches(cur_import[2]))
119124
return matches
120125
else:
121126
return None
122127

128+
123129
def find_modules(path):
124130
"""Find all modules (and packages) for a given directory."""
125131
if not os.path.isdir(path):

0 commit comments

Comments
 (0)
X Tutup