Create translations from file extension to archive format

        Returns
        -------
        dict
            The mapping from file extension to archive format
        dict
            The mapping from archive format to default file extension
19191
def _archive_extensions():
        
        if six.PY3:
            ext_map = {}
            fmt_map = {}
            for key, exts, desc in shutil.get_unpack_formats():
                fmt_map[key] = exts[0]
                for ext in exts:
                    ext_map[ext] = key
        else:
            ext_map = {'.tar': 'tar',
                       '.tar.bz2': 'bztar',
                       '.tar.gz': 'gztar',
                       '.tar.xz': 'xztar',
                       '.tbz2': 'bztar',
                       '.tgz': 'gztar',
                       '.txz': 'xztar',
                       '.zip': 'zip'}
            fmt_map = {'bztar': '.tar.bz2',
                       'gztar': '.tar.gz',
                       'tar': '.tar',
                       'xztar': '.tar.xz',
                       'zip': '.zip'}
        return ext_map, fmt_map
190209
def from_archive(archive_filename, py_interpreter=sys.executable):
    
    with _extract_to_tempdir(archive_filename) as root_dir:
        data = _setup_py_run_from_dir(root_dir, py_interpreter)
    return data
366396
def filer(filelist):
    
    # Initialise the dictionary
    filedict = dict()
    for seqfile in filelist:
        # Split off the file extension and remove the path from the name
        strainname = os.path.splitext(os.path.basename(seqfile))[0]
        # Populate the dictionary
        filedict[strainname] = seqfile
    return filedict
803373
def regenerate(location='http://www.iana.org/assignments/language-subtag-registry',
               filename=None, default_encoding='utf-8'):
    
    paren = re.compile('\([^)]*\)')

    # Get the language list.
    data = urllib2.urlopen(location)
    if ('content-type' in data.headers and
                'charset=' in data.headers['content-type']):
        encoding = data.headers['content-type'].split('charset=')[-1]
    else:
        encoding = default_encoding
    content = data.read().decode(encoding)
    languages = []
    info = {}
    p = None
    for line in content.splitlines():
        if line == '%%':
            if 'Type' in info and info['Type'] == 'language':
                languages.append(info)
            info = {}
        elif ':' not in line and p:
            info[p[0]] = paren.sub('', p[2]+line).strip()
        else:
            p = line.partition(':')
            if not p[0] in info: # Keep the first description as it should be the most common
                info[p[0]] = paren.sub('', p[2]).strip()

    languages_lines = map(lambda x:'("%s", _(u"%s")),'%(x['Subtag'],x['Description']), languages)

    # Generate and save the file.
    if not filename:
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'languages.py')
    # TODO: first make a backup of the file if it exists already.
    f = codecs.open(filename, 'w', 'utf-8')
    f.write(TEMPLATE % {
        'languages': '\n    '.join(languages_lines),
    })
    f.close()
300889
def get_archive_format (filename):
    """"""
    mime, compression = util.guess_mime(filename)
    if not (mime or compression):
        raise util.PatoolError("unknown archive format for file `%s'" % filename)
    if mime in ArchiveMimetypes:
        format = ArchiveMimetypes[mime]
    else:
        raise util.PatoolError("unknown archive format for file `%s' (mime-type is `%s')" % (filename, mime))
    if format == compression:
        # file cannot be in same format compressed
        compression = None
    return format, compression
1088898
def to_array(self):
        
        array = super(PassportElementErrorFiles, self).to_array()
        array['source'] = u(self.source)  # py2: type unicode, py3: type str
        array['type'] = u(self.type)  # py2: type unicode, py3: type str
        array['file_hashes'] = self._as_array(self.file_hashes)  # type list of str
        array['message'] = u(self.message)  # py2: type unicode, py3: type str
        return array
