Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tuneit
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vladimir Vaskov
tuneit
Commits
1f0981e2
Commit
1f0981e2
authored
Jan 18, 2025
by
Roman Alifanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some improvements
parent
c0ee9f94
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
27 deletions
+80
-27
backends.py
src/settings/backends.py
+33
-22
main.py
src/settings/main.py
+12
-4
gvariant.py
src/settings/tools/gvariant.py
+34
-0
window.py
src/window.py
+1
-1
No files found.
src/settings/backends.py
View file @
1f0981e2
...
@@ -67,11 +67,11 @@ class FileBackend(Backend):
...
@@ -67,11 +67,11 @@ class FileBackend(Backend):
ext
=
ext
.
lower
()
ext
=
ext
.
lower
()
if
ext
==
'.json'
:
if
ext
==
'.json'
:
return
'json'
return
'json'
elif
ext
==
'.yaml'
or
ext
==
'.yml'
:
elif
ext
in
[
'.yaml'
,
'.yml'
]
:
return
'yaml'
return
'yaml'
elif
ext
==
'.ini'
:
elif
ext
==
'.ini'
:
return
'ini'
return
'ini'
elif
ext
==
'.sh'
or
ext
==
'.conf'
:
elif
ext
in
[
'.sh'
,
'.conf'
]
:
return
'text'
return
'text'
else
:
else
:
return
'text'
return
'text'
...
@@ -97,12 +97,15 @@ class FileBackend(Backend):
...
@@ -97,12 +97,15 @@ class FileBackend(Backend):
def
_write_file
(
self
,
data
):
def
_write_file
(
self
,
data
):
try
:
try
:
with
open
(
self
.
file_path
,
'
w
'
,
encoding
=
self
.
encoding
)
as
file
:
with
open
(
self
.
file_path
,
'
r+
'
,
encoding
=
self
.
encoding
)
as
file
:
if
self
.
file_type
==
'json'
:
if
self
.
file_type
==
'json'
:
file
.
seek
(
0
)
json
.
dump
(
data
,
file
,
indent
=
4
)
json
.
dump
(
data
,
file
,
indent
=
4
)
elif
self
.
file_type
==
'yaml'
:
elif
self
.
file_type
==
'yaml'
:
file
.
seek
(
0
)
yaml
.
dump
(
data
,
file
,
default_flow_style
=
False
)
yaml
.
dump
(
data
,
file
,
default_flow_style
=
False
)
elif
self
.
file_type
==
'ini'
:
elif
self
.
file_type
==
'ini'
:
file
.
seek
(
0
)
config
=
ConfigParser
()
config
=
ConfigParser
()
for
section
,
values
in
data
.
items
():
for
section
,
values
in
data
.
items
():
config
[
section
]
=
values
config
[
section
]
=
values
...
@@ -126,39 +129,47 @@ class FileBackend(Backend):
...
@@ -126,39 +129,47 @@ class FileBackend(Backend):
return
config
return
config
def
_write_text_config
(
self
,
file
,
data
):
def
_write_text_config
(
self
,
file
,
data
):
existing_lines
=
[]
existing_lines
=
file
.
readlines
()
with
open
(
self
.
file_path
,
'r'
,
encoding
=
self
.
encoding
)
as
file_read
:
style
=
self
.
_detect_text_style
(
existing_lines
)
existing_lines
=
file_read
.
readlines
()
file
.
seek
(
0
)
existing_style
=
self
.
_detect_text_style
(
existing_lines
)
file
.
truncate
()
for
line
in
existing_lines
:
if
not
line
.
strip
()
or
line
.
startswith
(
'#'
):
file
.
write
(
line
)
elif
'='
in
line
:
key
,
_
=
line
.
split
(
'='
,
1
)
if
key
.
strip
()
in
data
:
if
style
==
'space_around'
:
file
.
write
(
f
"{key.strip()} = {data[key.strip()]}
\n
"
)
elif
style
==
'no_space'
:
file
.
write
(
f
"{key.strip()}={data[key.strip()]}
\n
"
)
else
:
file
.
write
(
f
"{key.strip()} = {data[key.strip()]}
\n
"
)
else
:
file
.
write
(
line
)
for
key
,
value
in
data
.
items
():
for
key
,
value
in
data
.
items
():
if
existing_style
==
'space_around'
:
if
not
any
(
key
.
strip
()
==
line
.
split
(
'='
,
1
)[
0
]
.
strip
()
for
line
in
existing_lines
):
file
.
write
(
f
"{key} = {value}
\n
"
)
elif
existing_style
==
'no_space'
:
file
.
write
(
f
"{key}={value}
\n
"
)
else
:
file
.
write
(
f
"{key} = {value}
\n
"
)
file
.
write
(
f
"{key} = {value}
\n
"
)
def
_detect_text_style
(
self
,
lines
):
def
_detect_text_style
(
self
,
lines
):
style
=
None
for
line
in
lines
:
for
line
in
lines
:
line
=
line
.
strip
()
line
=
line
.
strip
()
if
'='
in
line
:
if
'='
in
line
:
if
line
.
startswith
(
' '
)
and
line
.
endswith
(
' '
):
if
line
.
startswith
(
' '
)
and
line
.
endswith
(
' '
):
style
=
'space_around'
return
'space_around'
break
elif
line
.
find
(
'='
)
==
len
(
line
.
split
(
'='
)[
0
]):
elif
line
.
find
(
'='
)
==
len
(
line
.
split
(
'='
)[
0
]):
style
=
'no_space'
return
'no_space'
break
return
'space_around'
return
style
or
'space_around'
def
get_value
(
self
,
key
,
gtype
):
def
get_value
(
self
,
key
,
gtype
):
data
=
self
.
_read_file
()
data
=
self
.
_read_file
()
if
data
is
None
:
if
data
is
None
:
return
None
return
None
if
self
.
file_type
==
'json'
or
self
.
file_type
==
'yaml'
:
if
self
.
file_type
in
[
'json'
,
'yaml'
]
:
return
data
.
get
(
key
,
None
)
return
data
.
get
(
key
,
None
)
elif
self
.
file_type
==
'ini'
:
elif
self
.
file_type
==
'ini'
:
section
,
key_name
=
key
.
split
(
'.'
,
1
)
section
,
key_name
=
key
.
split
(
'.'
,
1
)
...
@@ -173,7 +184,7 @@ class FileBackend(Backend):
...
@@ -173,7 +184,7 @@ class FileBackend(Backend):
if
data
is
None
:
if
data
is
None
:
return
None
return
None
if
self
.
file_type
==
'json'
or
self
.
file_type
==
'yaml'
:
if
self
.
file_type
in
[
'json'
,
'yaml'
]
:
if
isinstance
(
data
.
get
(
key
),
list
):
if
isinstance
(
data
.
get
(
key
),
list
):
return
(
min
(
data
[
key
]),
max
(
data
[
key
]))
return
(
min
(
data
[
key
]),
max
(
data
[
key
]))
elif
self
.
file_type
==
'ini'
:
elif
self
.
file_type
==
'ini'
:
...
@@ -185,7 +196,7 @@ class FileBackend(Backend):
...
@@ -185,7 +196,7 @@ class FileBackend(Backend):
if
data
is
None
:
if
data
is
None
:
return
return
if
self
.
file_type
==
'json'
or
self
.
file_type
==
'yaml'
:
if
self
.
file_type
in
[
'json'
,
'yaml'
]
:
data
[
key
]
=
value
data
[
key
]
=
value
elif
self
.
file_type
==
'ini'
:
elif
self
.
file_type
==
'ini'
:
section
,
key_name
=
key
.
split
(
'.'
,
1
)
section
,
key_name
=
key
.
split
(
'.'
,
1
)
...
...
src/settings/
__init__
.py
→
src/settings/
main
.py
View file @
1f0981e2
...
@@ -4,6 +4,7 @@ from .backends import backend_factory
...
@@ -4,6 +4,7 @@ from .backends import backend_factory
from
.daemon_client
import
dclient
from
.daemon_client
import
dclient
from
.tools.yml_tools
import
load_modules
,
merge_categories_by_name
from
.tools.yml_tools
import
load_modules
,
merge_categories_by_name
from
.tools.gvariant
import
convert_by_gvariant
from
.widgets
import
WidgetFactory
from
.widgets
import
WidgetFactory
...
@@ -18,7 +19,9 @@ class Setting:
...
@@ -18,7 +19,9 @@ class Setting:
self
.
key
=
setting_data
.
get
(
'key'
)
self
.
key
=
setting_data
.
get
(
'key'
)
self
.
default
=
setting_data
.
get
(
'default'
)
self
.
default
=
setting_data
.
get
(
'default'
)
self
.
gtype
=
setting_data
.
get
(
'gtype'
,
[])
self
.
gtype
=
setting_data
.
get
(
'gtype'
,
[])
self
.
map
=
setting_data
.
get
(
'map'
,
self
.
_default_map
())
self
.
map
=
setting_data
.
get
(
'map'
)
if
self
.
map
is
None
:
self
.
map
=
self
.
_default_map
()
self
.
data
=
setting_data
.
get
(
'data'
,
{})
self
.
data
=
setting_data
.
get
(
'data'
,
{})
if
len
(
self
.
gtype
)
>
2
:
if
len
(
self
.
gtype
)
>
2
:
...
@@ -68,10 +71,15 @@ class Setting:
...
@@ -68,10 +71,15 @@ class Setting:
return
list
(
self
.
map
.
values
())
.
index
(
current_value
)
if
current_value
in
self
.
map
.
values
()
else
0
return
list
(
self
.
map
.
values
())
.
index
(
current_value
)
if
current_value
in
self
.
map
.
values
()
else
0
def
_get_backend_value
(
self
):
def
_get_backend_value
(
self
):
value
=
None
backend
=
self
.
_get_backend
()
backend
=
self
.
_get_backend
()
if
backend
:
if
backend
:
return
backend
.
get_value
(
self
.
key
,
self
.
gtype
)
value
=
backend
.
get_value
(
self
.
key
,
self
.
gtype
)
return
self
.
default
if
value
is
None
:
value
=
self
.
default
return
value
def
_get_backend_range
(
self
):
def
_get_backend_range
(
self
):
backend
=
self
.
_get_backend
()
backend
=
self
.
_get_backend
()
...
@@ -81,7 +89,7 @@ class Setting:
...
@@ -81,7 +89,7 @@ class Setting:
def
_set_backend_value
(
self
,
value
):
def
_set_backend_value
(
self
,
value
):
backend
=
self
.
_get_backend
()
backend
=
self
.
_get_backend
()
if
backend
:
if
backend
:
backend
.
set_value
(
self
.
key
,
value
,
self
.
gtype
)
backend
.
set_value
(
self
.
key
,
convert_by_gvariant
(
value
,
self
.
gtype
)
,
self
.
gtype
)
def
_get_backend
(
self
):
def
_get_backend
(
self
):
if
self
.
root
is
True
:
if
self
.
root
is
True
:
...
...
src/settings/tools/gvariant.py
0 → 100644
View file @
1f0981e2
def
convert_by_gvariant
(
value
,
gtype
):
"""
Приводит значение к нужному типу в зависимости от GVariant gtype.
:param value: Исходное значение
:param gtype: Тип GVariant ('b', 'y', 'n', 'q', 'i', 'u', 'x', 't', 'd', 's')
:return: Значение, приведенное к указанному типу
"""
try
:
if
gtype
==
'b'
:
# Boolean
return
bool
(
value
)
elif
gtype
==
'y'
:
# Byte
return
max
(
0
,
min
(
255
,
int
(
value
)))
# Ограничение диапазона
elif
gtype
==
'n'
:
# Int16
return
max
(
-
32768
,
min
(
32767
,
int
(
value
)))
# Ограничение диапазона
elif
gtype
==
'q'
:
# Uint16
return
max
(
0
,
min
(
65535
,
int
(
value
)))
# Ограничение диапазона
elif
gtype
==
'i'
:
# Int32
return
max
(
-
2147483648
,
min
(
2147483647
,
int
(
value
)))
elif
gtype
==
'u'
:
# Uint32
return
max
(
0
,
min
(
4294967295
,
int
(
value
)))
elif
gtype
==
'x'
:
# Int64
return
max
(
-
9223372036854775808
,
min
(
9223372036854775807
,
int
(
value
)))
elif
gtype
==
't'
:
# Uint64
return
max
(
0
,
min
(
18446744073709551615
,
int
(
value
)))
elif
gtype
==
'd'
:
# Double
return
float
(
value
)
elif
gtype
==
's'
:
# String
return
str
(
value
)
else
:
raise
ValueError
(
f
"Неизвестный GVariant тип: {gtype}"
)
except
(
ValueError
,
TypeError
)
as
e
:
print
(
f
"Ошибка приведения типа: {e}"
)
return
None
src/window.py
View file @
1f0981e2
...
@@ -19,7 +19,7 @@
...
@@ -19,7 +19,7 @@
from
gi.repository
import
GObject
,
Adw
,
Gtk
from
gi.repository
import
GObject
,
Adw
,
Gtk
from
.settings
import
init_settings_stack
from
.settings
.main
import
init_settings_stack
@Gtk.Template
(
resource_path
=
'/ru.ximperlinux.TuneIt/window.ui'
)
@Gtk.Template
(
resource_path
=
'/ru.ximperlinux.TuneIt/window.ui'
)
class
TuneitWindow
(
Adw
.
ApplicationWindow
):
class
TuneitWindow
(
Adw
.
ApplicationWindow
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment