Skip to content

Commit

Permalink
Implement basic feature_build functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
Wrench56 committed Jul 1, 2024
1 parent 00f3220 commit c0c1f60
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/backend/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def all_settings(request: Request) -> PlainTextResponse:


@app.post('/settings/{id_}', response_class=PlainTextResponse)
async def update_setting(request: Request, id_: int) -> PlainTextResponse:
async def update_setting(request: Request, id_: str) -> PlainTextResponse:
response = PlainTextResponse()
if not database.uuid_exists(request.cookies.get('auth_cookie')):
response.status_code = 401
Expand Down
69 changes: 69 additions & 0 deletions src/backend/utils/feature_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from typing import List

import logging

from utils import config

_FILE_OF_FEATURE = {
'STATUS_IN_SETTINGS': 'routes/setting/+page.svelte'
}


def disable_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
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)


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:
logging.error(f'Non-existing feature: "{feature}"')
return ""
path = config.get('frontend').get('path')
return f'{path}/{filename}'


def _find_feature(feature: str, lines: list) -> int:
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


def _read_file(filename: str) -> list:
with open(filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
f.close()
return lines


def _write_file(filename: str, lines: list) -> None:
with open(filename, 'w', encoding='utf-8') as f:
f.writelines(lines)
f.close()
22 changes: 16 additions & 6 deletions src/backend/utils/settings.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
from typing import List
from typing import Dict, List, Optional

_settings: List[str] = ['E', 'E']
from utils import feature_build

_settings: Dict[str, str] = {
'BUILD_MODE': 'E',
'STATUS_IN_SETTINGS': 'E'
}

def update_setting(id_: int, value: str) -> bool:

def update_setting(id_: str, value: str) -> bool:
if id_ in feature_build.get_features():
if value == 'E':
feature_build.enable_feature(id_)
else:
feature_build.disable_feature(id_)
_settings[id_] = value
return True


def get_all() -> List[str]:
return _settings.copy()
return list(_settings.values())


def get_setting(id_: int) -> str:
return _settings[id_]
def get_setting(id_: str) -> Optional[str]:
return _settings.get(id_)


def get_default() -> List[str]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
<svelte:window on:keydown={handleKeydown} />

<div class="container">
<Setting id="0" bind:api={settings_list[0]}>
<Setting id="BUILD_MODE" bind:api={settings_list[0]}>
<pre>Build mode</pre>
</Setting>
<Setting id="1" bind:api={settings_list[1]}>
<Setting id="STATUS_IN_SETTINGS" bind:api={settings_list[1]}>
<pre>General status on settings page</pre>
</Setting>
</div>
Expand Down

0 comments on commit c0c1f60

Please sign in to comment.