/
update_service_CH_ADMIN.py
106 lines (97 loc) · 4.44 KB
/
update_service_CH_ADMIN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
# © 2008 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from .currency_getter_interface import CurrencyGetterInterface
import logging
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
_logger = logging.getLogger(__name__)
class CH_ADMINGetter(CurrencyGetterInterface):
"""Implementation of Currency_getter_factory interface
for Admin.ch service.
"""
code = 'CH_ADMIN'
name = 'Admin.ch'
supported_currency_array = [
"AED", "ALL", "ARS", "AUD", "AZN", "BAM", "BDT", "BGN", "BHD", "BRL",
"CAD", "CHF", "CLP", "CNY", "COP", "CRC", "CZK", "DKK", "DOP", "EGP",
"ETB", "EUR", "GBP", "GTQ", "HKD", "HNL", "HRK", "HUF", "IDR", "ILS",
"INR", "ISK", "JPY", "KES", "KHR", "KRW", "KWD", "KYD", "KZT", "LBP",
"LKR", "LTL", "LVL", "LYD", "MAD", "MUR", "MXN", "MYR", "NGN", "NOK",
"NZD", "OMR", "PAB", "PEN", "PHP", "PKR", "PLN", "QAR", "RON", "RSD",
"RUB", "SAR", "SEK", "SGD", "THB", "TND", "TRY", "TWD", "TZS", "UAH",
"USD", "UYU", "VEF", "VND", "ZAR"]
def rate_retrieve(self, dom, ns, curr):
"""Parse a dom node to retrieve currencies data"""
res = {}
xpath_rate_currency = ("/def:wechselkurse/def:devise[@code='%s']/"
"def:kurs/text()") % (curr.lower())
xpath_rate_ref = ("/def:wechselkurse/def:devise[@code='%s']/"
"def:waehrung/text()") % (curr.lower())
res['rate_currency'] = float(
dom.xpath(xpath_rate_currency, namespaces=ns)[0]
)
res['rate_ref'] = float(
(dom.xpath(xpath_rate_ref, namespaces=ns)[0]).split(' ')[0]
)
return res
def get_updated_currency(self, currency_array, main_currency,
max_delta_days):
"""Implementation of abstract method of Curreny_getter_interface"""
url = ('http://www.pwebapps.ezv.admin.ch/apps/rates/rate/'
'getxml?activeSearchType=today')
# We do not want to update the main currency
if main_currency in currency_array:
currency_array.remove(main_currency)
# Move to new XML lib cf Launchpad bug #645263
from lxml import etree
_logger.debug("Admin.ch currency rate service : connecting...")
rawfile = self.get_url(url)
dom = etree.fromstring(rawfile)
_logger.debug("Admin.ch sent a valid XML file")
adminch_ns = {
'def': 'http://www.pwebapps.ezv.admin.ch/apps/rates'
}
rate_date = dom.xpath(
'/def:wechselkurse/def:datum/text()',
namespaces=adminch_ns
)
rate_date = rate_date[0]
rate_date_datetime = datetime.strptime(rate_date,
DEFAULT_SERVER_DATE_FORMAT)
self.check_rate_date(rate_date_datetime, max_delta_days)
# we dynamically update supported currencies
self.supported_currency_array = dom.xpath(
"/def:wechselkurse/def:devise/@code",
namespaces=adminch_ns
)
self.supported_currency_array = [x.upper() for x
in self.supported_currency_array]
self.supported_currency_array.append('CHF')
_logger.debug(
"Supported currencies = " + str(self.supported_currency_array)
)
self.validate_cur(main_currency)
if main_currency != 'CHF':
main_curr_data = self.rate_retrieve(dom, adminch_ns, main_currency)
# 1 MAIN_CURRENCY = main_rate CHF
rate_curr = main_curr_data['rate_currency']
rate_ref = main_curr_data['rate_ref']
main_rate = rate_curr / rate_ref
for curr in currency_array:
self.validate_cur(curr)
if curr == 'CHF':
rate = main_rate
else:
curr_data = self.rate_retrieve(dom, adminch_ns, curr)
# 1 MAIN_CURRENCY = rate CURR
if main_currency == 'CHF':
rate = curr_data['rate_ref'] / curr_data['rate_currency']
else:
rate = (main_rate * curr_data['rate_ref'] /
curr_data['rate_currency'])
self.updated_currency[curr] = rate
_logger.debug(
"Rate retrieved : 1 %s = %s %s" % (main_currency, rate, curr)
)
return self.updated_currency, self.log_info