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 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 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 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_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 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 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 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_skill_manager(): # pragma: no cover return SkillsManager(CONFIG.getpath(SKILLS_DIR), CONFIG.get(LANGUAGE), default_git_url=CONFIG.get(REPO_URL))
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()
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 INSTALLED_APPS = [ 'corsheaders', 'assistant.apps.AssistantConfig', 'rest_framework', 'django.contrib.admin',