Use output instead of storage_file

Closes https://git.vvvvvvaria.org/decentral1se/xbotlib/issues/9.
This commit is contained in:
Luke Murphy 2021-02-01 22:29:05 +01:00
parent 3bbb799ed4
commit e2fb6d4f4a
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
4 changed files with 28 additions and 23 deletions

View File

@ -5,6 +5,7 @@
- Fix logging of exceptions and increase info for stack traces ([#2](https://github.com/decentral1se/xbotlib/issues/2)) - 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)) - 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)) - 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) # xbotlib 0.15.2 (2021-01-24)

View File

@ -153,7 +153,7 @@ simplest way to run your bot locally.
- **serve**: turn on the web server (default: `False`) - **serve**: turn on the web server (default: `False`)
- **port**: the port to serve from (default: `8080`) - **port**: the port to serve from (default: `8080`)
- **storage**: storage back-end (default: `file`) - **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 #### 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`) - **-t TEMPLATE, --template TEMPLATE**: the template to render (default: `index.html.j2`)
- **-s, --serve**: turn on the web server (default: `False`) - **-s, --serve**: turn on the web server (default: `False`)
- **-st {file,redis}, --storage {file,redis}**: choice of storage back-end (default: `file`) - **-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 #### Using the environment
@ -193,14 +193,15 @@ deployments.
- **XBOT_SERVE**: Turn on the web server (default: `False`) - **XBOT_SERVE**: Turn on the web server (default: `False`)
- **XBOT_PORT**: The port to serve from (default: `8080`) - **XBOT_PORT**: The port to serve from (default: `8080`)
- **XBOT_STORAGE**: choice of storage back-end (default: `file`) - **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 ### Storage back-end
In order to store data you can make use of the `self.db` attribute of the `Bot` 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 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 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 ```python
def group(self, message): 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 Any and all contributions most welcome! Happy to hear how you use the library
or what could be improved from a usability perspective. or what could be improved from a usability perspective.
To test, install [Tox](https://tox.readthedocs.io/en/latest/) (`pip install To test, install [Tox](https://tox.readthedocs.io/en/latest/) (`pip install tox`) and run `tox` to run the test suite locally.
tox`) and run `tox` to run the test suite locally.

View File

@ -1,5 +1,7 @@
"""Unit tests for xbotlib module.""" """Unit tests for xbotlib module."""
from pathlib import Path
from pytest import fixture from pytest import fixture
from xbotlib import Config, SimpleMessage from xbotlib import Config, SimpleMessage
@ -53,7 +55,7 @@ def config():
"template": "index.html.j2", "template": "index.html.j2",
"serve": True, "serve": True,
"storage": "file", "storage": "file",
"storage_file": "foo.json", "output": ".",
} }
}, },
) )
@ -91,4 +93,4 @@ def test_config(config):
assert config.template == "index.html.j2" assert config.template == "index.html.j2"
assert config.serve assert config.serve
assert config.storage == "file" assert config.storage == "file"
assert config.storage_file == "foo.json" assert config.output == "."

View File

@ -213,9 +213,9 @@ class Config:
return self.section.get("storage", None) return self.section.get("storage", None)
@property @property
def storage_file(self): def output(self):
"""Path to the file based storage back-end.""" """Path to the output directory."""
return self.section.get("storage_file", None) return self.section.get("output", None)
class Bot(ClientXMPP): class Bot(ClientXMPP):
@ -322,10 +322,10 @@ class Bot(ClientXMPP):
choices=("file", "redis"), choices=("file", "redis"),
) )
self.parser.add_argument( self.parser.add_argument(
"-stf", "-o",
"--storage-file", "--output",
dest="storage_file", dest="output",
help="path to file based storage back-end", help="path to output directory",
) )
self.args = self.parser.parse_args() self.args = self.parser.parse_args()
@ -458,11 +458,11 @@ class Bot(ClientXMPP):
or environ.get("XBOT_STORAGE", None) or environ.get("XBOT_STORAGE", None)
or "file" or "file"
) )
storage_file = ( output = (
self.args.storage_file self.args.output
or self.config.storage_file or self.config.output
or environ.get("XBOT_STORAGE_FILE", None) or environ.get("XBOT_OUTPUT", None)
or f"{nick}.json" or "."
) )
if not account: if not account:
@ -492,7 +492,7 @@ class Bot(ClientXMPP):
self.template = self.load_template(template) self.template = self.load_template(template)
self.storage = storage self.storage = storage
self.storage_file = Path(storage_file).absolute() self.output = Path(output).absolute()
def load_template(self, template): def load_template(self, template):
"""Load template via Jinja.""" """Load template via Jinja."""
@ -657,12 +657,14 @@ class Bot(ClientXMPP):
def init_storage(self): def init_storage(self):
"""Initialise the storage back-end.""" """Initialise the storage back-end."""
file_storage_path = f"{self.output}/{self.nick}.json"
if self.storage == "file": if self.storage == "file":
try: try:
self.db = SimpleDatabase(self.storage_file) self.db = SimpleDatabase(file_storage_path)
self.log.info("Successfully loaded file storage") self.log.info("Successfully loaded file storage")
except Exception as exception: 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) self.log.error(message)
exit(1) exit(1)
else: else: