From 98473ea46b06273fa321c841460776cf99047917 Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Wed, 9 Jun 2010 20:06:55 +0000 Subject: [PATCH] Optimize by combining many write calls into one plus other minor tweaks svn: r15546 --- src/plugins/view/geoview.py | 683 ++++++++++++++++++------------------ 1 file changed, 345 insertions(+), 338 deletions(-) diff --git a/src/plugins/view/geoview.py b/src/plugins/view/geoview.py index 5baadc728..c1af30978 100644 --- a/src/plugins/view/geoview.py +++ b/src/plugins/view/geoview.py @@ -132,20 +132,60 @@ MRU_BTM = [ #------------------------------------------------------------------------- # -# Javascript template +# Constants # #------------------------------------------------------------------------- -_HTMLHEADER = ''' - - - - This is used to pass messages between javascript and python - - %(css)s\n") - self.mapview.write("\n") - self.mapview.write("\n") + self.mapview.write( + "\n" + + "\n"+ + "\n" + ) self.years.hide() if h4mess: self.mapview.write("

%s

\n" % h4mess) @@ -1680,47 +1686,45 @@ class GeoView(HtmlView): self.yearint, self.maxyear) self.years.show() - self.mapview.write("
\n") - self.mapview.write("
\n" % - (self.height - self.header_size )) - self.mapview.write("\n" % upath) - self.mapview.write("\n" % lang ) + upath = urlparse.urlunsplit( + ('file', '', URL_SEP.join(fpath.split(os.sep)), '', '') + ) + self.mapview.write( + '\n' % upath + + '\n' + ) def _createmapstractiontrailer(self): """ @@ -1757,9 +1764,6 @@ class GeoView(HtmlView): """ self.mapview.write(_HTMLTRAILER) - # _HTMLTRAILER.format( - # ) - # ) self.mapview.close() def _set_center_and_zoom(self, ptype): @@ -1783,13 +1787,13 @@ class GeoView(HtmlView): signmaxlon = _get_sign(self.maxlon) signmaxlat = _get_sign(self.maxlat) if signminlon == signmaxlon: - maxlong = abs(abs(self.minlon)-abs(self.maxlon)) + maxlong = abs(abs(self.minlon) - abs(self.maxlon)) else: - maxlong = abs(abs(self.minlon)+abs(self.maxlon)) + maxlong = abs(abs(self.minlon) + abs(self.maxlon)) if signminlat == signmaxlat: - maxlat = abs(abs(self.minlat)-abs(self.maxlat)) + maxlat = abs(abs(self.minlat) - abs(self.maxlat)) else: - maxlat = abs(abs(self.minlat)+abs(self.maxlat)) + maxlat = abs(abs(self.minlat) + abs(self.maxlat)) # Calculate the zoom. all places must be displayed on the map. zoomlat = _get_zoom_lat(maxlat) zoomlong = _get_zoom_long(maxlong) @@ -1865,9 +1869,8 @@ class GeoView(HtmlView): for page in range(0, pages, 1): self.nbpages += 1 ftype = {1:'P', 2:'E', 3:'F', 4:'I'}.get(ptype, 'X') - filename = os.path.join(GEOVIEW_SUBPATH, - "GeoV-%c-%05d.html" % - (ftype, self.nbpages)) + filename = os.path.join( + GEOVIEW_SUBPATH, "GeoV-%c-%05d.html" % (ftype, self.nbpages)) if self.nbpages == 1: self.htmlfile = filename self._createmapstractionheader(filename) @@ -1905,8 +1908,8 @@ class GeoView(HtmlView): elif displaytype == "event": self._createmapstractionevents(self.dbstate) else: - self._createmapstractionheader(os.path.join(GEOVIEW_SUBPATH, - "error.html")) + self._createmapstractionheader( + os.path.join(GEOVIEW_SUBPATH, "error.html")) self._createmapnotimplemented() self._createmapstractiontrailer() @@ -1923,11 +1926,7 @@ class GeoView(HtmlView): """ Create a list of places with coordinates. """ - found = 0 - for place_info in self.place_list: - if place_info[0] == place: - found = 1 - break + found = any(p[0] == place for p in self.place_list) if not found: self.nbplaces += 1 self.place_list.append([place, name, evttype, lat, @@ -1965,12 +1964,14 @@ class GeoView(HtmlView): if differtype: # in case multiple evts value = 'gramps-geo-default' # we use default icon. if ( value == "gramps-geo-default" ): - value = value.replace("default","\" + default_icon + \"") + value = value.replace("default",'" + default_icon + "') ipath = os.path.join(const.ROOT_DIR, 'images/22x22/', '%s.png' % value ) upath = urlparse.urlunsplit(('file', '', URL_SEP.join(ipath.split(os.sep)), '', '')) - self.mapview.write("my_marker.setIcon(\"%s\",[22,22],[0,22]);" % upath) - self.mapview.write("my_marker.setShadowIcon(\"%s\",[0,0]);" % upath) + self.mapview.write( + 'my_marker.setIcon("%s",[22,22],[0,22]);' % upath + + 'my_marker.setShadowIcon("%s",[0,0]);' % upath + ) def _show_title(self, title): """ @@ -1993,17 +1994,21 @@ class GeoView(HtmlView): self.mapview.write(" function setcenterandzoom(map,uzoom,ulat,ulon){\n") if self.mustcenter: self.centered = True - self.mapview.write(" var point = new LatLonPoint") - self.mapview.write("(ulat,ulon);") - self.mapview.write("map.setCenterAndZoom") - self.mapview.write("(point, uzoom);\n") + self.mapview.write( + " var point = new LatLonPoint" + "(ulat,ulon);" + "map.setCenterAndZoom" + "(point, uzoom);\n" + ) self.setattr = False - self.mapview.write("}\n") - self.mapview.write(" function setmarkers(map) {\n") - self.mapview.write(" if ( args.map != \"openstreetmap\" ) {") - self.mapview.write(" default_icon = \"altmap\";") - self.mapview.write(" } else { ") - self.mapview.write(" default_icon = \"mainmap\"; }\n") + self.mapview.write( + '}\n' + ' function setmarkers(map) {\n' + ' if ( args.map != "openstreetmap" ) {' + ' default_icon = "altmap";' + ' } else { ' + ' default_icon = "mainmap"; }\n' + ) differtype = False savetype = None index_mark = 0 @@ -2012,24 +2017,25 @@ class GeoView(HtmlView): index_mark += 1 if index_mark < self.last_index: continue - if ( indm >= firstm ) and ( indm <= lastm ): + if lastm >= indm >= firstm: ininterval = True if ininterval: - current = { - 2 : [mark[3], mark[4]], + current = {2 : [mark[3], mark[4]], }.get(formatype, mark[0]) if last != current: if not divclose: if ininterval: - self.mapview.write("\");") + self.mapview.write('");') divclose = True years = "" if mark[2]: for year in self.yearinmarker: years += "%d " % year years += "end" - self.mapview.write("my_marker.setAttribute") - self.mapview.write("('year','%s');" % years) + self.mapview.write( + "my_marker.setAttribute" + + "('year','%s');" % years + ) self.yearinmarker = [] self._set_icon(savetype, differtype, formatype) differtype = False @@ -2038,28 +2044,31 @@ class GeoView(HtmlView): if (indm % NB_MARKERS_PER_PAGE) == 0: self.last_index = index_mark ininterval = False - last = { - 2 : [mark[3], mark[4]], + last = {2 : [mark[3], mark[4]], }.get(formatype, mark[0]) - if ( indm >= firstm ) and ( indm <= lastm ): + if lastm >= indm >= firstm: ind = indm % NB_MARKERS_PER_PAGE self.plist.append([ mark[0], ind, self.nbpages] ) indm += 1 - self.mapview.write("\n var point = new LatLonPoint") + self.mapview.write( + "\n var point = new LatLonPoint" + - self.mapview.write("(%s,%s);" % (mark[3], mark[4])) - self.mapview.write("my_marker = new Marker(point);") - self.mapview.write("gmarkers[%d]=my_marker;" % ind ) - self.mapview.write("my_marker.setLabel") - self.mapview.write("(\"%s\");" % _escape(mark[0])) + "(%s,%s);" % (mark[3], mark[4]) + + "my_marker = new Marker(point);" + + "gmarkers[%d]=my_marker;" % ind + + "my_marker.setLabel" + + '("%s");' % _escape(mark[0]) + ) self.yearinmarker.append(mark[7]) divclose = False differtype = False if mark[8] and not differtype: savetype = mark[8] - self.mapview.write("my_marker.setInfoBubble(\"
") - self.mapview.write("%s
" % _escape(mark[0])) + self.mapview.write( + 'my_marker.setInfoBubble("
" + + "%s
" % _escape(mark[0]) + ) if formatype == 1: self.mapview.write("
%s" % _escape(mark[5])) else: @@ -2073,11 +2082,7 @@ class GeoView(HtmlView): else: self.mapview.write("
%s - %s" % (mark[7], _escape(mark[5]))) - ret = 1 - for year in self.yearinmarker: - if year == mark[7]: - ret = 0 - if (ret): + if not any(y == mark[7] for y in self.yearinmarker): self.yearinmarker.append(mark[7]) else: indm += 1 @@ -2087,39 +2092,44 @@ class GeoView(HtmlView): for year in self.yearinmarker: years += "%d " % year years += "end" - self.mapview.write("
\");") - self.mapview.write("my_marker.setAttribute('year','%s');" % years) + self.mapview.write( + '
");' + + "my_marker.setAttribute('year','%s');" % years + ) self._set_icon(savetype, differtype, formatype) self.mapview.write("map.addMarker(my_marker);") if self.nbmarkers == 0: # We have no valid geographic point to center the map. longitude = 0.0 latitude = 0.0 - self.mapview.write("\nvar point = new LatLonPoint") - self.mapview.write("(%s,%s);\n" % (latitude, longitude)) - self.mapview.write(" map.setCenterAndZoom") - self.mapview.write("(point, %d);\n" % 2) - self.mapview.write(" my_marker = new Marker(point);\n") - self.mapview.write(" my_marker.setLabel") - self.mapview.write("(\"%s\");\n" % _("No location.")) - self.mapview.write(" my_marker.setInfoBubble(\"
") - self.mapview.write(_("You have no places in your family tree " - " with coordinates.")) - self.mapview.write("
") - self.mapview.write(_("You are looking at the default map.")) - self.mapview.write("
\");\n") + self.mapview.write( + "\nvar point = new LatLonPoint" + + "(%s,%s);\n" % (latitude, longitude) + + " map.setCenterAndZoom" + + "(point, %d);\n" % 2 + + " my_marker = new Marker(point);\n" + + " my_marker.setLabel" + + '("%s");\n' % _("No location.") + + ' my_marker.setInfoBubble("
" + + _("You have no places in your family tree " + " with coordinates.") + + "
" + + _("You are looking at the default map.") + + '
");\n' + ) self._set_icon(None, True, 1) self.mapview.write(" map.addMarker(my_marker);") - self.mapview.write("\n}") - self.mapview.write("\n\n") + self.mapview.write( + "\n}" + "\n\n" + ) def _createpersonmarkers(self, dbstate, person, comment): """ Create all markers for the specified person. """ - latitude = "" - longitude = "" + latitude = longitude = "" if person: event_ref = person.get_birth_ref() if event_ref: @@ -2134,8 +2144,7 @@ class GeoView(HtmlView): latitude, longitude = conv_lat_lon(latitude, longitude, "D.D8") if comment: - descr1 = _("%(comment)s : birth place.") % { - 'comment': comment} + descr1 = _("%s : birth place.") % comment else: descr1 = _("birth place.") descr = place.get_title() @@ -2156,8 +2165,7 @@ class GeoView(HtmlView): else: self._append_to_places_without_coord( place.gramps_id, descr) - latitude = "" - longitude = "" + latitude = longitude = "" event_ref = person.get_death_ref() if event_ref: event = dbstate.db.get_event_from_handle(event_ref.ref) @@ -2172,8 +2180,7 @@ class GeoView(HtmlView): longitude, "D.D8") descr = place.get_title() if comment: - descr1 = _("%(comment)s : death place.") % { - 'comment': comment} + descr1 = _("%s : death place.") % comment else: descr1 = _("death place.") # place.get_longitude and place.get_latitude return @@ -2210,11 +2217,11 @@ class GeoView(HtmlView): longitude = "" self.center = True - if self.generic_filter == None or not config.get('interface.filter'): + if self.generic_filter is None or not config.get('interface.filter'): places_handle = dbstate.db.iter_place_handles() else: - places_handle = self.generic_filter.apply(dbstate.db, - dbstate.db.iter_place_handles()) + places_handle = self.generic_filter.apply( + dbstate.db, dbstate.db.iter_place_handles()) for place_hdl in places_handle: place = dbstate.db.get_place_from_handle(place_hdl) descr = place.get_title() @@ -2253,11 +2260,11 @@ class GeoView(HtmlView): longitude = "" self.center = True - if self.generic_filter == None or not config.get('interface.filter'): + if self.generic_filter is None or not config.get('interface.filter'): events_handle = dbstate.db.iter_event_handles() else: - events_handle = self.generic_filter.apply(dbstate.db, - dbstate.db.iter_event_handles()) + events_handle = self.generic_filter.apply( + dbstate.db, dbstate.db.iter_event_handles()) for event_hdl in events_handle: event = dbstate.db.get_event_from_handle(event_hdl) place_handle = event.get_place_handle() @@ -2458,8 +2465,8 @@ class GeoView(HtmlView): """ Return the css style sheet needed for GeoView. """ - dblp = "\n" + dblp = ' - - - - %(title)s - - -

%(title)s

-

%(content)s

- - - """ % { 'height' : 600, - 'title' : _('Start page for the Geography View'), - 'content': _('You don\'t see a map here for one of the following ' - 'reasons :
    ' - '
  1. Your database is empty or not yet selected.' - '
  2. You have not selected a person yet.
  3. ' - '
  4. You have no places in your database.
  5. ' - '
  6. The selected places have no coordinates.
  7. ' - '
') - } + content = _('You don\'t see a map here for one of the following ' + 'reasons :
    ' + '
  1. Your database is empty or not yet selected.
  2. ' + '
  3. You have not selected a person yet.
  4. ' + '
  5. You have no places in your database.
  6. ' + '
  7. The selected places have no coordinates.
  8. ' + '
') + filename = os.path.join(tmpdir, 'geography.html') # Now we have two views : Web and Geography, we need to create the # startpage only once. if not os.path.exists(filename): ufd = file(filename, "w+") - ufd.write(data) + ufd.write( + _HTMLHEADER % { + 'title': _('Start page for the Geography View'), + 'lang' : 'en', + 'css' : '', + } + + ' \n' + ) + ufd.write('''\ + +

%(title)s

+

%(content)s

+ + + ''' % { + 'title' : _('Start page for the Geography View'), + 'content': content, + } + ) ufd.close() - return urlparse.urlunsplit(('file', '', - URL_SEP.join(filename.split(os.sep)), - '', '')) + return urlparse.urlunsplit( + ('file', '', URL_SEP.join(filename.split(os.sep)), '', '') + ) def _create_message_page(self, message): """ This function creates a page which contains a message. """ tmpdir = GEOVIEW_SUBPATH - data = """ - - - - - Message - - -

%(content)s

- - - """ % { - 'content': message - } - filename = os.path.join(tmpdir, 'message.html') ufd = file(filename, "w+") - ufd.write(data) + ufd.write( + _HTMLHEADER % { + 'title' : 'Message', + 'lang' : 'en', + 'css' : '', + } + + ' \n' + ) + ufd.write( + '\n' + '

%s

\n' % message + + '\n' + '' + ) ufd.close() - return urlparse.urlunsplit(('file', '', - URL_SEP.join(filename.split(os.sep)), - '', '')) + return urlparse.urlunsplit( + ('file', '', URL_SEP.join(filename.split(os.sep)), '', '') + ) def __test_network(self): """ @@ -2658,7 +2665,7 @@ class GeoView(HtmlView): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(self._config.get('preferences.network-timeout')) sock.connect((self._config.get('preferences.network-site'), 80)) - if sock != None: + if sock is not None: if self.no_network == True: self.no_network = False self._change_map(self.usedmap)