encdlg.py
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
###############################################################################
# Name: encdlg.py #
# Purpose: Encoding Dialog #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2008 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
Editra Control Library: Encoding Dialog
A simple choice dialog for selecting a file encoding type from. The dialog
can work with either a passed in list of choices to display or by default will
list all encodings found on the system using their normalized names.
@summary: Encoding choice dialog
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: encdlg.py 70230 2012-01-01 01:47:42Z CJP $"
__revision__ = "$Revision: 70230 $"
__all__ = ['EncodingDialog', 'GetAllEncodings']
#--------------------------------------------------------------------------#
# Imports
import locale
import encodings
import wx
# Editra Control Library Imports
import choicedlg
#--------------------------------------------------------------------------#
# Globals
EncodingDialogNameStr = u"EncodingDialog"
#--------------------------------------------------------------------------#
class EncodingDialog(choicedlg.ChoiceDialog):
"""Dialog for choosing an file encoding from the list of available
encodings on the system.
"""
def __init__(self, parent, id=wx.ID_ANY, msg=u'', title=u'',
elist=list(), default=u'',
style=wx.CAPTION, pos=wx.DefaultPosition,
size=wx.DefaultSize,
name=EncodingDialogNameStr):
"""Create the encoding dialog
@param parent: Parent Window
@keyword id: Dialog ID
@keyword msg: Dialog Message
@keyword title: Dialog Title
@keyword elist: list of encodings to use or None to use all
@keyword default: Default selected encoding
@keyword style: Dialog Style bitmask
@keyword pos: Dialog Postion
@keyword size: Dialog Size
@keyword name: Dialog Name
"""
if not len(elist):
elist = GetAllEncodings()
default = encodings.normalize_encoding(default)
if default and default.lower() in elist:
sel = default.lower()
else:
sel = locale.getpreferredencoding(False)
super(EncodingDialog, self).__init__(parent, id, msg, title,
elist, sel, pos, size, style)
def GetEncoding(self):
"""Get the selected encoding
@return: string
"""
return self.GetStringSelection()
#--------------------------------------------------------------------------#
# Utilities
def GetAllEncodings():
"""Get all encodings found on the system
@return: list of strings
"""
elist = encodings.aliases.aliases.values()
elist = list(set(elist))
elist.sort()
elist = [ enc for enc in elist if not enc.endswith('codec') ]
return elist
#--------------------------------------------------------------------------#