Skip to content

Commit d467e31

Browse files
committed
Added from_name method for VocationFilter
1 parent 7a05f84 commit d467e31

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

docs/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Enumerations are provided for various values in order to avoid depending on stri
4848
:members:
4949
:undoc-members:
5050

51+
.. automethod:: VocationFilter.from_name
52+
5153
.. autoclass:: WorldLocation
5254
:members:
5355
:undoc-members:

tibiapy/enums.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ class VocationFilter(Enum):
8686
SORCERERS = 3
8787
DRUIDS = 4
8888

89+
@classmethod
90+
def from_name(cls, name, all_fallback=True):
91+
"""Gets a vocation filter from a vocation's name.
92+
93+
Parameters
94+
----------
95+
name: :class:`str`
96+
The name of the vocation.
97+
all_fallback: :class:`bool`
98+
Whether to return :py:attr:`ALL` if no match is found. Otherwise, ``None`` will be returned.
99+
100+
Returns
101+
-------
102+
VocationFilter, optional:
103+
The matching vocation filter.
104+
"""
105+
name = name.upper()
106+
for vocation in cls: # type: VocationFilter
107+
if vocation.name in name or vocation.name[:-1] in name and vocation != cls.ALL:
108+
return vocation
109+
if all_fallback or name.upper() == "ALL":
110+
return cls.ALL
111+
return None
112+
89113

90114
class WorldLocation(BaseEnum):
91115
"""The possible physical locations for servers."""

0 commit comments

Comments
 (0)