]> git.phdru.name Git - phdru.name/phdru.name.git/commitdiff
A number of minor optimizations.
authorOleg Broytman <phd@phdru.name>
Fri, 3 Mar 2006 18:18:19 +0000 (18:18 +0000)
committerOleg Broytman <phd@phdru.name>
Fri, 3 Mar 2006 18:18:19 +0000 (18:18 +0000)
git-svn-id: file:///home/phd/archive/SVN/phdru.name/scripts@13 7bb0bf08-9e0d-0410-b083-99cee3bf18b8

reindex_blog.py

index c4d9be3358e6904ef9256879b0174efb9fcf729c..cf830d1cb94ced402bf1a9cf3502f3afe439cfc1 100755 (executable)
@@ -92,8 +92,16 @@ months_names_ru0 = ['', "
 ]
 
 
-def write_template(year, month, day, titles):
-   index_name = os.path.join(year, month, day, "index.tmpl")
+def write_template(level, year, month, day, titles):
+   path = []
+   if level >= 1:
+      path.append(year)
+   if level >= 2:
+      path.append(month)
+   if level == 3:
+      path.append(day)
+   path.append("index.tmpl")
+   index_name = os.path.join(*path)
    try:
       index_tmpl = open(index_name, 'r')
       old_text = index_tmpl.read()
@@ -101,18 +109,13 @@ def write_template(year, month, day, titles):
    except IOError:
       old_text = None
 
-   new_text = []
-   show_year = not year
-   show_month = not month
-   show_day = not day
-
-   new_text.append("""\
+   new_text = ["""\
 ## THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
 #extends phd_pp_ru
 #implements respond
-""")
+"""]
 
-   if show_year:
+   if level == 0:
       new_text.append("""\
 #attr $Title = "Oleg BroytMann's blog"
 #attr $Description = "BroytMann Russian Blog Index Document"
@@ -122,8 +125,7 @@ def write_template(year, month, day, titles):
 <H1>Журнал</H1>
 """ % {"cyear": year or 2005})
 
-   elif show_month:
-
+   elif level == 1:
       new_text.append("""\
 #attr $Title = "Oleg BroytMann's blog: %(year)s"
 #attr $Description = "BroytMann Russian Blog %(year)s Index Document"
@@ -133,9 +135,8 @@ def write_template(year, month, day, titles):
 <H1>Журнал: %(year)s</H1>
 """ % {"year": year, "cyear": year or 2005})
 
-   elif show_day:
-
-      month = int(month)
+   elif level == 2:
+      imonth = int(month)
       new_text.append("""\
 #attr $Title = "Oleg BroytMann's blog: %(month_abbr_en)s %(year)s"
 #attr $Description = "BroytMann Russian Blog %(month_name_en)s %(year)s Index Document"
@@ -145,14 +146,13 @@ def write_template(year, month, day, titles):
 <H1>Журнал: %(month_name_ru0)s %(year)s</H1>
 """ % {
       "year": year, "cyear": year or 2005,
-      "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
-      "month_name_ru0": months_names_ru0[month],
+      "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
+      "month_name_ru0": months_names_ru0[imonth],
    })
 
-   else:
-
-      day = int(day)
-      month = int(month)
+   elif level == 3:
+      iday = int(day)
+      imonth = int(month)
 
       new_text.append("""\
 #attr $Title = "Oleg BroytMann's blog: %(day)d %(month_abbr_en)s %(year)s"
@@ -163,29 +163,28 @@ def write_template(year, month, day, titles):
 <H1>Журнал: %(day)d %(month_name_ru0)s %(year)s</H1>
 """ % {
       "year": year, "cyear": year or 2005,
-      "month_abbr_en": months_abbrs_en[month], "month_name_en": months_names_en[month],
-      "month_name_ru0": months_names_ru0[month],
-      "day": day
+      "month_abbr_en": months_abbrs_en[imonth], "month_name_en": months_names_en[imonth],
+      "month_name_ru0": months_names_ru0[imonth],
+      "day": iday
    })
 
    save_titles = titles[:]
    titles.reverse()
 
    save_day = None
-   for key, tmpl, title, lead in titles:
-      year, month, day = key
+   for year, month, day, filename, title, lead in titles:
       href = []
-      if show_year:
+      if level == 0:
          href.append(year)
-      if show_month:
+      if level <= 1:
          href.append(month)
-      if show_day:
+      if level <= 2:
          href.append(day)
-      href.append(tmpl)
+      href.append(filename)
       href = '/'.join(href)
       if day[0] == '0': day = day[1:]
       if save_day <> day:
-         if show_year:
+         if level == 0:
             new_text.append('\n<h2>%s %s %s</h2>' % (day, months_names_ru[int(month)], year))
          else:
             new_text.append('\n<h2>%s %s</h2>' % (day, months_names_ru[int(month)]))
@@ -200,10 +199,9 @@ def write_template(year, month, day, titles):
 </p>
 ''' % (lead, href, title))
 
-   if show_year:
+   if level == 0:
       years = {}
-      for key, tmpl, title, lead in save_titles:
-         year, month, day = key
+      for year, month, day, filename, title, lead in save_titles:
          years[year] = True
       first_year = True
       new_text.append('''
@@ -244,13 +242,15 @@ for year in sorted(years.keys()):
          day_titles = []
          key = year, month, day
          if key in blog:
-            for tmpl, title, lead in blog[key]:
-               if tmpl.endswith(".tmpl"): tmpl = tmpl[:-len("tmpl")] + "html"
-               all_titles.append((key, tmpl, title, lead))
-               year_titles.append((key, tmpl, title, lead))
-               month_titles.append((key, tmpl, title, lead))
-               day_titles.append((key, tmpl, title, lead))
-         write_template(year, month, day, day_titles)
-      write_template(year, month, '', month_titles)
-   write_template(year, '', '', year_titles)
-write_template('', '', '', all_titles[-20:])
+            for filename, title, lead in blog[key]:
+               if filename.endswith(".tmpl"):
+                  filename = filename[:-len("tmpl")] + "html"
+               value = (year, month, day, filename, title, lead)
+               all_titles.append(value)
+               year_titles.append(value)
+               month_titles.append(value)
+               day_titles.append(value)
+         write_template(3, year, month, day, day_titles)
+      write_template(2, year, month, day, month_titles)
+   write_template(1, year, month, day, year_titles)
+write_template(0, year, month, day, all_titles[-20:])