]> git.zarvox.org Git - shortlog.git/commitdiff
Make things more Pythonic.
authorDrew Fisher <drew.m.fisher@gmail.com>
Mon, 19 Mar 2012 05:30:03 +0000 (22:30 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Mon, 19 Mar 2012 05:30:03 +0000 (22:30 -0700)
Signed-off-by: Drew Fisher <drew.m.fisher@gmail.com>
shortlog.py

index 56a3e0f89070260a0111c3a6dd6eef1035f11589..16dde4b22ce37546cbf142e6fb1e3578e9ba1d6d 100644 (file)
@@ -8,6 +8,9 @@ import smtplib
 import hashlib
 
 pwd = os.path.dirname( os.path.realpath( __file__ ) )
+entriesdir = os.path.join(pwd, "entries")
+commentsdir = os.path.join(pwd, "comments")
+templatesdir = os.path.join(pwd, "templates")
 
 web.config.debug = True
 
@@ -51,7 +54,7 @@ app = web.application(urls, globals())
 # read the paragraphs from a file
 def readFile(filepath):
        f = open(filepath)
-       lines = map(str.strip, f.readlines())
+       lines = [l.strip() for l in f.readlines()]
        return makeParas(lines)
 
 def loadComment(filepath):
@@ -68,7 +71,7 @@ def loadComment(filepath):
        return (nameline, emailline, websiteline, commentbody, timestamp, gravatar)
 
 def getCommentFiles(date): # Returns a list of paths to comment files associated with date
-       filelist = filter( lambda x: re.match(date, x), os.listdir(pwd + "/comments"))
+       filelist = [x for x in os.listdir(commentsdir) if re.match(date, x)]
        return filelist
 
 # Collect lines into paragraph blocks.  Returns a list of strings, each containing a well-formed HTML block.
@@ -85,55 +88,53 @@ def makeParas(lines):
                paragraphs.append("\n".join(thispara))
 
        # wrap paragraphs in <p></p> if they aren't other HTML tags that don't want to be wrapped in <p> tags
-       return map( lambda x: x if "<ol>" in x or "<ul>" in x or "<pre>" in x or "<table>" in x or "<video>" in x else "<p>" + x + "</p>", paragraphs)
+       #return map( lambda x: x if "<ol>" in x or "<ul>" in x or "<pre>" in x or "<table>" in x or "<video>" in x else "<p>" + x + "</p>", paragraphs)
+       return [x if "<ol>" in x or "<ul>" in x or "<pre>" in x or "<table>" in x or "<video>" in x else "<p>" + x + "</p>" for x in paragraphs]
 
 
 def getFileList(numdays=7):
-       files = os.listdir(pwd + "/entries")
+       files = os.listdir(entriesdir)
        today = datetime.date.today()
        then = today - datetime.timedelta(days=numdays)
-       return filter( lambda x: x > then.isoformat() , sorted(files))
+       return [ x for x in sorted(files) if x > then.isoformat() ]
 
 class year:
        def GET(self, theyear):
-               files = sorted(os.listdir(pwd + "/entries"))
-               filelist = filter( lambda x: re.match(theyear, x), files)
-               filelist = map( lambda x: pwd + "/entries/" + x, filelist)
+               files = sorted(os.listdir(entriesdir))
+               filelist = [ os.path.join(entriesdir, x) for x in files if re.match(theyear,x) ]
                # It makes more sense to sort fixed chunks of time in chronological order, rather than feed-style
                #filelist.reverse()
-               dates = map( lambda filename: re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename), filelist )
+               dates = [ re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename) for filename in filelist ]
                entries = map( readFile, filelist)
                commentcounts = map(len, map( getCommentFiles, dates))
-               render = web.template.render(pwd + "/templates")
+               render = web.template.render(templatesdir)
                return render.multiday(zip(dates, entries, commentcounts), "Entries from %s" % theyear)
 
 class month:
        def GET(self, theyear, themonth):
-               files = sorted(os.listdir(pwd + "/entries"))
-               filelist = filter( lambda x: re.match("%s-%s" % (theyear, themonth), x), files)
-               filelist = map( lambda x: pwd + "/entries/" + x, filelist)
+               files = sorted(os.listdir(entriesdir))
+               filelist = [ os.path.join(entriesdir, x) for x in files if re.match("%s-%s" % (theyear, themonth), x) ]
                #filelist.reverse()
-               dates = map( lambda filename: re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename), filelist )
+               dates = [ re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename) for filename in filelist ]
                entries = map( readFile, filelist)
                commentcounts = map(len, map( getCommentFiles, dates))
-               render = web.template.render(pwd + "/templates")
+               render = web.template.render(templatesdir)
                return render.multiday(zip(dates, entries, commentcounts), "Entries from %s, %s" % (monthnames[themonth], theyear))
 
 class day:
        def GET(self, year, month, day):
