2007-05-20 Don Allingham <don@gramps-project.org>

* src/Lru.py: pylint fixes
	* src/DisplayState.py: pylint fixes
	* src/Errors.py: pylint fixes
	* src/DbState.py: pylint fixes



svn: r8501
This commit is contained in:
Don Allingham
2007-05-21 03:44:18 +00:00
parent a598df4f6d
commit 263ab79ad9
5 changed files with 180 additions and 86 deletions

View File

@ -1,3 +1,9 @@
2007-05-20 Don Allingham <don@gramps-project.org>
* src/Lru.py: pylint fixes
* src/DisplayState.py: pylint fixes
* src/Errors.py: pylint fixes
* src/DbState.py: pylint fixes
2007-05-19 Don Allingham <don@gramps-project.org>
* src/Bookmarks.py: pylint fixes
* src/ColumnOrder.py: pylint fixes

View File

@ -26,10 +26,12 @@ __author__ = "Donald N. Allingham"
__revision__ = "$Revision: 8032 $"
from GrampsDb import GrampsDBCallback, GrampsDbBase
import Config
class DbState(GrampsDBCallback):
"""
Provides a class to encapsulate the state of the database..
"""
__signals__ = {
'database-changed' : (GrampsDbBase, ),
@ -38,12 +40,20 @@ class DbState(GrampsDBCallback):
}
def __init__(self):
"""
Initalize the state with an empty (and useless) GrampsDbBase. This is
just a place holder until a real DB is assigned.
"""
GrampsDBCallback.__init__(self)
self.db = GrampsDbBase()
self.open = False
self.active = None
def change_active_person(self, person):
"""
Changes the active person and emits a signal to notify those who
are interested.
"""
self.active = person
if person:
try:
@ -52,18 +62,31 @@ class DbState(GrampsDBCallback):
self.emit('active-changed', ("", ))
def change_active_handle(self, handle):
"""
Changes the active person based on the person's handle
"""
self.change_active_person(self.db.get_person_from_handle(handle))
def get_active_person(self):
if self.active: # Fetch a fresh version from DB to not return a stale one
"""
Gets the current active person. Creates a new instance to make sure that
the data is active.
"""
if self.active:
self.active = self.db.get_person_from_handle(self.active.handle)
return self.active
def change_database(self, database):
"""
Closes the existing db, and opens a new one.
"""
self.db.close()
self.change_database_noclose(database)
def change_database_noclose(self, database):
"""
Changes the current database. and resets the configuration prefixes.
"""
self.db = database
self.db.set_prefixes(
Config.get(Config.IPREFIX),
@ -79,9 +102,15 @@ class DbState(GrampsDBCallback):
self.open = True
def signal_change(self):
"""
Emits the database-changed signal with the new database
"""
self.emit('database-changed', (self.db, ))
def no_database(self):
"""
Closes the database without a new database
"""
self.db.close()
self.db = GrampsDbBase()
self.active = None

View File

@ -34,7 +34,7 @@ from gettext import gettext as _
#
#-------------------------------------------------------------------------
import logging
log = logging.getLogger(".DisplayState")
__LOG = logging.getLogger(".DisplayState")
#-------------------------------------------------------------------------
#
@ -89,8 +89,8 @@ class History(GrampsDb.GrampsDBCallback):
else:
del_id = person_handle
hc = self.history.count(del_id)
for c in range(hc):
history_count = self.history.count(del_id)
for c in range(history_count):
self.history.remove(del_id)
self.index -= 1
@ -147,8 +147,8 @@ class History(GrampsDb.GrampsDBCallback):
#
#-------------------------------------------------------------------------
_rct_top = '<ui><menubar name="MenuBar"><menu action="FileMenu"><menu action="OpenRecent">'
_rct_btm = '</menu></menu></menubar></ui>'
__RCT_TOP = '<ui><menubar name="MenuBar"><menu action="FileMenu"><menu action="OpenRecent">'
__RCT_BTM = '</menu></menu></menubar></ui>'
import RecentFiles
import os
@ -168,8 +168,8 @@ class RecentDocsMenu:
self.fileopen(name, dbtype)
def build(self):
f = StringIO()
f.write(_rct_top)
buf = StringIO()
buf.write(__RCT_TOP)
gramps_rf = RecentFiles.GrampsRecentFiles()
count = 0
@ -189,7 +189,7 @@ class RecentDocsMenu:
try:
filename = os.path.basename(item.get_path()).replace('_', '__')
action_id = "RecentMenu%d" % count
f.write('<menuitem action="%s"/>' % action_id)
buf.write('<menuitem action="%s"/>' % action_id)
actions.append((action_id, None, filename, None, None,
make_callback(item, self.load)))
mitem = gtk.MenuItem(filename)
@ -200,26 +200,27 @@ class RecentDocsMenu:
pass # ignore no longer existing files
count += 1
f.write(_rct_btm)
buf.write(__RCT_BTM)
self.action_group.add_actions(actions)
self.uimanager.insert_action_group(self.action_group, 1)
self.active = self.uimanager.add_ui_from_string(f.getvalue())
self.active = self.uimanager.add_ui_from_string(buf.getvalue())
self.uimanager.ensure_update()
f.close()
buf.close()
new_menu.show()
self.uistate.set_open_recent_menu(new_menu)
def make_callback(n,f):
return lambda x: f(n)
def make_callback(val, func):
return lambda x: func(val)
def by_time(a,b):
return cmp(b.get_time(),a.get_time())
def by_time(first, second):
return cmp(second.get_time(), first.get_time())
from GrampsLogger import RotateHandler
class WarnHandler(RotateHandler):
def __init__(self, capacity, button):
RotateHandler.__init__(self, capacity)
self.setLevel(logging.WARN)
@ -242,9 +243,9 @@ class WarnHandler(RotateHandler):
def display(self, obj):
obj.hide()
g = gtk.glade.XML(const.gladeFile,'scrollmsg')
top = g.get_widget('scrollmsg')
msg = g.get_widget('msg')
xml = gtk.glade.XML(const.gladeFile, 'scrollmsg')
top = xml.get_widget('scrollmsg')
msg = xml.get_widget('msg')
buf = msg.get_buffer()
for i in self.get_formatted_log():
buf.insert_at_cursor(i + '\n')
@ -278,11 +279,11 @@ class DisplayState(GrampsDb.GrampsDBCallback):
self.last_bar = self.status.insert(min_width=15, ralign=True)
formatter = logging.Formatter('%(levelname)s %(name)s: %(message)s')
self.rh = WarnHandler(capacity=400,button=warnbtn)
self.rh.setFormatter(formatter)
self.rh.setLevel(logging.WARNING)
self.rhandler = WarnHandler(capacity=400, button=warnbtn)
self.rhandler.setFormatter(formatter)
self.rhandler.setLevel(logging.WARNING)
self.log = logging.getLogger()
self.log.addHandler(self.rh)
self.log.addHandler(self.rhandler)
# This call has been moved one level up,
# but this connection is still made!
# self.dbstate.connect('database-changed', self.db_changed)
@ -385,7 +386,7 @@ if __name__ == "__main__":
import GrampsWidgets
rh = WarnHandler(capacity=400,button=GrampsWidgets.WarnButton())
log = logging.getLogger()
log.setLevel(logging.WARN)
log.addHandler(rh)
rhandler = WarnHandler(capacity=400, button=GrampsWidgets.WarnButton())
__LOG = logging.getLogger()
__LOG.setLevel(logging.WARN)
__LOG.addHandler(rhandler)

View File

@ -20,6 +20,10 @@
# $Id$
"""
Provide Error objects
"""
class FilterError(Exception):
"""Error used to report Filter errors"""
def __init__(self, value, value2=""):
@ -28,9 +32,11 @@ class FilterError(Exception):
self.value2 = value2
def __str__(self):
"Return string representation"
return self.value
def messages(self):
"Return the messages"
return (self.value, self.value2)
class DateError(Exception):
@ -40,6 +46,7 @@ class DateError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class DatabaseError(Exception):
@ -49,6 +56,7 @@ class DatabaseError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class ReportError(Exception):
@ -59,9 +67,11 @@ class ReportError(Exception):
self.value2 = value2
def __str__(self):
"Return string representation"
return self.value
def messages(self):
"Return the messages"
return (self.value, self.value2)
class GedcomError(Exception):
@ -71,6 +81,7 @@ class GedcomError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class PluginError(Exception):
@ -80,6 +91,7 @@ class PluginError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class HandleError(Exception):
@ -89,6 +101,7 @@ class HandleError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class GConfSchemaError(Exception):
@ -98,9 +111,9 @@ class GConfSchemaError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class WindowActiveError(Exception):
"""Error used to report that the request window is already displayed."""
def __init__(self, value):
@ -108,6 +121,7 @@ class WindowActiveError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class UnavailableError(Exception):
@ -116,6 +130,7 @@ class UnavailableError(Exception):
self.value = value
def __str__(self):
"Return string representation"
return self.value
class MaskError(Exception):

View File

@ -17,13 +17,23 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Least recently used algorithm
"""
class Node:
"""
Node to be stored in the LRU structure
"""
def __init__(self, prev, me):
self.prev = prev
self.me = me
self.next = None
class LRU: #Implementation of a length-limited O(1) LRU cache
class LRU:
"""
Implementation of a length-limited O(1) LRU cache
"""
def __init__(self, count):
self.count = max(count, 2)
self.d = {}
@ -31,12 +41,21 @@ class LRU: #Implementation of a length-limited O(1) LRU cache
self.last = None
def __contains__(self, obj):
"""
Returns True if the object is contained in the LRU
"""
return obj in self.d
def __getitem__(self, obj):
"""
Returns item assocated with Obj
"""
return self.d[obj].me[1]
def __setitem__(self, obj, val):
"""
Sets the item in the LRU, removing an old entry if needed
"""
if obj in self.d:
del self[obj]
nobj = Node(self.last, (obj, val))
@ -59,6 +78,9 @@ class LRU: #Implementation of a length-limited O(1) LRU cache
del a
def __delitem__(self, obj):
"""
Delete the object from the LRU
"""
nobj = self.d[obj]
if nobj.prev:
nobj.prev.next = nobj.next
@ -71,6 +93,9 @@ class LRU: #Implementation of a length-limited O(1) LRU cache
del self.d[obj]
def __iter__(self):
"""
Iterate over the LRU
"""
cur = self.first
while cur != None:
cur2 = cur.next
@ -79,6 +104,9 @@ class LRU: #Implementation of a length-limited O(1) LRU cache
raise StopIteration
def iteritems(self):
"""
Return items in the LRU using a generator
"""
cur = self.first
while cur != None:
cur2 = cur.next
@ -87,17 +115,32 @@ class LRU: #Implementation of a length-limited O(1) LRU cache
raise StopIteration
def iterkeys(self):
"""
Return keys in the LRU using a generator
"""
return iter(self.d)
def itervalues(self):
for i,j in self.iteritems():
yield j
"""
Return items and keys in the LRU using a generator
"""
for data in self.iteritems():
yield data[1]
def keys(self):
return [i for i,j in self.iteritems()]
"""
Return all keys
"""
return [data[0] for data in self.iteritems()]
def values(self):
return [j for i,j in self.iteritems()]
"""
Return all values
"""
return [data[1] for data in self.iteritems()]
def items(self):
return [i for i in self.iteritems()]
"""
Return all items
"""
return [data[0] for data in self.iteritems()]