8000 Initial commit · coffeepenbit/bookstack@4760ca8 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 4760ca8

Browse files
committed
Initial commit
0 parents  commit 4760ca8

File tree

11 files changed

+366
-0
lines changed

11 files changed

+366
-0
lines changed

.bumpversion.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[bumpversion]
2+
current_version = 0.1.0
3+
commit = True
4+
tag = False
5+
6+
[bumpversion:file:src/bookstack/__init__.py]

.gitignore

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Project specific
2+
tests/
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# C extensions
10+
*.so
11+
12+
# Distribution / packaging
13+
.Python
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
lib/
21+
lib64/
22+
parts/
23+
sdist/
24+
var/
25+
wheels/
26+
pip-wheel-metadata/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST
32+
33+
# PyInstaller
34+
# Usually these files are written by a python script from a template
35+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
36+
*.manifest
37+
*.spec
38+
39+
# Installer logs
40+
pip-log.txt
41+
pip-delete-this-directory.txt
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py,cover
54+
.hypothesis/
55+
.pytest_cache/
56+
cover/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
.pybuilder/
80+
target/
81+
82+
# Jupyter Notebook
83+
.ipynb_checkpoints
84+
85+
# IPython
86+
profile_default/
87+
ipython_config.py
88+
89+
# pyenv
90+
# For a library or package, you might want to ignore these files since the code is
91+
# intended to run in multiple environments; otherwise, check them in:
92+
# .python-version
93+
94+
# pipenv
95+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
97+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
98+
# install all needed dependencies.
99+
#Pipfile.lock
100+
101+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
102+
__pypackages__/
103+
104+
# Celery stuff
105+
celerybeat-schedule
106+
celerybeat.pid
107+
108+
# SageMath parsed files
109+
*.sage.py
110+
111+
# Environments
112+
.env
113+
.venv
114+
env/
115+
venv/
116+
ENV/
117+
env.bak/
118+
venv.bak/
119+
120+
# Spyder project settings
121+
.spyderproject
122+
.spyproject
123+
124+
# Rope project settings
125+
.ropeproject
126+
127+
# mkdocs documentation
128+
/site
129+
130+
# mypy
131+
.mypy_cache/
132+
.dmypy.json
133+
dmypy.json
134+
135+
# Pyre type checker
136+
.pyre/
137+
138+
# pytype static type analyzer
139+
.pytype/
140+
141+
# Cython debug symbols
142+
cython_debug/

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.hmtml)
6+
7+
## [Unreleased]
8+
9+
## [Released]
10+
### [0.1.0] - YYYY-MM-DD
11+
Initial release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 coffeepenbit
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# bookstack
2+
A Python wrapper for [BookStack's](https://www.bookstackapp.com) API
3+
4+
- TODO document examples
5+
- Remove `tests` directory from `.gitignore`

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
junit_family=xunit1

setup.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from setuptools import setup, find_packages
2+
import os
3+
4+
5+
here = os.path.abspath(os.path.dirname(__file__))
6+
7+
about = {}
8+
with open(os.path.join(here, 'src', 'bookstack', '__init__.py'), 'r', encoding='utf-8') as f:
9+
exec(f.read(), about)
10+
11+
with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
12+
long_description = f.read()
13+
14+
install_requires = [
15+
'requests'
16+
]
17+
18+
extras_require = {
19+
'dev': [
20+
'bump2version',
21+
'pylint',
22+
'pytest',
23+
'pytest-cov',
24+
'pytest-vcr',
25+
'tox'
26+
]
27+
}
28+
29+
30+
setup(
31+
name=about['__title__'],
32+
version=about['__version__'],
33+
url=about['__url__'],
34+
description=about['__description__'],
35+
long_description=long_description,
36+
author=about['__author__'],
37+
author_email=about['__author_email__'],
38+
long_description_content_type='text/markdown',
39+
packages=find_packages('src'),
40+
package_dir={'': 'src'},
41+
install_requires=install_requires,
42+
extras_require=extras_require,
43+
classifiers=[
44+
"Programming Language :: Python :: 3",
45+
"License :: OSI Approved :: MIT License",
46+
"Operating System :: OS Independent",
47+
]
48+
)

src/bookstack/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__author__ = 'coffeepenbit'
2+
__description__ = "A Python wrapper for BookStack's API"
3+
__author_email__ = 'coffeepenbit@gmail.com'
4+
__maintainer__ = 'coffeepenbit'
5+
__title__ = 'bookstack'
6+
__url__ = "https://github.com/coffeepenbit/bookstack"
7+
__version__ = '0.1.0-alpha.2'

src/bookstack/models.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import urllib
2+
3+
import requests
4+
5+
6+
class BookStack:
7+
def __init__(self, base_url, token_id=None, token_secret=None):
8+
self.api_base_url = self._create_api_base_url(base_url)
9+
self.token_id = None
10+
self.token_secret = None
11+
12+
self._session = BaseURLSession(self.api_base_url)
13+
self._session.auth = Auth(token_id, token_secret)
14+
15+
self.endpoint_paths = {
16+
'docs': 'docs.json/',
17+
'books': 'books/',
18+
'shelves': 'shelves/',
19+
'export_text': 'export/plaintext',
20+
'export_html': 'export/html'
21+
}
22+
23+
@staticmethod
24+
def _create_api_base_url(base_url):
25+
parsed_url = urllib.parse.urlparse(base_url)
26+
if 'api' in str(parsed_url.path):
27+
parsed_url = parsed_url
28+
else:
29+
parsed_url = parsed_url._replace(**{'path': 'api/'})
30+
31+
return parsed_url.geturl()
32+
33+
def get_docs(self):
34+
return self._session.get(self.endpoint_paths['docs']).json()
35+
36+
def get_books(self):
37+
return self._session.get(self.endpoint_paths['books']).json()
38+
39+
def read_book(self, book_id):
40+
return self._session.get(
41+
urllib.parse.urljoin(
42+
self.endpoint_paths['books'],
43+
str(book_id)
44+
)
45+
).json()
46+
47+
def export_text(self, book_id):
48+
return self._session.get(
49+
urllib.parse.urljoin(
50+
self.endpoint_paths['books'],
51+
f"{book_id}/{self.endpoint_paths['export_text']}"
52+
)
53+
).text
54+
55+
def export_html(self, book_id):
56+
return self._session.get(
57+
urllib.parse.urljoin(
58+
self.endpoint_paths['books'],
59+
f"{book_id}/{self.endpoint_paths['export_html']}"
60+
)
61+
).text
62+
63+
64+
class BaseURLSession(requests.Session):
65+
def __init__(self, base_url, *args, **kwargs):
66+
super().__init__(*args, **kwargs)
67+
self.base_url = base_url
68+
69+
def get(self, url_path, *args, **kwargs):
70+
url = urllib.parse.urljoin(self.base_url, url_path)
71+
return super().get(url, *args, **kwargs)
72+
73+
74+
class Auth(requests.auth.AuthBase):
75+
def __init__(self,
76+
token_id,
77+
token_secret,
78+
header_key='Authorization'
79+
):
80+
self.header_key = header_key
81+
self.token_id = token_id
82+
self.token_secret = token_secret
83+
84+
def __call__(self, r):
85+
r.headers[self.header_key] = f'Token {self.token_id}:{self.token_secret}'
86+
87+
return r

src/bookstack/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import logging
2+
3+
import yaml
4+
5+
6+
def configure_loggers(loglevel, logger, logging_format):
7+
formatter = logging.Formatter(logging_format)
8+
stream_handler = logging.StreamHandler()
9+
stream_handler.setLevel(loglevel)
10+
stream_handler.setFormatter(formatter)
11+
logger.addHandler(stream_handler)
12+
logger.setLevel(loglevel)
13+
14+
15+
def get_conf(conf_filepath):
16+
try:
17+
with open(conf_filepath, 'r') as config_file:
18+
return yaml.safe_load(config_file)
19+
except FileNotFoundError:
20+
raise FileNotFoundError(
21+
f"Configuration file was not found at: \"{conf_filepath}.\""
22+
)

0 commit comments

Comments
 (0)
0