Skip to content

Commit e123a8a

Browse files
committed
Refactoring logs and naming.
1 parent aff82e6 commit e123a8a

26 files changed

+131
-111
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Exclude python executables and cached folders
22
*.pyc
3-
__pycache__/
3+
__pycache__
44

55
# Exclude pycharm metadata
66
.idea/

src/jarvis/jarvis/core/console_manager.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def clear(self):
4444

4545
subprocess.call('tput reset' if os.name == 'posix' else 'cls', shell=True)
4646

47-
def console_output(self, text='', debug_log=None, info_log=None, error_log=None, refresh_console=True):
47+
def console_output(self, text='', debug_log=None, info_log=None, warn_log=None, error_log=None, refresh_console=True):
4848
"""
4949
This method creates the assistant output.
5050
The output has four sectors:
@@ -117,6 +117,9 @@ def console_output(self, text='', debug_log=None, info_log=None, error_log=None,
117117
if info_log:
118118
logging.info(info_log)
119119

120+
if warn_log:
121+
logging.warning(warn_log)
122+
120123
if error_log:
121124
logging.error(error_log)
122125

src/jarvis/jarvis/core/nlp_processor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _create_response(self, sentence):
122122
logging.info('The user speech has an inverse question')
123123
answer = 'I ' + ' ' + verb + ' ' + noun
124124
else:
125-
logging.info('Unclassified question type')
125+
logging.debug('Unclassified question type')
126126
answer = ''
127127

128128
return re.sub('\s\s+', ' ', answer)

src/jarvis/jarvis/core/processor.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -57,42 +57,42 @@ def run(self):
5757
- STEP 5: Insert user transcript and response in history collection (in MongoDB)
5858
5959
"""
60-
# STEP 1
60+
6161
transcript = jarvis.input_engine.recognize_input()
62+
skill = self.skill_analyzer.extract(transcript)
63+
64+
if skill:
65+
skill_to_execute = {'voice_transcript': transcript, 'skill': skill}
66+
67+
response = self.response_creator.create_positive_response(transcript) if skill_to_execute \
68+
else self.response_creator.create_negative_response(transcript)
69+
jarvis.output_engine.assistant_response(response)
6270

63-
# STEP 2
64-
skill_to_execute = self._extract_skill(transcript)
71+
self._execute_skill(skill_to_execute)
6572

66-
# STEP 3
67-
response = self.response_creator.create_positive_response(transcript) if skill_to_execute \
68-
else self.response_creator.create_negative_response(transcript)
69-
jarvis.output_engine.assistant_response(response)
73+
else:
74+
skill_to_execute = {'voice_transcript': transcript, 'skill': {
75+
'name': WolframSkills.call_wolframalpha.__name__}
76+
}
7077

71-
# STEP 4
72-
self._execute_skill(skill_to_execute)
78+
response = WolframSkills.call_wolframalpha(transcript)
7379

74-
# STEP 5
7580
record = {'user_transcript': transcript,
7681
'response': response if response else '--',
7782
'executed_skill': skill_to_execute if skill_to_execute else '--'
7883
}
7984

8085
db.insert_many_documents('history', [record])
8186

82-
def _extract_skill(self, transcript):
83-
skill = self.skill_analyzer.extract(transcript)
84-
if skill:
85-
return {'voice_transcript': transcript, 'skill': skill}
86-
else:
87-
return WolframSkills.call_wolframalpha(transcript)
88-
8987
def _execute_skill(self, skill):
9088
if skill:
89+
skill_func_name = skill.get('skill').get('func')
90+
self.console_manager.console_output(info_log='Executing skill {0}'.format(skill_func_name))
9191
try:
9292
ActivationSkills.enable_assistant()
93-
logging.debug('Executing skill {0}'.format(skill.get('skill').get('name')))
9493
skill_func_name = skill.get('skill').get('func')
9594
skill_func = skill_objects[skill_func_name]
9695
skill_func(**skill)
9796
except Exception as e:
98-
self.console_manager.console_output(error_log="Error with the execution of skill with message {0}".format(e))
97+
self.console_manager.console_output(error_log="Failed to execute skill {0} with message {1}"
98+
.format(skill_func_name, e))

