Skip to content

Commit

Permalink
added flake8 config; fixed code style
Browse files Browse the repository at this point in the history
- added flake8 config file so that check is always the same
- included flake confi in travis.yml
- added plugins directoy to check
-  corrected code style
  • Loading branch information
andweber committed Aug 21, 2016
1 parent 2f68a40 commit 4f4628e
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 101 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
select=E,F,W,C
ignore=E128
max-line-length = 80
exclude = __init__.py
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ install:
- "pip install flake8 "
- "python ./setup.py install"
before_script:
- "flake8 setup.py jasper tests"
- "flake8 --config ./.flake8 setup.py jasper tests plugins"
script:
- "coverage run ./setup.py test"
after_success:
Expand Down
8 changes: 4 additions & 4 deletions jasper/audioengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ def record(self, chunksize, *args):
def play_fp(self, fp, chunksize=1024, add_padding=False):
w = wave.open(fp, 'rb')
channels = w.getnchannels()
bits = w.getsampwidth()*8
bits = w.getsampwidth() * 8
rate = w.getframerate()
with self.open_stream(bits, channels, rate,
chunksize=chunksize) as stream:
data = w.readframes(chunksize)
if add_padding and len(data) > 0:
data += b'\00'*(chunksize - len(data))
data += b'\00' * (chunksize - len(data))
while data:
stream.write(data)
data = w.readframes(chunksize)
if add_padding and len(data) > 0:
data += b'\00'*(chunksize - len(data))
data += b'\00' * (chunksize - len(data))
w.close()

