Skip to content

Commit 2240670

Browse files
committed
Add the capability to lookup for config files in /Users/faltet and /etc (on unix)
1 parent b0c55c8 commit 2240670

5 files changed

Lines changed: 81 additions & 40 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,18 @@ Tests will use a copy of Caterva2's `root-example` directory. After they finish
9494
For this quick start, let's:
9595

9696
- create a virtual environment and install Caterva2 with the `[server,clients]` extras (see above).
97-
- copy the configuration file `caterva2.sample.toml` to `caterva2.toml`.
97+
- copy the configuration file `caterva2.sample.toml` to `caterva2.toml`, `~/.caterva2.toml`, or `/etc/caterva2.toml`.
98+
- copy the server configuration file `cat2-server.sample.toml` to `cat2-server.toml`, `~/.cat2-server.toml`, or `/etc/cat2-server.toml`.
9899

99-
Servers (and clients, to a limited extent) may get their configuration from a `caterva2.toml` file at the current directory (or an alternative file given with the `--conf` option). See also [configuration.md](configuration.md) in Caterva2 tutorials.
100+
Clients will search for configuration in the following order: current directory (`./caterva2.toml`), home directory (`~/.caterva2.toml`), and system-wide (`/etc/caterva2.toml` on Unix). You can also specify an alternative file with the `--conf` option. See also [configuration.md](configuration.md) in Caterva2 tutorials for more details. Server will look for `cat2-server.toml` instead, either in the current directory, home directory, or system-wide; you can also specify an alternative file with the `--conf` option.
100101

101102
Then run the server:
102103

103104
```sh
104105
CATERVA2_SECRET=c2sikrit cat2-server & # server
105106
```
106107

107-
The `CATERVA2_SECRET` environment variable is obligatory and is explained below in the following section.
108+
The `CATERVA2_SECRET` environment variable is mandatory and is explained below in the following section.
108109

109110
Now, let's see the directories that have been created by the server:
110111

