#!/usr/bin/python3 # -*- coding: utf-8 -*- """Outil de calcul des fuseaux horaires des employés AUF. Copyright ©2017 Agence universitaire de la Francophonie Licence : GPL version 3 Auteur : Progfou Debian-Depends: python3, python3-tz, auf-refer """ import sys import os.path import urllib import xml.etree.ElementTree as etree import aufrefer import pytz # country_timezones def print_error(*k, **kv): print("\033[1;31m", *k, "\033[m", **kv, sep='', file=sys.stderr) def get_tz_from_address(country, city=None): tz = pytz.country_timezones(country.upper()) if len(tz) == 1: tz = tz[0] elif city == u'Montr\xe9al': tz = u'America/Toronto' elif city == u'Kinshasa': tz = u'Africa/Kinshasa' elif city == u'Lubumbashi': tz = u'Africa/Lubumbashi' elif city == u'S\xe3o Paulo': tz = u'America/Sao_Paulo' else: # show anomalies print_error("Erreur TZ : county='{}',city='{}' => tz='{}'".format(country, city, tz)) tz = None return tz # # récupération des fuseaux horaires « à la Microsoft », cf : # http://www.unicode.org/cldr/charts/latest/supplemental/zone_tzid.html # microsoft_timezones = {} _windowsZones_url = "http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml" _windowsZones_filename = 'windowsZones.xml' if not os.path.exists(_windowsZones_filename): with urllib.request.urlopen(_windowsZones_url) as u: with open(_windowsZones_filename, 'wb') as f: f.write(u.read()) if os.path.exists(_windowsZones_filename): tree = etree.parse(_windowsZones_filename) for e in tree.findall('.//mapZone'): for tz in e.get('type').split(): microsoft_timezones[tz] = e.get('other') implantations = { x['id']: x for x in aufrefer.get('datamaster-implantation.json') } print('"Email";"IANA Timezone";"Microsoft Timezone"') for employe in aufrefer.get('datamaster-employe.json'): if not employe['courriel']: print_error("Employé sans courriel : {prenom} {nom} ({id})".format(**employe)) continue try: implantation = implantations[employe['implantation_physique']] except: print_error("Implantation inexistante ({implantation_physique})" " pour l'employé : {prenom} {nom} ({id})".format(**employe)) continue # enrichissement dynamique des données d'implantations if 'tz' not in implantation: implantation['tz'] = get_tz_from_address( country=implantation['adresse_physique_pays'], city=implantation['adresse_physique_ville'] ) # résultat print('"{}";"{}";"{}"'.format(employe['courriel'], implantation['tz'], microsoft_timezones.get(implantation['tz'], None)) )