File: utf8migrationtool

package info (click to toggle)
utf8-migration-tool 0.5.4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 192 kB
  • ctags: 129
  • sloc: python: 828; makefile: 6
file content (116 lines) | stat: -rw-r--r-- 3,841 bytes parent folder | download | duplicates (5)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python

import sys, locale, os, gtk, gobject

try:
    locale.setlocale(locale.LC_ALL, '')
except:
    pass

sys.path.append("/usr/share/utf8-migration-tool/pylib")

import configure
from wizard.wizard import Wizard
from gdmConfigParser import gdmConfigParser

w = Wizard()
w.load_steps()

section = "Desktop"
option = "Language"

dmrclocale = ""

def findEncoding(locale):
    f = open("/usr/share/i18n/SUPPORTED")
    for line in f.readlines():
        if line.split(" ")[0] == locale:
            return line.split(" ")[1].strip()
    print "Locale '%s' does not exist in /usr/share/i18n/SUPPORTED."%locale
    return None

def getconfig():
    config = gdmConfigParser()
    try: 
        config.readfp(open(os.path.expanduser('~/.dmrc')))
    except :
        print "Error while reading ~/.dmrc:", sys.exc_info()[1]
        print "Continuing anyway."
    return config

dmrc = getconfig()
currentEncoding = ""
if not dmrc.has_section(section):
    dmrc.add_section(section)
if not dmrc.has_option(section, option):
    loc = "%s.%s"
    dmrc.set(section, option, locale.getlocale(locale.LC_MESSAGES)[0])
    currentEncoding = locale.getlocale(locale.LC_MESSAGES)[1]

dmrclocale = dmrc.get(section, option)

if currentEncoding == "" or currentEncoding == None:
    currentEncoding = findEncoding(dmrclocale)

if currentEncoding != None:
    currentEncoding = currentEncoding.upper()

if currentEncoding == "" or currentEncoding == None:
     dialog = gtk.MessageDialog(
         parent         = None,
         flags          = gtk.DIALOG_DESTROY_WITH_PARENT,
         type           = gtk.MESSAGE_INFO,
         buttons        = gtk.BUTTONS_OK,
         message_format = "Your current encoding was not found, or you are using the C locale.  Please pick a supported language from /usr/share/i18n/SUPPORTED and log in again.")
     dialog.set_title('No UTF8 equivalent locale')
     dialog.connect('response', lambda dialog, response: sys.exit(1))
     dialog.show()
     gtk.main()

w['Login'].currentLocale = dmrclocale
w['Login'].currentEncoding = currentEncoding
w['FileNameConversion'].currentEncoding = currentEncoding

# If the current encoding is not UTF-8, append .UTF-8 to the current
# locale (and remove the locale's codeset or modifier first).
# Also, if the current encoding is UTF-8, but if according to
# /usr/share/i18n/SUPPORTED, the current locale is not UTF-8, append the
# .UTF-8 suffix. The latter can happen when the current locale is guessed
# with getlocale().
if currentEncoding != "UTF-8" or findEncoding(dmrclocale) != "UTF-8":
    import sre
    match = sre.match("^([^.@]*)(\.[^@]*)?(@.*)?$", w['Login'].currentLocale)
    w['Login'].newLocale = match.group(1) + ".UTF-8"
    if match.group(3) and match.group(3) != "@euro":
        w['Login'].newLocale += match.group(3)
else:
    w['Login'].newLocale = w['Login'].currentLocale

w['Login'].newEncoding = "UTF-8"

def change_setup(self, user_data):
    w = user_data
    try:
        dmrc.set(section, option, w['Login'].getNewLocale())
        f = open(os.path.expanduser('~/.dmrc'), "w")
        dmrc.write(f)
        f.close()
    except:
        print "Error while writing the new ~/.dmrc:", sys.exc_info()[1]
    for (oldfile, newfile) in w["FileNameConversion"].get_files():
# FIXME: rename all the files?
#        or only the selected ones?
        if os.path.exists(newfile):
            print "'%s' already exists. Do not rename '%s' to '%s'."%(newfile, oldfile, newfile)
        else:
            try:
                os.rename(oldfile, newfile)
            except:
                print "Cannot rename '%s' to '%s': %s"%(oldfile,
                                                        newfile,
                                                        sys.exc_info()[1])

w.connect("finished", change_setup, w)

w.run(True, main=True)