Erik Grinaker is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

erikg / Revelation

Revelation is a password manager for the GNOME desktop, released under the GNU GPL license. It stores all your accounts and passwords in a single, secure place, and gives you access to it through a user-friendly graphical interface.

Clone this repository (size: 2.1 MB): HTTPS / SSH
hg clone https://bitbucket.org/erikg/revelation
hg clone ssh://hg@bitbucket.org/erikg/revelation

Revelation / src / lib / datahandler / xhtml.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 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
 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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#
# Revelation - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# Module for exporting to XHTML files
#
#
# Copyright (c) 2003-2006 Erik Grinaker
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

import base
from revelation import config, data, entry

import gettext, time

_ = gettext.gettext


IMAGEPATH       = "http://oss.codepoet.no/revelation/img/fileicons"


class XHTML(base.DataHandler):
        "Data handler for XHTML export"

        name            = "XHTML / CSS"
        importer        = False
        exporter        = True
        encryption      = False


        def __init__(self):
                base.DataHandler.__init__(self)


        def __generate_css(self):
                "Generates a CSS for the XHTML document"

                css = """
                /* containers */
                body {
                        margin: 20px 10px;
                        padding: 0px;

                        font-family: sans-serif;
                        font-size: 10pt;
                        line-height: 1.3;

                        color: #333333;
                        background-color: #ffffff;
                }

                div {
                        margin: 0px;
                        padding: 0px;
                }

                p {
                        margin: 0px 0px 5px 0px;
                        padding: 0px;
                }

                table {
                        font-family: sans-serif;
                        font-size: 10pt;
                        line-height: 1.3;

                        margin: 0px;
                        border-collapse: collapse;

                        color: #333333;
                        background-color: transparent;
                }

                td {
                        text-align: left;
                        vertical-align: top;
                }

                th {
                        text-align: left;
                        vertical-align: top;

                        font-weight: bold;
                        color: #000000;
                }



                /* headings */
                h1, h2, h3 {
                        margin: 0px;
                        padding: 0px;
                        line-height: 1;
                        font-weight: bold;
                        color: #000000;
                }

                h1 {
                        margin-bottom: 20px;
                        font-size: 16pt;
                }

                h2 {
                        margin-bottom: 5px;
                        font-size: 14pt;
                }

                h3 {
                        font-size: 10pt;
                }



                /* content */
                a {
                        color: #3333cc;
                        text-decoration: none;
                }

                a:hover {
                        text-decoration: underline;
                }

                img {
                        margin: 0px;
                        padding: 0px;
                        border: none;
                }

                strong {
                        color: #000000;
                }



                /* the content area */
                #content {
                        margin-right: 220px;
                }



                /* the sidebar */
                #sidebar {
                        width: 200px;
                        float: right;
                }

                #sidebar h2 {
                        margin: 20px 0px 10px 0px;
                        padding: 0px 0px 5px 0px;
                        border-bottom: 1px dashed #3366cc;
                        text-transform: lowercase;
                }

                #sidebar h2 img.icon {
                        width: 24px;
                        height: 24px;
                        margin-right: 5px;
                        vertical-align: middle;
                }

                #sidebar h3 {
                        margin-bottom: 2px;
                        text-transform: lowercase;
                }

                #sidebar p {
                        margin: 0px 5px 10px 15px;
                }

                #sidebar ul {
                        list-style: none;
                        padding: 0px;
                        margin: 0px 0px 10px 0px;
                }

                #sidebar ul ul {
                        margin: 0px 0px 0px 15px;
                }

                #sidebar ul.accountlist {
                        margin-left: 15px;
                }



                /* the entrylist */
                #entrylist {
                        width: 99%;
                        padding: 0px;
                        margin: 0px;
                }



                /* the footer */
                #footer {
                        margin: 50px 220px 10px 0px;
                        font-size: 8pt;
                        text-align: center;
                        color: #aaaaaa;
                }

                #footer a {
                        color: #8888ee;
                }



                /* folders */
                li.folder {
                        width: 100%;
                        list-style-type: none;
                }

                li.folder .folder-data {
                        margin: 0px 0px 10px 0px;
                        padding: 2px 5px 3px 5px;

                        border: 1px solid #3366cc;
                        background-color: #e5ecf9;
                }

                li.folder .folder-data h2 {
                        margin: 0px;
                }

                li.folder .folder-data p {
                        margin: 0px;
                }



                /* accounts  */
                li.account {
                        list-style-type: none;
                        width: 350px;
                        margin: 0px 0px 10px 0px;
                        border: 1px solid #3366cc;
                }

                li.account .heading {
                        padding: 2px 5px;
                        background-color: #e5ecf9;
                        border-bottom: 1px solid #3366cc;
                }

                li.account .heading h2 {
                        margin: 0px 0px 1px 0px;
                }

                li.account .heading img {
                        float: right;
                        width: 24px;
                        height: 24px;
                }

                li.account .heading .description {
                        margin: 0px;
                }

                li.account .heading .type {
                        color: #000000;
                        font-weight: bold;
                }

                li.account .data {
                        padding: 5px;
                }

                li.account .data .fields {
                        margin: 0px 0px 3px 0px;
                }

                li.account .data .fields td {
                        padding: 1px 0px;
                }

                li.account .data .fields th {
                        padding: 1px 5px 1px 0px;
                }

                li.account .updated {
                        font-size: 8pt;
                        text-align: right;
                        margin: 0px;
                }
"""

                return css


        def __generate_entry(self, entrystore, iter, depth = 0):
                "Generates xhtml for an entry"

                tabs = "\t" * (depth + 2)
                e = entrystore.get_entry(iter)

                if e is not None:
                        e.path = self.__get_entryid(entrystore, iter)


                xhtml = ""

                if e is None:

                        xhtml += "<div id=\"content\">\n"
                        xhtml += "      <ul id=\"entrylist\">\n"

                        for i in range(entrystore.iter_n_children(iter)):
                                child = entrystore.iter_nth_child(iter, i)
                                xhtml += self.__generate_entry(entrystore, child, depth)

                        xhtml += "      </ul>\n"
                        xhtml += "</div>\n"


                elif type(e) == entry.FolderEntry:
                        xhtml += tabs + "<li class=\"folder\" id=\"%s\">\n" % e.path
                        xhtml += tabs + "       <div class=\"folder-data\">\n"
                        xhtml += tabs + "               <h2>%s</h2>\n" % e.name

                        if e.description != "":
                                xhtml += tabs + "               <p class=\"description\">%s</p>\n" % e.description

                        xhtml += tabs + "       </div>\n"
                        xhtml += tabs + "\n"
                        xhtml += tabs + "       <ul>\n"

                        for i in range(entrystore.iter_n_children(iter)):
                                child = entrystore.iter_nth_child(iter, i)
                                xhtml += self.__generate_entry(entrystore, child, depth + 2)

                        xhtml += tabs + "       </ul>\n"
                        xhtml += tabs + "</li>\n"
                        xhtml += tabs + "\n"


                else:

                        xhtml += tabs + "<li class=\"account\" id=\"%s\">\n" % e.path
                        xhtml += tabs + "       <div class=\"heading\">\n"
                        xhtml += tabs + "               <img src=\"%s/entry/%s.png\" alt=\"%s\" />\n" % ( IMAGEPATH, e.id, e.typename )
                        xhtml += tabs + "               <h2>%s</h2>\n" % e.name
                        xhtml += tabs + "               <p class=\"description\"><span class=\"type\">%s</span>%s</p>\n" % ( e.typename + (e.description != "" and "; " or ""), e.description )
                        xhtml += tabs + "       </div>\n"
                        xhtml += tabs + "\n"

                        xhtml += tabs + "       <div class=\"data\">\n"

                        fields = []
                        for field in e.fields:
                                if field.value != "":
                                        fields.append(field)

                        if len(fields) > 0:
                                xhtml += tabs + "               <table class=\"fields\">\n"

                                for field in fields:
                                        xhtml += tabs + "                       <tr>\n"
                                        xhtml += tabs + "                               <th>" + field.name + ":</th>\n"
                                        xhtml += tabs + "                               <td>" + field.value + "</td>\n"
                                        xhtml += tabs + "                       </tr>\n"

                                xhtml += tabs + "               </table>\n"

                        xhtml += tabs + "               <p class=\"updated\">Updated " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(e.updated)) + "</p>\n"
                        xhtml += tabs + "       </div>\n"
                        xhtml += tabs + "</li>\n"
                        xhtml += tabs + "\n"

                return xhtml


        def __generate_footer(self):
                "Generates an xhtml footer"

                xhtml = ""
                xhtml += "<div id=\"footer\">\n"
                xhtml += "      Generated by <a href=\"%s\">%s %s</a>" % ( config.URL, config.APPNAME, config.VERSION )
                xhtml += "</div>\n"
                xhtml += "</body>\n"
                xhtml += "</html>\n"

                return xhtml


        def __generate_header(self):
                "Generates an xhtml header"

                xhtml = ""
                xhtml += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
                xhtml += "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
                xhtml += "<head>\n"
                xhtml += "      <title>Revelation account list</title>\n"
                xhtml += "      <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n"
                xhtml += "\n"
                xhtml += "      <style type=\"text/css\">\n"
                xhtml += self.__generate_css()
                xhtml += "      </style>\n"
                xhtml += "</head>\n"
                xhtml += "\n"
                xhtml += "<body>\n"
                xhtml += "<h1>" + _('Revelation account list') + "</h1>\n"
                xhtml += "\n"

                return xhtml


        def __generate_sidebar(self, entrystore):
                "Generates a sidebar"

                xhtml = ""
                xhtml += "<div id=\"sidebar\">\n"
                xhtml += self.__generate_sidebar_fileinfo()
                xhtml += self.__generate_sidebar_foldertree(entrystore)
                xhtml += self.__generate_sidebar_accountlist(entrystore)
                xhtml += "</div>\n"

                return xhtml


        def __generate_sidebar_accountlist(self, entrystore):
                "Generates an account list"

                # find the entries
                entries = {}
                iter = entrystore.iter_nth_child(None, 0)

                while iter is not None:
                        e = entrystore.get_entry(iter)
                        e.path = self.__get_entryid(entrystore, iter)

                        if type(e) != entry.FolderEntry:
                                if not entries.has_key(type(e)):
                                        entries[type(e)] = []

                                entries[type(e)].append(e)

                        iter = entrystore.iter_traverse_next(iter)


                # generate the xhtml
                xhtml = ""
                xhtml += ("     <h2><img src=\"%s/sidebar/accountlist.png\" class=\"icon\" alt=\"Account list\" />" + _('Account list') + "</h2>\n") % IMAGEPATH
                xhtml += "\n"

                for entrytype in entry.ENTRYLIST:
                        if not entries.has_key(entrytype):
                                continue

                        xhtml += "      <h3>%s:</h3>\n" % entrytype().typename
                        xhtml += "\n"
                        xhtml += "      <ul class=\"accountlist\">\n"

                        entrylist = entries[entrytype]
                        entrylist.sort(lambda x,y: cmp(x.name.lower(), y.name.lower()))

                        for e in entrylist:
                                xhtml += "              <li><a href=\"#%s\">%s</a></li>\n" % ( str(e.path), e.name )

                        xhtml += "      </ul>\n"
                        xhtml += "\n"

                return xhtml


        def __generate_sidebar_fileinfo(self):
                "Generates file info for the sidebar"

                xhtml = ""
                xhtml += "      <h2 style=\"margin-top: 0px;\"><img src=\"%s/sidebar/file.png\" class=\"icon\" alt=\"File info\" />File info</h2>\n" % IMAGEPATH
                xhtml += "\n"
                xhtml += "      <h3>" + _('Created:') + "</h3>\n"
                xhtml += "      <p>%s</p>\n" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                xhtml += "\n"
                xhtml += "      <h3>" + _('Revelation version:') + "</h3>\n"
                xhtml += "      <p>%s</p>\n" % config.VERSION
                xhtml += "\n"

                return xhtml


        def __generate_sidebar_foldertree(self, entrystore, parent = None, depth = 0):
                "Generates a folder tree for the sidebar"

                tabs = "\t" * ((depth * 2) - 2)
                xhtml = ""

                if parent is None:
                        xhtml += "      <h2><img src=\"%s/sidebar/foldertree.png\" class=\"icon\" alt=\"Folder tree\" />Folder tree</h2>\n" % IMAGEPATH
                        xhtml += "\n"

                # fetch folder list
                folders = []
                for i in range(entrystore.iter_n_children(parent)):
                        iter = entrystore.iter_nth_child(parent, i)
                        e = entrystore.get_entry(iter)
                        e.path = self.__get_entryid(entrystore, iter)
                        e.iter = iter

                        if type(e) == entry.FolderEntry:
                                folders.append(e)


                # generate xhtml
                if len(folders) > 0:
                        xhtml += tabs + "<ul>\n"

                        for e in folders:
                                childxhtml = self.__generate_sidebar_foldertree(entrystore, e.iter, depth + 1)

                                if childxhtml != "":
                                        xhtml += tabs + "       <li>\n"
                                        xhtml += tabs + "               <a href=\"#%s\">%s</a>\n" % ( str(e.path), e.name )
                                        xhtml += childxhtml
                                        xhtml += tabs + "       </li>\n"

                                else:
                                        xhtml += tabs + "       <li><a href=\"#%s\">%s</a></li>\n" % ( str(e.path), e.name )

                        xhtml += tabs + "</ul>\n"


                return xhtml


        def __get_entryid(self, entrystore, iter):
                "Returns an entry id for an iter"

                path = "entry-"

                for i in entrystore.get_path(iter):
                        path += str(i) + "-"

                path = path[:-1]

                return path


        def export_data(self, entrystore, password = None):
                "Exports data from an entrystore into a XHTML document"

                xhtml = ""
                xhtml += self.__generate_header()
                xhtml += self.__generate_sidebar(entrystore)
                xhtml += self.__generate_entry(entrystore, None)
                xhtml += self.__generate_footer()

                return xhtml