4
4
5
5
import os , json , aiofiles
6
6
7
- from sys import _getframe
8
- from functools import wraps
9
- from inspect import signature
10
-
11
-
12
- # Taken from https://github.com/dabeaz/curio/blob/master/curio/meta.py
13
- def from_coroutine (level = 2 , _cache = {}):
14
- f_code = _getframe (level ).f_code
15
- if f_code in _cache :
16
- return _cache [f_code ]
17
- if f_code .co_flags & _CO_FROM_COROUTINE :
18
- _cache [f_code ] = True
19
- return True
20
- else :
21
- if (f_code .co_flags & _CO_NESTED and f_code .co_name [0 ] == '<' ):
22
- return from_coroutine (level + 2 )
23
- else :
24
- _cache [f_code ] = False
25
- return False
26
-
27
- def awaitable (syncfunc ):
28
- def decorate (asyncfunc ):
29
- if signature (syncfunc ) != signature (asyncfunc ):
30
- raise TypeError (f'{ syncfunc .__name__ } and async { asyncfunc .__name__ } have different signatures' )
31
-
32
- @wraps (asyncfunc )
33
- def wrapper (* args , ** kwargs ):
34
- if from_coroutine ():
35
- return asyncfunc (* args , ** kwargs )
36
- else :
37
- return syncfunc (* args , ** kwargs )
38
- wrapper ._syncfunc = syncfunc
39
- wrapper ._asyncfunc = asyncfunc
40
- wrapper ._awaitable = True
41
- wrapper .__doc__ = syncfunc .__doc__ or asyncfunc .__doc__
42
- return wrapper
43
- return decorate
44
-
45
-
46
- # Read json file
47
- def json_read ():
48
- with open (f"{ os .path .dirname (__file__ )} /data/languages.json" , "r" ) as jsn_f :
49
- codes = json .load (jsn_f )
50
- return codes
51
-
52
- @awaitable (json_read )
53
7
async def json_read ():
54
8
async with aiofiles .open (f"{ os .path .dirname (__file__ )} /data/languages.json" , "r" ) as jsn_f :
55
9
codes = json .load (jsn_f )
@@ -58,20 +12,22 @@ async def json_read():
58
12
59
13
# Get language code
60
14
def get_lang_code (name ):
61
- dt = json_read ()
62
- return list (dt .keys ())[list (dt .values ()).index (str (name ).lower ())]
15
+ with open (f"{ os .path .dirname (__file__ )} /data/languages.json" , "r" ) as jsn_f :
16
+ dt = json .load (jsn_f )
17
+ return list (dt .keys ())[list (dt .values ()).index (str (name ).lower ())]
63
18
64
- @ awaitable ( get_lang_code )
65
- async def get_lang_code ( name ) :
66
- dt = await json_read ( )
67
- return list (dt .keys ())[list (dt .values ()).index (str (name ).lower ())]
19
+ async def get_lang_code_async ( name ):
20
+ async with aiofiles . open ( f" { os . path . dirname ( __file__ ) } /data/languages.json" , "r" ) as jsn_f :
21
+ dt = json . load ( jsn_f )
22
+ return list (dt .keys ())[list (dt .values ()).index (str (name ).lower ())]
68
23
69
24
# Get language name
70
25
def get_lang_name (code ):
71
- dt = json_read ()
72
- return dt [str (code )]
26
+ with open (f"{ os .path .dirname (__file__ )} /data/languages.json" , "r" ) as jsn_f :
27
+ dt = json .load (jsn_f )
28
+ return dt [str (code )]
73
29
74
- @ awaitable ( get_lang_name )
75
- async def get_lang_name ( code ) :
76
- dt = await json_read ( )
77
- return dt [str (name )]
30
+ async def get_lang_name_async ( code ):
31
+ async with aiofiles . open ( f" { os . path . dirname ( __file__ ) } /data/languages.json" , "r" ) as jsn_f :
32
+ dt = json . load ( jsn_f )
33
+ return dt [str (name )]
0 commit comments