1
- from functools import lru_cache
1
+ from functools import lru_cache , total_ordering
2
2
import os
3
3
import shelve
4
+ from contextlib import contextmanager
4
5
import sys
5
6
import weakref
6
7
from config import Config
7
8
from i18n import I18nAuto
8
9
10
+ @lru_cache
11
+ def load_config ():
12
+ return Config (), I18nAuto ()
13
+
14
+ @lru_cache
15
+ def get_cwd ():
16
+ CWD = os .getcwd ()
17
+ if CWD not in sys .path :
18
+ sys .path .append (CWD )
19
+ return CWD
20
+
9
21
MENU_ITEMS = {
10
22
"Get help" : "https://github.com/SayanoAI/RVC-Studio/discussions" ,
11
23
"Report a Bug" : "https://github.com/SayanoAI/RVC-Studio/issues" ,
20
32
N_THREADS_OPTIONS = [1 ,2 ,4 ,8 ,12 ,16 ]
21
33
SR_MAP = {"32k" : 32000 ,"40k" : 40000 , "48k" : 48000 }
22
34
35
+ BASE_DIR = get_cwd ()
36
+ BASE_MODELS_DIR = os .path .join (BASE_DIR ,"models" )
37
+ SONG_DIR = os .path .join (BASE_DIR ,"songs" )
38
+ BASE_CACHE_DIR = os .path .join (BASE_DIR ,".cache" )
39
+ DATASETS_DIR = os .path .join (BASE_DIR ,"datasets" )
40
+ LOG_DIR = os .path .join (BASE_DIR ,"logs" )
41
+ OUTPUT_DIR = os .path .join (BASE_DIR ,"output" )
42
+
23
43
class ObjectNamespace (dict ):
24
44
def __init__ (self ,** kwargs ): super ().__init__ (kwargs )
25
45
def __missing__ (self , name : str ): return ObjectNamespace ()
@@ -38,45 +58,91 @@ def __setstate__(self, state):
38
58
def __getstate__ (self ): return dict (** self )
39
59
40
60
class PersistedDict :
41
- def __init__ (self ,fname ,** kwargs ):
42
- self .__fname__ = fname
43
- if len (kwargs ):
44
- with shelve .open (fname ) as shelf :
45
- for k in kwargs :
46
- shelf [k ] = kwargs [k ]
47
61
48
- def __missing__ (self , name : str ): return print (f"Attribute { name } is missing" )
62
+ # initialize the class with an optional filename and dict arguments
63
+ def __init__ (self , filename = None , ** data ):
64
+ # store the filename as an attribute
65
+ self .filename = filename
49
66
50
- def __getitem__ (self , name : str ):
51
- if name .startswith ("__" ) and name .endswith ("__" ): return super ().__getattribute__ (name )
52
- with shelve .open (self .__fname__ ) as shelf :
53
- return shelf .get (name ,None )
67
+ for key , value in data .items ():
68
+ # recursively convert the values to NestedDict
69
+ self .__setattr__ (key , value )
70
+
71
+ # define a context manager to open and close the shelve file
72
+ @contextmanager
73
+ def open_shelf (self ):
74
+ # if filename is given, open the shelve file
75
+ shelf = shelve .open (self .filename ) if self .filename else {}
76
+
77
+ # yield the shelf as the resource
78
+ yield shelf
79
+ if hasattr (shelf ,"close" ):
80
+ # close the shelf when exiting the context
81
+ shelf .close ()
82
+
83
+ # define a method to get the attribute value given a key
84
+ def __getattr__ (self , key : str ):
85
+ is_private = key .startswith ("_" ) and key .endswith ("_" )
54
86
55
- def __setitem__ (self , name : str , value ):
56
- with shelve .open (self .__fname__ ) as shelf :
57
- shelf [name ] = value
87
+ # if the key is filename, set it as an attribute
88
+ if key == "filename" or is_private :
89
+ if key in self .__dict__ : return self .__dict__ [key ]
90
+ else : return None
91
+
92
+ # use the context manager to open the shelve file
93
+ with self .open_shelf () as shelf :
94
+ # if the key exists in the shelve file, return the value
95
+ # return getattr(shelf, key, None)
96
+ if key in shelf :
97
+ return shelf [key ]
98
+ # else, return None
99
+ else :
100
+ return None
58
101
59
- def get (self , name : str , value ): return self .__getitem__ (name ) or value
102
+ # define a method to set the attribute value given a key
103
+ def __setattr__ (self , key , value ):
104
+ # if the key is filename, set it as an attribute
105
+ if key == "filename" :
106
+ self .__dict__ [key ] = value
107
+ # else, use the context manager to open the shelve file
108
+ else :
109
+ with self .open_shelf () as shelf :
110
+ # store the value in the shelve file
111
+ print (f"{ key } ={ value } " )
112
+ shelf [key ] = value
60
113
61
- def set (self , name : str , value ): return self .__setitem__ (name , value )
114
+ # define a method to represent the class as a dict
115
+ def __repr__ (self ):
116
+ # initialize an empty dict
117
+ result = {}
118
+ # use the context manager to open the shelve file
119
+ with self .open_shelf () as shelf :
120
+ # loop through the keys in the shelve file
121
+ for key in shelf .keys ():
122
+ # add the key and value to the result
123
+ result [key ] = shelf [key ]
124
+ # return the result
125
+ return str (result )
62
126
63
- @lru_cache
64
- def load_config ():
65
- return Config (), I18nAuto ()
127
+ def __setitem__ (self , key , value ): self .__setattr__ (key , value )
128
+ def __getitem__ (self , key ): self .__getattr__ (key )
129
+ def __lt__ (self ,_ ): return False
130
+ def __eq__ (self ,other ):
131
+ if hasattr (other ,"filename" ): return self .filename == other .filename
132
+ else : return False
133
+ def __call__ (self ,* args ,** kwargs ):
134
+ print (f"{ args = } , { kwargs = } " )
135
+ return str (self )
66
136
67
- @lru_cache
68
- def get_cwd ():
69
- CWD = os .getcwd ()
70
- if CWD not in sys .path :
71
- sys .path .append (CWD )
72
- return CWD
73
137
74
138
@lru_cache
75
139
def get_servers ():
76
- servers = PersistedDict (os .path .join (get_cwd (),".cache" ,"servers.shelve" ))
140
+ os .makedirs (BASE_CACHE_DIR ,exist_ok = True )
141
+ fname = os .path .join (BASE_CACHE_DIR ,"servers.shelve" )
142
+ servers = PersistedDict (fname )
77
143
return servers
78
144
79
145
config , i18n = load_config ()
80
146
SERVERS = get_servers ()
81
- RVC_INFERENCE_URL = f" { SERVERS [ 'RVC' ][ 'url' ] } /rvc"
82
- UVR_INFERENCE_URL = f" { SERVERS [ 'RVC' ][ 'url' ] } /uvr"
147
+ RVC_INFERENCE_URL = SERVERS . RVC_INFERENCE_URL
148
+ UVR_INFERENCE_URL = SERVERS . UVR_INFERENCE_URL
0 commit comments