-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance
feature_build.py
with NOT build sections and multifile support
- Loading branch information
Showing
1 changed file
with
83 additions
and
43 deletions.
There are no files selected for viewing
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,69 +1,109 @@ | ||
from typing import List | ||
from typing import Dict, List, Optional, Tuple | ||
|
||
import logging | ||
|
||
from utils import config | ||
|
||
_FILE_OF_FEATURE = { | ||
'STATUS_IN_SETTINGS': 'routes/setting/+page.svelte' | ||
_FILE_OF_FEATURE: Dict[str, Tuple] = { | ||
'STATUS_IN_SETTINGS': ('src/routes/settings/+page.svelte', 'src/lib/components/settings/Box.svelte',), | ||
'!STATUS_IN_SETTINGS': ('src/lib/components/settings/Box.svelte',) | ||
} | ||
|
||
|
||
def disable_feature(feature: str) -> None: | ||
filename = _decode_feature_file(feature) | ||
if len(filename) == 0: | ||
def disable_feature(feature: str, _recursive: bool = True) -> None: | ||
unused_warn = True | ||
if _recursive: | ||
enable_feature(f'!{feature}', False) | ||
filenames = _decode_feature_files(feature) | ||
if not filenames: | ||
return | ||
lines = _read_file(filename) | ||
line_num = _find_feature(feature, lines) | ||
if line_num < 0: | ||
for filename in filenames: | ||
lines = _read_file(filename) | ||
for line_num in _find_html_features(feature, lines): | ||
lines[line_num] = lines[line_num].replace('-->', '') | ||
unused_warn = False | ||
for line_num in _find_css_js_features(feature, lines): | ||
lines[line_num] = lines[line_num].replace('*/', '') | ||
unused_warn = False | ||
if unused_warn: | ||
logging.warning(f'Couldn\'t find feature "{feature}" in file "{filename}"') | ||
_write_file(filename, lines) | ||
|
||
|
||
def enable_feature(feature: str, _recursive: bool = True) -> None: | ||
def _enable_feature_at(lines: list, line_num: int, feature: str, enable_str: str) -> None: | ||
if lines[line_num].strip()[-1] != feature[-1]: | ||
print(lines[line_num]) | ||
print(feature[-1]) | ||
print(lines[line_num]) | ||
logging.error(f'Corrupted feature definition for feature "{feature}"') | ||
return | ||
lines[line_num] = lines[line_num].strip() | ||
lines[line_num] += enable_str | ||
|
||
unused_warn = True | ||
if _recursive: | ||
disable_feature(f'!{feature}', False) | ||
filenames = _decode_feature_files(feature) | ||
if not filenames: | ||
return | ||
lines[line_num].replace('-->', '') | ||
_write_file(filename, lines) | ||
|
||
|
||
def enable_feature(feature: str) -> None: | ||
filename = _decode_feature_file(feature) | ||
if len(filename) == 0: | ||
return | ||
lines = _read_file(filename) | ||
line_num = _find_feature(feature, lines) | ||
if line_num < 0: | ||
return | ||
if lines[-1] != feature[-1]: | ||
logging.error(f'Corrupted feature definition for feature "{feature}"') | ||
return | ||
lines[line_num] += '-->' | ||
_write_file(filename, lines) | ||
for filename in filenames: | ||
lines = _read_file(filename) | ||
for line_num in _find_html_features(feature, lines): | ||
_enable_feature_at(lines, line_num, feature, '-->\n') | ||
unused_warn = False | ||
for line_num in _find_css_js_features(feature, lines): | ||
_enable_feature_at(lines, line_num, feature, '*/\n') | ||
unused_warn = False | ||
if unused_warn: | ||
logging.warning(f'Couldn\'t find feature "{feature}" in file "{filename}"') | ||
_write_file(filename, lines) | ||
|
||
|
||
def get_features() -> List[str]: | ||
return list(_FILE_OF_FEATURE.keys()) | ||
|
||
def _decode_feature_file(feature: str) -> str: | ||
filename = _FILE_OF_FEATURE.get(feature) | ||
if not filename: | ||
|
||
def _decode_feature_files(feature: str) -> Optional[List[str]]: | ||
filenames = _FILE_OF_FEATURE.get(feature) | ||
if not filenames: | ||
logging.error(f'Non-existing feature: "{feature}"') | ||
return "" | ||
return None | ||
path = config.get('frontend').get('path') | ||
return f'{path}/{filename}' | ||
return [f'{path}/{filename}' for filename in filenames] | ||
|
||
|
||
def _find_html_features(feature: str, lines: list) -> List[int]: | ||
return _find_features(lines, f'<!--{feature}') | ||
|
||
|
||
def _find_css_js_features(feature: str, lines: list) -> List[int]: | ||
return _find_features(lines, f'/*{feature}') | ||
|
||
|
||
def _find_feature(feature: str, lines: list) -> int: | ||
def _find_features(lines: list, search_for: str) -> List[int]: | ||
features = [] | ||
for i, line in enumerate(lines): | ||
if line.startswith(f'<!--{feature}'): | ||
return i | ||
logging.error(f'Couldn\'t find feature definition "{feature}" in file') | ||
return -1 | ||
if line.strip().startswith(search_for): | ||
features.append(i) | ||
return features | ||
|
||
|
||
def _read_file(filename: str) -> list: | ||
with open(filename, 'r', encoding='utf-8') as f: | ||
lines = f.readlines() | ||
f.close() | ||
return lines | ||
try: | ||
with open(filename, 'r', encoding='utf-8') as f: | ||
lines = f.readlines() | ||
f.close() | ||
return lines | ||
except FileNotFoundError: | ||
logging.error(f'File "{filename}" containing a feature does not exist') | ||
return [] | ||
|
||
|
||
def _write_file(filename: str, lines: list) -> None: | ||
with open(filename, 'w', encoding='utf-8') as f: | ||
f.writelines(lines) | ||
f.close() | ||
try: | ||
with open(filename, 'w', encoding='utf-8') as f: | ||
f.writelines(lines) | ||
f.close() | ||
except FileNotFoundError: | ||
logging.error(f'File "{filename}" containing a feature does not exist') |