forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlags.py
56 lines (47 loc) · 1.2 KB
/
Flags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import wx
import os
import base64
import Utils
def GetFlagFName( ioc ):
return os.path.join( Utils.getImageFolder(), 'flags', ioc + '.png' )
flagImageCache = {}
def GetFlagImage( ioc ):
ioc = ioc.upper()
try:
return flagImageCache[ioc]
except KeyError:
pass
fname = GetFlagFName( ioc )
if not os.path.exists(fname):
flagImageCache[ioc] = None
else:
flagImageCache[ioc] = wx.Image( fname, wx.BITMAP_TYPE_PNG )
return flagImageCache[ioc]
flagBase64Cache = {}
def GetFlagBase64( ioc ):
ioc = ioc.upper()
try:
return flagBase64Cache[ioc]
except KeyError:
pass
fname = GetFlagFName( ioc )
try:
with open(fname, 'rb') as f:
flagBase64Cache[ioc] = 'data:image/png;base64,{}'.format(base64.standard_b64encode(f.read()).decode())
except Exception:
flagBase64Cache[ioc] = None
return flagBase64Cache[ioc]
def GetFlagBase64ForUCI( codes ):
flags = {}
for c in codes:
ioc = c[:3]
if ioc not in flags:
flag = GetFlagBase64( ioc )
if flag:
flags[ioc] = flag
return flags
if __name__ == '__main__':
print( GetFlagBase64( 'CAN' ) )
print( GetFlagBase64( 'USA' ) )
print( GetFlagBase64( 'SUI' ) )
print( GetFlagBase64ForUCI( ['CAN19920203', 'USA19870407', 'SUI20020905'] ) )