Not logged in
Check-in [1fe0c0788c]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Finished basic template inheritance. Code runs again.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:1fe0c0788c80a2456c5d51d55cd2ad00a143eff1
User & Date: petr 2010-11-09 16:47:29
Context
2010-11-09
20:18
beautified css check-in: 06f8638f83 user: petr tags: trunk
16:47
Finished basic template inheritance. Code runs again. check-in: 1fe0c0788c user: petr tags: trunk
2010-11-07
18:31
Updating templating system with inheritance. check-in: 9e738d7f54 user: petr tags: trunk
Changes

Changes to simple.py.

19
20
21
22
23
24
25
























26
27
28
29
30
31
32
..
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

85
86
87
88
89
90
91
92
            entries = line.split(separator)
        out.append("<tr>")
        for entry in entries:
            out.extend(["<td>", entry, "</td>"])
        out.append("</tr>")
    return "".join(out)


























def missed2table(data, header=None, separator=",", strip=True):
    out = []
    for line in data:
        if strip:
            entries = line.strip().split(separator)
        else:
................................................................................
        self.request.send_header("Content-type", "text/html")
        self.request.end_headers()
        self.write = self.request.wfile.write


class Index(Handler):
    def get(self):
        t_global = Template(open("templates/global.tpl").read())
        t_index = Template(open("templates/index.tpl").read())
        p = open("../lgvp/missed.txt", "r")
        n1 = missed2table(p)
        p = open("../lgvp/received.txt", "r")
        n2 = missed2table(p)
        p = open("../lgvp/dialed.txt", "r")
        n3 = missed2table(p)
        self.write(t.safe_substitute({"missed": n1, "received": n2, "dialed": n3}))



class Settings(Handler):
    def get(self):
        f = open("templates/settings.html")
        t = Template(f.read())
        files = glob.glob("../lgvp/*.cfg")
        dump = ""
        for gf in files:
            cfgfile = open(gf, "r")
            for line in cfgfile:
                dump = dump + line + "<br/>"
        self.write(t.safe_substitute({"dump": dump}))


class Phonebook(Handler):
    def get(self):
        f = open("templates/phonebook.html")
        t = Template(f.read())
        p = open("../lgvp/phonenumber.txt", "r")
        n = csv2table(p, separator="\t")

        self.write(t.safe_substitute(dict(numbers=n)))


mapping = {"/": Index,
        "/settings": Settings,
        "/phonebook": Phonebook}









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
<






|
>




|
<






|




|
<

|
>
|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
..
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91
92

93
94
95
96
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
114
115
            entries = line.split(separator)
        out.append("<tr>")
        for entry in entries:
            out.extend(["<td>", entry, "</td>"])
        out.append("</tr>")
    return "".join(out)

def loadDSV(data, separator=",", strip=True):
    out = []
    for line in data:
        if strip:
            entries = line.strip().split(separator)
        else:
            entries = line.split(separator)
        out.append(entries)
    return out

def maketable(data, header=None):
    out = []
    out.append("<table>")
    if header:
        out.append("<thead><tr>")
        out.append("".join(["<th>%s</th>" % val for val in header]))
        out.append("</tr><thead>")
    out.append("<tdata>")
    for line in data:
        out.append("<tr>")
        out.append("".join(["<td>%s</td>" % val for val in line]))
        out.append("</tr>")
    out.append("</tdata></table>")
    return "".join(out)

def missed2table(data, header=None, separator=",", strip=True):
    out = []
    for line in data:
        if strip:
            entries = line.strip().split(separator)
        else:
................................................................................
        self.request.send_header("Content-type", "text/html")
        self.request.end_headers()
        self.write = self.request.wfile.write


class Index(Handler):
    def get(self):
        t_index = Template("templates/global.tpl", content="templates/index.tpl")

        p = open("../lgvp/missed.txt", "r")
        n1 = missed2table(p)
        p = open("../lgvp/received.txt", "r")
        n2 = missed2table(p)
        p = open("../lgvp/dialed.txt", "r")
        n3 = missed2table(p)
        self.write(t_index.safe_substitute({"missed": n1, "received": n2,
            "dialed": n3, "title": "All calls"}))


class Settings(Handler):
    def get(self):
        t = Template("templates/global.tpl", content="templates/settings.tpl")

        files = glob.glob("../lgvp/*.cfg")
        dump = ""
        for gf in files:
            cfgfile = open(gf, "r")
            for line in cfgfile:
                dump = dump + line + "<br/>"
        self.write(t.safe_substitute({"dump": dump, "title": "Settings"}))


class Phonebook(Handler):
    def get(self):
        t = Template("templates/global.tpl", content="templates/phonebook.tpl")

        p = open("../lgvp/phonenumber.txt", "r")
        n = maketable(loadDSV(p, separator="\t"),
                header=("Name","Number"))
        self.write(t.safe_substitute(dict(phonebook=n, title="Phonebook")))


mapping = {"/": Index,
        "/settings": Settings,
        "/phonebook": Phonebook}


Changes to template.py.

1
2


3
4
5
6
7
8
9
..
43
44
45
46
47
48
49
50
51







52
53
54
55
56
57
58
####################################################################
import re as _re



class _multimap:
    """Helper class for combining multiple mappings.

    Used by .{safe_,}substitute() to combine the mapping and keyword
    arguments.
    """
................................................................................
class Template:
    """A string class for supporting $-substitutions."""
    __metaclass__ = _TemplateMetaclass

    delimiter = '$'
    idpattern = r'[_a-z][_a-z0-9]*'

    def __init__(self, template):
        self.template = template








    # Search for $$, $identifier, ${identifier}, and any bare $'s

    def _invalid(self, mo):
        i = mo.start('invalid')
        lines = self.template[:i].splitlines(True)
        if not lines:


>
>







 







|
|
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
..
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
####################################################################
import re as _re
import os


class _multimap:
    """Helper class for combining multiple mappings.

    Used by .{safe_,}substitute() to combine the mapping and keyword
    arguments.
    """
................................................................................
class Template:
    """A string class for supporting $-substitutions."""
    __metaclass__ = _TemplateMetaclass

    delimiter = '$'
    idpattern = r'[_a-z][_a-z0-9]*'

    def __init__(self, template, **kwargs):
        self.template = open(template, "r").read()
        if kwargs:
            subs = {}
            for k, v in kwargs.items():
                t = open(v, "r").read()
                subs[k] = t
            self.template = self.safe_substitute(subs)


    # Search for $$, $identifier, ${identifier}, and any bare $'s

    def _invalid(self, mo):
        i = mo.start('invalid')
        lines = self.template[:i].splitlines(True)
        if not lines:

Changes to templates/phonebook.tpl.


1
2
3
4
5
6
7
8
9
10
11
12

<h1>Phonebook</h1>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Number</th>
        </tr>
    </thead>
    <tbody>
        $numbers
    </tbody>
</table>
>
|
<
<
<
<
<
<
<
<
<
<
<
1
2











<h1>$title</h1>
$phonebook