~bkidwell/zim/pyzim-win-installer

« back to all changes in this revision

Viewing changes to zim/plugins/calendar.py

  • Committer: Jaap Karssenberg
  • Date: 2011-08-25 20:48:51 UTC
  • Revision ID: jaap.karssenberg@gmail.com-20110825204851-jn09j13visuhr7wf
Added 3 small checks to prevent crashes

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
year_path_re = re.compile(r'^(.*:)?\d{4}$')
65
65
 
66
66
def daterange_from_path(path):
 
67
        '''Determine the calendar dates mapped by a specific page
 
68
        @param path: a L{Path} object
 
69
        @returns: a 3-tuple of:
 
70
          - the page type (one of "C{day}", "C{week}", "C{month}", or "C{year}")
 
71
          - a C{datetime.date} object for the start date
 
72
          - a C{datetime.date} object for the end date
 
73
        or C{None} when the page does not map a date
 
74
        '''
67
75
        if date_path_re.match(path.name):
68
76
                type = 'day'
69
77
                year, month, day = map(int, path.name.rsplit(':', 3)[-3:])
93
101
                date = datetime.date(year, 1, 1)
94
102
                end_date = datetime.date(year, 12, 31)
95
103
        else:
96
 
                return # Not a calendar path
 
104
                return None# Not a calendar path
97
105
        return type, date, end_date
98
106
 
 
107
 
99
108
class CalendarPlugin(PluginClass):
100
109
 
101
110
        plugin_info = {
114
123
        plugin_preferences = (
115
124
                # key, type, label, default
116
125
                ('embedded', 'bool', _('Show calendar in sidepane instead of as dialog'), False), # T: preferences option
117
 
                ('granularity', 'choice', _('Use a page for each'), 'Day', ['Day', 'Week', 'Month', 'Year']), 
 
126
                ('granularity', 'choice', _('Use a page for each'), 'Day', ['Day', 'Week', 'Month', 'Year']),
118
127
                ('namespace', 'namespace', _('Namespace'), ':Calendar'), # T: input label
119
128
        )
120
129
 
222
231
                or for the template used to create a new page. Will set parameters in
223
232
                the template dict to be used in the template.
224
233
                '''
225
 
                type, date, end_date = daterange_from_path(page)
226
 
                dict['calendar_plugin'] = {
227
 
                        'page_type': type,
228
 
                        'date': date,
229
 
                        'end_date': end_date
230
 
                }
 
234
                daterange = daterange_from_path(page)
 
235
                if daterange:
 
236
                        dict['calendar_plugin'] = {
 
237
                                'page_type': daterange[0],
 
238
                                'date': daterange[1],
 
239
                                'end_date': daterange[2],
 
240
                        }
231
241
 
232
242
        def suggest_link(self, source, text):
233
243
                #~ if date_path_re.match(path.text):