Skip to content

Commit b4bdb0a

Browse files
committed
Refactoring: Minor changes.
1 parent 43c0992 commit b4bdb0a

File tree

6 files changed

+107
-26
lines changed

6 files changed

+107
-26
lines changed

src/jarvis/jarvis/core/console.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from jarvis import settings
2929
from jarvis.utils.mongoDB import db
30-
from jarvis.utils.console import jarvis_logo, start_text, OutputStyler, add_dashes
30+
from jarvis.utils.console import jarvis_logo, start_text, OutputStyler, headerize
3131
from jarvis.enumerations import MongoCollections, InputMode
3232

3333

@@ -93,7 +93,7 @@ def console_output(self, text='', debug_log=None, info_log=None, warn_log=None,
9393
settings_documents = db.get_documents(collection=MongoCollections.GENERAL_SETTINGS.value)
9494
if settings_documents:
9595
settings_ = settings_documents[0]
96-
print(OutputStyler.HEADER + add_dashes('GENERAL INFO') + OutputStyler.ENDC)
96+
print(OutputStyler.HEADER + headerize('GENERAL INFO') + OutputStyler.ENDC)
9797
enabled = OutputStyler.GREEN + 'ENABLED' + OutputStyler.ENDC if settings_['response_in_speech'] else OutputStyler.WARNING + 'NOT ENABLED' + OutputStyler.ENDC
9898
print(OutputStyler.BOLD + 'RESPONSE IN SPEECH: ' + enabled)
9999
print(OutputStyler.BOLD + 'INPUT MODE: ' + OutputStyler.GREEN + '{0}'.format(settings_['input_mode'].upper() + OutputStyler.ENDC) + OutputStyler.ENDC)
@@ -103,7 +103,7 @@ def console_output(self, text='', debug_log=None, info_log=None, warn_log=None,
103103
# ----------------------------------------------------------------------------------------------------------
104104
# System info sector
105105
# ----------------------------------------------------------------------------------------------------------
106-
print(OutputStyler.HEADER + add_dashes('SYSTEM') + OutputStyler.ENDC)
106+
print(OutputStyler.HEADER + headerize('SYSTEM') + OutputStyler.ENDC)
107107
print(OutputStyler.BOLD +
108108
'RAM USAGE: {0:.2f} GB'.format(self._get_memory()) + OutputStyler.ENDC)
109109

@@ -128,18 +128,18 @@ def console_output(self, text='', debug_log=None, info_log=None, warn_log=None,
128128

129129
lines = subprocess.check_output(['tail', '-' + str(MAX_NUMBER_OF_LOG_LINES), log_path]).decode("utf-8")
130130
actual_number_of_log_lines = len(lines)
131-
print(OutputStyler.HEADER + add_dashes('LOG -{0} (Total Lines: {1})'.format(log_path,
132-
actual_number_of_log_lines)
133-
) + OutputStyler.ENDC)
131+
print(OutputStyler.HEADER + headerize('LOG -{0} (Total Lines: {1})'.format(log_path,
132+
actual_number_of_log_lines)
133+
) + OutputStyler.ENDC)
134134
print(OutputStyler.BOLD + lines + OutputStyler.ENDC)
135135

136136
# ----------------------------------------------------------------------------------------------------------
137137
# Assistant input/output sector
138138
# ----------------------------------------------------------------------------------------------------------
139-
print(OutputStyler.HEADER + add_dashes('ASSISTANT') + OutputStyler.ENDC)
139+
print(OutputStyler.HEADER + headerize('ASSISTANT') + OutputStyler.ENDC)
140140
if text:
141141
print(OutputStyler.BOLD + '> ' + text + '\r' + OutputStyler.ENDC)
142-
print(OutputStyler.HEADER + add_dashes('-') + OutputStyler.ENDC)
142+
print(OutputStyler.HEADER + headerize() + OutputStyler.ENDC)
143143
else:
144144
if text:
145145
print(OutputStyler.BOLD + text + '\r' + OutputStyler.ENDC)

src/jarvis/jarvis/core/processor.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ def run(self):
7070
self._execute_skill(skill_to_execute)
7171

7272
else:
73-
skill_to_execute = {'voice_transcript': transcript, 'skill': {
74-
'name': WolframSkills.call_wolframalpha.__name__}
73+
# If no skill to execute, it calls the WolframAlpha API
74+
skill_to_execute = {'voice_transcript': transcript,
75+
'skill': {'name': WolframSkills.call_wolframalpha.__name__}
7576
}
7677

7778
response = WolframSkills.call_wolframalpha(transcript)

src/jarvis/jarvis/skills/collection/configuration.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ def configure_assistant(cls, **kwargs):
4141

4242
console.print_console_header('Configure assistant')
4343
cls.console('NOTE: Current name: {0}'.format(assistant_name), refresh_console=False)
44-
console.add_dashes()
44+
console.headerize()
4545
cls.response('Set new assistant name: ', refresh_console=False)
4646
new_assistant_name = cls.user_input()
4747

48-
console.add_dashes()
48+
console.headerize()
4949

5050
cls.console('NOTE: Current mode: {0}'.format(input_mode), refresh_console=False)
51-
console.add_dashes()
51+
console.headerize()
5252
cls.response('Set new input mode (text or voice): ')
5353
input_mode_values = [mode.value for mode in InputMode]
5454
new_input_mode = input.validate_input_with_choices(available_choices=input_mode_values)
5555

56-
console.add_dashes()
56+
console.headerize()
5757
cls.response('Do you want response in speech?', refresh_console=False)
5858
new_response_in_speech = input.check_input_to_continue()
5959

src/jarvis/jarvis/skills/collection/datetime.py

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def tell_the_time(cls, **kwargs):
5050

5151
now = datetime.now()
5252
hour, minute = now.hour, now.minute
53-
# TODO: tim_in_text only in speech response
5453
converted_time = cls._time_in_text(hour, minute)
5554
cls.response('The current time is: {0}'.format(converted_time))
5655

src/jarvis/jarvis/skills/collection/general.py

+70
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,31 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
import subprocess
24+
2325
from jarvis.skills.skill import AssistantSkill
2426
import jarvis
2527

2628

29+
def get_master_volume():
30+
stdout, stderr = subprocess.Popen('/usr/bin/amixer sget Master', shell=True,
31+
stdout=subprocess.PIPE).communicate()
32+
list_len = len(str(stdout).split('\n'))
33+
amixer_stdout = str(stdout).split('\n')[list_len - 1]
34+
35+
find_start = amixer_stdout.find('[') + 1
36+
find_end = amixer_stdout.find('%]', find_start)
37+
38+
return float(amixer_stdout[find_start:find_end])
39+
40+
41+
def set_master_volume(volume):
42+
val = float(int(volume))
43+
44+
proc = subprocess.Popen('/usr/bin/amixer sset Master ' + str(val) + '%', shell=True, stdout=subprocess.PIPE)
45+
proc.wait()
46+
47+
2748
class UtilSkills(AssistantSkill):
2849

2950
@classmethod
@@ -37,3 +58,52 @@ def speech_interruption(cls, **kwargs):
3758
def clear_console(cls, **kwargs):
3859
cls.console("Sure")
3960

61+
@classmethod
62+
def increase_master_volume(cls, **kwargs):
63+
# Limits: Playback 0 - 31
64+
step = 2
65+
volume = get_master_volume()
66+
if volume > 31:
67+
cls.response("The speakers volume is already max")
68+
69+
increased_volume = volume + step
70+
if increased_volume > 31:
71+
set_master_volume(31)
72+
else:
73+
set_master_volume(increased_volume)
74+
cls.response("I increased the speakers volume")
75+
76+
@classmethod
77+
def reduce_master_volume(cls, **kwargs):
78+
# Limits: Playback 0 - 31
79+
step = 2
80+
volume = get_master_volume()
81+
if volume < 0:
82+
cls.response("The speakers volume is already muted")
83+
84+
reduced_volume = volume - step
85+
if reduced_volume < 0:
86+
set_master_volume(0)
87+
else:
88+
set_master_volume(reduced_volume)
89+
cls.response("I reduced the speakers volume")
90+
91+
@classmethod
92+
def mute_master_volume(cls, **kwargs):
93+
# Limits: Playback 0 - 31
94+
volume = get_master_volume()
95+
if volume == 0:
96+
cls.response("The speakers volume is already muted")
97+
else:
98+
set_master_volume(0)
99+
cls.response("I mute the master speakers")
100+
101+
@classmethod
102+
def max_master_volume(cls, **kwargs):
103+
# Limits: Playback 0 - 31
104+
volume = get_master_volume()
105+
if volume == 31:
106+
cls.response("The speakers volume is already max")
107+
else:
108+
set_master_volume(31)
109+
cls.response("I set max the master speakers")

src/jarvis/jarvis/utils/console.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,42 @@ class OutputStyler:
3939

4040
user_input = OutputStyler.CYAN + ':-$ ' + OutputStyler.ENDC
4141

42+
DASH = '='
4243

43-
def add_dashes(text='='):
44+
45+
def headerize(text=DASH):
4446
"""
45-
Add dashes based on terminal length
47+
Add dashes based on terminal length.
48+
49+
Example:
50+
---------------------------------------------------
51+
text --> Result
52+
---------------------------------------------------
53+
54+
SYSTEM --> ================ SYSTEM ================
55+
None --> ========================================
56+
4657
"""
58+
4759
process = os.popen('stty size', 'r')
4860
result = process.read()
4961
process.close()
50-
if result:
51-
rows, columns = result.split()
62+
terminal_height, terminal_length = result.split()
63+
if text:
5264
text_length = len(text)
53-
remaining_places = int(columns) - text_length
65+
remaining_places = int(terminal_length) - text_length
5466
if remaining_places > 0:
55-
return '=' * (remaining_places // 2 - 1) + ' ' + text + ' ' + '=' * (remaining_places // 2 - 1)
56-
else:
57-
return text
67+
return DASH * (remaining_places // 2 - 1) + ' ' + text + ' ' + DASH * (remaining_places // 2 - 1)
5868
else:
59-
return text
69+
# If there is no text, it returns a line with the length of terminal.
70+
return DASH * int(terminal_length)
6071

6172

62-
def print_console_header(text='-'):
73+
def print_console_header(text=DASH):
6374
"""
6475
Create a dynamic header based on terminal length.
6576
"""
66-
print(add_dashes(text))
77+
print(headerize(text))
6778

6879

6980
jarvis_logo = "\n" \

0 commit comments

Comments
 (0)