1
# -*- coding: utf-8 -*-
2
# Copyright © 2007 Lateef Alabi-Oki
4
# This file is part of Scribes.
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.
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.
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
22
This module documents a class that manages encoding information for
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
32
class EncodingManager(object):
34
This class creates an object that manages encoding information for
38
def __init__(self, editor, encoding=None):
42
@param self: Reference to the EncodingManager instance.
43
@type self: A EncodingManager object.
45
@param editor: Reference to the text editor.
46
@type editor: An Editor object.
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)
52
def __init_attributes(self, editor, encoding):
54
Initialize data attributes.
56
@param self: Reference to the EncodingManager instance.
57
@type self: An EncodingManager object.
59
@param editor: Reference to the text editor.
60
@type editor: An Editor object.
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()
70
########################################################################
74
########################################################################
76
def get_encoding(self, uri=None, string=None):
78
Get the encoding for the file.
80
@param self: Reference to the EncodingManager instance.
81
@type self: An EncodingManager object.
83
@param uri: Reference to a file.
84
@type uri: A String object.
86
@param string: A string of characters
87
@type string: A String object.
89
@return: Return the encoding of the file.
90
@rtype: A String object.
92
from operator import truth
93
if truth(self.__user_defined_encoding):
94
encoding = self.__user_defined_encoding
96
encoding = self.__get_encoding_from_database(uri)
98
self.__determined_encoding = encoding
101
elif truth(self.__determined_encoding):
102
encoding = self.__determined_encoding
107
def set_encoding(self, encoding):
109
Set the encoding of the file.
111
@param self: Reference to the EncodingManager instance.
112
@type self: A EncodingManager object.
114
@param encoding: The encoding to set the file to.
115
@type encoding: A String object.
117
self.__user_defined_encoding = self.__determined_encoding = encoding
122
Destroy instance of this class.
124
@param self: Reference to the EncodingManager instance.
125
@type self: An EncodingManager object.
127
self.__set_encoding_in_database()
131
########################################################################
135
########################################################################
137
def __get_encoding_from_database(self, uri):
139
See if encoding data has been stored in the encoding database
140
and get it if possible.
142
@param self: Reference to the EncodingManager instance.
143
@type self: A EncodingManager object.
145
@param uri: Reference to a file stored in the database.
146
@type uri: A String object.
148
@param encoding: Get encoding from database.
149
@type encoding: A String object.
151
# Get encoding stored in database, if any.
152
from encoding_metadata import get_encoding_from_database
153
encoding = get_encoding_from_database(uri)
156
def __set_encoding_in_database(self):
158
Set encoding database.
160
Only encodings other than UTF-8 are stored in the database.
162
@param self: Reference to the EncodingManager instance.
163
@type self: An EncodingManager object.
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)
180
Destroy instance of this class.
182
@param self: Reference to the EncodingManager instance.
183
@type self: A EncodingManager object.
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)
194
########################################################################
196
# Signal and Event Handlers
198
########################################################################
200
def __close_document_no_save_cb(self, *args):
202
Handles callback when the "close-document-no-save" signal is emitted.
204
@param self: Reference to the EncodingManager instance.
205
@type self: An EncodingManager object.
207
@param *args: Irrelevant arguments.
208
@type *args: A List object.
213
def __loaded_document_cb(self, *args):
215
Handles callback when the "loaded-document" signal is emitted.
217
@param self: Reference to the EncodingManager instance.
218
@type self: An EncodingManager object.
220
from thread import start_new_thread
221
start_new_thread(self.__set_encoding_in_database, ())