Skip to content

Commit

Permalink
storage: Use pydantic-extra-types for MAC
Browse files Browse the repository at this point in the history
Relates to #243
  • Loading branch information
postlund committed Sep 5, 2023
1 parent 45c9876 commit d394708
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
1 change: 1 addition & 0 deletions base_versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ifaddr==0.1.7
mediafile==0.8.1
miniaudio==1.45
protobuf==4.22.0
pydantic-extra-types==2.1.0
pydantic-settings==2.0.2
requests==2.23.0
srptools==0.2.0
Expand Down
20 changes: 10 additions & 10 deletions docs/api/pyatv.settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h1 class="title">Module <code>pyatv.settings</code></h1>
</header>
<section id="section-intro">
<p>Settings for configuring pyatv.</p>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L1-L118" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L1-L101" class="git-link">Browse git</a></div>
</section>
<section>
</section>
Expand All @@ -117,7 +117,7 @@ <h2 class="section-title" id="header-classes">Classes</h2>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L67-L72" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L50-L55" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
Expand Down Expand Up @@ -149,7 +149,7 @@ <h3>Class variables</h3>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L75-L79" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L58-L62" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
Expand Down Expand Up @@ -177,7 +177,7 @@ <h3>Class variables</h3>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L82-L86" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L65-L69" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
Expand Down Expand Up @@ -205,7 +205,7 @@ <h3>Class variables</h3>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L55-L64" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L38-L47" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
Expand All @@ -216,7 +216,7 @@ <h3>Class variables</h3>
<dd>
<section class="desc"></section>
</dd>
<dt id="pyatv.settings.InfoSettings.mac"><code class="name">var <span class="ident">mac</span> -> typing_extensions.Annotated[str, AfterValidator(func=<function _mac_validator at 0x7f20f85b61f0>)] = 02:70:79:61:74:76</code></dt>
<dt id="pyatv.settings.InfoSettings.mac"><code class="name">var <span class="ident">mac</span> -> pydantic_extra_types.mac_address.MacAddress = 02:70:79:61:74:76</code></dt>
<dd>
<section class="desc"></section>
</dd>
Expand Down Expand Up @@ -253,7 +253,7 @@ <h3>Class variables</h3>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L89-L93" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L72-L76" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
Expand Down Expand Up @@ -281,7 +281,7 @@ <h3>Class variables</h3>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L104-L111" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L87-L94" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
Expand Down Expand Up @@ -321,7 +321,7 @@ <h3>Class variables</h3>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L96-L101" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L79-L84" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
Expand Down Expand Up @@ -353,7 +353,7 @@ <h3>Class variables</h3>
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L114-L118" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/settings.py#L97-L101" class="git-link">Browse git</a></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic_settings.main.BaseSettings</li>
Expand Down
19 changes: 1 addition & 18 deletions pyatv/settings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Settings for configuring pyatv."""
import re
from typing import Optional

from pydantic import BaseModel, Field
from pydantic.functional_validators import AfterValidator
from pydantic_extra_types.mac_address import MacAddress
from pydantic_settings import BaseSettings
from typing_extensions import Annotated

__pdoc__ = {
"InfoSettings.model_config": False,
Expand All @@ -28,21 +26,6 @@

__pdoc_dev_page__ = "/development/storage"


# TODO: Replace with pydantic-extra-types when a release is out with MAC
# address validation included
_MAC_REGEX = r"[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}"


def _mac_validator(mac_addr: str) -> str:
assert (
re.match(_MAC_REGEX, mac_addr) is not None
), f"{mac_addr} is not a valid MAC address"
return mac_addr


MacAddress = Annotated[str, AfterValidator(_mac_validator)]

DEFAULT_NAME = "pyatv"
DEFUALT_MAC = "02:70:79:61:74:76" # Locally administrated (02) + "pyatv" in hex
DEFAULT_DEVICE_ID = "FF:70:79:61:74:76" # 0xFF + "pyatv"
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ifaddr==0.2.0
mediafile==0.12.0
miniaudio==1.59
protobuf==4.24.2
pydantic-extra-types==2.1.0
pydantic-settings==2.0.3
requests==2.31.0
srptools==1.0.1
Expand Down

0 comments on commit d394708

Please sign in to comment.