Skip to content

Commit 1ae5c75

Browse files
committed
refactor(config): Remove unused code, fix typos and add comments for clearer information
1 parent 3afecef commit 1ae5c75

File tree

21 files changed

+125
-183
lines changed

21 files changed

+125
-183
lines changed

.github/workflows/release-and-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
uses: actions/checkout@v4
1515
with:
1616
fetch-depth: 0
17-
- name: Set up Pythonx`
17+
- name: Set up Python
1818
uses: actions/setup-python@v4
1919
with:
2020
python-version: '3.10'

openadapt/alembic/context_loader.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""Context loader for alembic migrations."""
2+
3+
14
import pathlib
25

36
from alembic.config import Config

openadapt/app/dashboard/app/settings/(api_keys)/form.tsx

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use client';
22

3-
import { Button, Fieldset, Flex, Grid, Stack, Text, TextInput } from '@mantine/core';
3+
import { Button, Fieldset, Flex, Grid, Stack, TextInput } from '@mantine/core';
44
import { useForm } from '@mantine/form';
5-
import { notifications } from '@mantine/notifications';
65
import React, { useEffect } from 'react'
6+
import { saveSettings } from '../utils';
77

88
type Props = {
99
settings: Record<string, string>,
@@ -24,40 +24,8 @@ export const Form = ({
2424
function resetForm() {
2525
form.reset();
2626
}
27-
function saveSettings(values: Record<string, string>) {
28-
fetch('/api/settings', {
29-
method: 'POST',
30-
headers: {
31-
'Content-Type': 'application/json',
32-
},
33-
body: JSON.stringify(values),
34-
}).then(resp => {
35-
if (resp.ok) {
36-
notifications.show({
37-
title: 'Settings saved',
38-
message: 'Your settings have been saved',
39-
color: 'green',
40-
});
41-
return resp.json();
42-
} else {
43-
notifications.show({
44-
title: 'Failed to save settings',
45-
message: 'Please try again',
46-
color: 'red',
47-
})
48-
return null;
49-
}
50-
51-
}).then((resp) => {
52-
if (!resp) {
53-
return;
54-
}
55-
form.setInitialValues(values);
56-
form.setDirty({});
57-
});
58-
}
5927
return (
60-
<form onSubmit={form.onSubmit(saveSettings)}>
28+
<form onSubmit={form.onSubmit(saveSettings(form))}>
6129
<Grid>
6230
<Grid.Col span={6}>
6331
<Fieldset legend="PRIVACY">

openadapt/app/dashboard/app/settings/record_and_replay/form.tsx

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import { Button, Checkbox, Flex, Grid, TextInput } from '@mantine/core';
44
import { useForm } from '@mantine/form';
5-
import { notifications } from '@mantine/notifications';
65
import React, { useEffect } from 'react'
7-
import { validateRecordAndReplaySettings } from '../utils';
6+
import { saveSettings, validateRecordAndReplaySettings } from '../utils';
87

98
type Props = {
109
settings: Record<string, string>,
@@ -28,41 +27,9 @@ export const Form = ({
2827
function resetForm() {
2928
form.reset();
3029
}
31-
function saveSettings(values: Record<string, string>) {
32-
fetch('/api/settings', {
33-
method: 'POST',
34-
headers: {
35-
'Content-Type': 'application/json',
36-
},
37-
body: JSON.stringify(values),
38-
}).then(resp => {
39-
if (resp.ok) {
40-
notifications.show({
41-
title: 'Settings saved',
42-
message: 'Your settings have been saved',
43-
color: 'green',
44-
});
45-
return resp.json();
46-
} else {
47-
notifications.show({
48-
title: 'Failed to save settings',
49-
message: 'Please try again',
50-
color: 'red',
51-
})
52-
return null;
53-
}
54-
55-
}).then((resp) => {
56-
if (!resp) {
57-
return;
58-
}
59-
form.setInitialValues(values);
60-
form.setDirty({});
61-
});
62-
}
6330

6431
return (
65-
<form onSubmit={form.onSubmit(saveSettings)}>
32+
<form onSubmit={form.onSubmit(saveSettings(form))}>
6633
<Grid>
6734
<Grid.Col span={6}>
6835
<Checkbox label="Record window data" {...form.getInputProps('RECORD_WINDOW_DATA')} checked={form.values.RECORD_WINDOW_DATA} />

openadapt/app/dashboard/app/settings/scrubbing/form.tsx

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Button, Checkbox, Flex, Grid, TextInput } from '@mantine/core';
44
import { useForm } from '@mantine/form';
55
import { notifications } from '@mantine/notifications';
66
import React, { useEffect } from 'react'
7-
import { validateScrubbingSettings } from '../utils';
7+
import { saveSettings, validateScrubbingSettings } from '../utils';
88

99
type Props = {
1010
settings: Record<string, string>,
@@ -28,41 +28,9 @@ export const Form = ({
2828
function resetForm() {
2929
form.reset();
3030
}
31-
function saveSettings(values: Record<string, string>) {
32-
fetch('/api/settings', {
33-
method: 'POST',
34-
headers: {
35-
'Content-Type': 'application/json',
36-
},
37-
body: JSON.stringify(values),
38-
}).then(resp => {
39-
if (resp.ok) {
40-
notifications.show({
41-
title: 'Settings saved',
42-
message: 'Your settings have been saved',
43-
color: 'green',
44-
});
45-
return resp.json();
46-
} else {
47-
notifications.show({
48-
title: 'Failed to save settings',
49-
message: 'Please try again',
50-
color: 'red',
51-
})
52-
return null;
53-
}
54-
55-
}).then((resp) => {
56-
if (!resp) {
57-
return;
58-
}
59-
form.setInitialValues(values);
60-
form.setDirty({});
61-
});
62-
}
6331

6432
return (
65-
<form onSubmit={form.onSubmit(saveSettings)}>
33+
<form onSubmit={form.onSubmit(saveSettings(form))}>
6634
<Grid>
6735
<Grid.Col span={6}>
6836
<Checkbox label="Scrubbing Enabled" {...form.getInputProps('SCRUB_ENABLED')} checked={form.values.SCRUB_ENABLED} />

openadapt/app/dashboard/app/settings/utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { UseFormReturnType } from '@mantine/form';
2+
import { notifications } from '@mantine/notifications';
3+
4+
15
export function validateScrubbingSettings(settings: Record<string, string>) {
26
const errors: Record<string, string> = {}
37
if (settings.SCRUB_ENABLED) {
@@ -30,3 +34,41 @@ export function validateRecordAndReplaySettings(settings: Record<string, string>
3034
}
3135
return errors
3236
}
37+
38+
39+
export function saveSettings(
40+
form: UseFormReturnType<any>,
41+
) {
42+
return function(values: Record<string, string>) {
43+
fetch('/api/settings', {
44+
method: 'POST',
45+
headers: {
46+
'Content-Type': 'application/json',
47+
},
48+
body: JSON.stringify(values),
49+
}).then(resp => {
50+
if (resp.ok) {
51+
notifications.show({
52+
title: 'Settings saved',
53+
message: 'Your settings have been saved',
54+
color: 'green',
55+
});
56+
return resp.json();
57+
} else {
58+
notifications.show({
59+
title: 'Failed to save settings',
60+
message: 'Please try again',
61+
color: 'red',
62+
})
63+
return null;
64+
}
65+
66+
}).then((resp) => {
67+
if (!resp) {
68+
return;
69+
}
70+
form.setInitialValues(values);
71+
form.setDirty({});
72+
});
73+
}
74+
}

openadapt/app/dashboard/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"@/*": ["./*"]
2424
}
2525
},
26-
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/settings/(api_keys)"],
26+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
2727
"exclude": ["node_modules"]
2828
}

openadapt/app/visualize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from nicegui import events, ui
1010
from notifypy import Notify
1111

12-
from openadapt.build_utils import override_stdout_stderr
12+
from openadapt.build_utils import redirect_stdout_stderr
1313

14-
with override_stdout_stderr():
14+
with redirect_stdout_stderr():
1515
from tqdm import tqdm
1616

1717
import click
@@ -294,7 +294,7 @@ def on_change_closure(e: events.ValueChangeEventArguments) -> None:
294294
)
295295
)
296296

297-
with override_stdout_stderr():
297+
with redirect_stdout_stderr():
298298
with tqdm(
299299
total=num_events,
300300
desc="Generating Visualization" if not SCRUB else "Scrubbing Visualization",

openadapt/build.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@
122122

123123
subprocess.call(spec)
124124

125-
# add import sys ; sys.setrecursionlimit(sys.getrecursionlimit() * 5)
126-
# to line 2 of OpenAdapt.spec
127-
with open("OpenAdapt.spec", "r+") as f:
128-
lines = f.readlines()
129-
lines[1] = "import sys ; sys.setrecursionlimit(sys.getrecursionlimit() * 50)\n"
130-
f.seek(0)
131-
f.truncate()
132-
f.writelines(lines)
133-
134125
# building
135126
proc = subprocess.Popen("pyinstaller OpenAdapt.spec --noconfirm", shell=True)
136127
proc.wait()

openadapt/build_utils.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ def get_path_to_preferences_folder(
1616
# and set the path for all user preferences
1717
path = pathlib.Path.home() / "Library" / "Preferences" / "openadapt"
1818
if not path.exists():
19-
path.mkdir()
19+
path.mkdir(parents=True, exist_ok=True)
2020
return path
2121
else:
2222
# if windows, get the path to the %APPDATA% directory and set the path
2323
# for all user preferences
2424
path = pathlib.Path.home() / "AppData" / "Roaming" / "openadapt"
2525
if not path.exists():
26-
path.mkdir()
26+
path.mkdir(parents=True, exist_ok=True)
2727
return path
2828

2929

@@ -32,26 +32,27 @@ def is_running_from_executable() -> bool:
3232
return getattr(sys, "frozen", False)
3333

3434

35-
def override_stdout_stderr() -> object:
36-
"""Override stdout and stderr to /dev/null when running from an executable."""
35+
class RedirectOutput:
36+
"""Context manager to redirect stdout and stderr to /dev/null."""
3737

38-
class StdoutStderrOverride:
39-
def __init__(self) -> None:
40-
self.stdout = None
41-
self.stderr = None
38+
def __enter__(self) -> "RedirectOutput":
39+
"""Redirect stdout and stderr to /dev/null."""
40+
if is_running_from_executable():
41+
self.old_stdout = sys.stdout
42+
self.old_stderr = sys.stderr
43+
null_stream = open(os.devnull, "a")
44+
sys.stdout = sys.stderr = null_stream
45+
return self
4246

43-
def __enter__(self) -> None:
44-
if is_running_from_executable():
45-
self.old_stdout = sys.stdout
46-
self.old_stderr = sys.stderr
47-
sys.stdout = open(os.devnull, "a")
48-
sys.stderr = open(os.devnull, "a")
47+
def __exit__(self, exc_type: type, exc_value: Exception, traceback: type) -> None:
48+
"""Restore stdout and stderr."""
49+
if is_running_from_executable():
50+
sys.stdout.close()
51+
sys.stderr.close()
52+
sys.stdout = self.old_stdout
53+
sys.stderr = self.old_stderr
4954

50-
def __exit__(self, *args: tuple, **kwargs: dict) -> None:
51-
if is_running_from_executable():
52-
sys.stdout.close()
53-
sys.stderr.close()
54-
sys.stdout = self.old_stdout
55-
sys.stderr = self.old_stderr
5655

57-
return StdoutStderrOverride()
56+
def redirect_stdout_stderr() -> RedirectOutput:
57+
"""Get the RedirectOutput instance for use as a context manager."""
58+
return RedirectOutput()

0 commit comments

Comments
 (0)