def __init__(self, args): folder = abspath(expanduser(args.skill_folder)) self.entry = SkillEntry.from_folder(folder) skills_dir = abspath(expanduser(self.msm.skills_dir)) if join(skills_dir, basename(folder)) != folder: raise MskException('Skill folder, {}, not directly within skills directory, {}.'.format( args.skill_folder, self.msm.skills_dir ))
def perform(self): if not isdir(self.folder): raise MskException('Skill folder at {} does not exist'.format(self.folder)) if not isfile(join(self.folder, '__init__.py')): if not ask_yes_no("Folder doesn't appear to be a skill. Continue? (y/N)", False): return makedirs(join(self.folder, 'test', 'intent'), exist_ok=True) creator = TestCreator(self.folder) test_case = creator.adapt_creator.test_case or creator.padatious_creator.test_case intent_test_file = self.find_intent_test_file(creator.intent_name) with open(intent_test_file, 'w') as f: json.dump(test_case, f, indent=4, sort_keys=True) print('Generated test file:', intent_test_file)
def ask_choice(message: str, choices: list, allow_empty=False, on_empty=None) -> Optional[str]: if not choices: if allow_empty: print(on_empty) return None else: raise MskException(on_empty or 'Error with "{}"'.format(message)) print() print(message) print('\n'.join('{}. {}'.format(i + 1, choice) for i, choice in enumerate(choices))) print() def find_match(x): if not x and allow_empty: return ... try: return choices[int(x) - 1] except (ValueError, IndexError): pass def calc_conf(y): return SequenceMatcher(a=x, b=y).ratio() best_choice = max(choices, key=calc_conf) best_conf = calc_conf(best_choice) if best_conf > 0.8: return best_choice raise ValueError resp = find_match( ask_input('>', find_match, 'Please enter one of the options.')) return None if resp is ... else resp