PK’~H,s]||fileinspector.py# -*- coding: utf-8 -*- """ This file is part of fileinspector. fileinspector is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. fileinspector is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with fileinspector. If not, see . Created on Wed Mar 2 11:37:36 2016 @author: daniel """ # Python3 compatibility from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals __version__ = '0.1.0' import sys import mimetypes import warnings try: import magic except ImportError as e: warnings.warn("python-magic could not be imported so its features will be " "unavailable.\n{}".format(e), ImportWarning) magic = None def determine_category(mimetype): """ Determines the category to which the file can be attributed. Parameters ---------- mimetype : string The mimetype specified as / Returns ------- string : the category or False if no match was found. """ if 'image' in mimetype: return 'image' elif 'pdf' in mimetype: return 'pdf' elif "text/x-" in mimetype: return 'code' elif "text/plain" in mimetype: return 'text' elif "msword" in mimetype or \ "officedocument.wordprocessingml" in mimetype or \ "opendocument.text" in mimetype: return 'doc' elif "powerpoint" in mimetype or \ "presentation" in mimetype: return 'presentation' elif "excel" in mimetype or \ "spreadsheet" in mimetype: return 'spreadsheet' elif "zip" in mimetype or "x-tar" in mimetype\ or "compressed" in mimetype: return 'archive' elif "video" in mimetype: return 'video' elif "audio" in mimetype: return 'audio' # If nothing matched, simply return False return False def determine_type_with_magic(filename, mime=True): """ Determines the filetype using the python-magic library. Parameters ---------- filename : string The path to or name of the file (including extension) format : string (optional) The output format of the function. The default is 'mime', in which case the mimetype is returned in the format of / Other options: 'verbose' for the standard more wordy python-magic description of files. Returns ------- string : the mimetype in the specified format. """ if magic is None: raise ImportError("python-magic is not available. This function cannot be used") try: ftype = magic.from_file(filename,mime=mime) except IOError: ftype = False if type(ftype) == bytes: ftype = ftype.decode('utf-8') return ftype def determine_type_with_mimetypes(filename): """ Determines the filetype using mimetypes. Parameters ---------- filename : string The path to or name of the file (including extension) Returns ------- string : the determined mimetype as / or False if no type could be determined. """ mime, encoding = mimetypes.guess_type(filename) if mime is None: return False return mime def determine_type(filename, output="mime"): """ Determines the filetype. Tries to use python-magic first, but if this doesn't work out (because the file for instance cannot be accessed locally, or python-magic is not available for other reasons), it falls back to the mimetypes modules, which uses the filename + extension to make an educated guess about the filetype. Parameters ---------- filename : string The path to or name of the file (including extension) format : string (optional) The output format of the function. The default is 'mime', in which case the mimetype is returned in the format of / Other options are: - 'xdg' for a freedesktop specification (-). - 'verbose' for the standard python-magic output, if the module is \ available. If not, it defaults back to 'mime' Returns ------- found_type : string/boolean the mimetype in the specified format or False if nothing could be found. """ # Initialize ftype as false ftype = False # First try to use python-magic to determine the filetype, as it is not # fooled by incorrect or absent file extensions. # Only do this is python-magic is available if not magic is None: ftype = determine_type_with_magic(filename, (output!="verbose") ) # If python-magic is not available, or it could not determine the filetype, # use mimetypes if ftype == False: ftype = determine_type_with_mimetypes(filename) # freedesktop doesn't use / but - as mime # format. Translate if requested. if output=="xdg": ftype = translate_to_xdg(ftype) return ftype def translate_to_xdg(mimetype): """ Translates the mimetype into the xdg format of -. Parameters ---------- mimetype : string The specified mimetype specified as / Returns ------- xdg-type : string the mimetype translated to freedesktop.org format """ return mimetype.replace("/","-") if __name__ == "__main__": import os if len(sys.argv) < 2: files = filter(lambda x: not x in [".",".."], os.listdir(".")) print("Inspecting files in current folder.\nYou can also check specific files" " by passing them as arguments.\n") else: files = sys.argv[1:] for f in files: f_full = os.path.abspath(f) if(os.path.isdir(f_full)): continue print("{}:\t\t{}".format(f, determine_type(f_full,'verbose'))) PK"“~H^-า 4python_fileinspector-0.1.0.dist-info/DESCRIPTION.rstUNKNOWN PK"“~H ™ฃ!ซซ2python_fileinspector-0.1.0.dist-info/metadata.json{"classifiers": ["Development Status :: 4 - Beta", "Intended Audience :: Developers", "Topic :: Desktop Environment", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3"], "extensions": {"python.details": {"contacts": [{"email": "dschreij@gmail.com", "name": "Daniel Schreij", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}, "project_urls": {"Home": "https://github.com/dschreij/fileinspector"}}}, "generator": "bdist_wheel (0.26.0)", "metadata_version": "2.0", "name": "python-fileinspector", "summary": "A module to determine file mimetypes", "version": "0.1.0"}PK"“~HŸ\‹2python_fileinspector-0.1.0.dist-info/top_level.txtfileinspector PK"“~H์ndชnn*python_fileinspector-0.1.0.dist-info/WHEELWheel-Version: 1.0 Generator: bdist_wheel (0.26.0) Root-Is-Purelib: true Tag: py2-none-any Tag: py3-none-any PK"“~H<';66-python_fileinspector-0.1.0.dist-info/METADATAMetadata-Version: 2.0 Name: python-fileinspector Version: 0.1.0 Summary: A module to determine file mimetypes Home-page: https://github.com/dschreij/fileinspector Author: Daniel Schreij Author-email: dschreij@gmail.com License: UNKNOWN Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Intended Audience :: Developers Classifier: Topic :: Desktop Environment Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3) Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 3 UNKNOWN PK"“~Hค™nƒƒ+python_fileinspector-0.1.0.dist-info/RECORDfileinspector.py,sha256=yWdXAA3UM9o5ahlFg1qSChaJoVQ0MVlFFH__IO3iAfk,5756 python_fileinspector-0.1.0.dist-info/DESCRIPTION.rst,sha256=OCTuuN6LcWulhHS3d5rfjdsQtW22n7HENFRh6jC6ego,10 python_fileinspector-0.1.0.dist-info/METADATA,sha256=ydisV_co_G6IityCyrwqE6eyzz4APCbo5sFb7ikp-RA,566 python_fileinspector-0.1.0.dist-info/RECORD,, python_fileinspector-0.1.0.dist-info/WHEEL,sha256=GrqQvamwgBV4nLoJe0vhYRSWzWsx7xjlt74FT0SWYfE,110 python_fileinspector-0.1.0.dist-info/metadata.json,sha256=meh2lu539oAfjTavAUBGLgX92hI-fyZOWgXC6FYWiwE,683 python_fileinspector-0.1.0.dist-info/top_level.txt,sha256=YfdtZOlnSnbhl6-_q65CdWsgBvlzfH6HdFJVGDF5zKE,14 PK’~H,s]||fileinspector.pyPK"“~H^-า 4ชpython_fileinspector-0.1.0.dist-info/DESCRIPTION.rstPK"“~H ™ฃ!ซซ2python_fileinspector-0.1.0.dist-info/metadata.jsonPK"“~HŸ\‹2python_fileinspector-0.1.0.dist-info/top_level.txtPK"“~H์ndชnn*_python_fileinspector-0.1.0.dist-info/WHEELPK"“~H<';66-python_fileinspector-0.1.0.dist-info/METADATAPK"“~Hค™nƒƒ+–python_fileinspector-0.1.0.dist-info/RECORDPKlb