def play_file(self, filename, *args, **kwargs):
Expand Down Expand Up @@ -134,7 +134,7 @@ def print_device_info(self, verbose=False):
print(' None')
else:
n = 4
for chunk in (formats[i:i+n]
for chunk in (formats[i:i + n]
for i in range(0, len(formats), n)):
print(' %s' % ', '.join(
"(%d Bit %d CH @ %d Hz)" % fmt
Expand Down
20 changes: 10 additions & 10 deletions jasper/mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self, input_device, output_device,
self._logger.debug('Output padding: %s',
'yes' if self._output_padding else 'no')

self._threshold = 2.0**self._input_bits
self._threshold = 2.0 ** self._input_bits

@contextlib.contextmanager
def special_mode(self, name, phrases):
Expand All @@ -93,9 +93,9 @@ def special_mode(self, name, phrases):
self.active_stt_engine = original_stt_engine

def _snr(self, frames):
rms = audioop.rms(b''.join(frames), int(self._input_bits/8))
rms = audioop.rms(b''.join(frames), int(self._input_bits / 8))
if rms > 0 and self._threshold > 0:
return 20.0 * math.log(rms/self._threshold, 10)
return 20.0 * math.log(rms / self._threshold, 10)
else:
return 0

Expand All @@ -104,21 +104,21 @@ def _write_frames_to_file(self, frames, framerate, volume):
with tempfile.NamedTemporaryFile(mode='w+b') as f:
wav_fp = wave.open(f, 'wb')
wav_fp.setnchannels(self._input_channels)
wav_fp.setsampwidth(int(self._input_bits/8))
wav_fp.setsampwidth(int(self._input_bits / 8))
wav_fp.setframerate(framerate)
if self._input_rate == framerate:
fragment = ''.join(frames)
else:
fragment = audioop.ratecv(''.join(frames),
int(self._input_bits/8),
int(self._input_bits / 8),
self._input_channels,
self._input_rate,
framerate, None)[0]
if volume is not None:
maxvolume = audioop.minmax(fragment, self._input_bits/8)[1]
maxvolume = audioop.minmax(fragment, self._input_bits / 8)[1]
fragment = audioop.mul(
fragment, int(self._input_bits/8),
volume * (2.**15) / maxvolume)
fragment, int(self._input_bits / 8),
volume * (2. ** 15) / maxvolume)

wav_fp.writeframes(fragment)
wav_fp.close()
Expand Down Expand Up @@ -210,7 +210,7 @@ def listen(self):

def active_listen(self, timeout=3):
# record until <timeout> second of silence or double <timeout>.
n = int(round((self._input_rate/self._input_chunksize)*timeout))
n = int(round((self._input_rate / self._input_chunksize) * timeout))
if self._active_stt_reply:
self.say(self._active_stt_reply)
else:
Expand All @@ -223,7 +223,7 @@ def active_listen(self, timeout=3):
self._input_channels,
self._input_rate):
frames.append(frame)
if len(frames) >= 2*n or (
if len(frames) >= 2 * n or (
len(frames) > n and self._snr(frames[-n:]) <= 3):
break

Expand Down
5 changes: 3 additions & 2 deletions jasper/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def simple_request(var, cleanVar, cleanInput=None):

language = raw_input("\nWhat is your language ?" +
"available: en-US, fr-FR, de-DE : \n")
while not language or (language != 'en-US' and language != 'fr-FR'
and language != 'de-DE'):
while not language or (language != 'en-US' and
language != 'fr-FR' and
language != 'de-DE'):
language = raw_input("\nPlease choose your language," +
"available: en-US, fr-FR, de-DE : \n")
profile['language'] = language
Expand Down
11 changes: 5 additions & 6 deletions jasper/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ def say(self, phrase):


def get_plugin_instance(plugin_class, *extra_args):
info = type('', (object,), {
'name': 'pluginunittest',
'translations': {
'en-US': gettext.NullTranslations()
}
})()
info = type('', (object,),
{
'name': 'pluginunittest',
'translations':
{'en-US': gettext.NullTranslations()}})()
args = tuple(extra_args) + (info, TEST_PROFILE)
return plugin_class(*args)
2 changes: 1 addition & 1 deletion plugins/audioengine/alsa-ae/alsaaudioengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import alsaaudio
from jasper import plugin

ALSAAUDIO_BIT_MAPPING = {8: alsaaudio.PCM_FORMAT_S8,
ALSAAUDIO_BIT_MAPPING = {8: alsaaudio.PCM_FORMAT_S8,
16: alsaaudio.PCM_FORMAT_S16_LE,
24: alsaaudio.PCM_FORMAT_S24_LE,
32: alsaaudio.PCM_FORMAT_S32_LE}
Expand Down
4 changes: 2 additions & 2 deletions plugins/audioengine/pyaudio-ae/pyaudioengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pyaudio
from jasper import plugin

PYAUDIO_BIT_MAPPING = {8: pyaudio.paInt8,
PYAUDIO_BIT_MAPPING = {8: pyaudio.paInt8,
16: pyaudio.paInt16,
24: pyaudio.paInt24,
32: pyaudio.paInt32}
Expand Down Expand Up @@ -156,7 +156,7 @@ def open_stream(self, bits, channels, rate, chunksize=1024, output=True):
'output': output,
'input': not output,
('%s_device_index' % direction): self.index,
'frames_per_buffer': chunksize if output else chunksize*8 # Hacky
'frames_per_buffer': chunksize if output else chunksize * 8 # Hacky
}
stream = self._engine._pyaudio.open(**stream_kwargs)

Expand Down
130 changes: 65 additions & 65 deletions plugins/speechhandler/weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,54 @@
'SELECT * FROM weather.forecast WHERE woeid="%s" AND u="%s"'
YAHOO_YQL_URL = 'https://query.yahooapis.com/v1/public/yql'
YAHOO_YQL_WEATHER_CONDITION_CODES = {
0: 'tornado',
1: 'tropical storm',
2: 'hurricane',
3: 'severe thunderstorms',
4: 'thunderstorms',
5: 'mixed rain and snow',
6: 'mixed rain and sleet',
7: 'mixed snow and sleet',
8: 'freezing drizzle',
9: 'drizzle',
10: 'freezing rain',
11: 'showers',
12: 'showers',
13: 'snow flurries',
14: 'light snow showers',
15: 'blowing snow',
16: 'snow',
17: 'hail',
18: 'sleet',
19: 'dust',
20: 'foggy',
21: 'haze',
22: 'smoky',
23: 'blustery',
24: 'windy',
25: 'cold',
26: 'cloudy',
27: 'mostly cloudy at night',
28: 'mostly cloudy at day',
29: 'partly cloudy at night',
30: 'partly cloudy at day',
31: 'clear at night',
32: 'sunny',
33: 'fair at night',
34: 'fair at day',
35: 'mixed rain and hail',
36: 'hot',
37: 'isolated thunderstorms',
38: 'scattered thunderstorms',
39: 'scattered thunderstorms',
40: 'scattered showers',
41: 'heavy snow',
42: 'scattered snow showers',
43: 'heavy snow',
44: 'partly cloudy',
45: 'thundershowers',
46: 'snow showers',
47: 'isolated thundershowers'
0: 'tornado',
1: 'tropical storm',
2: 'hurricane',
3: 'severe thunderstorms',
4: 'thunderstorms',
5: 'mixed rain and snow',
6: 'mixed rain and sleet',
7: 'mixed snow and sleet',
8: 'freezing drizzle',
9: 'drizzle',
10: 'freezing rain',
11: 'showers',
12: 'showers',
13: 'snow flurries',
14: 'light snow showers',
15: 'blowing snow',
16: 'snow',
17: 'hail',
18: 'sleet',
19: 'dust',
20: 'foggy',
21: 'haze',
22: 'smoky',
23: 'blustery',
24: 'windy',
25: 'cold',
26: 'cloudy',
27: 'mostly cloudy at night',
28: 'mostly cloudy at day',
29: 'partly cloudy at night',
30: 'partly cloudy at day',
31: 'clear at night',
32: 'sunny',
33: 'fair at night',
34: 'fair at day',
35: 'mixed rain and hail',
36: 'hot',
37: 'isolated thunderstorms',
38: 'scattered thunderstorms',
39: 'scattered thunderstorms',
40: 'scattered showers',
41: 'heavy snow',
42: 'scattered snow showers',
43: 'heavy snow',
44: 'partly cloudy',
45: 'thundershowers',
46: 'snow showers',
47: 'isolated thundershowers'
}

WEEKDAY_NAMES = {
Expand All @@ -81,9 +81,9 @@
def yql_json_request(yql_query):
r = requests.get(YAHOO_YQL_URL,
params={
'q': yql_query,
'format': 'json',
'env': 'store://datatables.org/alltableswithkeys'},
'q': yql_query,
'format': 'json',
'env': 'store://datatables.org/alltableswithkeys'},
headers={'User-Agent': 'Mozilla/5.0'})
return r.json()

Expand All @@ -92,9 +92,9 @@ def get_woeid(location_name):
yql_query = YAHOO_YQL_QUERY_WOEID % location_name.replace('"', '')
r = requests.get(YAHOO_YQL_URL,
params={
'q': yql_query,
'format': 'json',
'env': 'store://datatables.org/alltableswithkeys'},
'q': yql_query,
'format': 'json',
'env': 'store://datatables.org/alltableswithkeys'},
headers={'User-Agent': 'Mozilla/5.0'})
content = r.json()
try:
Expand Down Expand Up @@ -134,7 +134,7 @@ def get_weather(woeid, unit="f"):
return Weather(city=channel['location']['city'],
date=current_date,
text=YAHOO_YQL_WEATHER_CONDITION_CODES[
int(channel['item']['condition']['code'])],
int(channel['item']['condition']['code'])],
temp=int(channel['item']['condition']['temp']),
forecast=forecast)

Expand Down Expand Up @@ -204,9 +204,9 @@ def handle(self, text, mic):
# Today
msg = self.gettext(
'Currently {text} at {temp} degrees in {city}.').format(
text=self.gettext(weather.text),
temp=weather.temp,
city=weather.city)
text=self.gettext(weather.text),
temp=weather.temp,
city=weather.city)
if len(weather.forecast) == 0:
mic.say(msg)
return
Expand Down Expand Up @@ -243,10 +243,10 @@ def _say_forecast_tomorrow(self, mic, weather):
mic.say(self.gettext(
'Tomorrow in {city}: {text} and temperatures ' +
'between {temp_low} and {temp_high} degrees.').format(
city=weather.city,
text=self.gettext(fc.text),
temp_low=fc.temp_low,
temp_high=fc.temp_high))
city=weather.city,
text=self.gettext(fc.text),
temp_low=fc.temp_low,
temp_high=fc.temp_high))
else:
mic.say(self.gettext(
"Sorry, I don't know what the weather in %s will " +
Expand All @@ -269,9 +269,9 @@ def _say_forecast(self, mic, weather):
forecast_msgs.append("%s: %s" % (date, self.gettext(
'{text} and temperatures between {temp_low} and ' +
'{temp_high} degrees.').format(
text=self.gettext(fc.text),
temp_low=fc.temp_low,
temp_high=fc.temp_high)))
text=self.gettext(fc.text),
temp_low=fc.temp_low,
temp_high=fc.temp_high)))
mic.say((self.gettext('Weather Forecast for the next %d days: ')
% len(weather.forecast)) + '... '.join(forecast_msgs))

