def download_prompt( iso_code: str, message: str, model_url: str, interactive: bool = True, silent: bool = False, ): """Ask user whether to download files. TODO: Make ft and stanza use this fn. Consider moving to other module. """ fetch_corpus = FetchCorpus(language=iso_code) if not interactive: if not silent: print(message) fetch_corpus.import_corpus(corpus_name=f"{iso_code}_models_cltk") # get_file_with_progress_bar(model_url=model_url, file_path=self.fp_zip) else: print(message) dl_is_allowed = query_yes_no( f"Do you want to download '{model_url}' to '~/cltk_data/{iso_code}'?" ) # type: bool if dl_is_allowed: fetch_corpus.import_corpus(corpus_name=f"{iso_code}_models_cltk") # get_file_with_progress_bar(model_url=model_url, file_path=self.fp_zip) else: raise CLTKException( f"Download of necessary model declined for '{iso_code}'. Following functions will likely fail." )
def _check_and_download_tlgu_source(self): """Check if tlgu downloaded, if not download it.""" path = make_cltk_path("grc/software/grc_software_tlgu/tlgu.h") if not os.path.isfile(path): dl_msg = f"This part of the CLTK depends upon TLGU, software written by Dimitri Marinakis `<http://tlgu.carmen.gr/>`_." print(dl_msg) repo_url = "https://github.com/cltk/grc_software_tlgu.git" dl_dir = os.path.split(path)[0] dl_question = ( f"Do you want to download TLGU from '{repo_url}' to '{dl_dir}'?" ) if self.interactive: do_download = query_yes_no(question=dl_question) else: do_download = True if do_download: fetch_corpus = FetchCorpus(language="grc") fetch_corpus.import_corpus(corpus_name="grc_software_tlgu") else: raise CLTKException( f"TLGU software required for this class to work.")
def __init__(self, interactive: bool = True): self.interactive = interactive self.lewis_yaml_fp = make_cltk_path( "lat", "lexicon", "cltk_lat_lewis_elementary_lexicon", "lewis.yaml") try: self.entries = self._load_entries() except FileNotFoundError: if self.interactive: dl_msg = f"This part of the CLTK depends upon Lewis's *An Elementary Latin Dictionary* (1890)." print(dl_msg) dl_question = "Do you want to download this?" do_download = query_yes_no(question=dl_question) else: do_download = True if do_download: fetch_corpus = FetchCorpus(language="lat") fetch_corpus.import_corpus( corpus_name="cltk_lat_lewis_elementary_lexicon") else: raise CLTKException( f"File '{self.lewis_yaml_fp}' is not found. It is required for this class." ) self.entries = self._load_entries()
def __init__(self, interactive: bool = True): self.interactive = interactive self.zoega_yaml_fp = make_cltk_path("non", "dictionary", "cltk_non_zoega_dictionary", "dictionary.yaml") try: self.entries = self._load_entries() except FileNotFoundError: if self.interactive: dl_msg = f"This part of the CLTK depends upon Zoëga's *A Concise Old Norse Dictionary* (1890)." print(dl_msg) dl_question = "Do you want to download this?" do_download = query_yes_no(question=dl_question) else: do_download = True if do_download: fetch_corpus = FetchCorpus(language="non") fetch_corpus.import_corpus( corpus_name="cltk_non_zoega_dictionary") else: raise CLTKException( f"File '{self.zoega_yaml_fp}' is not found. It is required for this class." ) self.entries = self._load_entries()
def test_query_no_def_invalid(self): """Test question function with I/O.""" with self.assertRaises(ValueError) as context: query_yes_no(question="Is anyone wiser than Socrates?", default="xxx")
def test_query_no_def_no(self): """Test question function with I/O.""" self.assertEqual( query_yes_no(question="Is anyone wiser than Socrates?", default="no"), False)
def test_query_yes(self): """Test question function with I/O.""" self.assertEqual( query_yes_no(question="Is anyone wiser than Socrates?"), True)
def _check_install(self): """Check if tlgu installed, if not install it.""" try: subprocess.check_output(["which", "tlgu"]) except subprocess.SubprocessError as sub_err: print("TLGU not installed.") logger.info("TLGU not installed: %s", sub_err) logger.info("Installing TLGU.") if not subprocess.check_output(["which", "gcc"]): logger.error("GCC seems not to be installed.") else: tlgu_path = make_cltk_path("grc/software/grc_software_tlgu") if self.interactive: install_question = "Do you want to install TLGU?" do_install = query_yes_no(question=install_question) if not do_install: raise CLTKException( "TLGU installation required for this class to work." ) else: print("Non-interactive installation. Continuing ...") command = "cd {0} && make install".format(tlgu_path) print(f"Going to run command: ``{command}``") try: p_out = subprocess.call(command, shell=True) except subprocess.SubprocessError as sub_err: print( "Error executing installation. Going to check output of ``subprocess.call()`` ..." ) raise CLTKException(sub_err) if p_out == 0: msg = "TLGU installed." print(msg) logger.info(msg) return True else: msg = "TLGU install without sudo failed. Going to try again with sudo (usually required for Linux) ..." print(msg) logger.error(msg) command = "cd {0} && sudo make install".format(tlgu_path) if self.interactive: install_question = "Do you want to install TLGU? with sudo?" do_install = query_yes_no(question=install_question) if not do_install: raise CLTKException( "TLGU installation required for this class to work." ) p_out = subprocess.call(command, shell=True) else: print("Going to run command:", command) p_out = subprocess.call(command, shell=True) if p_out == 0: msg = "TLGU installed." print(msg) logger.info(msg) else: msg = "TLGU install with sudo failed." print(msg) logger.error(msg) raise CLTKException( "TLGU installation required for this class to work.")