def on_forecast(req): appid = settings.get('appid', section='weather', additional_lookup=req.agent.meta) units = settings.get('units', default='metric', section='weather', additional_lookup=req.agent.meta) if not appid: req.agent.answer(req._('You must provide an WEATHER_APPID')) return req.agent.done() city = req.intent.slot('location').first().value date_slot = req.intent.slot('date').first() date = date_slot.value or datetime.utcnow() if not city: return req.agent.ask('location', req._('For where?')) forecasts = fetch_forecasts_for(city, date, date_slot.meta.get('grain'), appid, req.lang, units) if len(forecasts) > 0: req.agent.answer(req._('Here what I found for %s!') % city, cards=[ create_forecast_card(req, date, data, units) for date, data in forecasts.items() ]) else: req.agent.answer(req._('No results found for %s') % city) return req.agent.done()
def on_help_list(req): list_dir_path = settings.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() from_email = settings.get('from_email', section='pytlas_list') smtp_address = settings.get('smtp_address', section='pytlas_list') smtp_login = settings.get('smtp_login', section='pytlas_list') smtp_pwd = settings.get('smtp_pwd', section='pytlas_list') req.agent.answer( req._(help).format(list_dir_path, from_email, smtp_address, smtp_login, smtp_pwd)) return req.agent.done()
def test_it_should_returns_the_one_in_additional_lookup_first(self): d = { 'PYTLAS_A_KEY': 'an additional value', } expect(get('a key', default='a value', additional_lookup=d)).to.equal('an additional value')
def on_display_list(req): list_name = req.intent.slot('list_name').first().value if not list_name: return req.agent.ask('list_name', req._('Which list?')) list_dir_path = settings.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() list_path = build_list_file_path(list_name, list_dir_path) print(list_path) if not os.path.exists(list_path): req.agent.answer( req._('Hummm! The list "{0}" seems not exists.').format(list_name)) return req.agent.done() list_content = {} try: with open(list_path, 'r') as json_file: list_content = json.load(json_file) except: req.agent.answer( req._('Oops! Something bad append. I can\'t open the file "{0}"'). format(list_path)) return req.agent.done() req.agent.answer( req._('Your list "{0}" contains : {1}').format( list_content['name'], ','.join(list_content['items']))) return req.agent.done()
def on_remove_item(req): list_name = req.intent.slot('list_name').first().value if not list_name: return req.agent.ask('list_name', req._('From which list?')) list_dir_path = settings.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() list_path = build_list_file_path(list_name, list_dir_path) if not os.path.exists(list_path): req.agent.answer( req._('Hummm! The list "{0}" seems not exists').format(list_name)) return req.agent.done() item_name = req.intent.slot('item_name').first().value if not item_name: return req.agent.ask('item_name', req._('What do you want remove from your list?')) try: remove_from_file(item_name, list_path) except: req.agent.answer( req. _('Oops! Something bad append. I can\'t remove "{0}" from file "{1}"' ).format(item_name, list_path)) return req.agent.done() req.agent.answer( req._('Ok, "{0}" has been removed from your list "{1}"').format( item_name, list_name)) return req.agent.done()
def on_delete_list(req): list_name = req.intent.slot('list_name').first().value if not list_name: return req.agent.ask('list_name', req._('In which list?')) list_dir_path = settings.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() list_path = build_list_file_path(list_name, list_dir_path) if not os.path.exists(list_path): req.agent.answer( req._('Hummm! The list "{0}" seems not exists').format(list_name)) return req.agent.done() delete_confirmed = req.intent.slot('delete_confirmed').first().value yes = req._('Yes') no = req._('No') if not delete_confirmed: return req.agent.ask( 'delete_confirmed', req._('Would you like delete "{0}"?').format(list_name), [yes, no]) if delete_confirmed == no: return req.agent.done() try: os.remove(list_path) except: req.agent.answer( req._('Oops! Something bad append. I can\'t delete file "{0}"'). format(list_path)) return req.agent.done()
def on_create_list(req): list_name = req.intent.slot('list_name').first().value if not list_name: return req.agent.ask('list_name', req._('Please choose a name for your list.')) list_dir_path = settings.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() if not os.path.exists(list_dir_path): os.makedirs(list_dir_path) list_path = build_list_file_path(list_name, list_dir_path) if os.path.exists(list_path): req.agent.answer(req._('Oops! This list already exists.')) return req.agent.done() try: create_blank_file(list_name, list_path) except: req.agent.answer( req._('Oops! Something bad append. I can\'t create file "{0}"'). format(list_path)) return req.agent.done() req.agent.answer( req._('Ok! your list "{0}" has been created.').format(list_name)) return req.agent.done()
def install_skills(directory, stdout=None, *names): """Install or update given skills. Args: directory (str): Skills directory stdout (func): Function to call to output something names (list of str): list of skills to install Returns: list of str: List of installed or updated skills """ installed_skills = [] for name in names: url = name owner, repo = skill_parts_from_name(name) if stdout: # pragma: no cover stdout('Processing %s/%s' % (owner, repo)) dest = os.path.abspath(os.path.join(directory, to_skill_folder(owner, repo))) if name.startswith(owner): url = settings.get(settings.SETTING_DEFAULT_REPO_URL) + name logging.info('Will download skill from "%s" to "%s"' % (url, dest)) if os.path.isdir(dest): if stdout: # pragma: no cover stdout(' Skill folder already exists, updating') installed_skills.extend(update_skills(directory, stdout, name)) continue try: if stdout: # pragma: no cover stdout(' Cloning skill repository') subprocess.check_output(['git', 'clone', url, dest], stderr=subprocess.STDOUT) logging.info('Successfully cloned skill "%s"' % repo) install_dependencies_if_needed(dest, stdout) if stdout: # pragma: no cover stdout(' ✔️ Installed') logging.info('Successfully installed "%s"' % repo) installed_skills.append(name) except subprocess.CalledProcessError as e: if stdout: # pragma: no cover stdout(' ❌ Failed') logging.error("Could not clone the skill repo, make sure you didn't mispelled it and you have sufficient rights to clone it. \"%s\"" % e) return installed_skills
def test_it_should_retrieve_the_env_var_which_takes_precedence_if_any( self): set_setting('a_key', 'a value', section='envs') expect(get('a_key', section='envs')).to.equal('a value') os.environ['ENVS_A_KEY'] = 'an env value' expect(get('a_key', section='envs')).to.equal('an env value') expect( get('a_key', section='envs', additional_lookup={ 'ENVS_A_KEY': 'an added env value', })).to.equal('an added env value')
def on_add_item(req): list_name = req.intent.slot('list_name').first().value if not list_name: return req.agent.ask('list_name', req._('In which list?')) list_dir_path = settings.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() list_path = build_list_file_path(list_name, list_dir_path) if not os.path.exists(list_path): create_confirmed = req.intent.slot('create_confirmed').first().value yes = req._('Yes') no = req._('No') if not create_confirmed: return req.agent.ask( 'create_confirmed', req. _('Hummm! The list "{0}" seems not exists. Would you like create it?' ).format(list_name), [yes, no]) if create_confirmed == no: return req.agent.done() try: create_blank_file(list_name, list_path) except: req.agent.answer( req._('Oops! Something bad append. I can\'t create file "{0}"' ).format(list_path)) return req.agent.done() item_name = req.intent.slot('item_name').first().value if not item_name: return req.agent.ask('item_name', req._('What do you want to add in your list?')) try: append_in_file(item_name, list_path) except: req.agent.answer( req._( 'Oops! Something bad append. I can\'t add "{0}" in file "{1}"' ).format(item_name, list_path)) return req.agent.done() req.agent.answer( req._('Ok, "{0}" has been added in your list "{1}"').format( item_name, list_name)) return req.agent.done()
def instantiate_and_fit_interpreter(): # pragma: no cover import_skills(settings.getpath(settings.SETTING_SKILLS), settings.getbool(settings.SETTING_WATCH)) try: from pytlas.interpreters.snips import SnipsInterpreter interpreter = SnipsInterpreter( settings.get(settings.SETTING_LANG), settings.getpath(settings.SETTING_CACHE)) training_file = settings.getpath(settings.SETTING_TRAINING_FILE) if training_file: interpreter.fit_from_file(training_file) else: interpreter.fit_from_skill_data() return interpreter except ImportError: logging.critical( 'Could not import the "snips" interpreter, is "snips-nlu" installed?' )
def test_it_should_returns_the_default_if_not_found(self): expect(get('a key', default='a value')).to.equal('a value')
def test_it_should_retrieve_a_string_value(self): set_setting('key', 'value', section='strings') expect(get('key', section='strings')).to.equal('value')
def test_it_should_load_settings_from_a_path(self): expect(get('some_key', section='some_section')).to.be.none load(os.path.join(os.path.dirname(__file__), 'test.conf')) expect(get('some_key', section='some_section')).to.equal('some_value')
def on_send_list(req): list_name = req.intent.slot('list_name').first().value if not list_name: return req.agent.ask('list_name', req._('Which list?')) list_dir_path = settings.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() list_path = build_list_file_path(list_name, list_dir_path) if not os.path.exists(list_path): req.agent.answer( req._('Hummm! The list "{0}" seems not exists.').format(list_name)) return req.agent.done() from_email = req.intent.slot('from_email').first().value if not from_email: from_email = settings.get('from_email', section='pytlas_list') if not from_email: return req.agent.ask('from_email', req._('Please tell me from which email address')) to_email = req.intent.slot('to_email').first().value if not to_email: return req.agent.ask('to_email', req._('Please tell to which email address')) if to_email == "me": to_email = from_email smtp_address = req.intent.slot('smtp_address').first().value if not smtp_address: smtp_address = settings.get('smtp_address', section='pytlas_list') if not smtp_address: return req.agent.ask('smtp_address', req._('Please give me the smtp server address')) smtp_login = req.intent.slot('smtp_login').first().value if not smtp_login: smtp_login = settings.get('smtp_login', section='pytlas_list') if not smtp_login: return req.agent.ask('smtp_login', req._('Please give me the smtp server login')) smtp_pwd = req.intent.slot('smtp_pwd').first().value if not smtp_pwd: smtp_pwd = settings.get('smtp_pwd', section='pytlas_list') if not smtp_pwd: return req.agent.ask('smtp_pwd', req._('Please give me the smtp server pwd')) list_content = {} try: with open(list_path, 'r') as json_file: list_content = json.load(json_file) except: req.agent.answer( req._('Oops! Something bad append. I can\'t open the file "{0}"'). format(list_path)) return req.agent.done() msg = "\n" msg = msg + list_content['name'] + "\n" msg = msg + "=" * len(list_content['name']) + "\n" for item in list_content['items']: msg = msg + "-[ ]" + item + "\n" # print('sending to {0} from {1} on {2} using credential {3} {4} of \n{5}'.format(to_email, from_email, smtp_address, smtp_login, smtp_pwd, msg)) try: server = smtplib.SMTP(smtp_address + ":587") server.ehlo() server.starttls() #Next, log in to the server server.login(smtp_login, smtp_pwd) #Send the mail server.sendmail(from_email, to_email, msg) server.quit() except: req.agent.answer( req._('Hummm! Email sending failed. Cause : {0}.').format( sys.exc_info()[0])) return req.agent.done() req.agent.answer(req._('Email successfully sent.')) return req.agent.done()