Reorganized the functions that create file and directory names. Nothing

got broken, I hope. More changes to come.
        * src/plugins/NarrativeWeb.py


svn: r10270
This commit is contained in:
Kees Bakker
2008-03-11 21:19:36 +00:00
parent 3797e94d4c
commit c764b185cd

View File

@@ -208,7 +208,7 @@ class BasePage:
""" """
title - text for the <title> tag title - text for the <title> tag
options - dictionary with the options options - dictionary with the options
archive - TODO describe archive - an open tar archive (if not None) to write the file to
photo_list - TODO describe photo_list - TODO describe
gid - Gramps ID gid - Gramps ID
""" """
@@ -219,7 +219,7 @@ class BasePage:
self.archive = archive self.archive = archive
self.page_title = "" self.page_title = ""
self.warn_dir = True self.warn_dir = True
self.cur_name = None # Internal use. The name of the output file. self.cur_name = None # Internal use. The name of the output file, to be used for the tar archive.
# TODO. All of these attributes are not necessary, because we have # TODO. All of these attributes are not necessary, because we have
# als the options in self.options. Besides, we need to check which # als the options in self.options. Besides, we need to check which
@@ -282,12 +282,12 @@ class BasePage:
self.photo_list[handle] = [lnk] self.photo_list[handle] = [lnk]
ext = os.path.splitext(photo.get_path())[1] ext = os.path.splitext(photo.get_path())[1]
real_path = "%s/%s" % (self.build_path(handle, 'images'), handle+ext) real_path = "%s/%s" % (self.build_path('images', handle), handle+ext)
thumb_path = "%s/%s.png" % (self.build_path(handle, 'thumb'), handle) thumb_path = "%s/%s.png" % (self.build_path('thumb', handle), handle)
return (real_path, thumb_path) return (real_path, thumb_path)
def create_file(self, name): def create_file(self, name):
self.cur_name = self.build_name("", name) self.cur_name = name + self.ext
if self.archive: if self.archive:
self.string_io = StringIO() self.string_io = StringIO()
of = codecs.EncodedFile(self.string_io, 'utf-8', self.encoding, of = codecs.EncodedFile(self.string_io, 'utf-8', self.encoding,
@@ -298,24 +298,42 @@ class BasePage:
self.encoding, 'xmlcharrefreplace') self.encoding, 'xmlcharrefreplace')
return of return of
def link_path(self, name, path): def build_path(self, dirroot, name, up=False):
path = "%s/%s/%s" % (path, name[0].lower(), name[1].lower()) path = '%s/%s/%s' % (dirroot, name[0].lower(), name[1].lower())
path = self.build_name(path, name) if up:
path = '../../../' + path
return path return path
def create_link_file(self, name, path): def build_path_fname(self, path, name, up=False):
self.cur_name = self.link_path(name, path) """
Create a filename in a directory tree using the first to characters
for the first two directory levels. For example
0/2/02c0d8f888f566ae95ffbdca64274b51
"""
path = self.build_path(path, name, up)
return path + '/' + name + self.ext
def link_path(self, path, name):
path = "%s/%s/%s" % (path, name[0].lower(), name[1].lower())
return path + '/' + name + self.ext
def create_link_file(self, path, name):
"""
Create a file in a directory tree using the first to characters
for the first two directory levels. For example
0/2/02c0d8f888f566ae95ffbdca64274b51
"""
self.cur_name = self.link_path(path, name)
if self.archive: if self.archive:
self.string_io = StringIO() self.string_io = StringIO()
of = codecs.EncodedFile(self.string_io, 'utf-8', of = codecs.EncodedFile(self.string_io, 'utf-8',
self.encoding, 'xmlcharrefreplace') self.encoding, 'xmlcharrefreplace')
else: else:
dirname = os.path.join(self.html_dir, dirname = os.path.join(self.html_dir,
path, name[0].lower(), path, name[0].lower(), name[1].lower())
name[1].lower())
if not os.path.isdir(dirname): if not os.path.isdir(dirname):
os.makedirs(dirname) os.makedirs(dirname)
page_name = self.build_name(dirname, name) page_name = dirname + '/' + name + self.ext
of = codecs.EncodedFile(open(page_name, "w"), 'utf-8', of = codecs.EncodedFile(open(page_name, "w"), 'utf-8',
self.encoding, 'xmlcharrefreplace') self.encoding, 'xmlcharrefreplace')
return of return of
@@ -336,6 +354,7 @@ class BasePage:
self.cur_name = None self.cur_name = None
def lnkfmt(self, text): def lnkfmt(self, text):
"""This creates an MD5 hex string to be used as filename."""
return md5.new(text).hexdigest() return md5.new(text).hexdigest()
def display_footer(self, of, db): def display_footer(self, of, db):
@@ -425,12 +444,10 @@ class BasePage:
'GRAMPS</a> on %(date)s') % { 'date' : value } 'GRAMPS</a> on %(date)s') % { 'date' : value }
if self.linkhome: if self.linkhome:
home_person_handle = db.get_default_handle() home_person = db.get_default_person()
if home_person_handle: if home_person:
home_person = db.get_default_person() fname = self.build_path_fname('ppl', home_person.handle, up)
home_person_url = self.build_name( home_person_url = fname
self.build_path(home_person_handle, "ppl", up),
home_person.handle)
home_person_name = home_person.get_primary_name().get_regular_name() home_person_name = home_person.get_primary_name().get_regular_name()
msg += _('<br />for <a href="%s">%s</a>') % (home_person_url, home_person_name) msg += _('<br />for <a href="%s">%s</a>') % (home_person_url, home_person_name)
@@ -743,22 +760,6 @@ class BasePage:
of.write('\t\t</ol>\n') of.write('\t\t</ol>\n')
of.write('\t</div>\n') of.write('\t</div>\n')
def build_path(self, handle, dirroot, up=False):
path = ""
if up:
path = '../../../%s/%s/%s' % (dirroot,
handle[0].lower(),
handle[1].lower())
else:
path = "%s/%s/%s" % (dirroot, handle[0].lower(), handle[1].lower())
return path
def build_name(self, path, base):
name = base
if path:
name = path + "/" + name
return name + self.ext
def person_link(self, of, path, name, gid="", up=True): def person_link(self, of, path, name, gid="", up=True):
if up: if up:
path = "../../../" + path path = "../../../" + path
@@ -770,23 +771,22 @@ class BasePage:
def surname_link(self, of, name, opt_val=None, up=False): def surname_link(self, of, name, opt_val=None, up=False):
handle = self.lnkfmt(name) handle = self.lnkfmt(name)
dirpath = self.build_path(handle, 'srn', up) dirpath = self.build_path('srn', handle, up)
of.write('<a href="%s/%s%s">%s' % (dirpath, handle, self.ext, name)) of.write('<a href="%s/%s%s">%s' % (dirpath, handle, self.ext, name))
if opt_val != None: if opt_val != None:
of.write('&nbsp;(%d)' % opt_val) of.write('&nbsp;(%d)' % opt_val)
of.write('</a>') of.write('</a>')
def galleryNav_link(self, of, handle, name, up=False): def galleryNav_link(self, of, handle, name, up=False):
dirpath = self.build_path(handle, 'img', up) dirpath = self.build_path('img', handle, up)
of.write('<a id="%s" href="%s/%s%s">%s</a>' % (html_escape(name), dirpath, handle, self.ext, html_escape(name))) of.write('<a id="%s" href="%s/%s%s">%s</a>' % (html_escape(name), dirpath, handle, self.ext, html_escape(name)))
def media_ref_link(self, of, handle, name, up=False): def media_ref_link(self, of, handle, name, up=False):
dirpath = self.build_path(handle, 'img', up) dirpath = self.build_path('img', handle, up)
of.write('<a href="%s/%s%s">%s</a>' % (dirpath, handle, self.ext, html_escape(name))) of.write('<a href="%s/%s%s">%s</a>' % (dirpath, handle, self.ext, html_escape(name)))
def media_link(self, of, handle, path, name, up, usedescr=True): def media_link(self, of, handle, path, name, up, usedescr=True):
dirpath = self.build_path(handle, 'img', up) dirpath = self.build_path('img', handle, up)
of.write('\t\t<div class="thumbnail">\n') of.write('\t\t<div class="thumbnail">\n')
of.write('\t\t\t<a href="%s/%s%s">' % (dirpath, handle, self.ext)) of.write('\t\t\t<a href="%s/%s%s">' % (dirpath, handle, self.ext))
of.write('<img src="../../../%s" ' % path) of.write('<img src="../../../%s" ' % path)
@@ -797,7 +797,7 @@ class BasePage:
def doc_link(self, of, handle, name, up, usedescr=True): def doc_link(self, of, handle, name, up, usedescr=True):
path = os.path.join('images', 'document.png') path = os.path.join('images', 'document.png')
dirpath = self.build_path(handle, 'img', up) dirpath = self.build_path('img', handle, up)
of.write('\t\t<div class="thumbnail">\n') of.write('\t\t<div class="thumbnail">\n')
of.write('\t\t\t<a href="%s/%s%s">' % (dirpath, handle, self.ext)) of.write('\t\t\t<a href="%s/%s%s">' % (dirpath, handle, self.ext))
of.write('<img src="../../../%s" ' % path) of.write('<img src="../../../%s" ' % path)
@@ -807,14 +807,14 @@ class BasePage:
of.write('\t\t</div>\n') of.write('\t\t</div>\n')
def source_link(self, of, handle, name, gid="", up=False): def source_link(self, of, handle, name, gid="", up=False):
dirpath = self.build_path(handle, 'src', up) dirpath = self.build_path('src', handle, up)
of.write(' href="%s/%s%s">%s' % (dirpath, handle, self.ext, html_escape(name))) of.write(' href="%s/%s%s">%s' % (dirpath, handle, self.ext, html_escape(name)))
if not self.noid and gid != "": if not self.noid and gid != "":
of.write('&nbsp;<span class="grampsid">[%s]</span>' % gid) of.write('&nbsp;<span class="grampsid">[%s]</span>' % gid)
of.write('</a>') of.write('</a>')
def place_link(self, of, handle, name, gid="", up=False): def place_link(self, of, handle, name, gid="", up=False):
dirpath = self.build_path(handle, 'plc', up) dirpath = self.build_path('plc', handle, up)
of.write('<a href="%s/%s%s">%s' % (dirpath, handle, self.ext, html_escape(name))) of.write('<a href="%s/%s%s">%s' % (dirpath, handle, self.ext, html_escape(name)))
if not self.noid and gid != "": if not self.noid and gid != "":
@@ -822,7 +822,7 @@ class BasePage:
of.write('</a>') of.write('</a>')
def place_link_str(self, handle, name, gid="", up=False): def place_link_str(self, handle, name, gid="", up=False):
dirpath = self.build_path(handle, 'plc', up) dirpath = self.build_path('plc', handle, up)
retval = '<a href="%s/%s%s">%s' % (dirpath, handle, self.ext, html_escape(name)) retval = '<a href="%s/%s%s">%s' % (dirpath, handle, self.ext, html_escape(name))
if not self.noid and gid != "": if not self.noid and gid != "":
@@ -890,8 +890,8 @@ class IndividualListPage(BasePage):
# firstname column # firstname column
of.write('\t\t\t<td class="ColumnName">') of.write('\t\t\t<td class="ColumnName">')
path = self.build_path(person.handle, "ppl", False) fname = self.build_path_fname('ppl', person.handle)
self.person_link(of, self.build_name(path, person.handle), self.person_link(of, fname,
_nd.display_given(person), person.gramps_id, False) _nd.display_given(person), person.gramps_id, False)
of.write('</td>\n') of.write('</td>\n')
@@ -985,7 +985,7 @@ class SurnamePage(BasePage):
BasePage.__init__(self, title, options, archive, media_list, "") BasePage.__init__(self, title, options, archive, media_list, "")
of = self.create_link_file(md5.new(title).hexdigest(), 'srn') of = self.create_link_file('srn', self.lnkfmt(title))
self.display_header(of, db, title, get_researcher().get_name(), True) self.display_header(of, db, title, get_researcher().get_name(), True)
msg = _("This page contains an index of all the individuals in the " msg = _("This page contains an index of all the individuals in the "
@@ -1017,8 +1017,8 @@ class SurnamePage(BasePage):
person = db.get_person_from_handle(person_handle) person = db.get_person_from_handle(person_handle)
of.write('\t\t<tr>\n') of.write('\t\t<tr>\n')
of.write('\t\t\t<td class="ColumnName">') of.write('\t\t\t<td class="ColumnName">')
path = self.build_path(person.handle, "ppl", True) fname = self.build_path_fname('ppl', person.handle, True)
self.person_link(of, self.build_name(path, person.handle), self.person_link(of, fname,
person.get_primary_name().get_first_name(), person.get_primary_name().get_first_name(),
person.gramps_id, False) person.gramps_id, False)
of.write('</td>\n') of.write('</td>\n')
@@ -1177,7 +1177,7 @@ class PlacePage(BasePage):
place = db.get_place_from_handle( place_handle) place = db.get_place_from_handle( place_handle)
BasePage.__init__(self, title, options, archive, media_list, BasePage.__init__(self, title, options, archive, media_list,
place.gramps_id) place.gramps_id)
of = self.create_link_file(place.get_handle(), "plc") of = self.create_link_file('plc', place.get_handle())
self.page_title = ReportUtils.place_name(db, place_handle) self.page_title = ReportUtils.place_name(db, place_handle)
self.display_header(of, db, "%s - %s" % (_('Places'), self.page_title), get_researcher().get_name(), up=True) self.display_header(of, db, "%s - %s" % (_('Places'), self.page_title), get_researcher().get_name(), up=True)
@@ -1245,7 +1245,7 @@ class MediaPage(BasePage):
(prev, next, page_number, total_pages) = info (prev, next, page_number, total_pages) = info
photo = db.get_object_from_handle(handle) photo = db.get_object_from_handle(handle)
BasePage.__init__(self, title, options, archive, media_list, photo.gramps_id) BasePage.__init__(self, title, options, archive, media_list, photo.gramps_id)
of = self.create_link_file(handle, "img") of = self.create_link_file('img', handle)
self.db = db self.db = db
self.src_list = src_list self.src_list = src_list
@@ -1317,7 +1317,7 @@ class MediaPage(BasePage):
photo.get_path()), photo.get_path()),
thmb_path, 320): thmb_path, 320):
try: try:
path = "%s/%s.png" % (self.build_path(photo.handle, "preview"), photo.handle) path = "%s/%s.png" % (self.build_path("preview", photo.handle), photo.handle)
self.store_file(archive, self.html_dir, thmb_path, path) self.store_file(archive, self.html_dir, thmb_path, path)
os.unlink(thmb_path) os.unlink(thmb_path)
except IOError: except IOError:
@@ -1394,7 +1394,7 @@ class MediaPage(BasePage):
def copy_source_file(self, handle, photo): def copy_source_file(self, handle, photo):
ext = os.path.splitext(photo.get_path())[1] ext = os.path.splitext(photo.get_path())[1]
to_dir = self.build_path(handle, 'images') to_dir = self.build_path('images', handle)
newpath = to_dir + "/" + handle + ext newpath = to_dir + "/" + handle + ext
fullpath = Utils.media_path_full(self.db, photo.get_path()) fullpath = Utils.media_path_full(self.db, photo.get_path())
@@ -1415,7 +1415,7 @@ class MediaPage(BasePage):
return None return None
def copy_thumbnail(self, handle, photo): def copy_thumbnail(self, handle, photo):
to_dir = self.build_path(handle, 'thumb') to_dir = self.build_path('thumb', handle)
to_path = os.path.join(to_dir, handle+".png") to_path = os.path.join(to_dir, handle+".png")
if photo.get_mime_type(): if photo.get_mime_type():
from_path = ThumbNails.get_thumbnail_path(Utils.media_path_full( from_path = ThumbNails.get_thumbnail_path(Utils.media_path_full(
@@ -1702,7 +1702,7 @@ class SourcePage(BasePage):
source = db.get_source_from_handle( handle) source = db.get_source_from_handle( handle)
BasePage.__init__(self, title, options, archive, media_list, BasePage.__init__(self, title, options, archive, media_list,
source.gramps_id) source.gramps_id)
of = self.create_link_file(source.get_handle(), "src") of = self.create_link_file('src', source.get_handle())
self.page_title = source.get_title() self.page_title = source.get_title()
self.display_header(of, db, "%s - %s" % (_('Sources'), self.page_title), self.display_header(of, db, "%s - %s" % (_('Sources'), self.page_title),
get_researcher().get_name(), up=True) get_researcher().get_name(), up=True)
@@ -1914,7 +1914,7 @@ class IndividualPage(BasePage):
self.sort_name = _nd.sorted(self.person) self.sort_name = _nd.sorted(self.person)
self.name = _nd.sorted(self.person) self.name = _nd.sorted(self.person)
of = self.create_link_file(person.handle, "ppl") of = self.create_link_file('ppl', person.handle)
self.display_header(of, db, self.sort_name, self.display_header(of, db, self.sort_name,
get_researcher().get_name(), up=True) get_researcher().get_name(), up=True)
self.display_ind_general(of) self.display_ind_general(of)
@@ -1975,8 +1975,7 @@ class IndividualPage(BasePage):
of.write('\t\t\t\t<div class="box">') of.write('\t\t\t\t<div class="box">')
if person.handle in self.ind_list: if person.handle in self.ind_list:
person_name = _nd.display(person) person_name = _nd.display(person)
path = self.build_path(person.handle, "ppl", False) fname = self.build_path_fname('ppl', person.handle)
fname = self.build_name(path, person.handle)
self.person_link(of, fname, person_name) self.person_link(of, fname, person_name)
else: else:
of.write(_nd.display(person)) of.write(_nd.display(person))
@@ -2244,9 +2243,8 @@ class IndividualPage(BasePage):
if child_handle in self.ind_list: if child_handle in self.ind_list:
of.write("\t\t\t\t\t\t<li>") of.write("\t\t\t\t\t\t<li>")
child_name = _nd.display(child) child_name = _nd.display(child)
path = self.build_path(child_handle, "ppl", False) fname = self.build_path_fname('ppl', child_handle)
self.person_link(of, self.build_name(path, child_handle), self.person_link(of, fname, child_name, gid)
child_name, gid)
else: else:
of.write("\t\t\t\t\t\t<li>") of.write("\t\t\t\t\t\t<li>")
of.write(_nd.display(child)) of.write(_nd.display(child))
@@ -2256,12 +2254,10 @@ class IndividualPage(BasePage):
person = self.db.get_person_from_handle(handle) person = self.db.get_person_from_handle(handle)
of.write('\t\t\t\t<td class="ColumnAttribute">%s</td>\n' % title) of.write('\t\t\t\t<td class="ColumnAttribute">%s</td>\n' % title)
of.write('\t\t\t\t<td class="ColumnValue">') of.write('\t\t\t\t<td class="ColumnValue">')
val = person.gramps_id gid = person.gramps_id
if handle in self.ind_list: if handle in self.ind_list:
path = self.build_path(handle, "ppl", False) fname = self.build_path_fname('ppl', handle)
fname = self.build_name(path, handle) self.person_link(of, fname, _nd.display(person), gid)
self.person_link(of, fname, _nd.display(person),
val)
else: else:
of.write(_nd.display(person)) of.write(_nd.display(person))
if rel != gen.lib.ChildRefType.BIRTH: if rel != gen.lib.ChildRefType.BIRTH:
@@ -2430,8 +2426,7 @@ class IndividualPage(BasePage):
gid = spouse.get_gramps_id() gid = spouse.get_gramps_id()
if spouse_id in self.ind_list: if spouse_id in self.ind_list:
spouse_name = _nd.display(spouse) spouse_name = _nd.display(spouse)
path = self.build_path(spouse.handle, "ppl", False) fname = self.build_path_fname('ppl', spouse.handle)
fname = self.build_name(path, spouse.handle)
self.person_link(of, fname, spouse_name, gid) self.person_link(of, fname, spouse_name, gid)
else: else:
of.write(name) of.write(name)
@@ -2479,8 +2474,7 @@ class IndividualPage(BasePage):
person_link = person.handle in self.ind_list person_link = person.handle in self.ind_list
person_name = _nd.display(person) person_name = _nd.display(person)
if person_link: if person_link:
path = self.build_path(person.handle, "ppl", False) fname = self.build_path_fname('ppl', person.handle)
fname = self.build_name(path, person.handle)
self.person_link(of, fname, person_name) self.person_link(of, fname, person_name)
else: else:
of.write(person_name) of.write(person_name)