Improving styles loading (#2569)

* Core : changing path variable name

* Core : styles

* Terminal : style

* Core : styles

* Core : styles

* Fixing linting

* Fix linting
This commit is contained in:
Chavithra
2022-09-20 14:34:30 +02:00
committed by GitHub
parent d85ede6a22
commit 017725eb63
6 changed files with 44 additions and 50 deletions

View File

@@ -6,7 +6,6 @@ from openbb_terminal.core.config.paths import (
USER_DATA_DIRECTORY,
USER_ENV_FILE,
REPOSITORY_ENV_FILE,
USER_STYLES_DIRECTORY,
)
@@ -33,7 +32,7 @@ def create_files(list_files: List):
dirs_list = [
SETTINGS_DIRECTORY,
USER_DATA_DIRECTORY,
USER_STYLES_DIRECTORY,
USER_DATA_DIRECTORY / "styles",
]
dirs_files = [USER_ENV_FILE, REPOSITORY_ENV_FILE]
create_paths(dirs_list)

View File

@@ -3,11 +3,9 @@ from pathlib import Path
HOME_DIRECTORY = Path.home()
REPO_DIRECTORY = Path(__file__).parent.parent.parent.parent
REPOSITORY_DIRECTORY = Path(__file__).parent.parent.parent.parent
SETTINGS_DIRECTORY = HOME_DIRECTORY / ".openbb_terminal"
REPOSITORY_ENV_FILE = HOME_DIRECTORY / ".env"
USER_ENV_FILE = SETTINGS_DIRECTORY / ".env"
USER_DATA_DIRECTORY = HOME_DIRECTORY / "OpenBBUserData"
USER_STYLES_DIRECTORY = USER_DATA_DIRECTORY / "styles"

View File

@@ -6,7 +6,7 @@ import re
# IMPORTATION THIRDPARTY
# IMPORTATION INTERNAL
from openbb_terminal.core.config.paths import REPO_DIRECTORY
from openbb_terminal.core.config.paths import REPOSITORY_DIRECTORY
from openbb_terminal.core.log.generation.settings import AppSettings
@@ -57,9 +57,9 @@ class FormatterWithExceptions(logging.Formatter):
s_list.append("suspected_ip")
elif "@" in word and "." in word:
s_list.append("suspected_email")
elif f"{REPO_DIRECTORY.name}{os.sep}" in word:
elif f"{REPOSITORY_DIRECTORY.name}{os.sep}" in word:
s_list.append(
word.split(f"{REPO_DIRECTORY.name}{os.sep}")[1]
word.split(f"{REPOSITORY_DIRECTORY.name}{os.sep}")[1]
.replace('"', "")
.replace("'", "")
)

View File

@@ -1,7 +1,6 @@
"""Helper classes."""
__docformat__ = "numpy"
import os
from shutil import copyfile
from pathlib import Path
import argparse
import json
@@ -12,7 +11,7 @@ import matplotlib
import matplotlib.pyplot as plt
from matplotlib import font_manager, ticker
from openbb_terminal.core.config.paths import USER_STYLES_DIRECTORY, REPO_DIRECTORY
from openbb_terminal.core.config.paths import REPOSITORY_DIRECTORY, USER_DATA_DIRECTORY
class LineAnnotateDrawer:
@@ -100,9 +99,8 @@ class TerminalStyle:
styles as python dictionaries.
"""
_STYLES_FOLDER = USER_STYLES_DIRECTORY
DEFAULT_STYLES_LOCATION = os.path.join(_STYLES_FOLDER, "default")
USER_STYLES_LOCATION = os.path.join(_STYLES_FOLDER, "user")
DEFAULT_STYLES_LOCATION = REPOSITORY_DIRECTORY / "styles" / "default"
USER_STYLES_LOCATION = USER_DATA_DIRECTORY / "styles"
mpl_styles_available: Dict[str, str] = {}
mpl_style: str = ""
@@ -151,23 +149,6 @@ class TerminalStyle:
"""
# To import all styles from terminal repo folder to user data
if len(os.listdir(self._STYLES_FOLDER)) == 0:
repo_styles = REPO_DIRECTORY.joinpath("styles")
for root, _, filenames in os.walk(repo_styles.joinpath("default")):
os.mkdir(Path(self._STYLES_FOLDER).joinpath("default"))
for style in filenames:
copyfile(
os.path.join(root, style),
Path(self._STYLES_FOLDER).joinpath("default", style),
)
for root, _, filenames in os.walk(repo_styles.joinpath("user")):
os.mkdir(Path(self._STYLES_FOLDER).joinpath("user"))
for style in filenames:
copyfile(
os.path.join(root, style),
Path(self._STYLES_FOLDER).joinpath("user", style),
)
for folder in [self.DEFAULT_STYLES_LOCATION, self.USER_STYLES_LOCATION]:
self.load_available_styles_from_folder(folder)
self.load_custom_fonts_from_folder(folder)
@@ -202,7 +183,7 @@ class TerminalStyle:
self.applyMPLstyle()
def load_custom_fonts_from_folder(self, folder: str) -> None:
def load_custom_fonts_from_folder(self, folder: Path) -> None:
"""Load custom fonts form folder.
TTF and OTF fonts are loaded into the mpl font manager and are available for
@@ -213,12 +194,19 @@ class TerminalStyle:
folder : str
Path to the folder containing the fonts
"""
for font_file in os.listdir(folder):
if font_file.endswith(".otf") or font_file.endswith(".ttf"):
if not folder.exists():
return
for font_file in folder.iterdir():
if not font_file.is_file():
continue
if font_file.name.endswith(".otf") or font_file.name.endswith(".ttf"):
font_path = os.path.abspath(os.path.join(folder, font_file))
font_manager.fontManager.addfont(font_path)
def load_available_styles_from_folder(self, folder: str) -> None:
def load_available_styles_from_folder(self, folder: Path) -> None:
"""Load custom styles from folder.
Parses the styles/default and styles/user folders and loads style files.
@@ -233,22 +221,29 @@ class TerminalStyle:
folder : str
Path to the folder containing the stylesheets
"""
for stf in os.listdir(folder):
if stf.endswith(".mplstyle"):
self.mpl_styles_available[stf.replace(".mplstyle", "")] = os.path.join(
folder, stf
)
elif stf.endswith(".mplrc.json"):
if not folder.exists():
return
for stf in folder.iterdir():
if not stf.is_file():
continue
if stf.name.endswith(".mplstyle"):
self.mpl_styles_available[
stf.name.replace(".mplstyle", "")
] = os.path.join(folder, stf)
elif stf.name.endswith(".mplrc.json"):
self.mpl_rcparams_available[
stf.replace(".mplrc.json", "")
stf.name.replace(".mplrc.json", "")
] = os.path.join(folder, stf)
elif stf.endswith(".mpfstyle.json"):
elif stf.name.endswith(".mpfstyle.json"):
self.mpf_styles_available[
stf.replace(".mpfstyle.json", "")
stf.name.replace(".mpfstyle.json", "")
] = os.path.join(folder, stf)
elif stf.endswith(".richstyle.json"):
elif stf.name.endswith(".richstyle.json"):
self.console_styles_available[
stf.replace(".richstyle.json", "")
stf.name.replace(".richstyle.json", "")
] = os.path.join(folder, stf)
def applyMPLstyle(self):

View File

@@ -22,7 +22,7 @@ from openbb_terminal.core.config import ( # pylint: disable=unused-import # no
)
from openbb_terminal.common import feedparser_view
from openbb_terminal.core.config.paths import (
REPO_DIRECTORY,
REPOSITORY_DIRECTORY,
USER_ENV_FILE,
REPOSITORY_ENV_FILE,
HOME_DIRECTORY,
@@ -908,7 +908,7 @@ def main(
console.print("[green]OpenBB Terminal Integrated Tests:\n[/green]")
for file in test_files:
file = file.replace("//", "/")
repo_path_position = file.rfind(REPO_DIRECTORY.name)
repo_path_position = file.rfind(REPOSITORY_DIRECTORY.name)
if repo_path_position >= 0:
file_name = file[repo_path_position:].replace("\\", "/")
else:
@@ -926,7 +926,7 @@ def main(
if fails:
console.print("\n[red]Failures:[/red]\n")
for key, value in fails.items():
repo_path_position = key.rfind(REPO_DIRECTORY.name)
repo_path_position = key.rfind(REPOSITORY_DIRECTORY.name)
if repo_path_position >= 0:
file_name = key[repo_path_position:].replace("\\", "/")
else:

View File

@@ -2,7 +2,7 @@ import os
import pytest
from openbb_terminal.core.log.generation import formatter_with_exceptions
from openbb_terminal.core.log.generation.settings import AppSettings
from openbb_terminal.core.config.paths import REPO_DIRECTORY
from openbb_terminal.core.config.paths import REPOSITORY_DIRECTORY
app_settings = AppSettings(
commit_hash="MOCK_COMMIT_HASH",
@@ -33,7 +33,9 @@ def test_extract_log_extra(mocker):
def test_filter_piis():
text = f"test 1.1.1.1 chavi@chavi.com {REPO_DIRECTORY.name}{os.sep} dd{os.sep}dd"
text = (
f"test 1.1.1.1 chavi@chavi.com {REPOSITORY_DIRECTORY.name}{os.sep} dd{os.sep}dd"
)
value = formatter.filter_piis(text)
assert value