Skip to content

Commit

Permalink
Add an option to store .env files in a dedicated location
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
dormant-user committed Jun 22, 2024
1 parent ac982e4 commit 940c146
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 19 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,45 @@ User profile's permissions are automatically set based on the `admin` flag set i

</details>

> `.env` files can be placed in a dedicated directory, whose path can be set using the env var `SECRETS_PATH`
> before importing `pyfilebrowser`
<details>
<summary>Example</summary>

**Sample directory structure**
```text
root (current working directory)
├ secrets
| ├ .config.env
| ├ .github.env
| ├ .proxy.env
| └ .user.env
├ venv/
└ main.py
```

**Set custom location for secrets [programmatically]**
```python
import os

os.environ["secrets_path"] = os.path.join(os.getcwd(), "secrets")

import pyfilebrowser
...
```

**[OR]**

**Set custom location for secrets [CLI]**
```shell
export secrets_path="secrets"

pyfilebrowser run
```

</details>

Refer [samples] directory for sample `.env` files. For nested configuration settings, refer the [runbook]

> Any configuration changes made in the UI will be lost, unless backed up manually.<br>
Expand Down
32 changes: 32 additions & 0 deletions docs/README.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,38 @@ <h3><code class="docutils literal notranslate"><span class="pre">.env</span></co
</div></blockquote>
<p>Multiple user profiles can be loaded using <code class="docutils literal notranslate"><span class="pre">.user1.env</span></code>, <code class="docutils literal notranslate"><span class="pre">.user2.env</span></code> and so on.<br>
User profile’s permissions are automatically set based on the <code class="docutils literal notranslate"><span class="pre">admin</span></code> flag set in the env-var <code class="docutils literal notranslate"><span class="pre">authentication</span></code></p>
</details><blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">.env</span></code> files can be placed in a dedicated directory, whose path can be set using the env var <code class="docutils literal notranslate"><span class="pre">SECRETS_PATH</span></code>
before importing <code class="docutils literal notranslate"><span class="pre">pyfilebrowser</span></code></p>
</div></blockquote>
<details>
<summary>Example</summary><p><strong>Sample directory structure</strong></p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>root (current working directory)
├ secrets
| ├ .config.env
| ├ .github.env
| ├ .proxy.env
| └ .user.env
├ venv/
└ main.py
</pre></div>
</div>
<p><strong>Set custom location for secrets [programmatically]</strong></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>

<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;secrets_path&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">(),</span> <span class="s2">&quot;secrets&quot;</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">pyfilebrowser</span>
<span class="o">...</span>
</pre></div>
</div>
<p><strong>[OR]</strong></p>
<p><strong>Set custom location for secrets [CLI]</strong></p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span><span class="nb">export</span><span class="w"> </span><span class="nv">secrets_path</span><span class="o">=</span><span class="s2">&quot;secrets&quot;</span>

pyfilebrowser<span class="w"> </span>run
</pre></div>
</div>
</details><p>Refer <a class="reference external" href="https://github.com/thevickypedia/pyfilebrowser/tree/main/samples">samples</a> directory for sample <code class="docutils literal notranslate"><span class="pre">.env</span></code> files. For nested configuration settings, refer the <a class="reference external" href="https://thevickypedia.github.io/pyfilebrowser/">runbook</a></p>
<blockquote>
<div><p>Any configuration changes made in the UI will be lost, unless backed up manually.<br>
Expand Down
39 changes: 39 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,45 @@ User profile's permissions are automatically set based on the `admin` flag set i

</details>

> `.env` files can be placed in a dedicated directory, whose path can be set using the env var `SECRETS_PATH`
> before importing `pyfilebrowser`
<details>
<summary>Example</summary>

**Sample directory structure**
```text
root (current working directory)
├ secrets
| ├ .config.env
| ├ .github.env
| ├ .proxy.env
| └ .user.env
├ venv/
└ main.py
```

**Set custom location for secrets [programmatically]**
```python
import os

os.environ["secrets_path"] = os.path.join(os.getcwd(), "secrets")

import pyfilebrowser
...
```

**[OR]**

**Set custom location for secrets [CLI]**
```shell
export secrets_path="secrets"

pyfilebrowser run
```

</details>

Refer [samples] directory for sample `.env` files. For nested configuration settings, refer the [runbook]

> Any configuration changes made in the UI will be lost, unless backed up manually.<br>
Expand Down
39 changes: 39 additions & 0 deletions docs/_sources/README.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,45 @@ User profile's permissions are automatically set based on the `admin` flag set i

</details>

> `.env` files can be placed in a dedicated directory, whose path can be set using the env var `SECRETS_PATH`
> before importing `pyfilebrowser`

<details>
<summary>Example</summary>

**Sample directory structure**
```text
root (current working directory)
├ secrets
| ├ .config.env
| ├ .github.env
| ├ .proxy.env
| └ .user.env
├ venv/
└ main.py
```

**Set custom location for secrets [programmatically]**
```python
import os

os.environ["secrets_path"] = os.path.join(os.getcwd(), "secrets")

import pyfilebrowser
...
```

**[OR]**

**Set custom location for secrets [CLI]**
```shell
export secrets_path="secrets"

pyfilebrowser run
```

</details>

Refer [samples] directory for sample `.env` files. For nested configuration settings, refer the [runbook]

> Any configuration changes made in the UI will be lost, unless backed up manually.<br>
Expand Down
18 changes: 9 additions & 9 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ <h1>Settings<a class="headerlink" href="#settings" title="Permalink to this head
<dd><p>Environment variables configuration.</p>
<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.proxy.settings.EnvConfig.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.proxy.env'</span></em><a class="headerlink" href="#pyfilebrowser.proxy.settings.EnvConfig.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.proxy.env'</span></em><a class="headerlink" href="#pyfilebrowser.proxy.settings.EnvConfig.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -796,7 +796,7 @@ <h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.modals.config.Server.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Server.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Server.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -891,7 +891,7 @@ <h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.modals.config.Branding.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Branding.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Branding.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -945,7 +945,7 @@ <h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.modals.config.Tus.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Tus.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Tus.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -1041,7 +1041,7 @@ <h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.modals.config.Defaults.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Defaults.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Defaults.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -1143,7 +1143,7 @@ <h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.modals.config.Commands.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Commands.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Commands.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -1248,7 +1248,7 @@ <h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.modals.config.Config.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Config.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Config.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -1331,7 +1331,7 @@ <h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.modals.config.Auther.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Auther.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.config.env'</span></em><a class="headerlink" href="#pyfilebrowser.modals.config.Auther.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down Expand Up @@ -1855,7 +1855,7 @@ <h1>Download<a class="headerlink" href="#download" title="Permalink to this head

<dl class="py attribute">
<dt class="sig sig-object py" id="pyfilebrowser.squire.download.GitHub.Config.env_file">
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'.github.env'</span></em><a class="headerlink" href="#pyfilebrowser.squire.download.GitHub.Config.env_file" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">env_file</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'/Users/vicky/Desktop/git/pyfilebrowser/doc_gen/.github.env'</span></em><a class="headerlink" href="#pyfilebrowser.squire.download.GitHub.Config.env_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions pyfilebrowser/modals/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path
import socket as sock
from typing import List, Optional

Expand Down Expand Up @@ -41,7 +42,7 @@ class Config:
"""Environment variables configuration."""

env_prefix = "branding_"
env_file = ".config.env"
env_file = os.path.join(models.SECRETS_PATH, ".config.env")
extra = "ignore"


Expand All @@ -65,7 +66,7 @@ class Config:
"""Environment variables configuration."""

env_prefix = "tus_"
env_file = ".config.env"
env_file = os.path.join(models.SECRETS_PATH, ".config.env")
extra = "ignore"


Expand Down Expand Up @@ -103,7 +104,7 @@ class Config:
"""Environment variables configuration."""

env_prefix = "defaults_"
env_file = ".config.env"
env_file = os.path.join(models.SECRETS_PATH, ".config.env")
extra = "ignore"


Expand Down Expand Up @@ -143,7 +144,7 @@ class Config:
"""Environment variables configuration."""

env_prefix = "commands_"
env_file = ".config.env"
env_file = os.path.join(models.SECRETS_PATH, ".config.env")
extra = "ignore"


Expand Down Expand Up @@ -206,7 +207,7 @@ class Config:
"""Environment variables configuration."""

env_prefix = ""
env_file = ".config.env"
env_file = os.path.join(models.SECRETS_PATH, ".config.env")
extra = "ignore"


Expand Down Expand Up @@ -246,7 +247,7 @@ class Config:
"""Environment variables configuration."""

env_prefix = ""
env_file = ".config.env"
env_file = os.path.join(models.SECRETS_PATH, ".config.env")
extra = "ignore"


Expand All @@ -268,7 +269,7 @@ class Config:
"""Environment variables configuration."""

env_prefix = "auth_"
env_file = ".config.env"
env_file = os.path.join(models.SECRETS_PATH, ".config.env")
extra = "ignore"


Expand Down
5 changes: 5 additions & 0 deletions pyfilebrowser/modals/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import os
from enum import StrEnum
from typing import Optional

from pydantic import BaseModel

SECRETS_PATH = (
os.environ.get("SECRETS_PATH") or os.environ.get("secrets_path") or os.getcwd()
)


class Log(StrEnum):
"""Enum for different log options.
Expand Down
4 changes: 3 additions & 1 deletion pyfilebrowser/proxy/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from pydantic import BaseModel, Field, FilePath, HttpUrl, PositiveInt, field_validator
from pydantic_settings import BaseSettings

from pyfilebrowser.modals import models

# noinspection LongLine
IP_REGEX = re.compile(
r"""^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$""" # noqa: E501
Expand Down Expand Up @@ -209,7 +211,7 @@ def parse_rate_limit(
class Config:
"""Environment variables configuration."""

env_file = ".proxy.env"
env_file = os.path.join(models.SECRETS_PATH, ".proxy.env")
extra = "ignore"


Expand Down
4 changes: 3 additions & 1 deletion pyfilebrowser/squire/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from pydantic.fields import AliasChoices, Field
from pydantic_settings import BaseSettings

from pyfilebrowser.modals import models


def alias_choices(variable: str) -> AliasChoices:
"""Custom alias choices for environment variables for GitHub.
Expand Down Expand Up @@ -44,7 +46,7 @@ class Config:
"""Custom configuration for GitHub settings."""

env_prefix = ""
env_file = ".github.env"
env_file = os.path.join(models.SECRETS_PATH, ".github.env")
extra = "ignore"


Expand Down

0 comments on commit 940c146

Please sign in to comment.