labpack/parsing/magic.py
def analyze(self, file_path='', file_url='', byte_data=None):

        ''' a method to determine the mimetype and extension of a file from its byte data

        :param file_path: [optional] string with local path to file
        :param file_url: [optional] string with url of file
        :param byte_data: [optional] byte data from a file
        :return: dictionary with file details

        {
            'name': 'filename.ext',
            'mimetype': 'type/sub-type',
            'extension': '.ext'
        }
        '''

        title = '%s.analyze' % self.__class__.__name__

    # validate inputs
        input_fields = {
            'file_path': file_path,
            'file_url': file_url
        }
        for key, value in input_fields.items():
            if value:
                object_title = '%s(%s=%s)' % (title, key, str(value))
                self.fields.validate(value, '.%s' % key, object_title)
        if byte_data:
            if not isinstance(byte_data, bytes):
                raise ValueError("%s(byte_data=b'...') must be byte data" % title)

    # construct empty type map
        file_details = {
            'name': '',
            'mimetype': '',
            'extension': ''
        }
        magic_results = None

    # analyze file
        if file_path:
            import os
            if not os.path.exists(file_path):
                raise ValueError('%s(file_path=%s) is not a valid path.' % (title, file_path))
            split_file = os.path.split(file_path)
            file_details['name'] = split_file[0]
            if len(split_file) > 1:
                file_details['name'] = split_file[1]
            magic_results = self.magic.from_file(file_path)

    # analyze url
        elif file_url:
            from urllib.parse import urlsplit
            url_path = urlsplit(file_url).path
            path_segments = url_path.split('/')
            file_details['name'] = path_segments[-1]
            import requests
            try:
                response = requests.get(file_url)
            except Exception:
                from labpack.handlers.requests import handle_requests
                response_details = handle_requests(requests.Request(url=file_url))
                raise ValueError('%s(file_url=%s) created this error: %s' % (title, file_url, response_details['error']))
            magic_results = self.magic.from_buffer(response.content)

    # analyze buffer
        elif byte_data:
            magic_results = self.magic.from_buffer(byte_data)

        else:
            raise IndexError('%s(...) requires either a file_path, file_url or byte_data argument' % title)

        if magic_results:
            file_details['mimetype'] = magic_results
            mime_results = self.mimetypes.guess_extension(magic_results)
            if mime_results:
                file_details['extension'] = mime_results

        return file_details
 a method to determine the mimetype and extension of a file from its byte data

        :param file_path: [optional] string with local path to file
        :param file_url: [optional] string with url of file
        :param byte_data: [optional] byte data from a file
        :return: dictionary with file details

        {
            'name': 'filename.ext',
            'mimetype': 'type/sub-type',
            'extension': '.ext'
        }
        
a method to determine the mimetype and extension of a file from its byte data
a method to determine the mimetype and extension of a file from its byte data
def analyze(self, file_path='', file_url='', byte_data=None):
        title = '%s.analyze' % self.__class__.__name__
        input_fields = {
            'file_path': file_path,
            'file_url': file_url
        }
        for key, value in input_fields.items():
            if value:
                object_title = '%s(%s=%s)' % (title, key, str(value))
                self.fields.validate(value, '.%s' % key, object_title)
        if byte_data:
            if not isinstance(byte_data, bytes):
                raise ValueError("%s(byte_data=b'...') must be byte data" % title)
        file_details = {
            'name': '',
            'mimetype': '',
            'extension': ''
        }
        magic_results = None
        if file_path:
            import os
            if not os.path.exists(file_path):
                raise ValueError('%s(file_path=%s) is not a valid path.' % (title, file_path))
            split_file = os.path.split(file_path)
            file_details['name'] = split_file[0]
            if len(split_file) > 1:
                file_details['name'] = split_file[1]
            magic_results = self.magic.from_file(file_path)
        elif file_url:
            from urllib.parse import urlsplit
            url_path = urlsplit(file_url).path
            path_segments = url_path.split('/')
            file_details['name'] = path_segments[-1]
            import requests
            try:
                response = requests.get(file_url)
            except Exception:
                from labpack.handlers.requests import handle_requests
                response_details = handle_requests(requests.Request(url=file_url))
                raise ValueError('%s(file_url=%s) created this error: %s' % (title, file_url, response_details['error']))
            magic_results = self.magic.from_buffer(response.content)
        elif byte_data:
            magic_results = self.magic.from_buffer(byte_data)
        else:
            raise IndexError('%s(...) requires either a file_path, file_url or byte_data argument' % title)
        if magic_results:
            file_details['mimetype'] = magic_results
            mime_results = self.mimetypes.guess_extension(magic_results)
            if mime_results:
                file_details['extension'] = mime_results
        return file_details
def analyze ( self , file_path = '' , file_url = '' , byte_data = None ) : title = '%s.analyze' % self . __class__ . __name__ input_fields = { 'file_path' : file_path , 'file_url' : file_url } for key , value in input_fields . items ( ) : if value : object_title = '%s(%s=%s)' % ( title , key , str ( value ) ) self . fields . validate ( value , '.%s' % key , object_title ) if byte_data : if not isinstance ( byte_data , bytes ) : raise ValueError ( "%s(byte_data=b'...') must be byte data" % title ) file_details = { 'name' : '' , 'mimetype' : '' , 'extension' : '' } magic_results = None if file_path : import os if not os . path . exists ( file_path ) : raise ValueError ( '%s(file_path=%s) is not a valid path.' % ( title , file_path ) ) split_file = os . path . split ( file_path ) file_details [ 'name' ] = split_file [ 0 ] if len ( split_file ) > 1 : file_details [ 'name' ] = split_file [ 1 ] magic_results = self . magic . from_file ( file_path ) elif file_url : from urllib . parse import urlsplit url_path = urlsplit ( file_url ) . path path_segments = url_path . split ( '/' ) file_details [ 'name' ] = path_segments [ - 1 ] import requests try : response = requests . get ( file_url ) except Exception : from labpack . handlers . requests import handle_requests response_details = handle_requests ( requests . Request ( url = file_url ) ) raise ValueError ( '%s(file_url=%s) created this error: %s' % ( title , file_url , response_details [ 'error' ] ) ) magic_results = self . magic . from_buffer ( response . content ) elif byte_data : magic_results = self . magic . from_buffer ( byte_data ) else : raise IndexError ( '%s(...) requires either a file_path, file_url or byte_data argument' % title ) if magic_results : file_details [ 'mimetype' ] = magic_results mime_results = self . mimetypes . guess_extension ( magic_results ) if mime_results : file_details [ 'extension' ] = mime_results return file_details
a method to determine the mimetype and extension of a file from its byte data
a method to determine the mimetype and extension of a file from its byte data
''' a method to determine the mimetype and extension of a file from its byte data

        :param file_path: [optional] string with local path to file
        :param file_url: [optional] string with url of file
        :param byte_data: [optional] byte data from a file
        :return: dictionary with file details

        {
            'name': 'filename.ext',
            'mimetype': 'type/sub-type',
            'extension': '.ext'
        }
        ''' # validate inputs
 # construct empty type map
 # analyze file
 # analyze url
 # analyze buffer

dictionary with file details
{
'name': 'filename.ext',
'mimetype': 'type/sub-type',
'extension': '.ext'
}
dictionary with file details { ' name ' : ' filename . ext ' ' mimetype ' : ' type / sub - type ' ' extension ' : ' . ext ' }
self
file_path
[optional] string with local path to file
[ optional ] string with local path to file
file_url
[optional] string with url of file
[ optional ] string with url of file
byte_data
[optional] byte data from a file
[ optional ] byte data from a file