@@ -122,7 +123,7 @@ _caterva2
122123
6 directories, 2 files
123124
```
124125

125-
We see that we have a state directory with several subdirectories. The `personal` directory is where we will store our personal datasets. The `public` directory is where we will store datasets that are shared with the public. The `shared` directory is where we will store datasets that are shared with other users. The `media` directory is where the server will store temporary media files (images, videos, etc.) that are used by the web GUI.
126+
We see that we have a state directory with several subdirectories. The `personal` directory is where we will store our personal datasets. The `public` directory is where we will store datasets that are shared with the public. The `shared` directory is where we will store datasets that are shared with other users. The `media` directory is where the server will store temporary media files (images, videos, etc.) that are used by the web GUI for internal purposes. The `db.json` and `db.sqlite` files are where the server will store its metadata.
126127

127128
For populating the server with datasets, let's copy the `root-example` directory from the Caterva2 source code:
128129

‎caterva2.sample.toml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Example configuration for caterva2 clients in general
2-
[localdemo]
3-
url = "http://localhost:8000"
2+
[default]
3+
url = "https://cat2.cloud/demo"
44
username = ""
55
password = ""
6+
7+
[localuser]
8+
url = "http://localhost:8000"
9+
username = "user@example.com"
10+
password = "foobar11"

‎caterva2/utils.py‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import argparse
1111
import logging
1212
import pathlib
13+
import sys
1314
import tomllib as toml
1415

1516

@@ -112,6 +113,48 @@ def _add_conf_argument(default, parser):
112113
)
113114

114115

116+
def _find_config_file(filename):
117+
"""Find configuration file using a standard search path.
118+
119+
Search order:
120+
1. Current directory (./filename)
121+
2. User home directory (~/.filename or ~/filename without extension prefix)
122+
3. System-wide config (/etc/filename) - Unix/Linux only
123+
124+
Args:
125+
filename: The default config filename (e.g., 'caterva2.toml')
126+
127+
Returns:
128+
pathlib.Path of the first found config file, or Path(filename) if none found
129+
"""
130+
# First, check if --conf was explicitly provided
131+
parser = argparse.ArgumentParser(add_help=False)
132+
_add_conf_argument(filename, parser)
133+
opts = parser.parse_known_args()[0]
134+
135+
# If --conf was explicitly provided (not default), use it as-is
136+
if opts.conf != pathlib.Path(filename):
137+
return opts.conf
138+
139+
# Search path: current dir -> home dir -> /etc (Unix only)
140+
search_paths = [
141+
pathlib.Path.cwd() / filename, # Current directory
142+
pathlib.Path.home() / f".{filename}", # Home with dot prefix
143+
]
144+
145+
# Add /etc path on Unix-like systems (not Windows)
146+
if sys.platform != "win32":
147+
search_paths.append(pathlib.Path("/etc") / filename)
148+
149+
# Return first existing file, or the default if none found
150+
for path in search_paths:
151+
if path.exists():
152+
return path
153+
154+
# If no config file found, return the default (current dir) path
155+
return pathlib.Path(filename)
156+
157+
115158
def _get_conf(filename, prefix=None):
116159
"""Get settings from the configuration file, if existing.
117160
@@ -122,12 +165,10 @@ def _get_conf(filename, prefix=None):
122165
the key starts with a dot, like ``.path.to.item``, `prefix` is prepended
123166
to it.
124167
"""
125-
parser = argparse.ArgumentParser(add_help=False)
126-
_add_conf_argument(filename, parser)
127-
opts = parser.parse_known_args()[0]
168+
config_path = _find_config_file(filename)
128169

129170
try:
130-
with open(opts.conf, "rb") as conf_file:
171+
with open(config_path, "rb") as conf_file:
131172
conf = toml.load(conf_file)
132173
return Conf(conf, prefix=prefix)
133174
except FileNotFoundError:

‎doc/tutorials/configuration.md‎

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
(caterva2.toml)=
22
# The `caterva2.toml` configuration file
33

4-
We've seen that the `cat2-client` program accepts some command-line options to tune its operation (check the `--help` option). This is even more important for services as we shall see in following sections. Thus, Caterva2 programs support getting some settings from a TOML configuration file, by default `caterva2.toml` in the current directory (though you may override it with the `--conf` option).
4+
We've seen that the `cat2-client` program accepts some command-line options to tune its operation (check the `--help` option). This is even more important for services as we shall see in following sections. Thus, Caterva2 programs support getting some settings from a TOML configuration file.
5+
6+
## Configuration File Search Path
7+
8+
Caterva2 follows standard Unix/Linux conventions for locating configuration files. When looking for `caterva2.toml` (or `cat2-server.toml`), the search order is:
9+
10+
1. **Command-line option**: `--conf <path>` (if explicitly specified)
11+
2. **Current directory**: `./caterva2.toml`
12+
3. **Home directory**: `~/.caterva2.toml`
13+
4. **System-wide** (Unix/Linux only): `/etc/caterva2.toml`
14+
15+
The first existing file found in this order will be used. If no configuration file is found, the program will use default settings.
16+
17+
This approach is similar to how tools like `git`, `vim`, and `ssh` locate their configuration files, making it familiar to Unix/Linux users.
518

619
The configuration file may hold settings for different programs, with a separate section for each program. Thus, a program may check the file for its own settings, but also for those of other programs which may be of use to itself. This allows compact configurations in a single file. For instance, below is a sample configuration file for the server program and some client app:
720

821
```toml
9-
# Example configuration for a standalone server
10-
#
11-
# It's possible to run only the server. Then the configuration has only a
12-
# section for the server. And maybe another one for the client.
13-
14-
# The server section must define:
15-
#
16-
# - statedir: the directory where the server's data will be stored (default: _caterva2/sub)
17-
# - listen: where the server listens to (a unix socket or a host/port) (default: localhost:8002)
18-
# - urlbase: the base url users will use to reach the server (default: http://localhost:8002)
19-
# - quota: if defined, it will limit the disk usage (default: 0, no limit)
20-
# - maxusers: if defined, it will limit the number of users (default: 0, no limit)
21-
# - login: if true, users will need to authenticate (default: true)
22-
# - register: if true, users will be able to register (default: false)
23-
#
24-
[server]
25-
statedir = "_caterva2/state"
26-
#listen = "_caterva2/sub/uvicorn.socket"
27-
listen = "localhost:8000"
28-
urlbase = "http://localhost:8000"
29-
quota = "10G"
30-
maxusers = 5
31-
register = true # allow users to register
32-
33-
# The client section defines the credentials for the client to authenticate
34-
# against the server.
35-
[client]
22+
# Example configuration for caterva2 clients in general
23+
[default]
24+
url = "https://cat2.cloud/demo"
3625
username = ""
3726
password = ""
27+
28+
[localuser]
29+
url = "http://localhost:8000"
30+
username = "user@example.com"
31+
password = "foobar11"
3832
```
3933

4034
See [caterva2.sample.toml](https://github.com/ironArray/Caterva2/blob/main/caterva2.sample.toml) in Caterva2's source for all possible settings and their purpose.

‎doc/utilities/cat2-client.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ Running `cat2-client --help` should provide a list of supported commands that ma
1313
cat2-client [GENERIC_OPTION...] COMMAND [COMMAND_OPTION...] COMMAND_ARGUMENTS...
1414
```
1515

16-
Another relevant generic option besides `--help` is `--server`, which overrides the base of server URLs used by default. It should be a HTTP(S) URL, for example `http://sub.edu.example.org:3126`. Finally, the generic options `--username` and `--password` may be used in case your server requires user authentication.
16+
Another relevant generic option is `--server`, which selects the server to connect to by name (as defined in a section of the configuration file; see below). There is also `--url`, which overrides the base of server URLs used by default; it should be a HTTP(S) URL, for example `http://sub.edu.example.org:3126`. Finally, the generic options `--username` and `--password` may be used in case your server requires user authentication.
1717

1818
`--help` is also available as a command option which shows the options and arguments accepted by that command (e.g. `cat2-client roots --help`). Another command option is `--json`, which forces the output of commands that accept it to be in JSON format, as that may be more amenable for parsing by other programs.
1919

20-
`cat2-client` may use a TOML configuration file (`caterva2.toml` in the current directory unless overridden with the generic `--conf` option). It may get the server address from there (`urlbase` or `listen` settings in `[server]` section), as well as user authentication options (in the `[client]` section). Command-line options override settings read from the configuration file.
20+
`cat2-client` may use a TOML configuration file (`caterva2.toml` in the current directory unless overridden with the generic `--conf` option). Command-line options override settings read from the configuration file.
2121

2222
For a short tutorial on `cat2-client`, see [](Using-the-command-line-client).
2323

0 commit comments

Comments
 (0)