Use `output` instead of `storage_file`
Closes https://git.vvvvvvaria.org/decentral1se/xbotlib/issues/9.
This commit is contained in:
parent
3bbb799ed4
commit
e2fb6d4f4a
|
@ -5,6 +5,7 @@
|
|||
- Fix logging of exceptions and increase info for stack traces ([#2](https://github.com/decentral1se/xbotlib/issues/2))
|
||||
- Format JSON to human readable when saving ([#8](https://git.vvvvvvaria.org/decentral1se/xbotlib/issues/8))
|
||||
- Fix room formatting when storing in configuration files ([#10](https://git.vvvvvvaria.org/decentral1se/xbotlib/issues/10))
|
||||
- `--storage-file` goes away and is replaced by the `--output` option ([#9](https://git.vvvvvvaria.org/decentral1se/xbotlib/issues/9))
|
||||
|
||||
# xbotlib 0.15.2 (2021-01-24)
|
||||
|
||||
|
|
12
README.md
12
README.md
|
@ -153,7 +153,7 @@ simplest way to run your bot locally.
|
|||
- **serve**: turn on the web server (default: `False`)
|
||||
- **port**: the port to serve from (default: `8080`)
|
||||
- **storage**: storage back-end (default: `file`)
|
||||
- **storage_file**: path to file based storage back-end (default: `<nick>.json>`)
|
||||
- **output**: path to the output directory (default: `./`)
|
||||
|
||||
#### Using the command-line interface
|
||||
|
||||
|
@ -173,7 +173,7 @@ can use the `--help` option to see what is available (e.g. `python bot.py --help
|
|||
- **-t TEMPLATE, --template TEMPLATE**: the template to render (default: `index.html.j2`)
|
||||
- **-s, --serve**: turn on the web server (default: `False`)
|
||||
- **-st {file,redis}, --storage {file,redis}**: choice of storage back-end (default: `file`)
|
||||
- **-stf STORAGE_FILE, --storage-file STORAGE_FILE**: path to file based storage back-end (default: `<nick>.json`)
|
||||
- **-o OUTPUT, --output OUTPUT**: path to the output directory (default: `./`)
|
||||
|
||||
#### Using the environment
|
||||
|
||||
|
@ -193,14 +193,15 @@ deployments.
|
|||
- **XBOT_SERVE**: Turn on the web server (default: `False`)
|
||||
- **XBOT_PORT**: The port to serve from (default: `8080`)
|
||||
- **XBOT_STORAGE**: choice of storage back-end (default: `file`)
|
||||
- **XBOT_STORAGE_FILE**: path to file based storage back-end (default: `<nick>.json`)
|
||||
- **XBOT_OUTPUT**: path to the output directory (default: `./`)
|
||||
|
||||
### Storage back-end
|
||||
|
||||
In order to store data you can make use of the `self.db` attribute of the `Bot`
|
||||
class. It is a Python dictionary which will be saved to disk automatically for
|
||||
you as a `<nick>.json` in your current working directory. The name and path to
|
||||
this file can be configured using the storage file option.
|
||||
this file can be configured using the output option (e.g. `python bot.py
|
||||
--output /var/www/html`)
|
||||
|
||||
```python
|
||||
def group(self, message):
|
||||
|
@ -425,5 +426,4 @@ See the [LICENSE](./LICENSE.md).
|
|||
Any and all contributions most welcome! Happy to hear how you use the library
|
||||
or what could be improved from a usability perspective.
|
||||
|
||||
To test, install [Tox](https://tox.readthedocs.io/en/latest/) (`pip install
|
||||
tox`) and run `tox` to run the test suite locally.
|
||||
To test, install [Tox](https://tox.readthedocs.io/en/latest/) (`pip install tox`) and run `tox` to run the test suite locally.
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
"""Unit tests for xbotlib module."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from pytest import fixture
|
||||
|
||||
from xbotlib import Config, SimpleMessage
|
||||
|
@ -53,7 +55,7 @@ def config():
|
|||
"template": "index.html.j2",
|
||||
"serve": True,
|
||||
"storage": "file",
|
||||
"storage_file": "foo.json",
|
||||
"output": ".",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -91,4 +93,4 @@ def test_config(config):
|
|||
assert config.template == "index.html.j2"
|
||||
assert config.serve
|
||||
assert config.storage == "file"
|
||||
assert config.storage_file == "foo.json"
|
||||
assert config.output == "."
|
||||
|
|
32
xbotlib.py
32
xbotlib.py
|
@ -213,9 +213,9 @@ class Config:
|
|||
return self.section.get("storage", None)
|
||||
|
||||
@property
|
||||
def storage_file(self):
|
||||
"""Path to the file based storage back-end."""
|
||||
return self.section.get("storage_file", None)
|
||||
def output(self):
|
||||
"""Path to the output directory."""
|
||||
return self.section.get("output", None)
|
||||
|
||||
|
||||
class Bot(ClientXMPP):
|
||||
|
@ -322,10 +322,10 @@ class Bot(ClientXMPP):
|
|||
choices=("file", "redis"),
|
||||
)
|
||||
self.parser.add_argument(
|
||||
"-stf",
|
||||
"--storage-file",
|
||||
dest="storage_file",
|
||||
help="path to file based storage back-end",
|
||||
"-o",
|
||||
"--output",
|
||||
dest="output",
|
||||
help="path to output directory",
|
||||
)
|
||||
|
||||
self.args = self.parser.parse_args()
|
||||
|
@ -458,11 +458,11 @@ class Bot(ClientXMPP):
|
|||
or environ.get("XBOT_STORAGE", None)
|
||||
or "file"
|
||||
)
|
||||
storage_file = (
|
||||
self.args.storage_file
|
||||
or self.config.storage_file
|
||||
or environ.get("XBOT_STORAGE_FILE", None)
|
||||
or f"{nick}.json"
|
||||
output = (
|
||||
self.args.output
|
||||
or self.config.output
|
||||
or environ.get("XBOT_OUTPUT", None)
|
||||
or "."
|
||||
)
|
||||
|
||||
if not account:
|
||||
|
@ -492,7 +492,7 @@ class Bot(ClientXMPP):
|
|||
self.template = self.load_template(template)
|
||||
|
||||
self.storage = storage
|
||||
self.storage_file = Path(storage_file).absolute()
|
||||
self.output = Path(output).absolute()
|
||||
|
||||
def load_template(self, template):
|
||||
"""Load template via Jinja."""
|
||||
|
@ -657,12 +657,14 @@ class Bot(ClientXMPP):
|
|||
|
||||
def init_storage(self):
|
||||
"""Initialise the storage back-end."""
|
||||
file_storage_path = f"{self.output}/{self.nick}.json"
|
||||
|
||||
if self.storage == "file":
|
||||
try:
|
||||
self.db = SimpleDatabase(self.storage_file)
|
||||
self.db = SimpleDatabase(file_storage_path)
|
||||
self.log.info("Successfully loaded file storage")
|
||||
except Exception as exception:
|
||||
message = f"Failed to load {self.storage_file}: {exception}"
|
||||
message = f"Failed to load {file_storage_path}: {exception}"
|
||||
self.log.error(message)
|
||||
exit(1)
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue