Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
v1.0.10-beta, tested on Python 3.9.6
Browse files Browse the repository at this point in the history
Fix #1Fix #2
• Use `import typing as t` instead of `from typing import ...`
  • Loading branch information
Kevo committed Sep 11, 2021
1 parent 3876b63 commit 0c5212a
Show file tree
Hide file tree
Showing 62 changed files with 899 additions and 918 deletions.
4 changes: 2 additions & 2 deletions documentation/pyflarum/custom_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ <h3 id="types">Types:</h3>
<span class="sd"> - `AnyPost` - `Post` | `PostFromBulk` | `PostFromNotification`</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">TYPE_CHECKING</span>
<span class="k">if</span> <span class="n">TYPE_CHECKING</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">typing</span> <span class="k">as</span> <span class="nn">t</span>
<span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">.session</span> <span class="kn">import</span> <span class="n">FlarumUser</span>

<span class="kn">from</span> <span class="nn">.flarum.core</span> <span class="kn">import</span> <span class="n">BaseFlarumIndividualObject</span>
Expand Down
52 changes: 23 additions & 29 deletions documentation/pyflarum/datetime_conversions.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,49 +55,45 @@ <h1 class="modulename">

<details>
<summary>View Source</summary>
<div class="codehilite"><pre><span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<div class="codehilite"><pre><span></span><span class="kn">import</span> <span class="nn">typing</span> <span class="k">as</span> <span class="nn">t</span>

<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>


<span class="k">def</span> <span class="nf">flarum_to_datetime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="s1">&#39;str | datetime&#39;</span><span class="p">]</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">flarum_to_datetime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">:</span> <span class="s1">&#39;t.Optional[t.Union[str, datetime]]&#39;</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Converts Flarum&#39;s datetime string to Python&#39;s datetime object.</span>
<span class="sd"> Doesn&#39;t convert if the parameter is already a datetime object.</span>
<span class="sd"> </span>
<span class="sd"> Flarum&#39;s datetime format is `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> Flarum&#39;s datetime format is: `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">flarum_dt</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flarum_dt</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>

<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">,</span> <span class="n">datetime</span><span class="p">):</span>
<span class="k">return</span> <span class="n">flarum_dt</span>

<span class="k">else</span><span class="p">:</span>
<span class="n">strpped</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">,</span> <span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: datetime</span>
<span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">,</span> <span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: datetime</span>

<span class="k">return</span> <span class="n">strpped</span>