Expand Down
3 changes: 2 additions & 1 deletion plugins/stt/google-stt/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class GoogleSTTPlugin(plugin.STTPlugin):
To obtain an API key:
1. Join the Chromium Dev group:
https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev
https://groups.google.com/a/chromium.org/
forum/?fromgroups#!forum/chromium-dev
2. Create a project through the Google Developers console:
https://console.developers.google.com/project
3. Select your project. In the sidebar, navigate to "APIs & Auth." Activate
Expand Down
7 changes: 4 additions & 3 deletions plugins/stt/julius-stt/julius.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, *args, **kwargs):
"support!")

vocabulary_path = self.compile_vocabulary(
juliusvocab.compile_vocabulary)
juliusvocab.compile_vocabulary)

self._dfa_file = juliusvocab.get_dfa_path(vocabulary_path)
self._dict_file = juliusvocab.get_dict_path(vocabulary_path)
Expand Down Expand Up @@ -95,5 +95,6 @@ def transcribe(self, fp, mode=None):
self._logger.info('Transcribed: %r', transcribed)
return transcribed

def get_vocabulary_type(self):
return JuliusVocabulary
# FIXME: this is not working!, JuliusVocabulary is undefined
# def get_vocabulary_type(self):
# return JuliusVocabulary
2 changes: 1 addition & 1 deletion plugins/stt/pocketsphinx-stt/phonemeconversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def xsampa_to_arpabet(xsampa_string, sep=' '):
else num_remaining_chars)

for j in range(phone_length, 0, -1):
phone = s[i:i+j]
phone = s[i:i + j]
if phone in XSAMPA_TO_ARPABET_MAPPING:
result.append(XSAMPA_TO_ARPABET_MAPPING[phone])
i += j
Expand Down
Loading

0 comments on commit 4f4628e

Please sign in to comment.