code: def localize(self, mode="r", perm=None, parent_perm=None, **kwargs):
          
          if mode not in ("r", "w"):
              raise Exception("unknown mode '{}', use r or w".format(mode))
  
          # get additional arguments
          skip_copy = kwargs.pop("skip_copy", False)
          is_tmp = kwargs.pop("is_tmp", mode == "w")
  
          if mode == "r":
              if is_tmp:
                  # create a temporary target
                  tmp = self.__class__(is_tmp=self.ext(n=1) or True)
  
                  # always copy
                  self.copy_to_local(tmp)
  
                  # yield the copy
                  try:
                      yield tmp
                  finally:
                      tmp.remove()
              else:
                  # simply yield
                  yield self
  
          else:  # write mode
              if is_tmp:
                  # create a temporary target
                  tmp = self.__class__(is_tmp=self.ext(n=1) or True)
  
                  # copy when existing
                  if not skip_copy and self.exists():
                      self.copy_to_local(tmp)
  
                  # yield the copy
                  try:
                      yield tmp
  
                      # move back again
                      if tmp.exists():
                          tmp.move_to_local(self, dir_perm=parent_perm)
                          self.chmod(perm)
                      else:
                          logger.warning("cannot move non-existing localized file target {!r}".format(
                              self))
                  finally:
                      tmp.remove()
              else:
                  # create the parent dir
                  self.parent.touch(perm=parent_perm)
  
                  # simply yield
                  yield self
  
                  if self.exists():
                      self.chmod(perm)
docs: localize(mode="r", perm=None, parent_perm=None, skip_copy=False, is_tmp=None, **kwargs)