mirror of
https://github.com/gryf/pygtktalog.git
synced 2025-12-18 20:10:24 +01:00
* Code clean up.
* Removed unnecessary imports. * Adapted to PEP8.
This commit is contained in:
@@ -24,7 +24,6 @@
|
||||
|
||||
from gtkmvc import Model
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
import gtk
|
||||
@@ -33,18 +32,19 @@ import gobject
|
||||
from ConfigParser import ConfigParser
|
||||
|
||||
class Ini(object):
|
||||
|
||||
def __init__(self):
|
||||
self.ini = []
|
||||
|
||||
|
||||
def add_section(self, section):
|
||||
self.ini.append("[%s]" % section)
|
||||
|
||||
|
||||
def add_key(self, key, value):
|
||||
self.ini.append("%s=%s" % (key, value))
|
||||
|
||||
|
||||
def add_comment(self, comment):
|
||||
self.ini.append(";%s" % comment)
|
||||
|
||||
|
||||
def add_verb(self, verb):
|
||||
self.ini.append(verb)
|
||||
|
||||
@@ -53,11 +53,11 @@ class Ini(object):
|
||||
|
||||
class ConfigModel(Model):
|
||||
ini = Ini()
|
||||
|
||||
|
||||
__properties__ = {}
|
||||
|
||||
|
||||
filetype_list = ['Images', 'Movies']
|
||||
|
||||
|
||||
confd = {
|
||||
'savewin': True,
|
||||
'savepan': True,
|
||||
@@ -67,26 +67,26 @@ class ConfigModel(Model):
|
||||
'v': 300,
|
||||
'eject': True,
|
||||
'compress': True,
|
||||
|
||||
|
||||
'exportxls': False,
|
||||
|
||||
|
||||
'confirmquit': True,
|
||||
'confirmabandon': True,
|
||||
'mntwarn': True,
|
||||
'delwarn': True,
|
||||
|
||||
|
||||
'cd': '/mnt/cdrom',
|
||||
'ejectapp': 'eject -r',
|
||||
|
||||
|
||||
'imgview': False,
|
||||
'imgprog': 'gqview',
|
||||
|
||||
|
||||
'retrive': False,
|
||||
|
||||
|
||||
'thumbs': True,
|
||||
'exif': True,
|
||||
'gthumb': False,
|
||||
|
||||
|
||||
'extensions': {'bmp':'identify %s',
|
||||
'gif':'identify "%s"',
|
||||
'jpg':'identify "%s"',
|
||||
@@ -98,15 +98,15 @@ class ConfigModel(Model):
|
||||
'mpeg':'midentify "%s"',
|
||||
'wmv':'midentify "%s"',
|
||||
},
|
||||
|
||||
|
||||
'showtoolbar':True,
|
||||
'showstatusbar':True,
|
||||
}
|
||||
|
||||
|
||||
dictconf = {
|
||||
"save main window size" : "savewin",
|
||||
"save panes size" : "savepan",
|
||||
"main window width" : "wx",
|
||||
"save main window size": "savewin",
|
||||
"save panes size": "savepan",
|
||||
"main window width": "wx",
|
||||
"main window height": "wy",
|
||||
"horizontal panes": "h",
|
||||
"vertical panes":"v",
|
||||
@@ -128,7 +128,7 @@ class ConfigModel(Model):
|
||||
'use external image viewer':'imgview',
|
||||
'external image viewer program':'imgprog',
|
||||
}
|
||||
|
||||
|
||||
dbool = (
|
||||
'exportxls',
|
||||
'thumbs',
|
||||
@@ -148,25 +148,24 @@ class ConfigModel(Model):
|
||||
'retrive',
|
||||
'imgview',
|
||||
)
|
||||
|
||||
|
||||
recent = []
|
||||
RECENT_MAX = 10
|
||||
|
||||
|
||||
dstring = ('cd','ejectapp','imgprog')
|
||||
|
||||
|
||||
try:
|
||||
path = os.environ['HOME']
|
||||
except:
|
||||
path = "/tmp"
|
||||
|
||||
|
||||
def __init__(self):
|
||||
Model.__init__(self)
|
||||
self.category_tree = gtk.ListStore(gobject.TYPE_STRING)
|
||||
|
||||
|
||||
self.refresh_ext()
|
||||
return
|
||||
|
||||
|
||||
|
||||
def refresh_ext(self):
|
||||
self.ext_tree = gtk.ListStore(gobject.TYPE_STRING,
|
||||
gobject.TYPE_STRING)
|
||||
@@ -175,24 +174,25 @@ class ConfigModel(Model):
|
||||
myiter = self.ext_tree.insert_before(None,None)
|
||||
self.ext_tree.set_value(myiter, 0, i)
|
||||
self.ext_tree.set_value(myiter, 1, self.confd['extensions'][i])
|
||||
|
||||
|
||||
def save(self):
|
||||
try:
|
||||
os.lstat("%s/.pygtktalog" % self.path)
|
||||
except:
|
||||
print "Saving preferences to %s/.pygtktalog" % self.path
|
||||
if __debug__:
|
||||
print "m_config.py: save() Saving preferences to %s/.pygtktalog" % self.path
|
||||
print "m_config.py: save() Saving preferences to",
|
||||
print "%s/.pygtktalog" % self.path
|
||||
newIni = Ini()
|
||||
|
||||
|
||||
# main section
|
||||
newIni.add_section("pyGTKtalog conf")
|
||||
for opt in self.dictconf:
|
||||
newIni.add_key(opt,self.confd[self.dictconf[opt]])
|
||||
|
||||
|
||||
# recent section
|
||||
newIni.add_section("pyGTKtalog recent")
|
||||
|
||||
|
||||
count = 1
|
||||
max_count = self.RECENT_MAX + 1
|
||||
for opt in self.recent:
|
||||
@@ -201,26 +201,27 @@ class ConfigModel(Model):
|
||||
else:
|
||||
break
|
||||
count+=1
|
||||
|
||||
|
||||
# extensions sections
|
||||
newIni.add_section("extensions")
|
||||
count = 1
|
||||
for i in self.confd['extensions']:
|
||||
newIni.add_key(i, self.confd['extensions'][i])
|
||||
count+=1
|
||||
|
||||
|
||||
# write config
|
||||
try:
|
||||
f = open("%s/.pygtktalog" % self.path,"w")
|
||||
success = True
|
||||
except:
|
||||
if __debug__:
|
||||
print "m_config.py: save() Cannot open config file %s for writing." % (self.path, "/.pygtktalog")
|
||||
print "m_config.py: save() Cannot open config file",
|
||||
print "%s for writing." % (self.path, "/.pygtktalog")
|
||||
success = False
|
||||
f.write(newIni.show())
|
||||
f.close()
|
||||
return success
|
||||
|
||||
|
||||
def load(self):
|
||||
try:
|
||||
# try to read config file
|
||||
@@ -231,16 +232,18 @@ class ConfigModel(Model):
|
||||
for sec in parser.sections():
|
||||
if sec == 'pyGTKtalog conf':
|
||||
for opt in parser.options(sec):
|
||||
i = self.dictconf[opt]
|
||||
try:
|
||||
if self.dictconf[opt] in self.dbool:
|
||||
self.confd[self.dictconf[opt]] = parser.getboolean(sec,opt)
|
||||
self.confd[i] = parser.getboolean(sec,opt)
|
||||
elif self.dictconf[opt] in self.dstring:
|
||||
self.confd[self.dictconf[opt]] = parser.get(sec,opt)
|
||||
self.confd[i] = parser.get(sec,opt)
|
||||
else:
|
||||
self.confd[self.dictconf[opt]] = parser.getint(sec,opt)
|
||||
self.confd[i] = parser.getint(sec,opt)
|
||||
except:
|
||||
if __debug__:
|
||||
print "m_config.py: load() failed to parse option:", opt
|
||||
print "m_config.py: load() failed to parse",
|
||||
print "option:", opt
|
||||
pass
|
||||
elif sec == 'pyGTKtalog recent':
|
||||
for opt in parser.options(sec):
|
||||
@@ -248,22 +251,25 @@ class ConfigModel(Model):
|
||||
r[int(opt)] = parser.get(sec,opt)
|
||||
except:
|
||||
if __debug__:
|
||||
print "m_config.py: load() failed to parse option:", opt
|
||||
print "m_config.py: load() failed to parse",
|
||||
print "option:", opt
|
||||
pass
|
||||
elif sec == 'extensions':
|
||||
self.confd['extensions'] = {}
|
||||
for opt in parser.options(sec):
|
||||
try:
|
||||
self.confd['extensions'][opt] = parser.get(sec, opt)
|
||||
self.confd['extensions'][opt] = parser.get(sec,
|
||||
opt)
|
||||
except:
|
||||
if __debug__:
|
||||
print "m_config.py: load() failed to parse option:", opt
|
||||
print "m_config.py: load() failed to parse",
|
||||
print "option:", opt
|
||||
pass
|
||||
|
||||
|
||||
for i in range(1, self.RECENT_MAX + 1):
|
||||
if r.has_key(i):
|
||||
if i in r:
|
||||
self.recent.append(r[i])
|
||||
|
||||
|
||||
except:
|
||||
if __debug__:
|
||||
print "m_config.py: load() load config file failed"
|
||||
@@ -272,17 +278,17 @@ class ConfigModel(Model):
|
||||
def add_recent(self, path):
|
||||
if not path:
|
||||
return
|
||||
|
||||
|
||||
if path in self.recent:
|
||||
self.recent.remove(path)
|
||||
self.recent.insert(0,path)
|
||||
return
|
||||
|
||||
|
||||
self.recent.insert(0,path)
|
||||
if len(self.recent) > self.RECENT_MAX:
|
||||
self.recent = self.recent[:self.RECENT_MAX]
|
||||
return
|
||||
|
||||
|
||||
def __str__(self):
|
||||
"""show prefs in string way"""
|
||||
string = "[varname]\tvalue\n"
|
||||
|
||||
Reference in New Issue
Block a user