Takes an elixir style file name and decodes it's content.

    Values returned as a dictionary.  Elixir filenames have the format
    RUNID.TYPE.FILTER/EXPTIME.CHIPID.VERSION.fits
338214
def elixir_decode(elixir_filename):
    
    import re, pyfits
    
    parts_RE=re.compile(r'([^\.\s]+)')
    dataset_name = parts_RE.findall(elixir_filename)

    ### check that this was a valid elixir_filename
    if not dataset_name or len(dataset_name)<5 :
        raise ValueError('String %s does not parse as elixir filename'
                         % elixir_filename )
        
    comments={'exptime': 'Integration time (seconds)',
              'filter': 'Name of filter in position  ',
              'crunid': 'CFHT Q RunID',
              'obstype': 'Observation or Exposure type',
              'imageid': 'CCD chip number',
              'filename': 'file name at creation of this MEF file'
              }
        
    keywords={}
    keywords['filename']=elixir_filename
    keywords['runid']=dataset_name[0]
    keywords['obstype']=dataset_name[1]
    keywords['exptime']=None
    keywords['filter']=None

    ### if the third part of the name is all numbers we assume exposure time
    if re.match(r'\d+',dataset_name[2]):
        keyword['exptime']=int(dataset_name[2])
    else:
        keyword['filter']=dataset_name[2]
    
    keywords['imageid']=dataset_name[3]
    keywords['version']=dataset_name[4]

    header=pyfits.Header()
    for keyword in keywords.keys():
        if keywords[keyword]:
            header.update(keyword,keywords[keyword],comment=comment[keyword])

    return header
382597
def idx(self):
        
        filename = self.filename
        if not filename:
            return None
        name_part = posixpath.splitext(filename)[0]  # filename w/ext removed
        match = self._filename_re.match(name_part)
        if match is None:
            return None
        if match.group(2):
            return int(match.group(2))
        return None
905890
def readFromFile(self, filename):
        '''
        
        '''
        s = dict(np.load(filename))
        try:
            self.coeffs = s['coeffs'][()]
        except KeyError:
            #LEGENCY - remove
            self.coeffs = s
        try:
            self.opts = s['opts'][()]
        except KeyError:
            pass
        return self.coeffs
192950
def format_spec_to_regex(field_name, format_spec):
        """"""
        # NOTE: remove escaped backslashes so regex matches
        regex_match = fmt_spec_regex.match(format_spec.replace('\\', ''))
        if regex_match is None:
            raise ValueError("Invalid format specification: '{}'".format(format_spec))
        regex_dict = regex_match.groupdict()
        fill = regex_dict['fill']
        ftype = regex_dict['type']
        width = regex_dict['width']
        align = regex_dict['align']
        # NOTE: does not properly handle `=` alignment
        if fill is None:
            if width is not None and width[0] == '0':
                fill = '0'
            elif ftype in ['s', 'd']:
                fill = ' '

        char_type = spec_regexes[ftype]
        if ftype == 's' and align and align.endswith('='):
            raise ValueError("Invalid format specification: '{}'".format(format_spec))
        final_regex = char_type
        if ftype in allow_multiple and (not width or width == '0'):
            final_regex += r'*'
        elif width and width != '0':
            if not fill:
                # we know we have exactly this many characters
                final_regex += r'{{{}}}'.format(int(width))
            elif fill:
                # we don't know how many fill characters we have compared to
                # field characters so just match all characters and sort it out
                # later during type conversion.
                final_regex = r'.{{{}}}'.format(int(width))
            elif ftype in allow_multiple:
                final_regex += r'*'

        return r'(?P<{}>{})'.format(field_name, final_regex)
154779
def _decode_fname(self):
        """"""
        self.fname = self.fname[:self.fname.find('\0')]
        self.fname = self.fname.replace('\\', '/')
