6741d19ec5be2e76369d84bd705bde8767972e00aurium#!/usr/bin/env python
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner'''
6741d19ec5be2e76369d84bd705bde8767972e00auriumcalendar.py
6741d19ec5be2e76369d84bd705bde8767972e00auriumA calendar generator plugin for Inkscape, but also can be used as a standalone
6741d19ec5be2e76369d84bd705bde8767972e00auriumcommand line application.
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerCopyright (C) 2008 Aurelio A. Heckert <aurium(a)gmail.com>
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicoWeek number option added by Olav Vitters and Nicolas Dufour (2012)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicoMore on ISO week number calculation on:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicohttp://en.wikipedia.org/wiki/ISO_week_date
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico(The first week of a year is the week that contains the first Thursday
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicoof the year.)
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerThis program is free software; you can redistribute it and/or modify
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerit under the terms of the GNU General Public License as published by
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerthe Free Software Foundation; either version 2 of the License, or
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner(at your option) any later version.
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerThis program is distributed in the hope that it will be useful,
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerbut WITHOUT ANY WARRANTY; without even the implied warranty of
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerGNU General Public License for more details.
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerYou should have received a copy of the GNU General Public License
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenneralong with this program; if not, write to the Free Software
107e00c8104649437b9520d0ba298dba659e7cd7JazzyNicoFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner'''
6741d19ec5be2e76369d84bd705bde8767972e00aurium
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico__version__ = "0.3"
6741d19ec5be2e76369d84bd705bde8767972e00aurium
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicoimport calendar
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicoimport re
6741d19ec5be2e76369d84bd705bde8767972e00auriumfrom datetime import *
6741d19ec5be2e76369d84bd705bde8767972e00aurium
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicoimport inkex
e866cf6004410b86765759ca47d88ae2052306bfJazzyNicoimport simplestyle
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico
29efdf67a8f4416962af16ca1864a4832ea92e40apennerinkex.localize()
29efdf67a8f4416962af16ca1864a4832ea92e40apenner
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpennerclass SVGCalendar (inkex.Effect):
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner def __init__(self):
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner inkex.Effect.__init__(self)
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner self.OptionParser.add_option("--tab",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="tab")
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner self.OptionParser.add_option("--month",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="int",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="month", default=0,
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help="Month to be generated. If 0, then the entry year will be generated.")
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--year",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="int",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="year", default=0,
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help="Year to be generated. If 0, then the current year will be generated.")
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--fill-empty-day-boxes",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="inkbool",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="fill_edb", default=True,
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help="Fill empty day boxes with next month days.")
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.OptionParser.add_option("--show-week-number",
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico action="store", type="inkbool",
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico dest="show_weeknr", default=False,
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico help="Include a week number column.")
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--start-day",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="start_day", default="sun",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Week start day. ("sun" or "mon")')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--weekend",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="weekend", default="sat+sun",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Define the weekend days. ("sat+sun" or "sat" or "sun")')
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium self.OptionParser.add_option("--auto-organize",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium action="store", type="inkbool",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium dest="auto_organize", default=True,
63e3b8a7c227f111c060e81e1941679a156b1999helix help='Automatically set the size and positions.')
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium self.OptionParser.add_option("--months-per-line",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium action="store", type="int",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium dest="months_per_line", default=3,
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium help='Number of months side by side.')
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium self.OptionParser.add_option("--month-width",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium action="store", type="string",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium dest="month_width", default="6cm",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium help='The width of the month days box.')
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium self.OptionParser.add_option("--month-margin",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium action="store", type="string",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium dest="month_margin", default="1cm",
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium help='The space between the month boxes.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--color-year",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="color_year", default="#888",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Color for the year header.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--color-month",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="color_month", default="#666",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Color for the month name header.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--color-day-name",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="color_day_name", default="#999",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Color for the week day names header.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--color-day",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="color_day", default="#000",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Color for the common day box.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--color-weekend",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="color_weekend", default="#777",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Color for the weekend days.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--color-nmd",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="color_nmd", default="#BBB",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='Color for the next month day, in enpty day boxes.')
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.OptionParser.add_option("--color-weeknr",
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico action="store", type="string",
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico dest="color_weeknr", default="#808080",
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico help='Color for the week numbers.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--month-names",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
bad6d1a999834aab8f41efe219e093f4a057545aapenner dest="month_names", default='January February March ' + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'April May June '+ \
bad6d1a999834aab8f41efe219e093f4a057545aapenner 'July August September ' + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'October November December',
6741d19ec5be2e76369d84bd705bde8767972e00aurium help='The month names for localization.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.OptionParser.add_option("--day-names",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner action="store", type="string",
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner dest="day_names", default='Sun Mon Tue Wed Thu Fri Sat',
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner help='The week day names for localization.')
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.OptionParser.add_option("--weeknr-name",
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico action="store", type="string",
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico dest="weeknr_name", default='Wk',
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico help='The week number column name for localization.')
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium self.OptionParser.add_option("--encoding",
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium action="store", type="string",
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium dest="input_encode", default='utf-8',
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium help='The input encoding of the names.')
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium def validate_options(self):
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium #inkex.errormsg( self.options.input_encode )
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # Convert string names lists in real lists
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico m = re.match('\s*(.*[^\s])\s*', self.options.month_names)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.options.month_names = re.split('\s+', m.group(1))
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico m = re.match('\s*(.*[^\s])\s*', self.options.day_names)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.options.day_names = re.split('\s+', m.group(1))
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # Validate names lists
6741d19ec5be2e76369d84bd705bde8767972e00aurium if len(self.options.month_names) != 12:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.errormsg('The month name list "' + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico str(self.options.month_names) + \
6741d19ec5be2e76369d84bd705bde8767972e00aurium '" is invalid. Using default.')
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.options.month_names = ['January', 'February', 'March',
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'April', 'May', 'June',
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'July', 'August', 'September',
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'October', 'November', 'December']
6741d19ec5be2e76369d84bd705bde8767972e00aurium if len(self.options.day_names) != 7:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.errormsg('The day name list "'+
6741d19ec5be2e76369d84bd705bde8767972e00aurium str(self.options.day_names)+
6741d19ec5be2e76369d84bd705bde8767972e00aurium '" is invalid. Using default.')
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.options.day_names = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu',
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'Fri','Sat']
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # Convert year 0 to current year
6741d19ec5be2e76369d84bd705bde8767972e00aurium if self.options.year == 0: self.options.year = datetime.today().year
52bc35e1956391dd3b08ecbedd022da25e746c9cAurélio A. Heckert (a) # Year 1 starts it's week at monday, obligatorily
52bc35e1956391dd3b08ecbedd022da25e746c9cAurélio A. Heckert (a) if self.options.year == 1: self.options.start_day = 'mon'
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # Set the calendar start day
6741d19ec5be2e76369d84bd705bde8767972e00aurium if self.options.start_day=='sun':
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico calendar.setfirstweekday(6)
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico calendar.setfirstweekday(0)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # Convert string numbers with unit to user space float numbers
3626239912f1d2e7effe313bd1fab54e10364f09Johan B. C. Engelen self.options.month_width = self.unittouu( self.options.month_width )
3626239912f1d2e7effe313bd1fab54e10364f09Johan B. C. Engelen self.options.month_margin = self.unittouu( self.options.month_margin )
6741d19ec5be2e76369d84bd705bde8767972e00aurium
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # initial values
6741d19ec5be2e76369d84bd705bde8767972e00aurium month_x_pos = 0
6741d19ec5be2e76369d84bd705bde8767972e00aurium month_y_pos = 0
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico weeknr = 0
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium def calculate_size_and_positions(self):
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium #month_margin month_width months_per_line auto_organize
3626239912f1d2e7effe313bd1fab54e10364f09Johan B. C. Engelen self.doc_w = self.unittouu(self.document.getroot().get('width'))
3626239912f1d2e7effe313bd1fab54e10364f09Johan B. C. Engelen self.doc_h = self.unittouu(self.document.getroot().get('height'))
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.show_weeknr:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.cols_before = 1
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.cols_before = 0
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium if self.options.auto_organize:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.doc_h > self.doc_w:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.months_per_line = 3
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.months_per_line = 4
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.months_per_line = self.options.months_per_line
6741d19ec5be2e76369d84bd705bde8767972e00aurium #self.month_w = self.doc_w / self.months_per_line
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium if self.options.auto_organize:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.month_w = (self.doc_w * 0.8) / self.months_per_line
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.month_margin = self.month_w / 10
03f0cd31db1136c686a9b7e279766cc171dfae5aaurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.month_w = self.options.month_width
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.month_margin = self.options.month_margin
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.day_w = self.month_w / (7 + self.cols_before)
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.day_h = self.month_w / 9
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.month_h = self.day_w * 7
6741d19ec5be2e76369d84bd705bde8767972e00aurium if self.options.month == 0:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.year_margin = ((self.doc_w + self.day_w - \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.month_w * self.months_per_line) - \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.month_margin * \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.months_per_line - 1))) / 2) #- self.month_margin
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.year_margin = (self.doc_w - self.month_w) / 2
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_day = {
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'font-size': str(self.day_w / 2),
6741d19ec5be2e76369d84bd705bde8767972e00aurium 'font-family': 'arial',
6741d19ec5be2e76369d84bd705bde8767972e00aurium 'text-anchor': 'middle',
6741d19ec5be2e76369d84bd705bde8767972e00aurium 'text-align': 'center',
6741d19ec5be2e76369d84bd705bde8767972e00aurium 'fill': self.options.color_day
6741d19ec5be2e76369d84bd705bde8767972e00aurium }
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_weekend = self.style_day.copy()
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_weekend['fill'] = self.options.color_weekend
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_nmd = self.style_day.copy()
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_nmd['fill'] = self.options.color_nmd
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_month = self.style_day.copy()
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_month['fill'] = self.options.color_month
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.style_month['font-size'] = str(self.day_w / 1.5)
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_month['font-weight'] = 'bold'
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_day_name = self.style_day.copy()
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_day_name['fill'] = self.options.color_day_name
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.style_day_name['font-size'] = str(self.day_w / 3 )
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_year = self.style_day.copy()
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_year['fill'] = self.options.color_year
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.style_year['font-size'] = str(self.day_w * 2)
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.style_year['font-weight'] = 'bold'
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.style_weeknr = self.style_day.copy()
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.style_weeknr['fill'] = self.options.color_weeknr
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.style_weeknr['font-size'] = str(self.day_w / 3)
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium def is_weekend(self, pos):
6741d19ec5be2e76369d84bd705bde8767972e00aurium # weekend values: "sat+sun" or "sat" or "sun"
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.start_day == 'sun':
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sat+sun' and pos == 0: return True
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sat+sun' and pos == 6: return True
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sat' and pos == 6: return True
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sun' and pos == 0: return True
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sat+sun' and pos == 5: return True
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sat+sun' and pos == 6: return True
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sat' and pos == 5: return True
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.weekend == 'sun' and pos == 6: return True
6741d19ec5be2e76369d84bd705bde8767972e00aurium return False
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium def in_line_month(self, cal):
6741d19ec5be2e76369d84bd705bde8767972e00aurium cal2 = []
6741d19ec5be2e76369d84bd705bde8767972e00aurium for week in cal:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico for day in week:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if day != 0:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico cal2.append(day)
6741d19ec5be2e76369d84bd705bde8767972e00aurium return cal2
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium def write_month_header(self, g, m):
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner txt_atts = {'style': simplestyle.formatStyle(self.style_month),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'x': str((self.month_w - self.day_w) / 2),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'y': str(self.day_h / 5 )}
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium try:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.etree.SubElement(g, 'text', txt_atts).text = unicode(
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.options.month_names[m-1],
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.options.input_encode)
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium except:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.errormsg(_('You must select a correct system encoding.'))
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico exit(1)
6741d19ec5be2e76369d84bd705bde8767972e00aurium gw = inkex.etree.SubElement(g, 'g')
6741d19ec5be2e76369d84bd705bde8767972e00aurium week_x = 0
6741d19ec5be2e76369d84bd705bde8767972e00aurium if self.options.start_day=='sun':
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico day_names = self.options.day_names[:]
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico day_names = self.options.day_names[1:]
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico day_names.append(self.options.day_names[0])
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.show_weeknr:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico day_names.insert(0, self.options.weeknr_name)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico for wday in day_names:
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner txt_atts = {'style': simplestyle.formatStyle(self.style_day_name),
6741d19ec5be2e76369d84bd705bde8767972e00aurium 'x': str( self.day_w * week_x ),
6741d19ec5be2e76369d84bd705bde8767972e00aurium 'y': str( self.day_h ) }
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium try:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.etree.SubElement(gw, 'text', txt_atts).text = unicode(
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico wday,
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.options.input_encode)
d04fdb8e3c74ab565590728d96c52b5f3bbedf98aurium except:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.errormsg(_('You must select a correct system encoding.'))
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico exit(1)
6741d19ec5be2e76369d84bd705bde8767972e00aurium week_x += 1
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium def create_month(self, m):
6741d19ec5be2e76369d84bd705bde8767972e00aurium txt_atts = {
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'transform': 'translate(' +
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico str(self.year_margin + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.month_w + self.month_margin) * \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.month_x_pos) + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico ',' + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico str((self.day_h * 4) + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.month_h * self.month_y_pos)) + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico ')',
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'id': 'month_' + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico str(m) + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico '_' + \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico str(self.options.year)}
6741d19ec5be2e76369d84bd705bde8767972e00aurium g = inkex.etree.SubElement(self.year_g, 'g', txt_atts)
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.write_month_header(g, m)
6741d19ec5be2e76369d84bd705bde8767972e00aurium gdays = inkex.etree.SubElement(g, 'g')
6741d19ec5be2e76369d84bd705bde8767972e00aurium cal = calendar.monthcalendar(self.options.year,m)
6741d19ec5be2e76369d84bd705bde8767972e00aurium if m == 1:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.year > 1:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico before_month = \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.in_line_month(calendar.monthcalendar(self.options.year - 1, 12))
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico before_month = \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.in_line_month(calendar.monthcalendar(self.options.year, m - 1))
6741d19ec5be2e76369d84bd705bde8767972e00aurium if m == 12:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico next_month = \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.in_line_month(calendar.monthcalendar(self.options.year + 1, 1))
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico next_month = \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.in_line_month(calendar.monthcalendar(self.options.year, m + 1))
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if len(cal) < 6:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # add a line after the last week
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico cal.append([0, 0, 0, 0, 0, 0, 0])
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if len(cal) < 6:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # add a line before the first week (Feb 2009)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico cal.reverse()
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico cal.append([0, 0, 0, 0, 0, 0, 0])
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico cal.reverse()
6741d19ec5be2e76369d84bd705bde8767972e00aurium # How mutch before month days will be showed:
6741d19ec5be2e76369d84bd705bde8767972e00aurium bmd = cal[0].count(0) + cal[1].count(0)
6741d19ec5be2e76369d84bd705bde8767972e00aurium before = True
6741d19ec5be2e76369d84bd705bde8767972e00aurium week_y = 0
6741d19ec5be2e76369d84bd705bde8767972e00aurium for week in cal:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if (self.weeknr != 0 and \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico ((self.options.start_day == 'mon' and week[0] != 0) or \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.options.start_day == 'sun' and week[1] != 0))) or \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.weeknr == 0 and \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico ((self.options.start_day == 'mon' and week[3] > 0) or \
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico (self.options.start_day == 'sun' and week[4] > 0))):
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.weeknr += 1
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico week_x = 0
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.options.show_weeknr:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico # Remove leap week (starting previous year) and empty weeks
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.weeknr != 0 and not (week[0] == 0 and week[6] == 0):
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico style = self.style_weeknr
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico txt_atts = {'style': simplestyle.formatStyle(style),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'x': str(self.day_w * week_x),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'y': str(self.day_h * (week_y + 2))}
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.etree.SubElement(gdays, 'text', txt_atts).text = str(self.weeknr)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico week_x += 1
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico week_x += 1
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico for day in week:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico style = self.style_day
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if self.is_weekend(week_x - self.cols_before): style = self.style_weekend
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if day == 0: style = self.style_nmd
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico txt_atts = {'style': simplestyle.formatStyle(style),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'x': str(self.day_w * week_x),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'y': str(self.day_h * (week_y + 2))}
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if day == 0 and not self.options.fill_edb:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico pass # draw nothing
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico elif day == 0:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico if before:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.etree.SubElement(gdays, 'text', txt_atts).text = str(before_month[-bmd])
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico bmd -= 1
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.etree.SubElement(gdays, 'text', txt_atts).text = str(next_month[bmd])
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico bmd += 1
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico inkex.etree.SubElement(gdays, 'text', txt_atts).text = str(day)
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico before = False
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico week_x += 1
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico week_y += 1
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.month_x_pos += 1
6741d19ec5be2e76369d84bd705bde8767972e00aurium if self.month_x_pos >= self.months_per_line:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.month_x_pos = 0
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.month_y_pos += 1
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium def effect(self):
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.validate_options()
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.calculate_size_and_positions()
6741d19ec5be2e76369d84bd705bde8767972e00aurium parent = self.document.getroot()
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico txt_atts = {'id': 'year_'+str(self.options.year) }
6741d19ec5be2e76369d84bd705bde8767972e00aurium self.year_g = inkex.etree.SubElement(parent, 'g', txt_atts)
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner txt_atts = {'style': simplestyle.formatStyle(self.style_year),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'x': str(self.doc_w / 2 ),
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico 'y': str(self.day_w * 1.5)}
6741d19ec5be2e76369d84bd705bde8767972e00aurium inkex.etree.SubElement(self.year_g, 'text', txt_atts).text = str(self.options.year)
6741d19ec5be2e76369d84bd705bde8767972e00aurium if self.options.month == 0:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico for m in range(1, 13):
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.create_month(m)
6741d19ec5be2e76369d84bd705bde8767972e00aurium else:
e866cf6004410b86765759ca47d88ae2052306bfJazzyNico self.create_month(self.options.month)
6741d19ec5be2e76369d84bd705bde8767972e00aurium
6741d19ec5be2e76369d84bd705bde8767972e00aurium
83aaf46c9feab529aeb9add871c05c3d1177afccauriumif __name__ == '__main__': #pragma: no cover
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner e = SVGCalendar()
e0cdf8e0ac7cc0bfd89e9166fea83c1b01e8c246alvinpenner e.affect()