Пример #1
0
def TestOneInput(input_bytes):
    # We need to make the file an absolute path
    testfile_path = os.path.join(os.getcwd(), "testfile.tmp")
    with open(testfile_path, "wb") as f:
        f.write(input_bytes)

    # Test basic Mime API
    Mime.get_type2(testfile_path)
    Mime.get_type_by_contents(testfile_path)
    Mime.get_type_by_data(input_bytes)

    # Test GlobDB
    globs = Mime.GlobDB()
    try:
        globs.merge_file(testfile_path)
        globs.merge_file(testfile_path)
    except UnicodeError as e:
        pass
    except ValueError as e:
        if ("not enough values to unpack" in str(e)
                or "invalid literal for int" in str(e)):
            pass
        else:
            raise e

    # Test MagicDB
    magic = Mime.MagicDB()
    try:
        magic.merge_file(testfile_path)
        magic.finalise()
    except UnicodeDecodeError:
        pass
    except (OSError, ValueError) as e:
        msg = str(e)
        if ("Not a MIME magic file" in msg
                or "Malformed section heading" in msg):
            pass
        else:
            raise e

    # Cleanup
    os.remove(testfile_path)
Пример #2
0
    def test_get_type2(self):
        # File that doesn't exist - use the name
        self.check_mimetype(Mime.get_type2(example_file('test.gif')), 'image',
                            'gif')

        # File that does exist - use the contents
        self.check_mimetype(Mime.get_type2(example_file('png_file')), 'image',
                            'png')

        # Does exist - use name before contents
        self.check_mimetype(Mime.get_type2(example_file('file.png')), 'image',
                            'png')
        self.check_mimetype(Mime.get_type2(example_file('word.doc')),
                            'application', 'msword')

        # Ambiguous file extension
        glade_mime = Mime.get_type2(example_file('glade.ui'))
        self.assertEqual(glade_mime.media, 'application')
        # Grumble, this is still ambiguous on some systems
        self.assertIn(glade_mime.subtype, {'x-gtk-builder', 'x-glade'})
        self.check_mimetype(Mime.get_type2(example_file('qtdesigner.ui')),
                            'application', 'x-designer')

        # text/x-python has greater weight than text/x-readme
        self.check_mimetype(Mime.get_type2(example_file('README.py')), 'text',
                            'x-python')

        # Directory - special filesystem object
        self.check_mimetype(Mime.get_type2(example_file('subdir')), 'inode',
                            'directory')

        # Mystery files:
        mystery_missing = Mime.get_type2(example_file('mystery_missing'))
        self.check_mimetype(mystery_missing, 'application', 'octet-stream')
        mystery_binary = Mime.get_type2(example_file('mystery_binary'))
        self.check_mimetype(mystery_binary, 'application', 'octet-stream')
        mystery_text = Mime.get_type2(example_file('mystery_text'))
        self.check_mimetype(mystery_text, 'text', 'plain')
        mystery_exe = Mime.get_type2(example_file('mystery_exe'))
        self.check_mimetype(mystery_exe, 'application', 'executable')

        # Symlink
        self.check_mimetype(Mime.get_type2(example_file("png_symlink")),
                            'image', 'png')
        self.check_mimetype(
            Mime.get_type2(example_file("png_symlink"), follow=False), 'inode',
            'symlink')
Пример #3
0
    def on_event(self, event, extension):
        items = []

        # call Baloo
        query = event.get_argument()

        if query is None:
            return RenderResultListAction(items)

        # prepare query
        if extension.preferences['tags'] == "Yes":
            query = query.replace("#f", "type:Folder")
            query = query.replace("#img", "type:Image")
            query = query.replace("#doc", "type:Document")
            query = query.replace("#txt", "type:Text")
            query = query.replace("#audio", "type:Audio")
            query = query.replace("#z", "type:Archive")
            query = query.replace("#video", "type:Video")
            query = query.replace("#pres", "type:Presentation")
            query = query.replace("#ss", "type:Spreadsheet")

        # search
        out, err = Popen([
            "baloosearch", "-l", extension.preferences['limit_results'], query
        ],
                         stdout=PIPE).communicate()
        results = out.splitlines()

        # remove duplicates
        lines = list(dict.fromkeys(results))

        for line in lines:

            # extract path
            path = line.decode("UTF-8")

            # properties
            name = os.path.basename(path)
            mime = Mime.get_type2(path)

            # icon
            icon_path = "%s/%s.svg" % (extension.preferences['icons_path'],
                                       str(mime).replace("/", "-"))
            icon = icon_path if os.path.exists(
                icon_path) else "images/default.svg"

            # items
            item_name = name.replace("&", "&")
            item_description = path.replace("&", "&")

            items.append(
                ExtensionResultItem(
                    icon=icon,
                    name=item_name,
                    description=item_description,
                    on_enter=OpenAction(path),
                    on_alt_enter=RunScriptAction(
                        "dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:'"
                        + path + "' string:''")))

        return RenderResultListAction(items)