<span class="k">def</span> <span class="nf">datetime_to_flarum</span><span class="p">(</span><span class="n">dt</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="s1">&#39;datetime | str&#39;</span><span class="p">]</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">datetime_to_flarum</span><span class="p">(</span><span class="n">dt</span><span class="p">:</span> <span class="s1">&#39;t.Optional[t.Union[datetime, str]]&#39;</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Converts Python&#39;s datetime object to Flarum&#39;s datetime string.</span>
<span class="sd"> Doesn&#39;t convert if the parameter is already a string.</span>
<span class="sd"> </span>
<span class="sd"> Flarum&#39;s datetime format is `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> Flarum&#39;s datetime format is: `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">dt</span><span class="p">:</span>
<span class="k">if</span> <span class="n">dt</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>

<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="k">return</span> <span class="n">dt</span>

<span class="k">else</span><span class="p">:</span>
<span class="n">strfed</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: str</span>

<span class="k">return</span> <span class="n">strfed</span>
<span class="k">return</span> <span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: str</span>
</pre></div>

</details>
Expand All @@ -108,37 +104,37 @@ <h1 class="modulename">


<span class="def">def</span>
<span class="name">flarum_to_datetime</span><span class="signature">(flarum_dt: Optional[ForwardRef(&#39;str | datetime&#39;)] = None)</span>:
<span class="name">flarum_to_datetime</span><span class="signature">(
flarum_dt: Union[str, datetime.datetime, NoneType] = None
) -&gt; Optional[datetime.datetime]</span>:
</div>

<details>
<summary>View Source</summary>
<div class="codehilite"><pre><span></span><span class="k">def</span> <span class="nf">flarum_to_datetime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="s1">&#39;str | datetime&#39;</span><span class="p">]</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<div class="codehilite"><pre><span></span><span class="k">def</span> <span class="nf">flarum_to_datetime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">:</span> <span class="s1">&#39;t.Optional[t.Union[str, datetime]]&#39;</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Converts Flarum&#39;s datetime string to Python&#39;s datetime object.</span>
<span class="sd"> Doesn&#39;t convert if the parameter is already a datetime object.</span>
<span class="sd"> </span>
<span class="sd"> Flarum&#39;s datetime format is `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> Flarum&#39;s datetime format is: `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">flarum_dt</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flarum_dt</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>

<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">,</span> <span class="n">datetime</span><span class="p">):</span>
<span class="k">return</span> <span class="n">flarum_dt</span>

<span class="k">else</span><span class="p">:</span>
<span class="n">strpped</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">,</span> <span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: datetime</span>

<span class="k">return</span> <span class="n">strpped</span>
<span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">flarum_dt</span><span class="p">,</span> <span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: datetime</span>
</pre></div>

</details>

<div class="docstring"><p>Converts Flarum's datetime string to Python's datetime object.
Doesn't convert if the parameter is already a datetime object.</p>

<p>Flarum's datetime format is <code>%Y-%m-%dT%H:%M:%S%z</code></p>
<p>Flarum's datetime format is: <code>%Y-%m-%dT%H:%M:%S%z</code></p>
</div>


Expand All @@ -148,37 +144,35 @@ <h1 class="modulename">


<span class="def">def</span>
<span class="name">datetime_to_flarum</span><span class="signature">(dt: Optional[ForwardRef(&#39;datetime | str&#39;)] = None)</span>:
<span class="name">datetime_to_flarum</span><span class="signature">(dt: Union[str, datetime.datetime, NoneType] = None) -&gt; Optional[str]</span>:
</div>

<details>
<summary>View Source</summary>
<div class="codehilite"><pre><span></span><span class="k">def</span> <span class="nf">datetime_to_flarum</span><span class="p">(</span><span class="n">dt</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="s1">&#39;datetime | str&#39;</span><span class="p">]</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<div class="codehilite"><pre><span></span><span class="k">def</span> <span class="nf">datetime_to_flarum</span><span class="p">(</span><span class="n">dt</span><span class="p">:</span> <span class="s1">&#39;t.Optional[t.Union[datetime, str]]&#39;</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Converts Python&#39;s datetime object to Flarum&#39;s datetime string.</span>
<span class="sd"> Doesn&#39;t convert if the parameter is already a string.</span>
<span class="sd"> </span>
<span class="sd"> Flarum&#39;s datetime format is `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> Flarum&#39;s datetime format is: `%Y-%m-%dT%H:%M:%S%z`</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">dt</span><span class="p">:</span>
<span class="k">if</span> <span class="n">dt</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>

<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="k">return</span> <span class="n">dt</span>

<span class="k">else</span><span class="p">:</span>
<span class="n">strfed</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: str</span>

<span class="k">return</span> <span class="n">strfed</span>
<span class="k">return</span> <span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1">T%H:%M:%S%z&#39;</span><span class="p">)</span> <span class="c1"># type: str</span>
</pre></div>

</details>

<div class="docstring"><p>Converts Python's datetime object to Flarum's datetime string.
Doesn't convert if the parameter is already a string.</p>

<p>Flarum's datetime format is <code>%Y-%m-%dT%H:%M:%S%z</code></p>
<p>Flarum's datetime format is: <code>%Y-%m-%dT%H:%M:%S%z</code></p>
</div>


Expand Down
Loading

0 comments on commit 0c5212a

Please sign in to comment.