Opener that opens single bz2 compressed file.

    :param str path: Path. 
    :param str pattern: Regular expression pattern.
    :return: Filehandle(s).
766914
def bz2_opener(path, pattern='', verbose=False):
    
    source = path if is_url(path) else os.path.abspath(path)
    filename = os.path.basename(path)

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

    try:
        filehandle = bz2.open(io.BytesIO(urlopen(path).read())) if is_url(path) else bz2.open(path)
        filehandle.read(1)
        filehandle.seek(0)
        logger.verbose('Processing file: {}'.format(source))
        yield filehandle
    except (OSError, IOError):
        raise BZ2ValidationError
845327
def _open_file(self, mode, encoding=None):
        
        if self._filename[-4:] == '.bz2':
            self._file = bz2.open(self._filename, mode=mode, encoding=encoding)
        else:
            self._file = open(self._filename, mode=mode, encoding=encoding)
183274
def open_files(self, path):
        
        paths = []
        input_list = _patt.findall(path)
        if not input_list:
            input_list = [path]

        for path in input_list:
            # Strips trailing wildcard
            if path.endswith('*'):
                path = path[:-1]

            if os.path.isdir(path):
                continue

            self.logger.debug('Opening files matched by {0}'.format(path))
            info = iohelper.get_fileinfo(path)
            ext = iohelper.get_hdu_suffix(info.numhdu)
            files = glob.glob(info.filepath)  # Expand wildcard
            paths.extend(['{0}{1}'.format(f, ext) for f in files])

        if len(paths) > 0:
            self.load_paths(paths)
            return True

        return False
551263
def smart_open_read(path=None, mode='rb', encoding=None, try_gzip=False):
    
    assert mode in ('r', 'rb')
    assert path is None or isinstance(path, (str, _oldstr))
    assert isinstance(mode, (str, _oldstr))
    assert encoding is None or isinstance(encoding, (str, _oldstr))
    assert isinstance(try_gzip, bool)

    fh = None
    binfh = None
    gzfh = None
    if path is None:
        # open stdin
        fh = io.open(sys.stdin.fileno(), mode=mode, encoding=encoding)

    else:
        # open an actual file

        if try_gzip:
            # gzip.open defaults to mode 'rb'
            gzfh = try_open_gzip(path)

        if gzfh is not None:
            logger.debug('Opening gzip''ed file.')
            # wrap gzip stream
            binfh = io.BufferedReader(gzfh)
            if 'b' not in mode:
                # add a text wrapper on top
                logger.debug('Adding text wrapper.')
                fh = io.TextIOWrapper(binfh, encoding=encoding)

        else:
            fh = io.open(path, mode=mode, encoding=encoding)

    yield_fh = fh
    if fh is None:
        yield_fh = binfh

    try:
        yield yield_fh

    finally:
        # close all open files
        if fh is not None:
            # make sure we don't close stdin
            if fh.fileno() != sys.stdin.fileno():
                fh.close()

        if binfh is not None:
            binfh.close()

        if gzfh is not None:
            gzfh.close()
626873
def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
        
        if len(mode) > 1 or mode not in "rw":
            raise ValueError("mode must be 'r' or 'w'.")

        try:
            import bz2
        except ImportError:
            raise CompressionError("bz2 module is not available")

        if fileobj is not None:
            fileobj = _BZ2Proxy(fileobj, mode)
        else:
            fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)

        try:
            t = cls.taropen(name, mode, fileobj, **kwargs)
        except (IOError, EOFError):
            fileobj.close()
            raise ReadError("not a bzip2 file")
        t._extfileobj = False
        return t
979361
def extract_filezip(path_to_file, dest_path, target_zipfiles=None):
    

    target_zipfiles = ['.*'] if target_zipfiles is None else target_zipfiles

    files = []
    _, ext = os.path.splitext(path_to_file)

    if ext == '.zip':
        file = open(path_to_file, 'rb')
        with zipfile.ZipFile(file) as zip_file:
            regexp = '|'.join(target_zipfiles) if target_zipfiles else '.*'
            search_regex = re.compile(regexp)

            lista = [m.group() for x in zip_file.namelist()
                     for m in [search_regex.search(x)] if m]

            for zp_file in lista:
                try:
                    zip_file.extract(zp_file, dest_path)
                    files.append(os.path.join(dest_path, zp_file))
                except Exception as ex:
                    msg = 'Fail to extract {} in {} to {} - {}'.format(
                        zp_file, path_to_file, dest_path, ex)
                    logger.error(msg)
        file.close()
    else:
        logger.warning('Not zipfile passed in args')
    return files
