Further pylint improvements

This commit is contained in:
Nick Hall 2016-01-09 00:25:43 +00:00
parent 831330b6d0
commit c3eab4e6e3
6 changed files with 275 additions and 64 deletions

View File

@ -47,14 +47,13 @@ class BaseObject(object):
"""
Convert the object to a serialized tuple of data.
"""
assert False, "Needs to be overridden in the derived class"
raise NotImplementedError
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
assert False, "Needs to be overridden in the derived class"
return self
raise NotImplementedError
def to_struct(self):
"""
@ -75,7 +74,7 @@ class BaseObject(object):
:returns: Returns a struct containing the data of the object.
"""
assert False, "Needs to be overridden in the derived class"
raise NotImplementedError
def from_struct(self, struct):
"""
@ -91,7 +90,7 @@ class BaseObject(object):
:returns: Returns an object of this type.
"""
assert False, "Needs to be overridden in the derived class"
raise NotImplementedError
def matches_string(self, pattern, case_sensitive=False):
"""
@ -109,9 +108,6 @@ class BaseObject(object):
# Run through its own items
patern_upper = pattern.upper()
for item in self.get_text_data_list():
# Some items are strings, which will fail in item.upper(), and some items are unicode.
# Convert all items to unicode and the item.upper().find(patern_upper) will work OK.
item = str(item)
if not item:
continue
if case_sensitive:
@ -228,5 +224,8 @@ class BaseObject(object):
@classmethod
def create(cls, data):
"""
Create a new instance from serialized data.
"""
if data:
return cls().unserialize(data)

View File

@ -138,15 +138,14 @@ class CitationBase(object):
:param citation_handle_list: The list of citation handles to be removed
:type handle: list
"""
LOG.debug('enter remove_citation handle: %s self: %s citation_list: %s'
% (citation_handle_list, self, self.citation_list))
LOG.debug('enter remove_citation handle: %s self: %s citation_list: %s',
citation_handle_list, self, self.citation_list)
for handle in citation_handle_list:
if handle in self.citation_list:
LOG.debug('remove handle %s from citation_list %s' %
(handle, self.citation_list))
LOG.debug('remove handle %s from citation_list %s',
handle, self.citation_list)
self.citation_list.remove(handle)
LOG.debug('get_citation_child_list %s' %
self.get_citation_child_list())
LOG.debug('get_citation_child_list %s', self.get_citation_child_list())
for item in self.get_citation_child_list():
item.remove_citation_references(citation_handle_list)
@ -182,13 +181,13 @@ class CitationBase(object):
:returns: The list of :class:`~.citation.Citation` handles
:rtype: list
"""
list = self.citation_list
all_citations = self.citation_list
for item in self.get_citation_child_list():
list += item.get_citation_list()
all_citations += item.get_citation_list()
for subitem in item.get_citation_child_list():
list += subitem.get_citation_list()
return list
all_citations += subitem.get_citation_list()
return all_citations
def has_citation_reference(self, citation_handle):
"""
@ -205,7 +204,7 @@ class CitationBase(object):
if citation_ref == citation_handle:
return True
LOG.debug("citation child list %s" % self.get_citation_child_list())
LOG.debug("citation child list %s", self.get_citation_child_list())
for item in self.get_citation_child_list():
if item.has_citation_reference(citation_handle):
return True
@ -337,3 +336,16 @@ class IndirectCitationBase(object):
"""
return []
def get_citation_child_list(self):
"""
Return the list of child secondary objects that may refer citations.
All methods which inherit from CitationBase and have other child objects
with citations, should return here a list of child objects which are
CitationBase
:returns: Returns the list of child secondary child objects that may
refer citations.
:rtype: list
"""
return []

View File

@ -30,8 +30,6 @@ LDS Ordinance class for Gramps.
# Python modules
#
#-------------------------------------------------------------------------
from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
from warnings import warn
#-------------------------------------------------------------------------
@ -46,6 +44,8 @@ from .datebase import DateBase
from .placebase import PlaceBase
from .privacybase import PrivacyBase
from .const import IDENTICAL, EQUAL, DIFFERENT
from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
#-------------------------------------------------------------------------
#
@ -183,8 +183,10 @@ class LdsOrd(SecondaryObject, CitationBase, NoteBase,
:returns: Returns a serialized object
"""
default = LdsOrd()
return (CitationBase.from_struct(struct.get("citation_list", default.citation_list)),
NoteBase.from_struct(struct.get("note_list", default.note_list)),
return (CitationBase.from_struct(struct.get("citation_list",
default.citation_list)),
NoteBase.from_struct(struct.get("note_list",
default.note_list)),
DateBase.from_struct(struct.get("date", {})),
struct.get("type", {}),
struct.get("place", default.place),

View File

@ -71,6 +71,69 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
else:
self.gramps_id = None
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
struct) that represents all the data elements.
This method is used to recursively convert the object into a
self-documenting form that can easily be used for various
purposes, including diffs and queries.
These structures may be primitive Python types (string,
integer, boolean, etc.) or complex Python types (lists,
tuples, or dicts). If the return type is a dict, then the keys
of the dict match the fieldname of the object. If the return
struct (or value of a dict key) is a list, then it is a list
of structs. Otherwise, the struct is just the value of the
attribute.
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
These structures may be primitive Python types (string,
integer, boolean, etc.) or complex Python types (lists,
tuples, or dicts). If the return type is a dict, then the keys
of the dict match the fieldname of the object. If the return
struct (or value of a dict key) is a list, then it is a list
of structs. Otherwise, the struct is just the value of the
attribute.
:returns: Returns an object of this type.
"""
raise NotImplementedError
@classmethod
def get_labels(cls, _):
"""
Return labels.
"""
raise NotImplementedError
@classmethod
def get_schema(cls):
"""
Return schema.
"""
raise NotImplementedError
@classmethod
def get_label(cls, field, _):
"""
@ -83,7 +146,8 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
if chain[-1] in labels:
return labels[chain[-1]]
else:
raise Exception("%s has no such label on %s: '%s'" % (cls, path, field))
raise Exception("%s has no such label on %s: '%s'" %
(cls, path, field))
@classmethod
def get_field_type(cls, field):
@ -146,7 +210,8 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
elif ignore_errors:
return
else:
raise Exception("%s is not a valid field of %s; use %s" % (part, path, dir(path)))
raise Exception("%s is not a valid field of %s; use %s" %
(part, path, dir(path)))
return path
def set_field(self, field, value, ignore_errors=False):
@ -221,6 +286,15 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
"""
pass
def has_citation_reference(self, handle):
"""
Indicate if the object has a citation references.
In the base class, no such references exist. Derived classes should
override this if they provide citation references.
"""
return False
def has_media_reference(self, handle):
"""
Indicate if the object has a media references.
@ -232,10 +306,10 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
def remove_citation_references(self, handle_list):
"""
Remove the specified source references from the object.
Remove the specified citation references from the object.
In the base class no such references exist. Derived classes should
override this if they provide source references.
override this if they provide citation references.
"""
pass
@ -249,9 +323,17 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
pass
def replace_citation_references(self, old_handle, new_handle):
"""
Replace all references to the old citation handle with those to the new
citation handle.
"""
pass
def replace_media_references(self, old_handle, new_handle):
"""
Replace all references to the old media handle with those to the new
media handle.
"""
pass
#-------------------------------------------------------------------------
@ -283,6 +365,55 @@ class PrimaryObject(BasicPrimaryObject):
"""
BasicPrimaryObject.__init__(self, source)
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
struct) that represents all the data elements.
This method is used to recursively convert the object into a
self-documenting form that can easily be used for various
purposes, including diffs and queries.
These structures may be primitive Python types (string,
integer, boolean, etc.) or complex Python types (lists,
tuples, or dicts). If the return type is a dict, then the keys
of the dict match the fieldname of the object. If the return
struct (or value of a dict key) is a list, then it is a list
of structs. Otherwise, the struct is just the value of the
attribute.
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
These structures may be primitive Python types (string,
integer, boolean, etc.) or complex Python types (lists,
tuples, or dicts). If the return type is a dict, then the keys
of the dict match the fieldname of the object. If the return
struct (or value of a dict key) is a list, then it is a list
of structs. Otherwise, the struct is just the value of the
attribute.
:returns: Returns an object of this type.
"""
raise NotImplementedError
def has_handle_reference(self, classname, handle):
"""
Return True if the object has reference to a given handle of given

View File

@ -71,10 +71,22 @@ class RefBase(object):
objects.
:rtype: list
"""
assert False, "Must be overridden in the derived class"
raise NotImplementedError
def set_reference_handle(self, val):
self.ref = val
def set_reference_handle(self, handle):
"""
Set the reference handle.
:param handle: The reference handle.
:type handle: str
"""
self.ref = handle
def get_reference_handle(self):
"""
Return the reference handle.
:returns: The reference handle.
:rtype: str
"""
return self.ref

View File

@ -40,6 +40,55 @@ class SecondaryObject(BaseObject):
database.
"""
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
struct) that represents all the data elements.
This method is used to recursively convert the object into a
self-documenting form that can easily be used for various
purposes, including diffs and queries.
These structures may be primitive Python types (string,
integer, boolean, etc.) or complex Python types (lists,
tuples, or dicts). If the return type is a dict, then the keys
of the dict match the fieldname of the object. If the return
struct (or value of a dict key) is a list, then it is a list
of structs. Otherwise, the struct is just the value of the
attribute.
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
These structures may be primitive Python types (string,
integer, boolean, etc.) or complex Python types (lists,
tuples, or dicts). If the return type is a dict, then the keys
of the dict match the fieldname of the object. If the return
struct (or value of a dict key) is a list, then it is a list
of structs. Otherwise, the struct is just the value of the
attribute.
:returns: Returns an object of this type.
"""
raise NotImplementedError
def is_equal(self, source):
return self.serialize() == source.serialize()
@ -51,6 +100,13 @@ class SecondaryObject(BaseObject):
"""
pass
@classmethod
def get_labels(cls, _):
"""
Return labels.
"""
raise NotImplementedError
def get_label(self, field, _):
"""
Get the associated label given a field name of this object.
@ -67,4 +123,3 @@ class SecondaryObject(BaseObject):
return labels[chain[-1]]
else:
raise Exception("%s has no such label: '%s'" % (self, field))