~vorlon/ubuntu/saucy/gourmet/trunk

« back to all changes in this revision

Viewing changes to src/lib/plugins/import_export/plaintext_plugin/plaintext_importer_plugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Rolf Leggewie
  • Date: 2008-11-24 10:38:57 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081124103857-7ds1eemlqjpxhj2n
Tags: 0.14.2-1
* new upstream release
* add manpage, courtesy of François Wendling.  (Closes: #495796).
* add 06-change-sys-path-append.patch
  sys.path.append('/usr/share') can be dangerous

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from gourmet.plugin import ImporterPlugin
 
2
from gourmet.importers.importer import Tester
 
3
from gourmet.threadManager import get_thread_manager
 
4
from gourmet.importers.interactive_importer import InteractiveImporter
 
5
from gourmet import check_encodings
 
6
import os.path
 
7
 
 
8
MAX_PLAINTEXT_LENGTH = 100000
 
9
 
 
10
class PlainTextImporter (InteractiveImporter):
 
11
 
 
12
    name = 'Plain Text Importer'
 
13
 
 
14
    def __init__ (self, filename):
 
15
        self.filename = filename
 
16
        InteractiveImporter.__init__(self)
 
17
        
 
18
    def do_run (self):
 
19
        if os.path.getsize(self.filename) > MAX_PLAINTEXT_LENGTH*16:
 
20
            del data
 
21
            ifi.close()
 
22
            import gourmet.gtk_extras.dialog_extras as de
 
23
            de.show_message(title=_('Big File'),
 
24
                            label=_('File %s is too big to import'%self.filename),
 
25
                            sublabel=_('Your file exceeds the maximum length of %s characters. You probably didn\'t mean to import it anyway. If you really do want to import this file, use a text editor to split it into smaller files and try importing again.')%MAX_PLAINTEXT_LENGTH,
 
26
                            message_type=gtk.MESSAGE_ERROR)
 
27
            return
 
28
        ifi = file(self.filename,'r')
 
29
        data = '\n'.join(check_encodings.get_file(ifi))
 
30
        ifi.close()
 
31
        self.set_text(data)
 
32
        return InteractiveImporter.do_run(self)
 
33
 
 
34
class PlainTextImporterPlugin (ImporterPlugin):
 
35
 
 
36
    name = _('Plain Text file')
 
37
    patterns = ['*.txt','[^.]*','*']
 
38
    mimetypes = ['text/plain']
 
39
 
 
40
    def test_file (self, filename):
 
41
        '''Given a filename, test whether the file is of this type.'''
 
42
        return -1 # we are a fallback option
 
43
 
 
44
    def get_importer (self, filename):
 
45
        return PlainTextImporter(filename=filename)
 
46
                                                   
 
47
 
 
48
 
 
49