276692
def open_filechooser(title, parent=None, patterns=None,
                     folder=None, filter=None, multiple=False,
                     _before_run=None, action=None):
    

    assert not (patterns and filter)
    if multiple:
        if action is not None and action != gtk.FILE_CHOOSER_ACTION_OPEN:
            raise ValueError('`multiple` is only valid for the action '
                             '`gtk.FILE_CHOOSER_ACTION_OPEN`.')
        action = gtk.FILE_CHOOSER_ACTION_OPEN
    else:
        assert action is not None
    filechooser = gtk.FileChooserDialog(title,
                                        parent,
                                        action,
                                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                         gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    if multiple:
        filechooser.set_select_multiple(True)

    if patterns or filter:
        if not filter:
            filter = gtk.FileFilter()
            for pattern in patterns:
                filter.add_pattern(pattern)
        filechooser.set_filter(filter)
    filechooser.set_default_response(gtk.RESPONSE_OK)

    if folder:
        filechooser.set_current_folder(folder)

    try:
        if _before_run is not None:
            _before_run(filechooser)
        response = filechooser.run()
        if response not in (gtk.RESPONSE_OK, gtk.RESPONSE_NONE):
            return

        if multiple:
            return filechooser.get_filenames()
        else:
            return filechooser.get_filename()
    finally:
        _destroy(filechooser)
552785
def get_file(self,pattern):
        '''
         
        '''
        flag=[fnmatch.fnmatch(l,pattern) for l in self.filelist]
        id=self.fid_list.compress(flag)
        flag = self.fileid == id
        return flag
484077
def generate_file_template_load(path):
        
        path = os.path.expanduser(os.path.abspath(
                path
        ))

        def read_file():
            """
            Read file from path and return file contents
            :return:
            """
            with open(path) as template:
                return template.read()

        return read_file
131710
def _OpenFileObject(self, path_spec):
    
    if not path_spec.HasParent():
      raise errors.PathSpecError(
          'Unsupported path specification without parent.')

    parent_path_spec = path_spec.parent

    file_system = resolver.Resolver.OpenFileSystem(
        parent_path_spec, resolver_context=self._resolver_context)

    # Note that we cannot use pysmraw's glob function since it does not
    # handle the file system abstraction dfvfs provides.
    segment_file_path_specs = raw.RawGlobPathSpec(file_system, path_spec)
    if not segment_file_path_specs:
      return None

    if parent_path_spec.IsSystemLevel():
      # Typically the file-like object cache should have room for 127 items.
      self._resolver_context.SetMaximumNumberOfFileObjects(
          len(segment_file_path_specs) + 127)

    file_objects = []
    for segment_file_path_spec in segment_file_path_specs:
      file_object = resolver.Resolver.OpenFileObject(
          segment_file_path_spec, resolver_context=self._resolver_context)
      file_objects.append(file_object)

    raw_handle = pysmraw.handle()
    raw_handle.open_file_objects(file_objects)
    return raw_handle
906085
def open_zip(path, *args, **kwargs):
  """"""
  with contextlib.closing(PermPreservingZipFile(path, *args, **kwargs)) as zip:
    yield zip
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_
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
47018
def super_glob(pattern):
    ''
    pieces = pattern.split('/')
    if '**' in pieces:
        prefix = '/'.join(pieces[:pieces.index('**')])
        postfix = '/'.join(pieces[pieces.index('**') + 1:])
        roots = [dirname
                 for dirname, dirnames, filenames in os.walk(prefix)]
        patterns = [root + '/' + postfix for root in roots]
    else:
        patterns = ['/'.join(pieces)]
    return chain.from_iterable(glob(pattern) for pattern in patterns)
861056
def open_filezip(file_path, find_str):
    
    if zipfile.is_zipfile(file_path):
        zipf = zipfile.ZipFile(file_path)
        interesting_files = [f for f in zipf.infolist() if find_str in f]

        for inside_file in interesting_files:
            yield zipf.open(inside_file)
1204080
def open(self):
        ''''''
        options = copy(self.__options)

        # Get scheme and format if not already given
        compression = None
        if self.__scheme is None or self.__format is None:
            detected_scheme, detected_format = helpers.detect_scheme_and_format(self.__source)
            scheme = self.__scheme or detected_scheme
            format = self.__format or detected_format
            # Get compression
            for type in config.SUPPORTED_COMPRESSION:
                if self.__compression == type or detected_format == type:
                    compression = type
        else:
            scheme = self.__scheme
            format = self.__format

        # Initiate loader
        self.__loader = None
        if scheme is not None:
            loader_class = self.__custom_loaders.get(scheme)
            if loader_class is None:
                if scheme not in config.LOADERS:
                    message = 'Scheme "%s" is not supported' % scheme
                    raise exceptions.SchemeError(message)
                loader_path = config.LOADERS[scheme]
                if loader_path:
                    loader_class = helpers.import_attribute(loader_path)
            if loader_class is not None:
                loader_options = helpers.extract_options(options, loader_class.options)
                if compression and 'http_stream' in loader_class.options:
                    loader_options['http_stream'] = False
                self.__loader = loader_class(
                    bytes_sample_size=self.__bytes_sample_size,
                    **loader_options)

        # Zip compression
        if compression == 'zip' and six.PY3:
            source = self.__loader.load(self.__source, mode='b')
            with zipfile.ZipFile(source) as archive:
                name = archive.namelist()[0]
                if 'filename' in options.keys():
                    name = options['filename']
                    del options['filename']
                with archive.open(name) as file:
                    source = tempfile.NamedTemporaryFile(suffix='.' + name)
                    for line in file:
                        source.write(line)
                    source.seek(0)
            self.__source = source
            self.__loader = StreamLoader(bytes_sample_size=self.__bytes_sample_size)
            format = self.__format or helpers.detect_scheme_and_format(source.name)[1]
            scheme = 'stream'

        # Gzip compression
        elif compression == 'gz' and six.PY3:
            name = self.__source.replace('.gz', '')
            self.__source = gzip.open(self.__loader.load(self.__source, mode='b'))
            self.__loader = StreamLoader(bytes_sample_size=self.__bytes_sample_size)
            format = self.__format or helpers.detect_scheme_and_format(name)[1]
            scheme = 'stream'

        # Not supported compression
        elif compression:
            message = 'Compression "%s" is not supported for your Python version'
            raise exceptions.TabulatorException(message % compression)

        # Initiate parser
        parser_class = self.__custom_parsers.get(format)
        if parser_class is None:
            if format not in config.PARSERS:
                message = 'Format "%s" is not supported' % format
                raise exceptions.FormatError(message)
            parser_class = helpers.import_attribute(config.PARSERS[format])
        parser_options = helpers.extract_options(options, parser_class.options)
        self.__parser = parser_class(self.__loader,
                force_parse=self.__force_parse,
                **parser_options)

        # Bad options
        if options:
            message = 'Not supported option(s) "%s" for scheme "%s" and format "%s"'
            message = message % (', '.join(options), scheme, format)
            warnings.warn(message, UserWarning)

        # Open and setup
        self.__parser.open(self.__source, encoding=self.__encoding)
        self.__extract_sample()
        self.__extract_headers()
        if not self.__allow_html:
            self.__detect_html()

        # Set scheme/format/encoding
        self.__actual_scheme = scheme
        self.__actual_format = format
        self.__actual_encoding = self.__parser.encoding

        return self
221631
def _OpenFileObject(self, path_spec):
    
    if not path_spec.HasParent():
      raise errors.PathSpecError(
          'Unsupported path specification without parent.')

    resolver.Resolver.key_chain.ExtractCredentialsFromPathSpec(path_spec)

    file_object = resolver.Resolver.OpenFileObject(
        path_spec.parent, resolver_context=self._resolver_context)
    bde_volume = pybde.volume()

    bde.BDEVolumeOpen(
        bde_volume, path_spec, file_object, resolver.Resolver.key_chain)
    return bde_volume