Outil de calcul des fuseaux horaires des employés.
[auf-refer.git] / auf-employes-timezones
CommitLineData
4469f1d5
P
1#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3"""Outil de calcul des fuseaux horaires des employés AUF.
4
5Copyright ©2017 Agence universitaire de la Francophonie
6Licence : GPL version 3
7Auteur : Progfou <jean-christophe.andre@auf.org>
8
9Debian-Depends: python3, python3-tz, auf-refer
10"""
11import sys
12import os.path
13import urllib
14import xml.etree.ElementTree as etree
15import aufrefer
16import pytz # country_timezones
17
18def print_error(*k, **kv):
19 print("\033[1;31m", *k, "\033[m", **kv, sep='', file=sys.stderr)
20
21def get_tz_from_address(country, city=None):
22 tz = pytz.country_timezones(country.upper())
23 if len(tz) == 1:
24 tz = tz[0]
25 elif city == u'Montr\xe9al':
26 tz = u'America/Toronto'
27 elif city == u'Kinshasa':
28 tz = u'Africa/Kinshasa'
29 elif city == u'Lubumbashi':
30 tz = u'Africa/Lubumbashi'
31 elif city == u'S\xe3o Paulo':
32 tz = u'America/Sao_Paulo'
33 else: # show anomalies
34 print_error("Erreur TZ : county='{}',city='{}' => tz='{}'".format(country, city, tz))
35 tz = None
36 return tz
37
38#
39# récupération des fuseaux horaires « à la Microsoft », cf :
40# http://www.unicode.org/cldr/charts/latest/supplemental/zone_tzid.html
41#
42
43microsoft_timezones = {}
44_windowsZones_url = "http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml"
45_windowsZones_filename = 'windowsZones.xml'
46if not os.path.exists(_windowsZones_filename):
47 with urllib.request.urlopen(_windowsZones_url) as u:
48 with open(_windowsZones_filename, 'wb') as f:
49 f.write(u.read())
50if os.path.exists(_windowsZones_filename):
51 tree = etree.parse(_windowsZones_filename)
52 for e in tree.findall('.//mapZone'):
53 for tz in e.get('type').split():
54 microsoft_timezones[tz] = e.get('other')
55
56implantations = {
57 x['id']: x for x in aufrefer.get('datamaster-implantation.json')
58}
59
60print('"Email";"IANA Timezone";"Microsoft Timezone"')
61for employe in aufrefer.get('datamaster-employe.json'):
62 if not employe['courriel']:
63 print_error("Employé sans courriel : {prenom} {nom} ({id})".format(**employe))
64 continue
65 try:
66 implantation = implantations[employe['implantation_physique']]
67 except:
68 print_error("Implantation inexistante ({implantation_physique})"
69 " pour l'employé : {prenom} {nom} ({id})".format(**employe))
70 continue
71 # enrichissement dynamique des données d'implantations
72 if 'tz' not in implantation:
73 implantation['tz'] = get_tz_from_address(
74 country=implantation['adresse_physique_pays'],
75 city=implantation['adresse_physique_ville'] )
76 # résultat
77 print('"{}";"{}";"{}"'.format(employe['courriel'], implantation['tz'],
78 microsoft_timezones.get(implantation['tz'], None)) )