312123
def get_language_from_extension(file_name):
    

    filepart, extension = os.path.splitext(file_name)
    pathpart, filename = os.path.split(file_name)

    if filename == 'go.mod':
        return 'Go'

    if re.match(r'\.h.*$', extension, re.IGNORECASE) or re.match(r'\.c.*$', extension, re.IGNORECASE):

        if os.path.exists(u('{0}{1}').format(u(filepart), u('.c'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.C'))):
            return 'C'

        if os.path.exists(u('{0}{1}').format(u(filepart), u('.m'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.M'))):
            return 'Objective-C'

        if os.path.exists(u('{0}{1}').format(u(filepart), u('.mm'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.MM'))):
            return 'Objective-C++'

        available_extensions = extensions_in_same_folder(file_name)

        for ext in CppLexer.filenames:
            ext = ext.lstrip('*')
            if ext in available_extensions:
                return 'C++'

        if '.c' in available_extensions:
            return 'C'

    if re.match(r'\.m$', extension, re.IGNORECASE) and (os.path.exists(u('{0}{1}').format(u(filepart), u('.h'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.H')))):
        return 'Objective-C'

    if re.match(r'\.mm$', extension, re.IGNORECASE) and (os.path.exists(u('{0}{1}').format(u(filepart), u('.h'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.H')))):
        return 'Objective-C++'

    return None
688441
def compress_to(self, archive_path=None):
        
        if archive_path is None:
            archive = tempfile.NamedTemporaryFile(delete=False)
            tar_args = ()
            tar_kwargs = {'fileobj': archive}
            _return = archive.name
        else:
            tar_args = (archive_path)
            tar_kwargs = {}
            _return = archive_path
        tar_kwargs.update({'mode': 'w:gz'})
        with closing(tarfile.open(*tar_args, **tar_kwargs)) as tar:
            tar.add(self.path, arcname=self.file)

        return _return
766573
def files(self):
        """"""
        # try new file header format first, then fallback on old
        for header in (r"(.*)\t\[\[\[1\n", r"^(\d+)\n$"):
            header = re.compile(header)
            filename = None
            self.fd.seek(0)
            line = self.readline()
            while line:
                m = header.match(line)
                if m is not None:
                    filename = m.group(1)
                    try:
                        filelines = int(self.readline().rstrip())
                    except ValueError:
                        raise ArchiveError('invalid archive format')
                    filestart = self.fd.tell()
                    yield (filename, filelines, filestart)
                line = self.readline()
            if filename is not None:
                break
1162396
def _to_textfile(self, file_generator):
        
        to_path = file_generator.to_path \
            if file_generator.to_path.endswith(file_generator.file_extension[file_generator.to_format]) \
            else file_generator.to_path + file_generator.file_extension[file_generator.to_format]

        with open(to_path, mode="w") as outfile:
            for f in file_generator:
                outfile.write(f.writestr(file_generator.to_format))
4650
def to_zipfile(self, resource, zipfile):
        
        rpr_map = self.to_strings(resource)
        with ZipFile(zipfile, 'w') as zipf:
            for (mb_cls, rpr_string) in iteritems_(rpr_map):
                fn = get_collection_filename(mb_cls, self.__content_type)
                zipf.writestr(fn, rpr_string, compress_type=ZIP_DEFLATED)
824526
def setup_formats(self):
        
        methods = self.get_methods()
        for m in methods:
            #Methods named "from_X" will be assumed to convert from format X to the common format
            if m.startswith("from_"):
                self.input_formats.append(re.sub("from_" , "",m))
            #Methods named "to_X" will be assumed to convert from the common format to X
            elif m.startswith("to_"):
                self.output_formats.append(re.sub("to_","",m))
1332620
def _get_available_extensions():
    
    extensions = []

    # from filenames
    parsers_dir = os.path.join(os.path.dirname(__file__))
    glob_filename = os.path.join(parsers_dir, "*" + _FILENAME_SUFFIX + ".py")
    ext_re = re.compile(glob_filename.replace('*', "(?P<ext>\w+)"))
    for filename in glob.glob(glob_filename):
        ext_match = ext_re.match(filename)
        ext = ext_match.groups()[0]
        extensions.append(ext)
        extensions.append('.' + ext)

    # from relevant synonyms (don't use the '' synonym)
    for ext in EXTENSION_SYNONYMS.keys():
        if ext:
            extensions.append(ext)
            extensions.append(ext.replace('.', '', 1))
    extensions.sort()
    return extensions
1310419
def adapt_files(solver):
    

    print("adapting {0}'s files".format(solver))
    root = os.path.join('solvers', solver)

    for arch in to_extract[solver]:
        arch = os.path.join(root, arch)
        extract_archive(arch, solver, put_inside=True)

    for fnames in to_move[solver]:
        old = os.path.join(root, fnames[0])
        new = os.path.join(root, fnames[1])
        os.rename(old, new)

    for f in to_remove[solver]:
        f = os.path.join(root, f)
        if os.path.isdir(f):
            shutil.rmtree(f)
        else:
            os.remove(f)
1361905
def set_file_encoding(self, path, encoding):
        
        try:
            map = json.loads(self._settings.value('cachedFileEncodings'))
        except TypeError:
            map = {}
        map[path] = encoding
        self._settings.setValue('cachedFileEncodings', json.dumps(map))
618430
def to_array(self):
        
        array = super(File, self).to_array()
        array['file_id'] = u(self.file_id)  # py2: type unicode, py3: type str

        if self.file_size is not None:
            array['file_size'] = int(self.file_size)  # type int
        if self.file_path is not None:
            array['file_path'] = u(self.file_path)  # py2: type unicode, py3: type str

        return array
930255
def get_archive_file_list(archive_filename):
    
    if archive_filename.endswith('.zip'):
        with closing(zipfile.ZipFile(archive_filename)) as zf:
            return add_directories(zf.namelist())
    elif archive_filename.endswith(('.tar.gz', '.tar.bz2', '.tar')):
        with closing(tarfile.open(archive_filename)) as tf:
            return add_directories(list(map(unicodify, tf.getnames())))
    else:
        ext = os.path.splitext(archive_filename)[-1]
        raise Failure('Unrecognized archive type: %s' % ext)