• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

twaugh / diaro-render / 16

09 Jan 2018 06:07PM UTC coverage: 74.648%. Remained the same
16

Pull #3

travis-ci

web-flow
Update datestamp style, include folder title
Pull Request #3: Output fixes

0 of 1 new or added line in 1 file covered. (0.0%)

106 of 142 relevant lines covered (74.65%)

1.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.83
/diaro_render/cli/main.py
1
"""
2
Render Diaro data format into HTML
3

4
Copyright (C) 2017
5
Authors:
6
  Tim Waugh <tim@cyberelk.net>
7

8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12

13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17

18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
"""
22

23
from __future__ import absolute_import
2✔
24
from argparse import ArgumentParser
2✔
25
from diaro_render.data import Diaro
2✔
26
from datetime import datetime, timedelta
2✔
27
import os.path
2✔
28

29

30
class CLI(object):
2✔
31
    def __init__(self, args=None):
2✔
32
        parser = ArgumentParser('diaro-render')
2✔
33
        parser.add_argument('file', metavar='FILE', nargs=1,
2✔
34
                            help='path to DiaroBackup.xml')
35
        parser.add_argument('--folder', metavar='UID', action='append',
2✔
36
                            help='folder UID to filter by '
37
                            '(may be given more than once)')
38
        parser.add_argument('--mediapath', help='path to media files',
2✔
39
                            default='')
40
        parser.add_argument('--thumbsuffix', help='suffix for media thumbnails',
2✔
41
                            default='')
42
        parser.add_argument('--summary', action='store_true',
2✔
43
                            help='show summary instead of HTML output')
44
        parser.add_argument('--only-year', help='only render entries from year')
2✔
45
        self.namespace = parser.parse_args(args=args)
2✔
46

47
    def run(self):
2✔
48
        diaro = Diaro(self.namespace.file[0])
2✔
49
        if self.namespace.folder is None:
2✔
50
            # Display folders
51
            for uid, folder in diaro.folders.items():
2✔
52
                print("{uid}: {title}".format(uid=uid, title=folder.title))
×
53

54
            return
2✔
55

56
        entries = diaro.get_entries_for_folders(self.namespace.folder)
×
57

58
        if self.namespace.only_year:
×
59
            only_year = int(self.namespace.only_year)
×
60
            year_entries = [(datetime.fromtimestamp(entry.date / 1000.0).year,
×
61
                             entry) for entry in entries]
62
            entries = [entry for year, entry in year_entries
×
63
                       if year == only_year]
64

65
        if self.namespace.summary:
×
66
            for entry in entries:
×
67
                print("{date}: {title}".format(date=entry.date,
×
68
                                               title=entry.title))
69
            return
×
70

71
        # render HTML
72
        mediapath = self.namespace.mediapath
×
73
        thumbsuffix = self.namespace.thumbsuffix
×
74
        for entry in entries:
×
75
            dt = datetime.fromtimestamp(entry.date / 1000.0)
×
76
            date = dt.strftime('%A %d %B %Y')
×
77
            time = dt.strftime('%H:%M')
×
78
            photo = ''
×
79
            attachments = diaro.get_attachments_for_entry(entry.uid)
×
80
            for attachment in attachments:
×
81
                assert attachment.type == 'photo'
×
NEW
82
                fmt = '<div><a href="{imgfullpath}"><img src="{imgthumbpath}" alt="" /></a></div>'
×
83
                filename, ext = os.path.splitext(attachment.filename)
×
84
                photo += fmt.format(
×
85
                    imgfullpath=os.path.join(mediapath,
86
                                             attachment.filename),
87
                    imgthumbpath=os.path.join(mediapath,
88
                                              filename + thumbsuffix + ext))
89

90
            print("""\
×
91
<div>
92
  <!-- entry -->
93
  <h3>{title}</h3>
94
  <small><b>{date}</b> <i>{time}</i> ({foldertitle})</small>
95
  <p>{text}</p>
96
  {photo}
97
</div>
98
""".format(date=date, time=time,
99
           foldertitle=diaro.folders[entry.folder_uid].title,
100
           title=entry.title,
101
           text=entry.text,
102
           photo=photo))
103

104
def main():
2✔
105
    CLI().run()
×
106

107

108
if __name__ == '__main__':
2✔
109
    # Run directly:
110
    # python -m diaro_render.cli.main <FILE>
111
    main()
×
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc