Opener that opens files from tar archive.

    :param str path: Path.
    :param str pattern: Regular expression pattern.
    :return: Filehandle(s).
193144
def tararchive_opener(path, pattern='', verbose=False):
    
    with tarfile.open(fileobj=io.BytesIO(urlopen(path).read())) if is_url(path) else tarfile.open(path) as tararchive:
        for tarinfo in tararchive:
            if tarinfo.isfile():
                source = os.path.join(path, tarinfo.name)

                if pattern and not re.match(pattern, tarinfo.name):
                    logger.verbose('Skipping file: {}, did not match regex pattern "{}"'.format(os.path.abspath(tarinfo.name), pattern))
                    continue

                logger.verbose('Processing file: {}'.format(source))
                filehandle = tararchive.extractfile(tarinfo)
                yield filehandle
1301514
def read_file(path, verbose=False):
    
    for filehandle in filehandles(path, verbose=verbose):
        return CTfile.load(filehandle)
201203
def archiver_for_path(path_name):
  
  if path_name.endswith('.tar.gz'):
    return TGZ
  elif path_name.endswith('.tar.bz2'):
    return TBZ2
  else:
    _, ext = os.path.splitext(path_name)
    if ext:
      ext = ext[1:]  # Trim leading '.'.
    if not ext:
      raise ValueError('Could not determine archive type of path {}'.format(path_name))
    return create_archiver(ext)
968125
def doc_stream(path):
    
    with open(path, 'r') as f:
        for line in f:
            if line.strip():
                yield line
219452
def Open(self, path, ascii_codepage='cp1252'):
    
    path_specification = self._path_resolver.ResolvePath(path)
    if path_specification is None:
      return None

    return self._OpenPathSpec(path_specification)
544466
def read_file(cls, path):
        
        with open(path, encoding="utf-8") as file:
            file_contents = file.read()
            return cls.read(file_contents)
1300850
def opener_from_zipfile(zipfile):
    

    def opener(filename):
        inner_file = zipfile.open(filename)
        if PY3:
            from io import TextIOWrapper
            return TextIOWrapper(inner_file)
        else:
            return inner_file

    return opener
844576
def read_file(path):
    
    with open(must_exist(path)) as infile:
        r = infile.read()
    return r
76821
def _parse_known_pattern(self, pattern: str) -> List[str]:
        
        if pattern.endswith(os.path.sep):
            patterns = [
                filename
                for filename in os.listdir(pattern)
                if os.path.isdir(os.path.join(pattern, filename))
            ]
        else:
            patterns = [pattern]

        return patterns
1184472
def _open_archive(path):
    

    if path.endswith('.zip'):
        return zipfile.ZipFile(path, 'r')
    return tarfile.open(path, 'r')
429532
def extract_files_from(patterns) # rubocop:disable MethodLength
      files = []

      patterns.each do |pattern|
        if File.file?(pattern)
          files << pattern
        else
          begin
            ::Find.find(pattern) do |file|
              files << file if haml_file?(file)
            end
          rescue ::Errno::ENOENT
            # File didn't exist; it might be a file glob pattern
            matches = ::Dir.glob(pattern)
            if matches.any?
              files += matches
            else
              # One of the paths specified does not exist; raise a more
              # descriptive exception so we know which one
              raise HamlLint::Exceptions::InvalidFilePath,
                    "File path '#{pattern}' does not exist"
            end
          end
        end
      end

      files.uniq.sort.map { |file| normalize_path(file) }
    end
1114848
def directory_opener(path, pattern='', verbose=False):
    
    if not os.path.isdir(path):
        raise NotADirectoryError
    else:
        openers_list = [opener for opener in openers if not opener.__name__.startswith('directory')]  # remove directory

        for root, dirlist, filelist in os.walk(path):
            for filename in filelist:

                if pattern and not re.match(pattern, filename):
                    logger.verbose('Skipping file: {}, did not match regex pattern "{}"'.format(os.path.abspath(filename), pattern))
                    continue

                filename_path = os.path.abspath(os.path.join(root, filename))
                for filehandle in filehandles(filename_path, openers_list=openers_list, pattern=pattern, verbose=verbose):
                    yield filehandle
1321859
def open_resource(self, path: FilePath, mode: str='rb') -> IO[AnyStr]:
        
        if mode not in {'r', 'rb'}:
            raise ValueError('Files can only be opened for reading')
        return open(self.root_path / file_path_to_path(path), mode)
379447
def inspect_file(path):
    
    with open(path, 'rb') as f:
        labels, count = inspect(tx.decode(line) for line in f)
    return labels, count
215593
def read_file(*path):
    
    with open(os.path.join(*path)) as f:
        return f.read().strip()
259194
def get_opener(self, path):
        
        self.path = path
        opener = None
        # FIXME this parser/matcher should use `urlparse` stdlib
        for opener_name, signature in self.openers.items():
            if self.path.startswith(signature[0]):
                opener = signature[1](self.path)
                break
        if not opener:
            opener = FileOpener(self.path)
        return opener
746365
def _open(file_, mode='r'):
  """"""
  if isinstance(file_, string_types):
    _, ext = path.splitext(file_)
    if ext in {'.bz2', '.gz'}:
      s = tarfile.open(file_)
      return s.extractfile(s.next())
    else:
      return open(file_, mode)
  return file_