!1 Package Init

Merge pull request !1 from 窃格瓦拉/master
This commit is contained in:
openeuler-ci-bot 2020-02-17 20:11:19 +08:00 committed by Gitee
commit 460c5b4968
3 changed files with 223 additions and 0 deletions

View File

@ -0,0 +1,152 @@
From 9d1129f41f193a47d6791f44f14abe9479999266 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Wed, 8 Aug 2018 17:42:17 +0200
Subject: [PATCH] units_cur: validate rate data from server
---
units_cur | 72 ++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 55 insertions(+), 17 deletions(-)
diff --git a/units_cur b/units_cur
index 00281d8..d625570 100755
--- a/units_cur
+++ b/units_cur
@@ -28,8 +28,12 @@ from __future__ import absolute_import, division, print_function
#
#
-version = '4.2'
+version = '4.3'
+# Version 4.3: 20 July 2018
+#
+# Validate rate data from server
+#
# Version 4.2: 18 April 2018
#
# Handle case of empty/malformed entry returned from the server
@@ -55,6 +59,10 @@ from sys import exit, stderr, stdout
outfile_name = 'currency.units'
+# valid metals
+
+validmetals = ['silver','gold','platinum']
+
# This exchange rate table lists the currency ISO 4217 codes, their
# long text names, and any fixed definitions. If the definition is
# empty then units_cur will query the server for a value.
@@ -271,11 +279,19 @@ ap.add_argument('-v','--verbose',
help='display details when fetching currency data',
)
+
+def validfloat(x):
+ try:
+ float(x)
+ return True
+ except ValueError:
+ return False
+
outfile_name = ap.parse_args().output_file
verbose = ap.parse_args().verbose
try:
- res = requests.get('http://finance.yahoo.com/webservice/v1/symbols'
+ res = requests.get('https://finance.yahoo.com/webservice/v1/symbols'
'/allcurrencies/quote?format=json')
res.raise_for_status()
webdata = res.json()['list']['resources']
@@ -299,10 +315,16 @@ for data in webdata:
stderr.write('Got unknown currency with code {}\n'.format(code))
else:
if not currency[code][rate_index]:
- currency[code][rate_index] = '1|{} US$'.format(rate)
+ if validfloat(rate):
+ currency[code][rate_index] = '1|{} US$'.format(rate)
+ else:
+ stderr.write('Got invalid rate "{}" for currency "{}"\n'.format(
+ rate, code))
elif verbose:
- stderr.write('Got value "{}" for currency "{}" but '
- 'it is already defined\n'.format(rate, code))
+ if currency[code][rate_index] != '1|{} US$'.format(rate):
+ stderr.write('Got value "{}" for currency "{}" but '
+ 'it is already defined as {}\n'.format(rate, code,
+ currency[code][rate_index]))
# Delete currencies where we have no rate data
@@ -313,17 +335,15 @@ for code in currency.keys():
del currency[code]
try:
- req = requests.get('http://services.packetizer.com/spotprices/?f=json')
+ req = requests.get('https://services.packetizer.com/spotprices/?f=json')
req.raise_for_status()
metals = req.json()
except requests.exceptions.RequestException as e:
stderr.write('Error connecting to spotprices server:\n{}\n'.format(e))
exit(1)
-del metals['date']
-
try:
- req = requests.get('http://services.packetizer.com/btc/?f=json')
+ req = requests.get('https://services.packetizer.com/btc/?f=json')
req.raise_for_status()
bitcoin = req.json()
except requests.exceptions.RequestException as e:
@@ -344,13 +364,31 @@ ratestr = '\n'.join(
'{:{}}{}'.format(name, maxlen, rate) for (name, rate) in zip(cnames, crates)
)
-ozzystr = '\n'.join('{:19}{} US$/troyounce'.format(
- metal + 'price',
- price,
- ) for metal, price in metals.items())
-
-bitcoinstr = '{:{}}{} US$ # From services.packetizer.com/btc\n'.format(
+metallist = ['']*len(validmetals)
+for metal, price in metals.items():
+ if metal in validmetals:
+ metalindex = validmetals.index(metal)
+ if validfloat(price):
+ if not metallist[metalindex]:
+ metallist[validmetals.index(metal)] = '{:19}{} US$/troyounce'.format(
+ metal + 'price', price)
+ elif verbose:
+ stderr.write('Got value "{}" for metal "{}" but '
+ 'it is already defined\n'.format(price,metal))
+ else:
+ stderr.write('Got invalid rate "{}" for metal "{}"\n'.format(
+ price, metal))
+ elif metal != 'date' and verbose: # Don't print a message for the "date" entry
+ stderr.write('Got unknown metal "{}" with value "{}"\n',metal,price)
+metalstr = '\n'.join(metallist)
+
+if validfloat(bitcoin['usd']):
+ bitcoinstr = '{:{}}{} US$ # From services.packetizer.com/btc\n'.format(
'bitcoin',maxlen,bitcoin['usd'])
+else:
+ stderr.write('Got invalid bitcoin rate "{}"\n', bitcoint['usd'])
+ bitcointstr=''
+
outstr = (
"""# ISO Currency Codes
@@ -366,9 +404,9 @@ outstr = (
# Precious metals prices from Packetizer (services.packetizer.com/spotprices)
-{ozzystr}
+{metalstr}
-""".format(codestr=codestr, datestr=datestr, ratestr=ratestr, ozzystr=ozzystr,
+""".format(codestr=codestr, datestr=datestr, ratestr=ratestr, metalstr=metalstr,
bitcoinstr=bitcoinstr)
).replace('\n', linesep)
--
2.17.1

BIN
units-2.17.tar.gz Normal file

Binary file not shown.

71
units.spec Normal file
View File

@ -0,0 +1,71 @@
Name: units
Version: 2.17
Release: 8
Summary: A utility for converting amounts from one unit to another
License: GPLv3+
URL: https://www.gnu.org/software/units/units.html
Source: https://ftp.gnu.org/gnu/units/%{name}-%{version}.tar.gz
BuildRequires: automake bison gcc ncurses-devel python3-devel readline-devel
Requires(post): /sbin/install-info
Requires(preun): /sbin/install-info
Patch0001: 0001-units-2.17-units_cur-validate.patch
%description
Units is a program for units conversion and units calculation.
The program converts quantities expressed in various scales to
their equivalents in other scales.
Units can only handle multiplicative scale changes.
For example, it cannot convert Celsius to Fahrenheit
but it can convert temperature differences between those temperature scales.
%package help
Summary: Help documents for units
%description help
The units-help package conatins manual pages and other related files for units.
%prep
%autosetup -p1
sed -e 's|^AC_PATH_PROG(PYTHON, .*$|PYTHON=%{__python3}\nAC_SUBST(PYTHON)|' \
-i configure.ac
autoreconf -fiv
%build
%configure
%make_build
%install
%make_install
gzip $RPM_BUILD_ROOT%{_infodir}/units.info
ln -s units.1 %{buildroot}%{_mandir}/man1/units_cur.1
%check
make check
%post
if [ -e %{_infodir}/units.info.gz ]; then
/sbin/install-info %{_infodir}/units.info.gz %{_infodir}/dir || :
fi
%preun
if [ $1 = 0 -a -e %{_infodir}/units.info.gz ]; then
/sbin/install-info --delete %{_infodir}/units.info.gz %{_infodir}/dir || :
fi
%files
%doc COPYING ChangeLog NEWS README
%{_bindir}/*
%{_datadir}/units
%{_sharedstatedir}/units
%files help
%{_infodir}/*
%{_mandir}/man1/*
%changelog
* Fri Feb 07 2020 lihao <lihao129@huawei.com> - 2.17-8
- Package init