~cwayne/onehundredscopes/unity-lens-unzipper

« back to all changes in this revision

Viewing changes to unity_lens_unzipper/__init__.py

  • Committer: Chris Wayne
  • Date: 2012-10-08 19:41:43 UTC
  • Revision ID: chris.wayne@canonical.com-20121008194143-wwf2sl583vlbibjq
first working commit, lens shows contents of archived files without unzipping them on home lens

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import logging
2
2
import optparse
 
3
import re
 
4
import subprocess
 
5
import os
3
6
 
4
7
import locale
5
8
from locale import gettext as _
11
14
 
12
15
class UnzipperLens(SingleScopeLens):
13
16
 
 
17
    def __init__(self):
 
18
        SingleScopeLens.__init__(self)
 
19
        self._lens.props.search_in_global = True
 
20
        self._lens.props.visible = False
 
21
        
14
22
    class Meta:
15
23
        name = 'unzipper'
16
24
        description = 'Unzipper Lens'
17
25
        search_hint = 'Search Unzipper'
18
26
        icon = 'unzipper.svg'
19
 
        search_on_blank=True
 
27
        search_on_blank= False
 
28
        mimetype = ''
20
29
 
21
 
    # TODO: Add your categories
22
 
    example_category = ListViewCategory("Examples", 'help')
 
30
    archive_category = IconViewCategory("Archive Files", 'archive')
 
31
    
 
32
    def search_for_archive(self, search):
 
33
        try:
 
34
            search = os.path.expanduser(search)
 
35
            if 'tar' in search or 'gz' in search:
 
36
                self.mimetype = "application/x-compressed-tar"
 
37
                list_of_files = subprocess.check_output(['tar', '-tf', search], universal_newlines=True).splitlines()
 
38
            elif '.zip' in search:
 
39
                self.mimetype = 'application/zip'
 
40
                list_of_files = []
 
41
                list_of_files_raw = subprocess.check_output(['unzip', '-l', search], universal_newlines=True).splitlines()
 
42
                for line in list_of_files_raw:
 
43
                    if re.match('Archive', line) or re.match('---', line) or re.match("\ \ Length", line) or re.search('\d\ files', line):
 
44
                        pass
 
45
                    else:
 
46
                        list_of_files.append(line.split()[3])
 
47
            return list_of_files
 
48
        except:
 
49
            print "Error!"
 
50
            return []
 
51
            
 
52
        
23
53
 
24
54
    def search(self, search, results):
25
 
        # TODO: Add your search results
26
 
        results.append('https://wiki.ubuntu.com/Unity/Lenses/Singlet',
27
 
                         'ubuntu-logo',
28
 
                         self.example_category,
29
 
                         "text/html",
30
 
                         'Learn More',
31
 
                         'Find out how to write your Unity Lens',
32
 
                         'https://wiki.ubuntu.com/Unity/Lenses/Singlet')
 
55
        for result in self.search_for_archive(search):
 
56
            results.append('file://'+search,
 
57
                           'archive',
 
58
                           self.archive_category,
 
59
                           self.mimetype,
 
60
                           result,
 
61
                           'Find out how to write your Unity Lens',
 
62
                           'file://'+search)
33
63
        pass