723644
def decode(self, ids):
    """"""
    ids = text_encoder.pad_decr(ids)
    subword_ids = ids
    del ids

    subwords = []

    # Some ids correspond to bytes. Because unicode characters are composed of
    # possibly multiple bytes, we attempt to decode contiguous lists of bytes
    # all together. Invalid byte sequences are replaced with the unicode
    # replacement (i.e. unknown) character U+FFFD.
    prev_bytes = []

    def consume_prev_bytes():
      if prev_bytes:
        bytestr = b"".join(prev_bytes)
        bytes_text = bytestr.decode("utf-8", "replace")
        subwords.append(bytes_text)
      return []

    for subword_id in subword_ids:
      subword = self._id_to_subword(subword_id)
      if isinstance(subword, six.binary_type):
        # Byte-encoded
        prev_bytes.append(subword)
      else:
        # If there were bytes previously, convert to unicode.
        prev_bytes = consume_prev_bytes()
        trimmed, add_space = _trim_underscore_and_tell(subword)
        subwords.append(trimmed)
        if add_space:
          subwords.append(" ")
    # If there were trailing bytes, convert to unicode.
    prev_bytes = consume_prev_bytes()

    return tf.compat.as_text("".join(subwords))
751261
def get_content_hash(self):
        
        if not self.rexists():
            return SCons.Util.MD5signature('')
        fname = self.rfile().get_abspath()
        try:
            cs = SCons.Util.MD5filesignature(fname,
                chunksize=SCons.Node.FS.File.md5_chunksize*1024)
        except EnvironmentError as e:
            if not e.filename:
                e.filename = fname
            raise
        return cs
80936
def _trim_and_decode(ids, subtokenizer):
  """"""
  try:
    index = list(ids).index(tokenizer.EOS_ID)
    return subtokenizer.decode(ids[:index])
  except ValueError:  # No EOS found in sequence
    return subtokenizer.decode(ids)
1191893
def get_valid_filename(s):
    
    s = get_valid_filename_django(s)
    filename, ext = os.path.splitext(s)
    filename = slugify(filename)
    ext = slugify(ext)
    if ext:
        return "%s.%s" % (filename, ext)
    else:
        return "%s" % (filename,)
55144
def determine_file_extension_based_on_format(format_specifier):
	
	if format_specifier == FMT_INI:
		return 'ini'
	if format_specifier == FMT_DELIMITED:
		return ''
	if format_specifier == FMT_XML:
		return 'xml'
	if format_specifier == FMT_JSON:
		return 'json'
	if format_specifier == FMT_YAML:
		return 'yml'
	raise ValueError('invalid format specifier: {}'.format(format_specifier))
139913
def decode_ay(ay):
    """"""
    if ay is None:
        return ''
    elif isinstance(ay, str):
        return ay
    elif isinstance(ay, bytes):
        return ay.decode('utf-8')
    else:
        # dbus.Array([dbus.Byte]) or any similar sequence type:
        return bytearray(ay).rstrip(bytearray((0,))).decode('utf-8')
1221953
def get_filename4code(module, content, ext=None):
    
    imagedir = module + "-images"
    fn = hashlib.sha1(content.encode(sys.getfilesystemencoding())).hexdigest()
    try:
        os.mkdir(imagedir)
        sys.stderr.write('Created directory ' + imagedir + '\n')
    except OSError:
        pass
    if ext:
        fn += "." + ext
    return os.path.join(imagedir, fn)
29045
def _try_decode_dict(self, content):
        
        result = dict()
        for key, value in content.items():
            key = try_utf8_decode(key)
            if isinstance(value, dict):
                result[key] = self._try_decode_dict(value)
            elif isinstance(value, list):
                result[key] = self._try_decode_list(value)
            elif isinstance(value, tuple):
                result[key] = self._try_decode_tuple(value)
            else:
                result[key] = try_utf8_decode(value)
        return result
339577
def ext_from_filename(filename):
    
    try:
        base, ext = filename.lower().rsplit(".", 1)
    except ValueError:
        return ''
    ext = ".{0}".format(ext)
    all_exts = [x.extension for x in chain(magic_header_array,
                                           magic_footer_array)]

    if base[-4:].startswith("."):
        # For double extensions like like .tar.gz
        long_ext = base[-4:] + ext
        if long_ext in all_exts:
            return long_ext
    return ext
1068828
def format_file_params(files):
        '''
            
        '''
        files_payload = {}
        if files:
            for idx, filename in enumerate(files):
                files_payload["file[" + str(idx) + "]"] = open(filename, 'rb')
        return files_payload
1125317
def unicodify(filename):
    
    if isinstance(filename, bytes):
        return filename.decode(locale.getpreferredencoding())
    else:
        return filename
143937
def infer_format(filename:str) -> str:
    """"""
    _, ext = os.path.splitext(filename)
    return ext