def repl(config, user_identifier, device_identifier): """Starts a client REPL to communicate with a broker instance. """ if config: CONFIG.load_from_file(config) with MQTTChannel() as mqtt: client = ReplClient(device_identifier, user_identifier, mqtt) client.cmdloop()
def test_it_should_write_args_to_global_store(self): @write_to_store() def a_method(one, two): expect(one).to.equal('one value') expect(two).to.equal('two value') expect(CONFIG.get('one')).to.be.none a_method(one='one value', two='two value') expect(CONFIG.get('one')).to.equal('one value') expect(CONFIG.get('two')).to.equal('two value')
def serve(data_dir, default): """Starts the server. """ agents_factory = FromFile(data_dir, default) # Let's load the configuration and load the skills CONFIG.load_from_file(agents_factory._default_conf_path) # pylint: disable=protected-access import_skills(CONFIG.getpath(SKILLS_DIR, os.path.join(data_dir, 'skills')), CONFIG.getbool(WATCH)) server = Server(agents_factory) with MQTTChannel() as mqtt: mqtt.attach(server) input('Press any key, anytime to stop the broker')
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 = CONFIG.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_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 = CONFIG.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() 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 get_agent_for_user(user): agent = agents_by_user.get(user.id) if not agent: logging.info('Agent does not exist for "%s", creating it' % user.id) interpreter = SnipsInterpreter(user.profile.language[:2], CONFIG.getpath(CACHE_DIR)) interpreter.fit_from_skill_data() agent = Agent(interpreter, uid=user.id) agents_by_user[user.id] = agent else: logging.info('Agent found matching this user!') user_settings = user.setting_set.all() user_settings_dict = { setting.name: setting.value for setting in user_settings } # Update agent meta with user settings agent.meta.update(user_settings_dict) return agent
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 = CONFIG.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 list_skills(): # pragma: no cover """List installed skills for this instance. """ import_skills(CONFIG.getpath(SKILLS_DIR)) metas = instantiate_skill_manager().get() for meta in metas: click.echo(meta)
def instantiate_and_fit_interpreter(training_file=None): # pragma: no cover if not training_file: import_skills(CONFIG.getpath(SKILLS_DIR), CONFIG.getbool(WATCH)) try: from pytlas.understanding.snips import SnipsInterpreter # pylint: disable=import-outside-toplevel interpreter = SnipsInterpreter(CONFIG.get(LANGUAGE), CONFIG.getpath(CACHE_DIR)) 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_write_args_to_given_store(self): s = SettingsStore() @write_to_store(store=s) def a_method(three): expect(three).to.equal('three value') expect(s.get('three')).to.be.none a_method(three='three value') expect(s.get('three')).to.equal('three value') expect(CONFIG.get('three')).to.be.none
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 = CONFIG.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 should_load_resources(language_code: str) -> bool: """Determines if resources for the given language should be loaded. It will help keep only necessary stuff and avoid allocating space for unneeded resources. Args: language_code (str): Language to check Returns: bool: True if it should be loaded, false otherwise """ langs = CONFIG.getlist(SETTING_ALLOWED_LANGUAGES) return not langs or language_code in langs
def on_enumerate_list(req): list_dir_path = CONFIG.get('path', section='pytlas_list') if not list_dir_path: list_dir_path = default_pytlas_list_path() list_names = [] for f in os.listdir(list_dir_path): file_path = os.path.join(list_dir_path, f) if os.path.isfile(file_path) and os.path.splitext( file_path)[1] == '.json': with open(file_path, 'r') as json_file: list_content = json.load(json_file) list_names.append(list_content['name']) if len(list_names) == 0: result = req._("I found no list") elif len(list_names) == 1: result = req._("I found only one list : {0}").format(list_names[0]) else: found_list = ', '.join(list_names) result = req._("I found the following lists : {0}").format(found_list) req.agent.answer(result) return req.agent.done()
def instantiate_agent_prompt(sentence=None): # pragma: no cover interpreter = instantiate_and_fit_interpreter() Prompt(Agent(interpreter, transitions_graph_path=CONFIG.getpath(GRAPH_FILE)), parse_message=sentence).cmdloop()
def test_it_should_allow_load_resources(self): CONFIG.set(SETTING_ALLOWED_LANGUAGES, ['en', 'it']) expect(should_load_resources('en')).to.be.true expect(should_load_resources('it')).to.be.true expect(should_load_resources('fr')).to.be.false
def test_it_should_allow_load_resources_when_empty(self): CONFIG.set(SETTING_ALLOWED_LANGUAGES, []) expect(should_load_resources('fr')).to.be.true
def setup(self): CONFIG.load_from_file( os.path.join(os.path.dirname(__file__), '../../__settings/default/pytlas.ini'))
def teardown(self): CONFIG.reset()
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 = CONFIG.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 = CONFIG.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 = CONFIG.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 = CONFIG.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 = CONFIG.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()
def ready(self): install_logs(CONFIG.getbool(VERBOSE), CONFIG.getbool(DEBUG, section='web')) import_skills(CONFIG.getpath(SKILLS_DIR))
https://docs.djangoproject.com/en/2.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/settings/ """ import os from datetime import timedelta from pytlas.cli import DEBUG from pytlas.settings import CONFIG # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Read the configuration file CONFIG.load_from_file(os.path.join(BASE_DIR, 'pytlas.ini')) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = CONFIG.get('secret', section='web') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = CONFIG.getbool(DEBUG, section='web') # Must be set to an empty string to turn it off ALLOWED_HOSTS = [CONFIG.get('allowed_host', '*', section='web')] CORS_ORIGIN_ALLOW_ALL = True # Since the API is exposed to anyone # Application definition
def instantiate_skill_manager(): # pragma: no cover return SkillsManager(CONFIG.getpath(SKILLS_DIR), CONFIG.get(LANGUAGE), default_git_url=CONFIG.get(REPO_URL))
def teardown(self): # Restore the global loading settings CONFIG.set(SETTING_ALLOWED_LANGUAGES, [])
def main(config_file, skills_dir, language, repo_url, **kwargs): # pragma: no cover """An open-source 🤖 assistant library built for people and made to be super easy to setup and understand. """ if os.path.isfile(config_file): CONFIG.load_from_file(config_file) # Sets default settings value if not given in args or config file CONFIG.set(LANGUAGE, language or CONFIG.get(LANGUAGE, 'en')) CONFIG.set(SKILLS_DIR, skills_dir or CONFIG.get(SKILLS_DIR, os.getcwd())) CONFIG.set(REPO_URL, repo_url or CONFIG.get(REPO_URL, 'https://github.com/')) install_logs(CONFIG.get(VERBOSE), CONFIG.get(DEBUG))
def setup_module(module): global agent remove_test_tmp_folder() test_tmp_folder_path = get_test_tmp_folder_path() CONFIG.set('path',test_tmp_folder_path,section='pytlas_list') agent = create_skill_agent(os.path.dirname(__file__), lang='en')