class_docstr: Import CLTK corpora.
class_docstr_tok: ["Import","CLTK","corpora","."]
docstr: Download a remote or load local corpus into dir ``~/cltk_data``.
  
  TODO: maybe add ``from git import RemoteProgress``
  TODO: refactor this, it's getting kinda long
  
  :param corpus_name: The name of an available corpus.
  :param local_path: A filepath, required when importing local corpora.
  :param branch: What Git branch to clone.
docstr_tok: ["Download","a","remote","or","load","local","corpus","into","dir","``","~\/cltk_data","``",".","TODO",":","maybe","add","``","from","git","import","RemoteProgress","``","TODO",":","refactor","this",",","it","'s","getting","kinda","long",":","param","corpus_name",":","The","name","of","an","available","corpus",".",":","param","local_path",":","A","filepath",",","required","when","importing","local","corpora",".",":","param","branch",":","What","Git","branch","to","clone","."]
code: def import_corpus(
      self,
      corpus_name: str,
      local_path: str = None,
      branch: str = "master",
  ):
      """Download a remote or load local corpus into dir ``~/cltk_data``.
  
      TODO: maybe add ``from git import RemoteProgress``
      TODO: refactor this, it's getting kinda long
  
      :param corpus_name: The name of an available corpus.
      :param local_path: A filepath, required when importing local corpora.
      :param branch: What Git branch to clone.
      """
  
      matching_corpus_list = [
          _dict
          for _dict in self.all_corpora_for_lang
          if _dict["name"] == corpus_name
      ]
      if not matching_corpus_list:
          raise CorpusImportError(
              f"No corpus ``{corpus_name}`` for language ``{self.language}``."
          )
      if len(matching_corpus_list) > 1:
          raise CorpusImportError(
              f"Found more than one corpus with the name ``{corpus_name}``."
          )
      matching_corpus = matching_corpus_list[0]
      if matching_corpus.get("user_defined"):
          """{'origin': 'https://github.com/kylepjohnson/latin_corpus_newton_example.git',
          'type': 'text',
          'name': 'example_distributed_latin_corpus',
          'user_defined': True}
          """
          self._git_user_defined_corpus(
              matching_corpus["name"],
              matching_corpus["type"],
              matching_corpus["origin"],
          )
          return
      elif matching_corpus.get("location") == "local":
          # {'location': 'local', 'name': 'phi5', 'origin': None, 'type': 'text'}
          msg = "Importing from local path: '{}'".format(local_path)
          logger.info(msg)
          if corpus_name not in ["phi5", "phi7", "tlg"]:
              raise CorpusImportError(
                  f"Unsupported local corpus ``{corpus_name}``."
              )
          if corpus_name == "phi5":
              # normalize path for checking dir
              if local_path.endswith("/"):
                  local_path = local_path[:-1]
              # check for right corpus dir
              if os.path.split(local_path)[1] != "PHI5":
                  logger.info("Directory must be named 'PHI5'.")
          if corpus_name == "phi7":
              # normalize local_path for checking dir
              if local_path.endswith("/"):
                  local_path = local_path[:-1]
              # check for right corpus dir
              if os.path.split(local_path)[1] != "PHI7":
                  logger.info("Directory must be named 'PHI7'.")
          if corpus_name == "tlg":
              # normalize path for checking dir
              if local_path.endswith("/"):
                  local_path = local_path[:-1]
              # check for right corpus dir
              if os.path.split(local_path)[1] != "TLG_E":
                  logger.info("Directory must be named 'TLG_E'.")
          # move the dir-checking commands into a function
          data_dir = os.path.expanduser(CLTK_DATA_DIR)
          originals_dir = os.path.join(data_dir, "originals")
          # check for `originals` dir; if not present mkdir
          if not os.path.isdir(originals_dir):
              os.makedirs(originals_dir)
              msg = "Wrote directory at '{}'.".format(originals_dir)
              logger.info(msg)
          tlg_originals_dir = os.path.join(
              data_dir, "originals", corpus_name
          )
          # check for `originals/<corpus_name>`; if pres, delete
          if os.path.isdir(tlg_originals_dir):
              shutil.rmtree(tlg_originals_dir)
              msg = "Removed directory at '{}'.".format(
                  tlg_originals_dir
              )
              logger.info(msg)
          # copy_dir requires that target
          if not os.path.isdir(tlg_originals_dir):
              self._copy_dir_recursive(local_path, tlg_originals_dir)
      else:
          """{'type': 'text',
          'name': 'lat_text_perseus',
          'origin': 'https://github.com/cltk/lat_text_perseus.git'},
          """
          if (
              not matching_corpus.get("type")
              and not matching_corpus.get("name")
              and not matching_corpus.get("origin")
          ):
              raise ValueError(
                  f"Malformed record for ``{corpus_name}``."
              )
          git_uri = matching_corpus["origin"]
          type_dir_rel = os.path.join(
              CLTK_DATA_DIR, self.language, matching_corpus["type"]
          )
          type_dir = os.path.expanduser(type_dir_rel)
          target_dir = os.path.join(type_dir, corpus_name)
          target_file = os.path.join(type_dir, corpus_name, "README.md")
          # check if corpus already present
          # if not, clone
          if not os.path.isfile(target_file):
              if not os.path.isdir(type_dir):
                  os.makedirs(type_dir)
              try:
                  msg = "Cloning '{}' from '{}'".format(
                      corpus_name, git_uri
                  )
                  logger.info(msg)
                  Repo.clone_from(
                      git_uri,
                      target_dir,
                      branch=branch,
                      depth=1,
                      progress=ProgressPrinter(),
                  )
              except CorpusImportError as corpus_imp_err:
                  msg = "Git clone of '{}' failed: '{}'".format(
                      git_uri, corpus_imp_err
                  )
                  logger.error(msg)
          # if corpus is present, pull latest
          else:
              try:
                  repo = Repo(target_dir)
                  assert not repo.bare  # or: assert repo.exists()
                  git_origin = repo.remotes.origin
                  msg = "Pulling latest '{}' from '{}'.".format(
                      corpus_name, git_uri
                  )
                  logger.info(msg)
                  git_origin.pull()
              except CorpusImportError as corpus_imp_err:
                  msg = "Git pull of '{}' failed: '{}'".format(
                      git_uri, corpus_imp_err
                  )
                  logger.error(msg)
  
imports: import errno
  import os
  import shutil
  import sys
  from urllib.parse import urljoin
  import yaml
  from git import RemoteProgress, Repo
  from cltk.core.cltk_logger import logger
  from cltk.core.exceptions import CorpusImportError
  from cltk.languages.utils import get_lang
  from cltk.utils.utils import CLTK_DATA_DIR
label_desc: Use image node_id 9 for calling the FetchCorpus obj's underlying member method code with example usage: obj.import_corpus(corpus_name, local_path, branch) without return types