diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2014-10-06 16:52:53 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2014-10-06 16:52:53 +0200 |
| commit | 2b1e8411ab5eaa7e1349b78d03270ab02ae2950d (patch) | |
| tree | ed914cbf8964bbe26ba28e80589f4c289b3fac3a /client/modules | |
| parent | 890a5798fcf7f9b9dc06529aa4bc7fe903d7ca1a (diff) | |
| download | jasper-client-2b1e8411ab5eaa7e1349b78d03270ab02ae2950d.tar.gz jasper-client-2b1e8411ab5eaa7e1349b78d03270ab02ae2950d.zip | |
Fix flake8 style nits
Diffstat (limited to 'client/modules')
| -rw-r--r-- | client/modules/Birthday.py | 18 | ||||
| -rw-r--r-- | client/modules/Gmail.py | 7 | ||||
| -rw-r--r-- | client/modules/HN.py | 28 | ||||
| -rw-r--r-- | client/modules/Joke.py | 5 | ||||
| -rw-r--r-- | client/modules/Life.py | 3 | ||||
| -rw-r--r-- | client/modules/MPDControl.py | 73 | ||||
| -rw-r--r-- | client/modules/News.py | 24 | ||||
| -rw-r--r-- | client/modules/Notifications.py | 14 | ||||
| -rw-r--r-- | client/modules/Time.py | 3 | ||||
| -rw-r--r-- | client/modules/Unclear.py | 3 | ||||
| -rw-r--r-- | client/modules/Weather.py | 37 |
11 files changed, 129 insertions, 86 deletions
diff --git a/client/modules/Birthday.py b/client/modules/Birthday.py index b484a4f..200af94 100644 --- a/client/modules/Birthday.py +++ b/client/modules/Birthday.py @@ -1,7 +1,7 @@ # -*- coding: utf-8-*- import datetime import re -from facebook import * +import facebook from app_utils import getTimezone WORDS = ["BIRTHDAY"] @@ -15,18 +15,20 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ oauth_access_token = profile['keys']["FB_TOKEN"] - graph = GraphAPI(oauth_access_token) + graph = facebook.GraphAPI(oauth_access_token) try: - results = graph.request( - "me/friends", args={'fields': 'id,name,birthday'}) - except GraphAPIError: - mic.say( - "I have not been authorized to query your Facebook. If you would like to check birthdays in the future, please visit the Jasper dashboard.") + results = graph.request("me/friends", + args={'fields': 'id,name,birthday'}) + except facebook.GraphAPIError: + mic.say("I have not been authorized to query your Facebook. If you " + + "would like to check birthdays in the future, please visit " + + "the Jasper dashboard.") return except: mic.say( diff --git a/client/modules/Gmail.py b/client/modules/Gmail.py index 001cf9c..83ed3bd 100644 --- a/client/modules/Gmail.py +++ b/client/modules/Gmail.py @@ -3,7 +3,6 @@ import imaplib import email import re from dateutil import parser -from app_utils import * WORDS = ["EMAIL", "INBOX"] @@ -51,7 +50,8 @@ def fetchUnreadEmails(profile, since=None, markRead=False, limit=None): Fetches a list of unread email objects from a user's Gmail inbox. Arguments: - profile -- contains information related to the user (e.g., Gmail address) + profile -- contains information related to the user (e.g., Gmail + address) since -- if provided, no emails before this date will be returned markRead -- if True, marks all returned emails as read in target inbox @@ -93,7 +93,8 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., Gmail address) + profile -- contains information related to the user (e.g., Gmail + address) """ try: msgs = fetchUnreadEmails(profile, limit=5) diff --git a/client/modules/HN.py b/client/modules/HN.py index ec7ecef..d205829 100644 --- a/client/modules/HN.py +++ b/client/modules/HN.py @@ -51,7 +51,8 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ mic.say("Pulling up some stories.") stories = getTopStories(maxResults=3) @@ -93,17 +94,24 @@ def handle(text, mic, profile): if profile['prefers_email']: body += article_link else: - if not app_utils.emailUser(profile, SUBJECT="", BODY=article_link): - mic.say( - "I'm having trouble sending you these articles. Please make sure that your phone number and carrier are correct on the dashboard.") + if not app_utils.emailUser(profile, SUBJECT="", + BODY=article_link): + mic.say("I'm having trouble sending you these " + + "articles. Please make sure that your " + + "phone number and carrier are correct " + + "on the dashboard.") return # if prefers email, we send once, at the end if profile['prefers_email']: body += "</ul>" - if not app_utils.emailUser(profile, SUBJECT="From the Front Page of Hacker News", BODY=body): - mic.say( - "I'm having trouble sending you these articles. Please make sure that your phone number and carrier are correct on the dashboard.") + if not app_utils.emailUser(profile, + SUBJECT="From the Front Page of " + + "Hacker News", + BODY=body): + mic.say("I'm having trouble sending you these articles. " + + "Please make sure that your phone number and " + + "carrier are correct on the dashboard.") return mic.say("All done.") @@ -113,12 +121,12 @@ def handle(text, mic, profile): if not profile['prefers_email'] and profile['phone_number']: mic.say("Here are some front-page articles. " + - all_titles + ". Would you like me to send you these? If so, which?") + all_titles + ". Would you like me to send you these? " + + "If so, which?") handleResponse(mic.activeListen()) else: - mic.say( - "Here are some front-page articles. " + all_titles) + mic.say("Here are some front-page articles. " + all_titles) def isValid(text): diff --git a/client/modules/Joke.py b/client/modules/Joke.py index c560f4e..7192b03 100644 --- a/client/modules/Joke.py +++ b/client/modules/Joke.py @@ -6,7 +6,7 @@ import jasperpath WORDS = ["JOKE", "KNOCK KNOCK"] -def getRandomJoke(filename=jasperpath.data('text','JOKES.txt')): +def getRandomJoke(filename=jasperpath.data('text', 'JOKES.txt')): jokeFile = open(filename, "r") jokes = [] start = "" @@ -38,7 +38,8 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ joke = getRandomJoke() diff --git a/client/modules/Life.py b/client/modules/Life.py index 4cafd75..658e5cf 100644 --- a/client/modules/Life.py +++ b/client/modules/Life.py @@ -13,7 +13,8 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ messages = ["It's 42, you idiot.", "It's 42. How many times do I have to tell you?"] diff --git a/client/modules/MPDControl.py b/client/modules/MPDControl.py index 6e8e438..eefdcbd 100644 --- a/client/modules/MPDControl.py +++ b/client/modules/MPDControl.py @@ -11,14 +11,16 @@ from mic import Mic # Standard module stuff WORDS = ["MUSIC", "SPOTIFY"] + def handle(text, mic, profile): """ - Responds to user-input, typically speech text, by telling a joke. + Responds to user-input, typically speech text, by telling a joke. - Arguments: + Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ logger = logging.getLogger(__name__) @@ -34,7 +36,8 @@ def handle(text, mic, profile): mpdwrapper = MPDWrapper(**kwargs) except: logger.error("Couldn't connect to MPD server", exc_info=True) - mic.say("I'm sorry. It seems that Spotify is not enabled. Please read the documentation to learn how to configure Spotify.") + mic.say("I'm sorry. It seems that Spotify is not enabled. Please " + + "read the documentation to learn how to configure Spotify.") return mic.say("Please give me a moment, I'm loading your Spotify playlists.") @@ -49,6 +52,7 @@ def handle(text, mic, profile): return + def isValid(text): """ Returns True if the input is related to jokes/humor. @@ -58,6 +62,7 @@ def isValid(text): """ return any(word in text.upper() for word in WORDS) + # The interesting part class MusicMode(object): @@ -68,9 +73,9 @@ class MusicMode(object): self.music = mpdwrapper # index spotify playlists into new dictionary and language models - original = self.music.get_soup_playlist( - ) + ["STOP", "CLOSE", "PLAY", "PAUSE", - "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST"] + original = ["STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", + "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", + "PLAYLIST"] + self.music.get_soup_playlist() pronounced = g2p.translateWords(original) zipped = zip(original, pronounced) lines = ["%s %s" % (x, y) for x, y in zipped] @@ -84,16 +89,17 @@ class MusicMode(object): f.close() # make language model - os.system( - "text2idngram -vocab sentences_spotify.txt < sentences_spotify.txt -idngram spotify.idngram") - os.system( - "idngram2lm -idngram spotify.idngram -vocab sentences_spotify.txt -arpa languagemodel_spotify.lm") + os.system("text2idngram -vocab sentences_spotify.txt < " + + "sentences_spotify.txt -idngram spotify.idngram") + os.system("idngram2lm -idngram spotify.idngram -vocab " + + "sentences_spotify.txt -arpa languagemodel_spotify.lm") # create a new mic with the new music models self.mic = Mic( mic.speaker, mic.passive_stt_engine, - stt.PocketSphinxSTT(lmd_music="languagemodel_spotify.lm", dictd_music="dictionary_spotify.dic") + stt.PocketSphinxSTT(lmd_music="languagemodel_spotify.lm", + dictd_music="dictionary_spotify.dic") ) def delegateInput(self, input): @@ -195,6 +201,7 @@ class MusicMode(object): self.mic.say("Pardon?") self.music.play() + def reconnect(func, *default_args, **default_kwargs): """ Reconnects before running @@ -228,6 +235,7 @@ class Song(object): self.artist = artist self.album = album + class MPDWrapper(object): def __init__(self, server="localhost", port=6600): """ @@ -329,7 +337,7 @@ class MPDWrapper(object): def get_soup(self): """ - returns the list of unique words that comprise song and artist titles + Returns the list of unique words that comprise song and artist titles """ soup = [] @@ -340,17 +348,17 @@ class MPDWrapper(object): soup.extend(song_words) soup.extend(artist_words) - title_trans = ''.join( - chr(c) if chr(c).isupper() or chr(c).islower() else '_' for c in range(256)) - soup = [x.decode('utf-8').encode("ascii", "ignore").upper().translate(title_trans).replace("_", "") - for x in soup] + title_trans = ''.join(chr(c) if chr(c).isupper() or chr(c).islower() + else '_' for c in range(256)) + soup = [x.decode('utf-8').encode("ascii", "ignore").upper().translate( + title_trans).replace("_", "") for x in soup] soup = [x for x in soup if x != ""] return list(set(soup)) def get_soup_playlist(self): """ - returns the list of unique words that comprise playlist names + Returns the list of unique words that comprise playlist names """ soup = [] @@ -358,17 +366,17 @@ class MPDWrapper(object): for name in self.playlists: soup.extend(name.split(" ")) - title_trans = ''.join( - chr(c) if chr(c).isupper() or chr(c).islower() else '_' for c in range(256)) - soup = [x.decode('utf-8').encode("ascii", "ignore").upper().translate(title_trans).replace("_", "") - for x in soup] + title_trans = ''.join(chr(c) if chr(c).isupper() or chr(c).islower() + else '_' for c in range(256)) + soup = [x.decode('utf-8').encode("ascii", "ignore").upper().translate( + title_trans).replace("_", "") for x in soup] soup = [x for x in soup if x != ""] return list(set(soup)) def get_soup_separated(self): """ - returns the list of PHRASES that comprise song and artist titles + Returns the list of PHRASES that comprise song and artist titles """ title_soup = [song.title for song in self.songs] @@ -376,23 +384,26 @@ class MPDWrapper(object): soup = list(set(title_soup + artist_soup)) - title_trans = ''.join( - chr(c) if chr(c).isupper() or chr(c).islower() else '_' for c in range(256)) - soup = [x.decode('utf-8').encode("ascii", "ignore").upper().translate(title_trans).replace("_", " ") - for x in soup] + title_trans = ''.join(chr(c) if chr(c).isupper() or chr(c).islower() + else '_' for c in range(256)) + soup = [x.decode('utf-8').encode("ascii", "ignore").upper().translate( + title_trans).replace("_", " ") for x in soup] soup = [re.sub(' +', ' ', x) for x in soup if x != ""] return soup def fuzzy_songs(self, query): """ - Returns songs matching a query best as possible on either artist field, etc + Returns songs matching a query best as possible on either artist + field, etc """ query = query.upper() - matched_song_titles = difflib.get_close_matches(query, self.song_titles) - matched_song_artists = difflib.get_close_matches(query, self.song_artists) + matched_song_titles = difflib.get_close_matches(query, + self.song_titles) + matched_song_artists = difflib.get_close_matches(query, + self.song_artists) # if query is beautifully matched, then forget about everything else strict_priority_title = [x for x in matched_song_titles if x == query] @@ -420,4 +431,4 @@ class MPDWrapper(object): query = query.upper() lookup = {n.upper(): n for n in self.playlists} results = [lookup[r] for r in difflib.get_close_matches(query, lookup)] - return results
\ No newline at end of file + return results diff --git a/client/modules/News.py b/client/modules/News.py index d80b791..433ea05 100644 --- a/client/modules/News.py +++ b/client/modules/News.py @@ -41,7 +41,8 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ mic.say("Pulling up the news") articles = getTopArticles(maxResults=3) @@ -84,17 +85,23 @@ def handle(text, mic, profile): if profile['prefers_email']: body += article_link else: - if not app_utils.emailUser(profile, SUBJECT="", BODY=article_link): - mic.say( - "I'm having trouble sending you these articles. Please make sure that your phone number and carrier are correct on the dashboard.") + if not app_utils.emailUser(profile, SUBJECT="", + BODY=article_link): + mic.say("I'm having trouble sending you these " + + "articles. Please make sure that your " + + "phone number and carrier are correct " + + "on the dashboard.") return # if prefers email, we send once, at the end if profile['prefers_email']: body += "</ul>" - if not app_utils.emailUser(profile, SUBJECT="Your Top Headlines", BODY=body): - mic.say( - "I'm having trouble sending you these articles. Please make sure that your phone number and carrier are correct on the dashboard.") + if not app_utils.emailUser(profile, + SUBJECT="Your Top Headlines", + BODY=body): + mic.say("I'm having trouble sending you these articles. " + + "Please make sure that your phone number and " + + "carrier are correct on the dashboard.") return mic.say("All set") @@ -105,7 +112,8 @@ def handle(text, mic, profile): if 'phone_number' in profile: mic.say("Here are the current top headlines. " + all_titles + - ". Would you like me to send you these articles? If so, which?") + ". Would you like me to send you these articles? " + + "If so, which?") handleResponse(mic.activeListen()) else: diff --git a/client/modules/Notifications.py b/client/modules/Notifications.py index 9ee004c..2d413b6 100644 --- a/client/modules/Notifications.py +++ b/client/modules/Notifications.py @@ -1,6 +1,6 @@ # -*- coding: utf-8-*- import re -from facebook import * +import facebook WORDS = ["FACEBOOK", "NOTIFICATION"] @@ -15,17 +15,19 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ oauth_access_token = profile['keys']['FB_TOKEN'] - graph = GraphAPI(oauth_access_token) + graph = facebook.GraphAPI(oauth_access_token) try: results = graph.request("me/notifications") - except GraphAPIError: - mic.say( - "I have not been authorized to query your Facebook. If you would like to check your notifications in the future, please visit the Jasper dashboard.") + except facebook.GraphAPIError: + mic.say("I have not been authorized to query your Facebook. If you " + + "would like to check your notifications in the future, " + + "please visit the Jasper dashboard.") return except: mic.say( diff --git a/client/modules/Time.py b/client/modules/Time.py index 90ffdf9..8bc5c8e 100644 --- a/client/modules/Time.py +++ b/client/modules/Time.py @@ -14,7 +14,8 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ tz = getTimezone(profile) diff --git a/client/modules/Unclear.py b/client/modules/Unclear.py index 3900300..071eea3 100644 --- a/client/modules/Unclear.py +++ b/client/modules/Unclear.py @@ -14,7 +14,8 @@ def handle(text, mic, profile): Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ messages = ["I'm sorry, could you repeat that?", diff --git a/client/modules/Weather.py b/client/modules/Weather.py index 5922d33..7076f50 100644 --- a/client/modules/Weather.py +++ b/client/modules/Weather.py @@ -9,7 +9,9 @@ WORDS = ["WEATHER", "TODAY", "TOMORROW"] def replaceAcronyms(text): - """Replaces some commonly-used acronyms for an improved verbal weather report.""" + """ + Replaces some commonly-used acronyms for an improved verbal weather report. + """ def parseDirections(text): words = { @@ -39,19 +41,21 @@ def getForecast(profile): def handle(text, mic, profile): """ - Responds to user-input, typically speech text, with a summary of - the relevant weather for the requested date (typically, weather - information will not be available for days beyond tomorrow). + Responds to user-input, typically speech text, with a summary of + the relevant weather for the requested date (typically, weather + information will not be available for days beyond tomorrow). - Arguments: + Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) - profile -- contains information related to the user (e.g., phone number) + profile -- contains information related to the user (e.g., phone + number) """ if not profile['location']: mic.say( - "I'm sorry, I can't seem to access that information. Please make sure that you've set your location on the dashboard.") + "I'm sorry, I can't seem to access that information. Please make" + + "sure that you've set your location on the dashboard.") return tz = getTimezone(profile) @@ -77,14 +81,16 @@ def handle(text, mic, profile): for entry in forecast: try: date_desc = entry['title'].split()[0].strip().lower() - if date_desc == 'forecast': #For global forecasts - date_desc = entry['title'].split()[2].strip().lower() - weather_desc = entry['summary'] - - elif date_desc == 'current': #For first item of global forecasts - continue + if date_desc == 'forecast': + # For global forecasts + date_desc = entry['title'].split()[2].strip().lower() + weather_desc = entry['summary'] + elif date_desc == 'current': + # For first item of global forecasts + continue else: - weather_desc = entry['summary'].split('-')[1] #US forecasts + # US forecasts + weather_desc = entry['summary'].split('-')[1] if weekday == date_desc: output = date_keyword + \ @@ -108,4 +114,5 @@ def isValid(text): Arguments: text -- user-input, typically transcribed speech """ - return bool(re.search(r'\b(weathers?|temperature|forecast|outside|hot|cold|jacket|coat|rain)\b', text, re.IGNORECASE)) + return bool(re.search(r'\b(weathers?|temperature|forecast|outside|hot|' + + r'cold|jacket|coat|rain)\b', text, re.IGNORECASE)) |
