~sir-rainbow/+junk/scribes-on-win

« back to all changes in this revision

Viewing changes to SCRIBES/EncodingManager.py

  • Committer: goldenmyst
  • Date: 2007-09-25 17:15:52 UTC
  • Revision ID: goldenmyst@goldenmyst-desktop-20070925171552-mvrhxdd39iibs0sr
New branch. New Trigger Management System. New Trigger API. New Plugin Management System. Fix for bug triggered by PyGTK+ version 2.11 or better.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright © 2007 Lateef Alabi-Oki
 
3
#
 
4
# This file is part of Scribes.
 
5
#
 
6
# Scribes is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# Scribes is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with Scribes; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 
19
# USA
 
20
 
 
21
"""
 
22
This module documents a class that manages encoding information for
 
23
the text editor.
 
24
 
 
25
@author: Lateef Alabi-Oki
 
26
@organization: The Scribes Project
 
27
@copyright: Copyright © 2007 Lateef Alabi-Oki
 
28
@license: GNU GPLv2 or Later
 
29
@contact: mystilleef@gmail.com
 
30
"""
 
31
 
 
32
class EncodingManager(object):
 
33
        """
 
34
        This class creates an object that manages encoding information for
 
35
        the text editor.
 
36
        """
 
37
 
 
38
        def __init__(self, editor, encoding=None):
 
39
                """
 
40
                Initialize object.
 
41
 
 
42
                @param self: Reference to the EncodingManager instance.
 
43
                @type self: A EncodingManager object.
 
44
 
 
45
                @param editor: Reference to the text editor.
 
46
                @type editor: An Editor object.
 
47
                """
 
48
                self.__init_attributes(editor, encoding)
 
49
                #self.__signal_id_1 = editor.connect("close-document-no-save", self.__close_document_no_save_cb)
 
50
                self.__signal_id_1 = editor.connect("loaded-document", self.__loaded_document_cb)
 
51
 
 
52
        def __init_attributes(self, editor, encoding):
 
53
                """
 
54
                Initialize data attributes.
 
55
 
 
56
                @param self: Reference to the EncodingManager instance.
 
57
                @type self: An EncodingManager object.
 
58
 
 
59
                @param editor: Reference to the text editor.
 
60
                @type editor: An Editor object.
 
61
                """
 
62
                self.__editor = editor
 
63
                self.__user_defined_encoding = encoding
 
64
                self.__determined_encoding = None
 
65
                self.__signal_id_1 = None
 
66
                self.__store_id = editor.add_object("EncodingManager", self)
 
67
                self.__termination_id = editor.register_termination_id()
 
68
                return
 
69
 
 
70
########################################################################
 
71
#
 
72
#                                               Public Methods
 
73
#
 
74
########################################################################
 
75
 
 
76
        def get_encoding(self, uri=None, string=None):
 
77
                """
 
78
                Get the encoding for the file.
 
79
 
 
80
                @param self: Reference to the EncodingManager instance.
 
81
                @type self: An EncodingManager object.
 
82
 
 
83
                @param uri: Reference to a file.
 
84
                @type uri: A String object.
 
85
 
 
86
                @param string: A string of characters
 
87
                @type string: A String object.
 
88
 
 
89
                @return: Return the encoding of the file.
 
90
                @rtype: A String object.
 
91
                """
 
92
                from operator import truth
 
93
                if truth(self.__user_defined_encoding):
 
94
                        encoding = self.__user_defined_encoding
 
95
                elif truth(uri):
 
96
                        encoding = self.__get_encoding_from_database(uri)
 
97
                        if truth(encoding):
 
98
                                self.__determined_encoding = encoding
 
99
                        else:
 
100
                                encoding = "utf-8"
 
101
                elif truth(self.__determined_encoding):
 
102
                        encoding = self.__determined_encoding
 
103
                else:
 
104
                        encoding = "utf-8"
 
105
                return encoding
 
106
 
 
107
        def set_encoding(self, encoding):
 
108
                """
 
109
                Set the encoding of the file.
 
110
 
 
111
                @param self: Reference to the EncodingManager instance.
 
112
                @type self: A EncodingManager object.
 
113
 
 
114
                @param encoding: The encoding to set the file to.
 
115
                @type encoding: A String object.
 
116
                """
 
