| 1 |
from ..utils.search_pattern import wildcard_match |
| 2 |
|
| 3 |
|
| 4 |
class DefaultDynamicNaming(object): |
| 5 |
""" |
| 6 |
Decides what name to use on a segment generated from an incoming request. |
| 7 |
By default it takes the host name and compares it to a pre-defined pattern. |
| 8 |
If the host name matches that pattern, it returns the host name, otherwise |
| 9 |
it returns the fallback name. The host name usually comes from the incoming |
| 10 |
request's headers. |
| 11 |
""" |
| 12 |
def __init__(self, pattern, fallback): |
| 13 |
""" |
| 14 |
:param str pattern: the regex-like pattern to be compared against. |
| 15 |
Right now only ? and * are supported. An asterisk (*) represents |
| 16 |
any combination of characters. A question mark (?) represents |
| 17 |
any single character. |
| 18 |
:param str fallback: the fallback name to be used if the candidate name |
| 19 |
doesn't match the provided pattern. |
| 20 |
""" |
| 21 |
self._pattern = pattern |
| 22 |
self._fallback = fallback |
| 23 |
|
| 24 |
def get_name(self, host_name): |
| 25 |
""" |
| 26 |
Returns the segment name based on the input host name. |
| 27 |
""" |
| 28 |
if wildcard_match(self._pattern, host_name): |
| 29 |
return host_name |
| 30 |
else: |
| 31 |
return self._fallback |