-               filename = pwd + "/entries/" + year + "-" + month + "-" + day + ".txt"
+               filename = os.path.join(entriesdir, "%s-%s-%s.txt" % (year, month, day))
                if os.path.isfile(filename):
                        paras = readFile(filename)
-                       commentfilelist = os.listdir(pwd + "/comments")
-                       files = filter(lambda x: re.match("(.*)" + year + "-" + month + "-" + day + "_(\d\d\d\d\d\d).txt$", x), commentfilelist)
-                       files = sorted([pwd + "/comments/" + x for x in files])
+                       commentfilelist = os.listdir(commentsdir)
+                       files = sorted([os.path.join(commentsdir, filename) for filename in commentfilelist if re.match("(.*)%s-%s-%s_(\d\d\d\d\d\d).txt$" % (year, month, day), filename) ])
                        comments = []
                        for f in files:
                                comments.append(loadComment(f))
-                       render = web.template.render(pwd + "/templates")
+                       render = web.template.render(templatesdir)
                        return render.day("%s-%s-%s" % (year, month, day), paras, comments)
                else:
-                       render = web.template.render(pwd + "/templates")
+                       render = web.template.render(templatesdir)
                        return render.noentry("%s-%s-%s" % (year, month, day))
 
        def verify(self, test):
@@ -164,7 +165,7 @@ class day:
                # find a file name that isn't taken
                fname = ""
                for i in xrange(0, 1000000):
-                       fname = pwd + "/pending/" + year + "-" + month + "-" + day + "_" + ("%06d" % i) + ".txt"
+                       fname = os.path.join(pwd, "pending", "%s-%s-%s_%06d.txt" % (year, month, day, i))
                        if not os.path.exists(fname):
                                break
                f = open(fname, "a+")
@@ -186,32 +187,32 @@ class day:
 
 class pastyear:
        def GET(self):
-               filelist = map( lambda x: pwd + "/entries/" + x, getFileList(365) )
+               filelist = [ os.path.join(entriesdir, x) for x in getFileList(365)]
                filelist.reverse()
-               dates = map( lambda filename: re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename), filelist )
+               dates = [ re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename) for filename in filelist ]
                entries = map( readFile, filelist)
                commentcounts = map(len, map( getCommentFiles, dates))
-               render = web.template.render(pwd + "/templates")
+               render = web.template.render(templatesdir)
                return render.multiday(zip(dates, entries, commentcounts))
 
 class pastmonth:
        def GET(self):
-               filelist = map( lambda x: pwd + "/entries/" + x, getFileList(30) )
+               filelist = [ os.path.join(entriesdir, x) for x in getFileList(30)]
                filelist.reverse()
-               dates = map( lambda filename: re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename), filelist )
+               dates = [ re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename) for filename in filelist ]
                entries = map( readFile, filelist)
                commentcounts = map(len, map( getCommentFiles, dates))
-               render = web.template.render(pwd + "/templates")
+               render = web.template.render(templatesdir)
                return render.multiday(zip(dates, entries, commentcounts))
 
 class pastweek:
        def GET(self):
-               filelist = map( lambda x: pwd + "/entries/" + x, getFileList(7) )
+               filelist = [ os.path.join(entriesdir, x) for x in getFileList(7)]
                filelist.reverse()
-               dates = map( lambda filename: re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename), filelist )
+               dates = [ re.sub("(.*)(\d\d\d\d-\d\d-\d\d).txt", "\g<2>", filename) for filename in filelist ]
                entries = map( readFile, filelist)
                commentcounts = map(len, map( getCommentFiles, dates))
-               render = web.template.render(pwd + "/templates")
+               render = web.template.render(templatesdir)
                return render.multiday(zip(dates, entries, commentcounts))
 
 class redir:
@@ -221,9 +222,8 @@ class redir:
 class feed:
        def GET(self):
                web.header('Content-Type', 'application/atom+xml')
-               filelist = os.listdir(pwd + "/entries")
-               files = filter(lambda x: re.match("(.*)(\d\d\d\d-\d\d-\d\d).txt$", x), filelist)
-               files = [pwd + "/entries/" + x for x in files]
+               filelist = os.listdir(entriesdir)
+               files = [os.path.join(entriesdir, f) for f in filelist if re.match("(.*)(\d\d\d\d-\d\d-\d\d).txt$", f)]
                stats = [os.stat(f) for f in files]
                decorated = [(stat.st_mtime, filename) for (filename, stat) in zip(files, stats)]
                decorated.sort(reverse=True)
@@ -237,7 +237,7 @@ class feed:
                                        "content": "".join(readFile(d[1])),
                                        "timestamp": (mtime.isoformat() + "-06:00") }
                        entries.append(second)
-               render = web.template.render(pwd +"/templates")
+               render = web.template.render(templatesdir)
                return render.feed(group_timestamp, entries)
 
 # web.py 0.3+ way