117
                self.__user_defined_encoding = self.__determined_encoding = encoding
 
118
                return
 
119
 
 
120
        def destroy(self):
 
121
                """
 
122
                Destroy instance of this class.
 
123
 
 
124
                @param self: Reference to the EncodingManager instance.
 
125
                @type self: An EncodingManager object.
 
126
                """
 
127
                self.__set_encoding_in_database()
 
128
                self.__destroy()
 
129
                return
 
130
 
 
131
########################################################################
 
132
#
 
133
#                                                       Helper Methods
 
134
#
 
135
########################################################################
 
136
 
 
137
        def __get_encoding_from_database(self, uri):
 
138
                """
 
139
                See if encoding data has been stored in the encoding database
 
140
                and get it if possible.
 
141
 
 
142
                @param self: Reference to the EncodingManager instance.
 
143
                @type self: A EncodingManager object.
 
144
 
 
145
                @param uri: Reference to a file stored in the database.
 
146
                @type uri: A String object.
 
147
 
 
148
                @param encoding: Get encoding from database.
 
149
                @type encoding: A String object.
 
150
                """
 
151
                # Get encoding stored in database, if any.
 
152
                from encoding_metadata import get_encoding_from_database
 
153
                encoding  = get_encoding_from_database(uri)
 
154
                return encoding
 
155
 
 
156
        def __set_encoding_in_database(self):
 
157
                """
 
158
                Set encoding database.
 
159
 
 
160
                Only encodings other than UTF-8 are stored in the database.
 
161
 
 
162
                @param self: Reference to the EncodingManager instance.
 
163
                @type self: An EncodingManager object.
 
164
                """
 
165
                from operator import not_, contains, truth
 
166
                if not_(self.__editor.uri): return
 
167
                default_encodings = ("utf8", "utf-8", "UTF-8", "UTF8", "Utf8", "Utf-8")
 
168
                user_defined = contains(default_encodings, self.__user_defined_encoding)
 
169
                determined_encoding = contains(default_encodings, self.__determined_encoding)
 
170
                if truth(self.__user_defined_encoding):# and not_(user_defined):
 
171
                        from encoding_metadata import update_encoding_in_database
 
172
                        update_encoding_in_database(self.__editor.uri, self.__user_defined_encoding)
 
173
                elif truth(self.__determined_encoding) and not_(determined_encoding):
 
174
                        from encoding_metadata import update_encoding_in_database
 
175
                        update_encoding_in_database(self.__editor.uri, self.__determined_encoding)
 
176
                return
 
177
 
 
178
        def __destroy(self):
 
179
                """
 
180
                Destroy instance of this class.
 
181
 
 
182
                @param self: Reference to the EncodingManager instance.
 
183
                @type self: A EncodingManager object.
 
184
                """
 
185
                from utils import disconnect_signal, delete_attributes
 
186
                disconnect_signal(self.__signal_id_1, self.__editor)
 
187
                self.__editor.remove_object("EncodingManager", self.__store_id)
 
188
                self.__editor.unregister_termination_id(self.__termination_id)
 
189
                delete_attributes(self)
 
190
                self = None
 
191
                del self
 
192
                return
 
193
 
 
194
########################################################################
 
195
#
 
196
#                                               Signal and Event Handlers
 
197
#
 
198
########################################################################
 
199
 
 
200
        def __close_document_no_save_cb(self, *args):
 
201
                """
 
202
                Handles callback when the "close-document-no-save" signal is emitted.
 
203
 
 
204
                @param self: Reference to the EncodingManager instance.
 
205
                @type self: An EncodingManager object.
 
206
 
 
207
                @param *args: Irrelevant arguments.
 
208
                @type *args: A List object.
 
209
                """
 
210
                self.__destroy()
 
211
                return
 
212
 
 
213
        def __loaded_document_cb(self, *args):
 
214
                """
 
215
                Handles callback when the "loaded-document" signal is emitted.
 
216
 
 
217
                @param self: Reference to the EncodingManager instance.
 
218
                @type self: An EncodingManager object.
 
219
                """
 
220
                from thread import start_new_thread
 
221
                start_new_thread(self.__set_encoding_in_database, ())
 
222
                return