-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 'master'
[feature][WDT-116] Data provider for sphinx html See merge request !1
- Loading branch information
Showing
4 changed files
with
85 additions
and
32 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
conf_publisher/data_providers/sphinx_base_data_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
|
||
from . import DataProvider | ||
|
||
|
||
class SphinxBaseDataProvider(DataProvider): | ||
|
||
DEFAULT_SOURCE_EXT = None | ||
DEFAULT_ROOT_DIR = './' | ||
DEFAULT_SOURCE_DIR = 'docs/build/result' | ||
DEFAULT_IMAGES_DIR = '_images' | ||
DEFAULT_DOWNLOADS_DIR = '_downloads' | ||
|
||
def __init__(self, root_dir=None, base_dir=None, downloads_dir=None, images_dir=None, source_ext=None): | ||
self._root_dir = os.path.abspath(root_dir or './') | ||
|
||
self._source_dir = base_dir or self.DEFAULT_SOURCE_DIR | ||
if not os.path.isabs(self._source_dir): | ||
self._source_dir = os.path.join(self._root_dir, self._source_dir) | ||
|
||
self._downloads_dir = downloads_dir or self.DEFAULT_DOWNLOADS_DIR | ||
if not os.path.isabs(self._downloads_dir): | ||
self._downloads_dir = os.path.join(self._source_dir, self._downloads_dir) | ||
|
||
self._images_dir = images_dir or self.DEFAULT_IMAGES_DIR | ||
if not os.path.isabs(self._images_dir): | ||
self._images_dir = os.path.join(self._source_dir, self._images_dir) | ||
|
||
self._source_ext = source_ext or self.DEFAULT_SOURCE_EXT | ||
|
||
def get_source(self, filename): | ||
print self._source_dir, self._source_ext | ||
return os.path.join(self._source_dir, filename + self._source_ext) | ||
|
||
def get_source_data(self, filename): | ||
raise NotImplementedError() | ||
|
||
def get_image(self, filename): | ||
return os.path.join(self._images_dir, filename) | ||
|
||
def get_attachment(self, filename): | ||
return os.path.join(self._downloads_dir, filename) |
38 changes: 9 additions & 29 deletions
38
conf_publisher/data_providers/sphinx_fjson_data_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,18 @@ | ||
import os | ||
|
||
from . import DataProvider | ||
from .sphinx_base_data_provider import SphinxBaseDataProvider | ||
from ..serializers import json_serializer | ||
|
||
|
||
class SphinxFJsonDataProvider(DataProvider): | ||
def __init__(self, root_dir=None, base_dir=None, downloads_dir=None, images_dir=None, source_ext=None): | ||
self._root_dir = os.path.abspath(root_dir or './') | ||
|
||
self._source_dir = base_dir or 'docs/build/json' | ||
if not os.path.isabs(self._source_dir): | ||
self._source_dir = os.path.join(self._root_dir, self._source_dir) | ||
|
||
self._downloads_dir = downloads_dir or '_downloads' | ||
if not os.path.isabs(self._downloads_dir): | ||
self._downloads_dir = os.path.join(self._source_dir, self._downloads_dir) | ||
|
||
self._images_dir = images_dir or '_images' | ||
if not os.path.isabs(self._images_dir): | ||
self._images_dir = os.path.join(self._source_dir, self._images_dir) | ||
|
||
self._source_ext = source_ext or '.fjson' | ||
|
||
def get_source(self, filename): | ||
return os.path.join(self._source_dir, filename + self._source_ext) | ||
class SphinxFJsonDataProvider(SphinxBaseDataProvider): | ||
DEFAULT_SOURCE_EXT = '.fjson' | ||
DEFAULT_SOURCE_DIR = 'docs/build/json' | ||
|
||
def get_source_data(self, filename): | ||
with open(filename, 'r') as f: | ||
if os.path.isabs(filename): | ||
filepath = filename | ||
else: | ||
filepath = self.get_source(filename) | ||
with open(filepath, 'r') as f: | ||
content = json_serializer.load(f) | ||
|
||
return content['title'], content['body'] | ||
|
||
def get_image(self, filename): | ||
return os.path.join(self._images_dir, filename) | ||
|
||
def get_attachment(self, filename): | ||
return os.path.join(self._downloads_dir, filename) |
16 changes: 16 additions & 0 deletions
16
conf_publisher/data_providers/sphinx_html_data_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from .sphinx_base_data_provider import SphinxBaseDataProvider | ||
import re | ||
import codecs | ||
|
||
|
||
class SphinxHTMLDataProvider(SphinxBaseDataProvider): | ||
DEFAULT_SOURCE_EXT = '.html' | ||
DEFAULT_SOURCE_DIR = 'docs/build/html' | ||
|
||
def get_source_data(self, filename): | ||
with codecs.open(filename, 'r', encoding='utf-8') as f: | ||
content = f.read() | ||
title = re.findall(r'<title.*?>(.+?)</title>', content)[0] | ||
body = re.findall(r'<body.*?>(.+?)</body>', content, re.DOTALL)[0].strip() | ||
|
||
return title, body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters