"Fossies" - the Fresh Open Source Software Archive

Member "email-reminder-0.8.1/EmailReminder/EventList.py" (9 Sep 2020, 4436 Bytes) of package /linux/privat/email-reminder-0.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "EventList.py" see the Fossies "Dox" file reference documentation.

    1 # This file is part of Email-Reminder.
    2 #
    3 # Copyright (C) 2020 by Francois Marier
    4 #
    5 # This program is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU Affero General Public License as published by
    7 # the Free Software Foundation, either version 3 of the License, or
    8 # (at your option) any later version.
    9 #
   10 # This program is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 # GNU Affero General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU Affero General Public License
   16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 import sys
   19 from xml.dom.minidom import getDOMImplementation  # nosec
   20 import xml.parsers.expat
   21 
   22 import defusedxml.minidom as minidom
   23 
   24 from EmailReminder.Anniversary import AnniversaryModel
   25 from EmailReminder.Birthday import BirthdayModel
   26 from EmailReminder.Monthly import MonthlyModel
   27 from EmailReminder.Utils import readXMLValue, writeXMLValue
   28 from EmailReminder.Weekly import WeeklyModel
   29 from EmailReminder.Yearly import YearlyModel
   30 
   31 
   32 class EventList:
   33     SUPPORTED_TYPES = ('birthday', 'anniversary', 'yearly', 'monthly',
   34                        'weekly')
   35 
   36     def __init__(self, simulate, verbose):
   37         self.stores = {}
   38         self.filename = None
   39         self.document = None
   40         self.user = None
   41 
   42         self.simulate = simulate
   43         self.verbose = verbose
   44 
   45     def _createDocument(self):
   46         self.document = getDOMImplementation().createDocument(
   47             None, 'email-reminder_user', None)
   48 
   49     def getUser(self):
   50         user = {}
   51 
   52         if not self.user:
   53             return user
   54         for field in ('first_name', 'last_name', 'email'):
   55             user[field] = readXMLValue(self.user, field)
   56 
   57         return user
   58 
   59     def setUser(self, user):
   60         for field in ('first_name', 'last_name', 'email'):
   61             if field in user:
   62                 writeXMLValue(self.document, self.user, field, user[field])
   63 
   64     def load(self, filename):
   65         self.filename = filename
   66         if self.verbose:
   67             print("Loading events from %s" % self.filename)
   68         try:
   69             with open(self.filename) as fh:
   70                 try:
   71                     self.document = minidom.parseString(fh.read())
   72                 except xml.parsers.expat.ExpatError as err:
   73                     if err.code == 3:  # no elements
   74                         self._createDocument()
   75                     else:
   76                         print("Error: '%s' is not a valid configuration file "
   77                               "(%s)" % (filename, err), file=sys.stderr)
   78                         return False
   79         except FileNotFoundError:
   80             self._createDocument()
   81 
   82         users = self.document.getElementsByTagName('email-reminder_user')
   83         self.user = users.item(0)
   84 
   85         events = self.user.getElementsByTagName('events')
   86         if events:
   87             events = events.item(0)
   88         else:
   89             events = self.document.createElement('events')
   90             self.document.documentElement.appendChild(events)
   91 
   92         for event in events.getElementsByTagName('event'):
   93             for eventType in self.SUPPORTED_TYPES:
   94                 if event.getAttribute('type') == eventType:
   95                     self.model(eventType).addEvent(event)
   96                     break
   97 
   98         return True
   99 
  100     def save(self):
  101         if not self.document:
  102             return
  103         if self.simulate:
  104             return
  105 
  106         if self.verbose:
  107             print("Writing events to %s" % self.filename)
  108 
  109         with open(self.filename, 'w') as fh:
  110             fh.write(self.document.toxml())  # TODO: use toprettyxml()?
  111 
  112     def model(self, eventType):
  113         if eventType not in self.stores:
  114             if eventType == 'anniversary':
  115                 self.stores[eventType] = AnniversaryModel(self.document)
  116             elif eventType == 'birthday':
  117                 self.stores[eventType] = BirthdayModel(self.document)
  118             elif eventType == 'yearly':
  119                 self.stores[eventType] = YearlyModel(self.document)
  120             elif eventType == 'monthly':
  121                 self.stores[eventType] = MonthlyModel(self.document)
  122             elif eventType == 'weekly':
  123                 self.stores[eventType] = WeeklyModel(self.document)
  124         return self.stores[eventType]