updated on Tue Jan 10 04:01:21 UTC 2012
[aur-mirror.git] / text2utf / text2utf.py
blob5a8764042ed65090f86268cc7f736be5c2444876
1 #!/usr/bin/python
2 # vim: filencoding=utf-8 foldmethod=indent
3 import gettext
4 _ = gettext.gettext
5 import locale
6 locale.setlocale( locale.LC_ALL )
7 gettext.bindtextdomain( 'text2utf', '/usr/share/locale/' )
8 gettext.textdomain( 'text2utf' )
10 import md5
11 import sys
12 import urllib
13 import os
14 import shutil
15 import commands
17 def write2zenity(msg, msg_type='info'):
18 'zenity message sink'
19 os.system('zenity --%s --text="%s" --title "text2utf"' % (msg_type, msg))
21 def get_file(msg):
22 'zenity "file-input" dialog'
23 return commands.getoutput('zenity --file-selection --multiple --separator="|" --title "text2utf"')
25 def write2stdout(msg, msg_type='info'):
26 'stdout message sink'
27 print '%s: %s' % (msg_type, msg)
29 def subconvert(file):
30 os.system('dos2unix -U \'%s\'' % file)
31 kodowanie=commands.getoutput('/usr/bin/file \'%s\'' % file)
32 kodowanie=kodowanie[len(file)+2:]
33 iconv_cmd = '/usr/bin/iconv --from-code=%s --to-code=utf-8 --output=\'%s\' \'%s\''
34 from_enc = None
35 if ('UTF-8 Unicode' in kodowanie):
36 pass
37 elif ('ISO-8859 text' in kodowanie):
38 from_enc = 'iso-8859-2'
39 else:
40 from_enc = 'Windows-1250'
41 if not from_enc is None:
42 os.system(iconv_cmd % (from_enc, '%s.tmp' % file , file))
43 shutil.move('%s.tmp' % file, file)
45 class printing:
46 def __init__(self):
47 self.zenity=0
48 self.store=0
49 self.storemsginfo=[]
50 self.storemsgerror=[]
51 def printnow(self, msg, msg_type="info"):
52 if (self.zenity==0):
53 write2stdout(msg, msg_type)
54 else:
55 write2zenity(msg, msg_type)
56 def pr(self, msg, msg_type="info"):
57 if (self.store==0):
58 self.printnow(msg, msg_type)
59 else:
60 if (msg_type=="info"):
61 self.storemsginfo.append(msg)
62 elif (msg_type=="error"):
63 self.storemsgerror.append(msg)
64 def printstored(self):
65 if (self.store==1):
66 if (len(self.storemsginfo)>0):
67 self.printnow('\n'.join(self.storemsginfo), "info")
68 if (len(self.storemsgerror)>0):
69 self.printnow('\n'.join(self.storemsgerror), "error")
70 self.store=0
72 pr=printing()
73 def printout (msg, msg_type='info'):
74 pr.pr(msg, msg_type)
76 if 'zenity' in sys.argv[1:]:
77 pr.zenity=1
79 mname, filenames = '', [s for s in sys.argv[1:] if s != 'zenity']
80 if not filenames:
81 if (pr.zenity==1):
82 filenames = get_file(_('Chose text file to convert.')).split("|")
83 if not filenames[0]:
84 sys.exit(2)
85 else:
86 printout(_('Chose text file to convert.'), 'error')
87 sys.exit(2)
88 if (len(filenames)>1):
89 pr.store=1
90 for i in range(len(filenames)):
91 mname = filenames.pop(0)
92 if not os.path.isfile(mname):
93 printout( _('File:')+" %s " % (mname)+_('not exist!'), 'error')
94 continue
96 filesize = os.path.getsize(mname)
97 if filesize > 80000:
98 printout(_('Chosed file is too big!')+" %sMB" % (filesize/(1024*1024) ) )
99 continue
101 subconvert('%s' % mname)
102 printout(_('Convertet file:')+" %s." % (os.path.basename(mname)) )
104 pr.printstored()