[web] refactoring the config reading code

This commit is contained in:
Vinícius Moreira
2019-12-18 17:03:39 -03:00
parent b1e1fe5918
commit 6dba32cebe
3 changed files with 34 additions and 27 deletions

View File

@@ -134,13 +134,13 @@ will be pre-downloaded faster ( it does **NOT** modify your **pacman** settings
#### Web Applications ( web ) #### Web Applications ( web )
- It allows the installation of native Web applications by typing an address / URL in the search bar. - It allows the installation of native Web applications by typing an address / URL in the search bar.
- It also offers the possibility to customize the app to be generated the way you want: [TODO image or video] - It also offers the possibility to customize the generated app the way you want: [TODO image or video]
- It also provides some suggestions coming with predefined settings, and they also can be retrieved by searching their names. They are - It also provides some suggestions coming with predefined settings, and they also can be retrieved by searching their names. They are
defined at [suggestions.yml](https://github.com/vinifmor/bauh-files/blob/master/web/suggestions.yml), and downloaded during the application usage. defined at [suggestions.yml](https://github.com/vinifmor/bauh-files/blob/master/web/suggestions.yml), and downloaded during the application usage.
- It relies on [NodeJS](https://nodejs.org/en/), [Electron](https://electronjs.org/) and [nativefier](https://github.com/jiahaog/nativefier) to do all the magic, but you do not need them installed on your system. An isolated installation environment - It relies on [NodeJS](https://nodejs.org/en/), [Electron](https://electronjs.org/) and [nativefier](https://github.com/jiahaog/nativefier) to do all the magic, but you do not need them installed on your system. An isolated installation environment
will be generated at **~/.local/share/bauh/web/env**. will be generated at **~/.local/share/bauh/web/env**.
- The isolated environment is generated based on the settings defined in [environment.yml](https://github.com/vinifmor/bauh-files/blob/master/web/environment.yml) - The isolated environment is created based on the settings defined in [environment.yml](https://github.com/vinifmor/bauh-files/blob/master/web/environment.yml)
that is downloaded during runtime. ( downloaded during runtime ).
- Some applications require Javascript fixes to properly work. If it is a known fix, bauh will download the file JS file from [fix](https://github.com/vinifmor/bauh-files/tree/master/web/fix) and - Some applications require Javascript fixes to properly work. If it is a known fix, bauh will download the file JS file from [fix](https://github.com/vinifmor/bauh-files/tree/master/web/fix) and
install it with the generated app. install it with the generated app.
- The installed applications are located at **~/.local/share/bauh/installed**. - The installed applications are located at **~/.local/share/bauh/installed**.
@@ -148,12 +148,12 @@ install it with the generated app.
- When the Tray Mode **Start Minimized** is defined during the installation setup, a desktop entry will be also generated at **~/.config/autostart** - When the Tray Mode **Start Minimized** is defined during the installation setup, a desktop entry will be also generated at **~/.config/autostart**
allowing the application to launch automatically attached to system tray after the boot. allowing the application to launch automatically attached to system tray after the boot.
- The configuration file for the Web apps support is located at **~/.config/bauh/web.yml** and it allows the following customizations: - The configuration file for the Web apps support is located at **~/.config/bauh/web.yml** and it allows the following customizations:
` ```
environment: environment:
electron: electron:
version: null # set a custom Electron version here ( e.g: '6.1.4' ) version: null # set a custom Electron version here ( e.g: '6.1.4' )
system: false # set it to 'true' if you want to use the nativefier version globally installed on your system system: false # set it to 'true' if you want to use the nativefier version globally installed on your system
` ```
- Required packages: Arch systems ( **python-lxml**, **python-beautifulsoup4** ), Debian systems ( you will need to install the following packages with pip: **beautifulsoup4**, **lxml** ) - Required packages: Arch systems ( **python-lxml**, **python-beautifulsoup4** ), Debian systems ( you will need to install the following packages with pip: **beautifulsoup4**, **lxml** )
### General settings ### General settings

27
bauh/commons/config.py Normal file
View File

@@ -0,0 +1,27 @@
import os
from pathlib import Path
import yaml
from bauh.commons import util
def read_config(file_path: str, template: dict, update_file: bool = False) -> dict:
if not os.path.exists(file_path):
Path(file_path).mkdir(parents=True, exist_ok=True)
with open(file_path, 'w+') as f:
f.write(yaml.dump(template))
else:
with open(file_path) as f:
local_config = yaml.safe_load(f.read())
if local_config:
util.deep_update(template, local_config)
if update_file:
with open(file_path, 'w+') as f:
f.write(yaml.dump(template))
return template

View File

@@ -1,10 +1,4 @@
import os from bauh.commons.config import read_config as read
from pathlib import Path
import yaml
from bauh.api.constants import CONFIG_PATH
from bauh.commons import util
from bauh.gems.web import CONFIG_FILE from bauh.gems.web import CONFIG_FILE
@@ -15,20 +9,6 @@ def read_config(update_file: bool = False) -> dict:
'electron': {'version': None} 'electron': {'version': None}
} }
} }
if not os.path.exists(CONFIG_FILE):
Path(CONFIG_PATH).mkdir(parents=True, exist_ok=True)
with open(CONFIG_FILE, 'w+') as f: return read(CONFIG_FILE, default_config, update_file)
f.write(yaml.dump(default_config))
else:
with open(CONFIG_FILE) as f:
local_config = yaml.safe_load(f.read())
util.deep_update(default_config, local_config)
if update_file:
with open(CONFIG_FILE, 'w+') as f:
f.write(yaml.dump(default_config))
return default_config