From b8a38cd5e5810820ee2daba013d8a22e1cbf90f2 Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Sat, 9 Jan 2016 15:34:00 +0000 Subject: [PATCH] Improve pylint score to above 9 for most gen.lib objects --- gramps/gen/lib/attrbase.py | 4 +- gramps/gen/lib/gcalendar.py | 92 +++++++++++++++++----------------- gramps/gen/lib/primaryobj.py | 4 +- gramps/gen/lib/secondaryobj.py | 2 +- gramps/gen/lib/srcattribute.py | 16 ------ gramps/gen/lib/styledtext.py | 56 ++++++++++++--------- gramps/gen/lib/tableobj.py | 49 ++++++++++++++++++ 7 files changed, 131 insertions(+), 92 deletions(-) diff --git a/gramps/gen/lib/attrbase.py b/gramps/gen/lib/attrbase.py index 6d63f9b0e..c2d526969 100644 --- a/gramps/gen/lib/attrbase.py +++ b/gramps/gen/lib/attrbase.py @@ -28,7 +28,7 @@ AttributeRootBase class for Gramps. # Gramps modules # #------------------------------------------------------------------------- -from .attribute import Attribute +from .attribute import Attribute, AttributeRoot from .srcattribute import SrcAttribute from .const import IDENTICAL, EQUAL @@ -41,7 +41,7 @@ class AttributeRootBase(object): """ Base class for attribute-aware objects. """ - _CLASS = None + _CLASS = AttributeRoot def __init__(self, source=None): """ diff --git a/gramps/gen/lib/gcalendar.py b/gramps/gen/lib/gcalendar.py index 4af744751..503eeaf0a 100644 --- a/gramps/gen/lib/gcalendar.py +++ b/gramps/gen/lib/gcalendar.py @@ -34,30 +34,30 @@ import math # Constants # #------------------------------------------------------------------------- -_GRG_SDN_OFFSET = 32045 -_GRG_DAYS_PER_5_MONTHS = 153 -_GRG_DAYS_PER_4_YEARS = 1461 +_GRG_SDN_OFFSET = 32045 +_GRG_DAYS_PER_5_MONTHS = 153 +_GRG_DAYS_PER_4_YEARS = 1461 _GRG_DAYS_PER_400_YEARS = 146097 -_JLN_SDN_OFFSET = 32083 -_JLN_DAYS_PER_5_MONTHS = 153 -_JLN_DAYS_PER_4_YEARS = 1461 +_JLN_SDN_OFFSET = 32083 +_JLN_DAYS_PER_5_MONTHS = 153 +_JLN_DAYS_PER_4_YEARS = 1461 -_HBR_HALAKIM_PER_HOUR = 1080 -_HBR_HALAKIM_PER_DAY = 25920 -_HBR_HALAKIM_PER_LUNAR_CYCLE = 29 * _HBR_HALAKIM_PER_DAY + 13753 +_HBR_HALAKIM_PER_HOUR = 1080 +_HBR_HALAKIM_PER_DAY = 25920 +_HBR_HALAKIM_PER_LUNAR_CYCLE = 29 * _HBR_HALAKIM_PER_DAY + 13753 _HBR_HALAKIM_PER_METONIC_CYCLE = _HBR_HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7) -_HBR_SDN_OFFSET = 347997 -_HBR_NEW_MOON_OF_CREATION = 31524 -_HBR_NOON = 18 * _HBR_HALAKIM_PER_HOUR -_HBR_AM3_11_20 = (9 * _HBR_HALAKIM_PER_HOUR) + 204 -_HBR_AM9_32_43 = (15 * _HBR_HALAKIM_PER_HOUR) + 589 +_HBR_SDN_OFFSET = 347997 +_HBR_NEW_MOON_OF_CREATION = 31524 +_HBR_NOON = 18 * _HBR_HALAKIM_PER_HOUR +_HBR_AM3_11_20 = (9 * _HBR_HALAKIM_PER_HOUR) + 204 +_HBR_AM9_32_43 = (15 * _HBR_HALAKIM_PER_HOUR) + 589 -_HBR_SUNDAY = 0 -_HBR_MONDAY = 1 -_HBR_TUESDAY = 2 +_HBR_SUNDAY = 0 +_HBR_MONDAY = 1 +_HBR_TUESDAY = 2 _HBR_WEDNESDAY = 3 -_HBR_FRIDAY = 5 +_HBR_FRIDAY = 5 _HBR_MONTHS_PER_YEAR = [ 12, 12, 13, 12, 12, 13, 12, 13, 12, 12, @@ -69,25 +69,25 @@ _HBR_YEAR_OFFSET = [ 136, 148, 160, 173, 185, 197, 210, 222 ] -_FR_SDN_OFFSET = 2375474 -_FR_DAYS_PER_4_YEARS = 1461 -_FR_DAYS_PER_MONTH = 30 -_PRS_EPOCH = 1948320.5 -_ISM_EPOCH = 1948439.5 +_FR_SDN_OFFSET = 2375474 +_FR_DAYS_PER_4_YEARS = 1461 +_FR_DAYS_PER_MONTH = 30 +_PRS_EPOCH = 1948320.5 +_ISM_EPOCH = 1948439.5 def _tishri1(metonic_year, molad_day, molad_halakim): tishri1 = molad_day dow = tishri1 % 7 - leap_year = metonic_year in [ 2, 5, 7, 10, 13, 16, 18] - last_was_leap_year = metonic_year in [ 3, 6, 8, 11, 14, 17, 0] + leap_year = metonic_year in [2, 5, 7, 10, 13, 16, 18] + last_was_leap_year = metonic_year in [3, 6, 8, 11, 14, 17, 0] # Apply rules 2, 3 and 4. if ((molad_halakim >= _HBR_NOON) or - ((not leap_year) and dow == _HBR_TUESDAY and - molad_halakim >= _HBR_AM3_11_20) or - (last_was_leap_year and dow == _HBR_MONDAY - and molad_halakim >= _HBR_AM9_32_43)) : + ((not leap_year) and dow == _HBR_TUESDAY and + molad_halakim >= _HBR_AM3_11_20) or + (last_was_leap_year and dow == _HBR_MONDAY + and molad_halakim >= _HBR_AM9_32_43)): tishri1 += 1 dow += 1 if dow == 7: @@ -133,8 +133,8 @@ def _tishri_molad(input_day): break molad_halakim += (_HBR_HALAKIM_PER_LUNAR_CYCLE - * _HBR_MONTHS_PER_YEAR[metonic_year]) - molad_day += molad_halakim // _HBR_HALAKIM_PER_DAY + * _HBR_MONTHS_PER_YEAR[metonic_year]) + molad_day += molad_halakim // _HBR_HALAKIM_PER_DAY molad_halakim = molad_halakim % _HBR_HALAKIM_PER_DAY return (metonic_cycle, metonic_year, molad_day, molad_halakim) @@ -182,9 +182,9 @@ def _start_of_year(year): molad_day = molad_day + (molad_halakim // _HBR_HALAKIM_PER_DAY) molad_halakim = molad_halakim % _HBR_HALAKIM_PER_DAY - pTishri1 = _tishri1(metonic_year, molad_day, molad_halakim) + ptishri1 = _tishri1(metonic_year, molad_day, molad_halakim) - return (metonic_cycle, metonic_year, molad_day, molad_halakim, pTishri1) + return (metonic_cycle, metonic_year, molad_day, molad_halakim, ptishri1) def hebrew_sdn(year, month, day): """Convert a Jewish calendar date to an SDN number.""" @@ -225,16 +225,16 @@ def hebrew_sdn(year, month, day): molad_day, molad_halakim, tishri1_after) = _start_of_year(year+1) if _HBR_MONTHS_PER_YEAR[(year - 1) % 19] == 12: - length_of_adarI_andII = 29 + length_of_adar_1and2 = 29 else: - length_of_adarI_andII = 59 + length_of_adar_1and2 = 59 if month == 4: - sdn = tishri1_after + day - length_of_adarI_andII - 237 + sdn = tishri1_after + day - length_of_adar_1and2 - 237 elif month == 5: - sdn = tishri1_after + day - length_of_adarI_andII - 208 + sdn = tishri1_after + day - length_of_adar_1and2 - 208 else: - sdn = tishri1_after + day - length_of_adarI_andII - 178 + sdn = tishri1_after + day - length_of_adar_1and2 - 178 else: # It is Adar II or later - don't need the year length. (metonic_cycle, metonic_year, @@ -284,7 +284,7 @@ def hebrew_ymd(sdn): # Tishri 1 of the next year. halakim += (_HBR_HALAKIM_PER_LUNAR_CYCLE - * _HBR_MONTHS_PER_YEAR[metonic_year]) + * _HBR_MONTHS_PER_YEAR[metonic_year]) day1 += halakim // _HBR_HALAKIM_PER_DAY halakim = halakim % _HBR_HALAKIM_PER_DAY tishri1_after = _tishri1((metonic_year + 1) % 19, day1, halakim) @@ -348,7 +348,7 @@ def hebrew_ymd(sdn): year_length = tishri1_after - tishri1 day = input_day - tishri1 - 29 - if year_length == 355 or year_length == 385 : + if year_length == 355 or year_length == 385: # Heshvan has 30 days if day <= 30: month = 2 @@ -378,7 +378,7 @@ def julian_sdn(year, month, day): month -= 3 else: month += 9 - year -= 1 + year -= 1 return (year * _JLN_DAYS_PER_4_YEARS) // 4 \ + (month * _JLN_DAYS_PER_5_MONTHS + 2) // 5 \ @@ -423,13 +423,13 @@ def gregorian_sdn(year, month, day): month -= 3 else: month += 9 - year -= 1 + year -= 1 return(((year // 100) * _GRG_DAYS_PER_400_YEARS) // 4 + ((year % 100) * _GRG_DAYS_PER_4_YEARS) // 4 + (month * _GRG_DAYS_PER_5_MONTHS + 2) // 5 + day - - _GRG_SDN_OFFSET ) + - _GRG_SDN_OFFSET) def gregorian_ymd(sdn): """Convert an SDN number to a gregorian date.""" @@ -449,7 +449,7 @@ def gregorian_ymd(sdn): day = (temp % _GRG_DAYS_PER_5_MONTHS) // 5 + 1 # Convert to the normal beginning of the year - if month < 10 : + if month < 10: month = month + 3 else: year = year + 1 @@ -560,12 +560,12 @@ def swedish_sdn(year, month, day): def swedish_ymd(sdn): """Convert an SDN number to a Swedish calendar date.""" if sdn == 2346425: - return (1712, 2, 30) + return (1712, 2, 30) # Swedish Calendar elif 2342042 <= sdn < 2346425: return julian_ymd(sdn+1) # Gregorian Calendar (1753-03-01) - elif 2361390 <= sdn: + elif sdn >= 2361390: return gregorian_ymd(sdn) else: return julian_ymd(sdn) diff --git a/gramps/gen/lib/primaryobj.py b/gramps/gen/lib/primaryobj.py index 05663a12b..622c5275d 100644 --- a/gramps/gen/lib/primaryobj.py +++ b/gramps/gen/lib/primaryobj.py @@ -125,14 +125,14 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase): """ Return labels. """ - raise NotImplementedError + return {} @classmethod def get_schema(cls): """ Return schema. """ - raise NotImplementedError + return {} @classmethod def get_label(cls, field, _): diff --git a/gramps/gen/lib/secondaryobj.py b/gramps/gen/lib/secondaryobj.py index df232b344..03162de01 100644 --- a/gramps/gen/lib/secondaryobj.py +++ b/gramps/gen/lib/secondaryobj.py @@ -105,7 +105,7 @@ class SecondaryObject(BaseObject): """ Return labels. """ - raise NotImplementedError + return {} def get_label(self, field, _): """ diff --git a/gramps/gen/lib/srcattribute.py b/gramps/gen/lib/srcattribute.py index ce4d1836c..a30c6c727 100644 --- a/gramps/gen/lib/srcattribute.py +++ b/gramps/gen/lib/srcattribute.py @@ -53,19 +53,3 @@ class SrcAttribute(AttributeRoot): else: self.type = SrcAttributeType() self.value = "" - - - def get_text_data_list(self): - """ - Return the list of all textual attributes of the object. - - :returns: Returns the list of all textual attributes of the object. - :rtype: list - """ - sat = SrcAttributeType() - if self.type == sat.SRCTYPE: - #we convert to the native language if possible - if self.value and self.value in sat.E2I_SRCTYPEMAP: - return [sat.I2S_SRCTYPEMAP[sat.E2I_SRCTYPEMAP[self.value]]] - return [self.value] - diff --git a/gramps/gen/lib/styledtext.py b/gramps/gen/lib/styledtext.py index f9bc4583e..f084f752e 100644 --- a/gramps/gen/lib/styledtext.py +++ b/gramps/gen/lib/styledtext.py @@ -88,8 +88,11 @@ class StyledText(object): # special methods - def __str__(self): return self._string.__str__() - def __repr__(self): return self._string.__repr__() + def __str__(self): + return self._string.__str__() + + def __repr__(self): + return self._string.__repr__() def __add__(self, other): """Implement '+' operation on the class. @@ -104,12 +107,12 @@ class StyledText(object): if isinstance(other, StyledText): # need to join strings and merge tags - for tag in other._tags: + for tag in other.tags: tag.ranges = [(start + offset, end + offset) for (start, end) in tag.ranges] - return self.__class__("".join([self._string, other._string]), - self._tags + other._tags) + return self.__class__("".join([self._string, other.string]), + self._tags + other.tags) elif isinstance(other, str): # tags remain the same, only text becomes longer return self.__class__("".join([self._string, other]), self._tags) @@ -118,13 +121,13 @@ class StyledText(object): self._tags) def __eq__(self, other): - return self._string == other._string and self._tags == other._tags + return self._string == other.string and self._tags == other.tags def __ne__(self, other): - return self._string != other._string or self._tags != other._tags + return self._string != other.string or self._tags != other.tags def __lt__(self, other): - return self._string < other._string + return self._string < other.string def __le__(self, other): return self.__lt__(other) or self.__eq__(other) @@ -138,24 +141,25 @@ class StyledText(object): def __mod__(self, other): """Implement '%' operation on the class.""" - # test whether the formatting operation is valid at all + # This will raise an exception if the formatting operation is invalid self._string % other result = self.__class__(self._string, self._tags) - i0 = 0 + start = 0 while True: - i1 = result._string.find('%', i0) - if i1 < 0: + idx1 = result.string.find('%', start) + if idx1 < 0: break - if result._string[i1+1] == '(': - i2 = result._string.find(')', i1+3) - param_name = result._string[i1+2:i2] + if result.string[idx1+1] == '(': + idx2 = result.string.find(')', idx1+3) + param_name = result.string[idx1+2:idx2] else: - i2 = i1 + idx2 = idx1 param_name = None - for i3 in range(i2+1, len(result._string)): - if result._string[i3] in 'diouxXeEfFgGcrs%': + end = idx2 + 1 + for end in range(idx2+1, len(result.string)): + if result.string[end] in 'diouxXeEfFgGcrs%': break if param_name is not None: param = other[param_name] @@ -165,10 +169,12 @@ class StyledText(object): else: param = other if not isinstance(param, StyledText): - param = StyledText('%' + result._string[i2+1:i3+1] % param) - (before, after) = result.split(result._string[i1:i3+1], 1) - result = before + param + after - i0 = i3 + 1 + param_type = '%' + result.string[idx2+1:end+1] + param = StyledText(param_type % param) + parts = result.split(result.string[idx1:end+1], 1) + if len(parts) == 2: + result = parts[0] + param + parts[1] + start = end + 1 return result @@ -194,7 +200,7 @@ class StyledText(object): for text in seq: if isinstance(text, StyledText): - for tag in text._tags: + for tag in text.tags: tag.ranges = [(start + offset, end + offset) for (start, end) in tag.ranges] new_tags += [tag] @@ -320,8 +326,8 @@ class StyledText(object): """ default = StyledText() return (struct.get("string", default.string), - [StyledTextTag.from_struct(t) - for t in struct.get("tags", default.tags)]) + [StyledTextTag.from_struct(t) + for t in struct.get("tags", default.tags)]) def unserialize(self, data): """ diff --git a/gramps/gen/lib/tableobj.py b/gramps/gen/lib/tableobj.py index 24738e150..90411b054 100644 --- a/gramps/gen/lib/tableobj.py +++ b/gramps/gen/lib/tableobj.py @@ -78,6 +78,55 @@ class TableObject(BaseObject): self.handle = None self.change = 0 + 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 get_change_time(self): """ Return the time that the data was last changed.