Пример #4
0
def handleQuery(query):
    if query.isTriggered:
        query.disableSort()
    
        strip_query = query.string.strip()
        
        if strip_query:
            items = []
            
            if '#' in strip_query: 
                # prepare query for tags
                strip_query = strip_query.replace("#f",     "type:Folder")
                strip_query = strip_query.replace("#img",   "type:Image")
                strip_query = strip_query.replace("#doc",   "type:Document")
                strip_query = strip_query.replace("#txt",   "type:Text")
                strip_query = strip_query.replace("#audio", "type:Audio")
                strip_query = strip_query.replace("#z",     "type:Archive")
                strip_query = strip_query.replace("#video", "type:Video")
                strip_query = strip_query.replace("#pres",  "type:Presentation")
                strip_query = strip_query.replace("#ss",    "type:Spreadsheet")
            
            # search
            out = check_output(["baloosearch", strip_query])
            results = out.splitlines()
            
            # remove duplicates
            lines = list(dict.fromkeys(results))
            
            for line in lines:
                
                # extract path
                path = line.decode("UTF-8")
                
                # properties
                name = os.path.basename(path)
                mime = Mime.get_type2(path)
                
                # icon
                query_icon_path = "%s/%s.svg" % (icon_theme_path, str(mime).replace("/", "-"))
                query_icon = query_icon_path if os.path.exists(query_icon_path) else icon_path
                
                # items
                item_name = name.replace("&", "&")
                item_description = path.replace("&", "&")
            
                # add item to searchresult list
                items.append(Item(  id=__title__,
                                    icon=query_icon,
                                    text=item_name,
                                    subtext=item_description,
                                    actions=[
                                        TermAction(text="Open File", 
                                                   script="xdg-open '%s'" % path, 
                                                   behavior=TermAction.CloseBehavior.CloseOnSuccess),
                                        
                                        TermAction(text="Open Dir", 
                                                   script="dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:'" + path + "' string:''", 
                                                   behavior=TermAction.CloseBehavior.CloseOnSuccess),
                                        
                                        ClipAction("Copy Path", path)
                                    ]))
            
            # return searchresults to albert
            if items:
                return items
            
            else:
                return Item(id=__title__,
                            icon=icon_path,
                            text="Search '%s'" % query.string,
                            subtext="No results. Check if baloo indexer is configured correctly",
                            actions=[UrlAction("Baloo Indexer Documentation", "https://community.kde.org/Baloo")])
            
        else:
            return Item(id=__title__,
                        icon=icon_path,
                        text=__title__,
                        subtext="Enter a query to search for files with baloosearch")
Пример #5
0
 def test_get_type2(self):
     # File that doesn't exist - use the name
     self.check_mimetype(Mime.get_type2(example_file('test.png')), 'image', 'png')
     
     # File that does exist - use the contents
     self.check_mimetype(Mime.get_type2(example_file('png_file')), 'image', 'png')
     
     # Does exist - use name before contents
     self.check_mimetype(Mime.get_type2(example_file('file.png')), 'image', 'png')
     self.check_mimetype(Mime.get_type2(example_file('word.doc')), 'application', 'msword')
     
     # Ambiguous file extension
     self.check_mimetype(Mime.get_type2(example_file('glade.ui')), 'application', 'x-glade')
     self.check_mimetype(Mime.get_type2(example_file('qtdesigner.ui')), 'application', 'x-designer')
     
     # text/x-python has greater weight than text/x-readme
     self.check_mimetype(Mime.get_type2(example_file('README.py')), 'text', 'x-python')
     
     # Directory - special filesystem object
     self.check_mimetype(Mime.get_type2(example_file('subdir')), 'inode', 'directory')
     
     # Mystery files:
     mystery_missing = Mime.get_type2(example_file('mystery_missing'))
     self.check_mimetype(mystery_missing, 'application', 'octet-stream')
     mystery_binary = Mime.get_type2(example_file('mystery_binary'))
     self.check_mimetype(mystery_binary, 'application', 'octet-stream')
     mystery_text = Mime.get_type2(example_file('mystery_text'))
     self.check_mimetype(mystery_text, 'text', 'plain')
     mystery_exe = Mime.get_type2(example_file('mystery_exe'))
     self.check_mimetype(mystery_exe, 'application', 'executable')
     
     # Symlink
     self.check_mimetype(Mime.get_type2(example_file("png_symlink")),
                                 'image', 'png')
     self.check_mimetype(Mime.get_type2(example_file("png_symlink"), follow=False),
                                 'inode', 'symlink')
Пример #6
0
#!/usr/bin/env python
"""Run this manually to test xdg.Mime.get_type2 against all files in a directory.

Syntax: ./fuzz-mime.py /dir/to/test/
"""
from __future__ import print_function

import sys, os
from xdg import Mime

testdir = sys.argv[1]
files = os.listdir(testdir)

for f in files:
    f = os.path.join(testdir, f)
    try:
        print(f, Mime.get_type2(f))
    except:
        print(f)
        raise