<commit_before>#
# iohelper.py -- misc routines used in manipulating files, paths and urls.
#
# <NAME> (<EMAIL>)
#
# Copyright (c) <NAME>.  All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import os
import re

from ginga.misc import Bunch
from ginga.util.six.moves import urllib_parse


def get_fileinfo(filespec, cache_dir='/tmp', download=False):
    """
    Parse a file specification and return information about it.
    """
    # Loads first science extension by default.
    # This prevents [None] to be loaded instead.
    idx = ('SCI', 1)
    name_ext = '[SCI,1]'

    # User specified an HDU using bracket notation at end of path?

    # This matches
    #   image.fits[("NAME", 1)]
    #   image.fits[('NAME', 1)]
    #   image.fits['NAME', 1]
    #   image.fits["NAME", 1]
    #   image.fits[NAME, 1]
    #   image.fits[(NAME, 1)]
    match = re.match(r'^(.+)\[\(?[\'\"]?([A-Za-z]+)[\'\"]?\,([0-9])\)?\]$',
                     filespec)
    if match:
        filespec = match.group(1)
        hduname = match.group(2)
        extver = int(match.group(3))
        idx = (hduname, extver)
        name_ext = "[%s,%d]" % idx

    else:
        # This matches
        #   image.fits[1]
        #   image.fits["NAME"]
        #   image.fits['NAME']
        #   image.fits[NAME]
        match = re.match(r'^(.+)\[\(?[\'\"]?([A-Za-z0-9]+)[\'\"]?\)?\]$',
                         filespec)
        if match:
            filespec = match.group(1)
            idx = match.group(2)
            if re.match(r'^\d+$', idx):
                idx = int(idx)
                name_ext = "[%d]" % idx
            else:
                idx = idx.strip()
                name_ext = "[%s]" % idx
        else:
            filespec = filespec

    url = filespec
    filepath = None

    # Does this look like a URL?
    match = re.match(r"^(\w+)://(.+)$", filespec)
    if match:
        urlinfo = urllib_parse.urlparse(filespec)
        if urlinfo.scheme == 'file':
            # local file
            filepath = urlinfo.path
            match = re.match(r"^/(\w+\:)", filepath)
            if match:
                # This is a windows path with a drive letter?
                # strip the leading slash
                # NOTE: this seems like it should not be necessary and might
                # break some cases
                filepath = filepath[1:]

        else:
            path, filename = os.path.split(urlinfo.path)
            filepath = os.path.join(cache_dir, filename)

    else:<commit_msg>Removed fancy regex for extension parsing<commit_after>#
# iohelper.py -- misc routines used in manipulating files, paths and urls.
#
# <NAME> (<EMAIL>)
#
# Copyright (c) <NAME>.  All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import os
import re

from ginga.misc import Bunch
from ginga.util.six.moves import urllib_parse


def get_fileinfo(filespec, cache_dir='/tmp', download=False):
    """
    Parse a file specification and return information about it.
    """
    # Loads first science extension by default.
    # This prevents [None] to be loaded instead.
    idx = ('SCI', 1)
    name_ext = '[SCI,1]'

    # User specified an HDU using bracket notation at end of path?
    match = re.match(r'^(.+)\[(.+)\]$', filespec)
    if match:
        filespec = match.group(1)
        idx = match.group(2)
        if ',' in idx:
            hduname, extver = idx.split(',')
            hduname = hduname.strip()
            extver = int(extver)
            idx = (hduname, extver)
            name_ext = "[%s,%d]" % idx
        else:
            if re.match(r'^\d+$', idx):
                idx = int(idx)
                name_ext = "[%d]" % idx
            else:
                idx = idx.strip()
                name_ext = "[%s]" % idx
    else:
        filespec = filespec

    url = filespec
    filepath = None

    # Does this look like a URL?
    match = re.match(r"^(\w+)://(.+)$", filespec)
    if match:
        urlinfo = urllib_parse.urlparse(filespec)
        if urlinfo.scheme == 'file':
            # local file
            filepath = urlinfo.path
            match = re.match(r"^/(\w+\:)", filepath)
            if match:
                # This is a windows path with a drive letter?
                # strip the leading slash
                # NOTE: this seems like it should not be necessary and might
                # break some cases
                filepath = filepath[1:]

        else:
            path, filename = os.path.split(urlinfo.path)
            filepath = os.path.join(cache_dir, filename)

    else:
{'fraction_non_alphanumeric': 0.1360732538330494, 'fraction_numerical': 0.005110732538330494, 'mean_word_length': 1.8025059665871122, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1320660', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1438, 'n_words': 421}