def integration(self): # get Kara's path (so Kara can be called from anywhere) absPath = os.path.abspath(os.path.dirname(__file__)) print(yellow('\n[!] Creating Default Paths...')) # make default paths try: # abilities os.mkdir('Abilities') except FileExistsError: # error message print(red('[!] Abilities Path Already Exists!')) try: # cache os.mkdir('Cache') print(yellow('[!] Creating Cache...')) # write empty ability cache file with open('Cache/abilities.json', 'w') as file: file.write('{}') # empty json except FileExistsError: # error message print(red('[!] Cache Path Already Exists!')) print(yellow('[!] Referencing Templates...')) # main.py shutil.copyfile(absPath + '/Data/Templates/Integration/main.py', 'main.py') print(green('\n[+] Successfully Instanced New Integration!'))
def reload(self): # handle if link was made incorrectly try: importlib.reload(self.link) except AttributeError: print(red('\n[-] Cached Linking File Failed to Operate!')) sys.exit(1)
def __init__(self, abilitiesPath='Abilities/', cachePath='Cache/'): # custom paths self.abilitiesPath = abilitiesPath self.cachePath = cachePath # speech recognizer self.recognizer = sr.Recognizer() # speech microphone self.microphone = sr.Microphone() # wake words (my lisp sucks...) self.wake = ['kara', 'cara', 'kawa', 'cowra'] # init engine and voices self.engine = pyttsx3.init() self.voices = self.engine.getProperty('voices') # set properties self.engine.setProperty('rate', 125) self.engine.setProperty('volume', 1.0) self.engine.setProperty('voice', self.voices[1].id) # don't speak, just time how long abilities take to work self.debugTime = False # last command self.last = '' # import linking file try: # "Cache/" -> "Cache." cacheModule = cachePath.replace('/', '.') cacheModule = cacheModule.replace('\\', '.') # current path current = os.path.dirname(os.path.abspath(__file__)) current = current.replace('/', '.') current = current.replace('\\', '.') # remove full directory to leave relative path to Abilities cacheModule = cacheModule.replace(current, '') self.link = importlib.import_module('Kara.Core.Data.Cache.link')#cacheModule + 'link', package='Core') except: print(red('\n[-] Failed to Load Linking File!'))
def setup(path): try: # read requirements with open(path, 'r') as file: # iterate lines for line in file.readlines(): pkg = '' for char in line: # comment support if char == '#': break # correct pkg code on line pkg += char # remove excess whitespace pkg = pkg.strip() # pkg not empty if pkg: # install package subprocess.call('python -m pip install ' + pkg, shell=True) print() # empty trailing line # no access except PermissionError: print(red('\n[!] Permission Error Opening: \"' + path + '\"!'))
def compile(self, text): # no text given if not text: self.speak('Sorry, I didn\'t get that') return # read all logged abilities with open(self.cachePath + 'commands.json', 'r') as log: commands = json.load(log) # parse json # iterate abilities and their commands to find correct response for command in commands: # command match found match = False # use exact value if 'exact' in command: # given as string if isinstance(command['exact'], str): # string matches command if command['exact'] == text: match = True else: # given as list for val in command['exact']: if val == text: match = True break # keywords exist and match wasn't already found if 'keywords' in command and not match: # check command's keywords against given text for keywords in command['keywords']: # use True as default for greater efficency match = True # iterate all keywords for keyword in keywords.split(' '): # keyword doesn't match if not keyword in text.split(' '): match = False break # keywords match if match: break # check if a match was detected if match: # default new "last" as failed message last = 'failed' # target given if 'target' in command: func = command['target'] # "last" command last = 'self.link.{}(self, text)'.format(func) # pass Kara and command to command exec(last) else: print(red('\n[!] No Target Specified For Command!')) # don't log a repeat command (causes a weird infinite loop) if not '.repeat(' in last: # log command for "repeat" with open(self.cachePath + 'last.txt', 'w') as file: # write executable code for repeat function() file.write('Kara.link.{}(Kara, \"{}\")'.format(func, text)) return # no matches # execute fallback command with open(self.cachePath + 'fallback.txt') as last: # get fallback command fallback = last.readline() # no fallback given if not fallback: self.speak('Sorry, I don\'t know that') return # run fallback func exec(fallback)
def __init__(self, abilitiesPath='Data/Abilities/', cachePath='Data/Cache/', integrate=True): # custom paths self.abilitiesPath = abilitiesPath self.cachePath = cachePath # speech recognizer self.recognizer = sr.Recognizer() # speech microphone self.microphone = sr.Microphone() # wake words (my lisp sucks...) self.wake = ['kara', 'cara', 'kawa', 'cowra'] # all acceptable responses self.responses = {'yes': ['yes', 'yep', 'yeah', 'ya'], 'no': ['no', 'nah', 'nope']} # manual mode self.manual = False # integration mode self.integrate = integrate # init engine and voices try: self.engine = pyttsx3.init() except ModuleNotFoundError: print(red('\n[-] Dependency Not Found!')) print(yellow('[!] Install \"pywin32\" to Continue...')) sys.exit(0) self.voices = self.engine.getProperty('voices') # set properties self.engine.setProperty('rate', 125) self.engine.setProperty('volume', 1.0) self.engine.setProperty('voice', self.voices[1].id) # don't speak, just time how long abilities take to work self.debugTime = False self.linkPath = '' # import linking file # run in integration mode if integrate: # "Cache/" -> "Cache." cacheModule = cachePath.replace('/', '.') cacheModule = cacheModule.replace('\\', '.') # remove trainling dot if cacheModule[-1] == '.': cacheModule = cacheModule[:-1] # set path self.linkPath = cacheModule + '.link' # open module cache else: # set path self.linkPath = 'Kara.Core.Data.Cache.link' # attempt import try: self.link = importlib.import_module(self.linkPath) except: print(red('\n[-] Failed to Load Linking File!'))