2020-12-30 08:55:44 +00:00
|
|
|
import re
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
2021-01-14 23:13:11 +00:00
|
|
|
def expand_pairlist(wildcardpl: List[str], available_pairs: List[str],
|
|
|
|
keep_invalid: bool = False) -> List[str]:
|
2020-12-30 08:55:44 +00:00
|
|
|
"""
|
2020-12-30 09:14:22 +00:00
|
|
|
Expand pairlist potentially containing wildcards based on available markets.
|
|
|
|
This will implicitly filter all pairs in the wildcard-list which are not in available_pairs.
|
|
|
|
:param wildcardpl: List of Pairlists, which may contain regex
|
2020-12-30 09:21:05 +00:00
|
|
|
:param available_pairs: List of all available pairs (`exchange.get_markets().keys()`)
|
2021-01-14 23:13:11 +00:00
|
|
|
:param keep_invalid: If sets to True, drops invalid pairs silently while expanding regexes
|
2020-12-30 09:14:22 +00:00
|
|
|
:return expanded pairlist, with Regexes from wildcardpl applied to match all available pairs.
|
|
|
|
:raises: ValueError if a wildcard is invalid (like '*/BTC' - which should be `.*/BTC`)
|
2020-12-30 08:55:44 +00:00
|
|
|
"""
|
|
|
|
result = []
|
2021-01-14 23:13:11 +00:00
|
|
|
if keep_invalid:
|
|
|
|
for pair_wc in wildcardpl:
|
|
|
|
try:
|
|
|
|
comp = re.compile(pair_wc)
|
|
|
|
result_partial = [
|
2021-01-23 19:35:10 +00:00
|
|
|
pair for pair in available_pairs if re.fullmatch(comp, pair)
|
2021-01-14 23:13:11 +00:00
|
|
|
]
|
|
|
|
# Add all matching pairs.
|
|
|
|
# If there are no matching pairs (Pair not on exchange) keep it.
|
|
|
|
result += result_partial or [pair_wc]
|
|
|
|
except re.error as err:
|
|
|
|
raise ValueError(f"Wildcard error in {pair_wc}, {err}")
|
|
|
|
|
|
|
|
for element in result:
|
|
|
|
if not re.fullmatch(r'^[A-Za-z0-9/-]+$', element):
|
|
|
|
result.remove(element)
|
|
|
|
else:
|
|
|
|
for pair_wc in wildcardpl:
|
|
|
|
try:
|
|
|
|
comp = re.compile(pair_wc)
|
|
|
|
result += [
|
2021-01-23 19:35:10 +00:00
|
|
|
pair for pair in available_pairs if re.fullmatch(comp, pair)
|
2021-01-14 23:13:11 +00:00
|
|
|
]
|
|
|
|
except re.error as err:
|
|
|
|
raise ValueError(f"Wildcard error in {pair_wc}, {err}")
|
2020-12-30 08:55:44 +00:00
|
|
|
return result
|