src/jarvis/jarvis/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
ROOT_LOG_CONF = {
2626
'version': 1,
2727
'root': {
28-
'level': 'DEBUG',
28+
'level': 'INFO',
2929
'handlers': ['file'],
3030
},
3131
'handlers': {

src/jarvis/jarvis/skills/assistant_activation.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23-
import logging
2423
import sys
2524
import time
2625
from datetime import datetime
@@ -38,24 +37,21 @@ def enable_assistant(cls, **kwargs):
3837
"""
3938
Plays activation sound and creates the assistant response according to the day hour.
4039
"""
40+
4141
input_mode = db.get_documents(collection=MongoCollections.GENERAL_SETTINGS.value)[0]['input_mode']
4242
if input_mode == InputMode.VOICE.value:
43-
try:
44-
play_activation_sound()
45-
except Exception as e:
46-
logging.error("Error with the execution of skill with message {0}".format(e))
47-
cls.response("Sorry I faced an issue")
48-
43+
play_activation_sound()
4944

5045
@classmethod
5146
def disable_assistant(cls, **kwargs):
5247
"""
5348
- Clear console
5449
- Shutdown the assistant service
5550
"""
51+
5652
cls.response('Bye')
5753
time.sleep(1)
58-
logging.debug('Application terminated gracefully.')
54+
cls.console(info_log='Application terminated gracefully.')
5955
sys.exit()
6056

6157
@classmethod

src/jarvis/jarvis/skills/assistant_info.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23-
import logging
24-
2523
from jarvis.skills.assistant_skill import AssistantSkill
2624
from jarvis.utils.mongoDB import db
2725
from jarvis.utils.console import add_dashes
@@ -72,7 +70,7 @@ def tell_the_skills(cls, **kwargs):
7270
response = cls._create_skill_response(response_base)
7371
cls.response(response)
7472
except Exception as e:
75-
logging.error("Error with the execution of skill with message {0}".format(e))
73+
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
7674
cls.response("Sorry I faced an issue")
7775

7876
@classmethod

src/jarvis/jarvis/skills/assistant_skill.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@ class AssistantSkill:
2828
"""
2929
This class is the parent of all skill classes.
3030
"""
31+
3132
first_activation = True
3233
console_manager = ConsoleManager()
3334

3435
@classmethod
35-
def console(cls, text, refresh_console=True):
36+
def console(cls, text='', debug_log=None, info_log=None, warn_log=None, error_log=None, refresh_console=True):
3637
"""
3738
Prints the text only in console and update the console info.
3839
"""
39-
cls.console_manager.console_output(text=text, refresh_console=refresh_console)
40+
41+
cls.console_manager.console_output(text=text,
42+
debug_log=debug_log,
43+
info_log=info_log,
44+
warn_log=warn_log,
45+
error_log=error_log,
46+
refresh_console=refresh_console)
4047

4148
@classmethod
4249
def response(cls, text, refresh_console=True):
@@ -66,6 +73,7 @@ def extract_tags(cls, voice_transcript, tags):
6673
:param tags: string
6774
:return: set
6875
"""
76+
6977
try:
7078
transcript_words = voice_transcript.split()
7179
tags = tags.split(',')

src/jarvis/jarvis/skills/browser.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import wikipedia
2424
import requests
25-
import logging
2625
import time
2726
import re
2827
import urllib.request
@@ -47,16 +46,16 @@ def tell_me_about(cls, voice_transcript, skill):
4746
only_text_pattern = '([a-zA-Z]+)'
4847
for tag in tags:
4948
reg_ex = re.search(tag + ' ' + only_text_pattern, voice_transcript)
50-
try:
51-
if reg_ex:
52-
topic = reg_ex.group(1)
49+
if reg_ex:
50+
topic = reg_ex.group(1)
51+
try:
5352
response = cls._decoded_wiki_response(topic)
5453
cls.response(response)
55-
except Exception as e:
56-
logging.error("Error with the execution of skill with message {0}".format(e))
57-
cls.response(" I can't find what you want, and I will open a new tab in browser")
58-
time.sleep(1)
59-
cls._search_on_google(topic)
54+
except Exception as e:
55+
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
56+
cls.response(" I can't find what you want, and I will open a new tab in browser")
57+
time.sleep(1)
58+
cls._search_on_google(topic)
6059

6160
@classmethod
6261
def open_in_youtube(cls, voice_transcript, skill):
@@ -65,6 +64,7 @@ def open_in_youtube(cls, voice_transcript, skill):
6564
:param voice_transcript: string (e.g 'about google')
6665
:param skill: dict (e.g
6766
"""
67+
6868
tags = cls.extract_tags(voice_transcript, skill['tags'])
6969
for tag in tags:
7070
reg_ex = re.search(tag + ' ([a-zA-Z]+)', voice_transcript)
@@ -79,7 +79,7 @@ def open_in_youtube(cls, voice_transcript, skill):
7979
video = 'https://www.youtube.com' + vids[0]['href']
8080
subprocess.Popen(["python", "-m", "webbrowser", "-t", video], stdout=subprocess.PIPE, shell=False)
8181
except Exception as e:
82-
logging.error("Error with the execution of skill with message {0}".format(e))
82+
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
8383
cls.response("I can't find what do you want in Youtube..")
8484

8585
@classmethod
@@ -115,7 +115,7 @@ def open_website_in_browser(cls, voice_transcript, skill):
115115
webbrowser.open_new_tab(url)
116116
cls.response('I opened the {0}'.format(domain))
117117
except Exception as e:
118-
logging.error("Error with the execution of skill with message {0}".format(e))
118+
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
119119
cls.response("I can't find this domain..")
120120

121121
@classmethod
@@ -133,7 +133,7 @@ def tell_me_today_news(cls, **kwargs):
133133
response += data.decode()
134134
cls.response(response)
135135
except Exception as e:
136-
logging.error("Error with the execution of skill with message {0}".format(e))
136+
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
137137
cls.response("I can't find about daily news..")
138138

139139
@classmethod
@@ -171,6 +171,6 @@ def _search_on_google(cls, term):
171171
try:
172172
webbrowser.open_new_tab(url)
173173
except Exception as e:
174-
logging.error("Error with the execution of skill with message {0}".format(e))
174+
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
175175
cls.response("Sorry I faced an issue with google search")
176176

src/jarvis/jarvis/skills/datetime.py

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def tell_the_time(cls, **kwargs):
4747
"""
4848
Tells ths current time
4949
"""
50+
5051
now = datetime.now()
5152
hour, minute = now.hour, now.minute
5253
# TODO: tim_in_text only in speech response
@@ -58,6 +59,7 @@ def tell_the_date(cls, **kwargs):
5859
"""
5960
Tells ths current date
6061
"""
62+
6163
today = date.today()
6264
cls.response('The current date is: {0}'.format(today))
6365

src/jarvis/jarvis/skills/util_skills.py renamed to src/jarvis/jarvis/skills/general.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# SOFTWARE.
2222

2323
from jarvis.skills.assistant_skill import AssistantSkill
24+
import jarvis
2425

2526

2627
class UtilSkills(AssistantSkill):
@@ -30,7 +31,7 @@ def speech_interruption(cls, **kwargs):
3031
"""
3132
Stop assistant speech.
3233
"""
33-
cls.output_engine.stop_speaking = True
34+
jarvis.output_engine.stop_speaking = True
3435

3536
@classmethod
3637
def clear_console(cls, **kwargs):

src/jarvis/jarvis/skills/history.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,23 @@
2121
# SOFTWARE.
2222

2323
import re
24-
import logging
2524

2625
from jarvis.skills.assistant_skill import AssistantSkill
27-
from jarvis.utils import console
2826
from jarvis.utils.mongoDB import db
2927

3028
header = """
31-
------------------------
32-
- History -
33-
------------------------
34-
* Note: The default limit is 3. Change the limit by adding a number e.g
35-
show me user history 10.
29+
-----------------------------------------------------------------------------------------------
30+
- History -
31+
-----------------------------------------------------------------------------------------------
32+
* Note: The default limit is 3. Change the limit by adding a number e.g show me user history 10.
3633
3734
"""
3835

3936
response_base = """
4037
* User Transcript: {0}
4138
* Response: {1}
4239
* Executed Skill: {2}
43-
------------------------"""
40+
-----------------------------------------------------------------------------------------------"""
4441

4542

4643
class HistorySkills(AssistantSkill):
@@ -70,7 +67,7 @@ def _create_response(cls, documents):
7067
document.get('executed_skill') else '--'
7168
)
7269
except Exception as e:
73-
logging.error(e)
70+
cls.console(error_log=e)
7471
finally:
7572
from jarvis.utils import user_input, console
7673
return header + response

src/jarvis/jarvis/skills/internet.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import speedtest
2626

2727
from jarvis.skills.assistant_skill import AssistantSkill
28+
from jarvis.utils.startup import internet_connectivity_check
2829

2930

3031
class InternetSkills(AssistantSkill):
@@ -64,9 +65,9 @@ def internet_availability(cls, **kwargs):
6465
"""
6566
Tells to the user is the internet is available or not.
6667
"""
67-
try:
68-
_ = requests.get('http://www.google.com/', timeout=1)
69-
cls.response("Yes, the internet connection is ok")
70-
except requests.ConnectionError as e:
71-
cls.response("No the internet is down for now")
72-
logging.error("No internet connection with message: {0}".format(e))
68+
if internet_connectivity_check():
69+
cls.response("The internet connection is ok")
70+
return True
71+
else:
72+
cls.response("The internet is down for now")
73+
return False

src/jarvis/jarvis/skills/learnskills.py renamed to src/jarvis/jarvis/skills/learn.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
from jarvis.skills.assistant_skill import AssistantSkill
2525
from jarvis.utils.mongoDB import db
26-
from jarvis.utils import console, user_input
26+
from jarvis.utils import user_input
2727

2828
header = """
29-
-------------------------------------------------------------------------
29+
-----------------------------------------------------------------------------------------------
3030
I would like to learn, tell me the right answer!
31-
-------------------------------------------------------------------------
31+
-----------------------------------------------------------------------------------------------
3232
* Note: Create new skill! Write your question and the appropriate answer.
3333
\n
3434
"""

src/jarvis/jarvis/skills/libreoffice_suite.py renamed to src/jarvis/jarvis/skills/libreoffice.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import subprocess
2424

25-
2625
from jarvis.skills.assistant_skill import AssistantSkill
2726

2827

0 commit comments

Comments
 (0)