@ -0,0 +1,50 @@ |
|||||||
|
""" Manage information about each nationality. |
||||||
|
""" |
||||||
|
|
||||||
|
import sys |
||||||
|
import os |
||||||
|
import json |
||||||
|
|
||||||
|
_nat_info = None |
||||||
|
_base_dir = None |
||||||
|
|
||||||
|
# --------------------------------------------------------------------- |
||||||
|
|
||||||
|
def _get_key( nat ) : |
||||||
|
"""Get the JSON key for a nationality. |
||||||
|
|
||||||
|
We require the JSON keys to be lower-case, with no spaces (not strictly necessary, but not a bad idea :-/). |
||||||
|
""" |
||||||
|
return nat.lower().replace( " " , "-" ) |
||||||
|
|
||||||
|
# --------------------------------------------------------------------- |
||||||
|
|
||||||
|
def load( base_dir ) : |
||||||
|
"""Load the nationality information.""" |
||||||
|
global _base_dir , _nat_info |
||||||
|
_base_dir = os.path.abspath( base_dir ) |
||||||
|
fname = os.path.join( base_dir , "natinfo.json" ) |
||||||
|
if os.path.isfile( fname ) : |
||||||
|
with open( fname , "r" ) as fp : |
||||||
|
_nat_info = json.load( fp ) |
||||||
|
else : |
||||||
|
_nat_info = {} |
||||||
|
|
||||||
|
def dump() : |
||||||
|
"""Dump the nationality information.""" |
||||||
|
for nat in sorted(_nat_info.keys()) : |
||||||
|
print( "{}:".format( nat ) ) |
||||||
|
print( "- flag = {}".format( get_flag(nat) ) ) |
||||||
|
|
||||||
|
def get_flag( nat ) : |
||||||
|
"""Locate the flag image file for a nationality. |
||||||
|
|
||||||
|
These are set in the JSON data file at $/{nat}/flag. If there is no entry, the default is $/flags/${nat}.png |
||||||
|
""" |
||||||
|
key = _get_key( nat ) |
||||||
|
try : |
||||||
|
fname = _nat_info[ key ][ "flag" ] |
||||||
|
except ( KeyError , TypeError ) : |
||||||
|
fname = key + ".png" |
||||||
|
fname = os.path.join( _base_dir , os.path.join("flags", fname) ) |
||||||
|
return fname if os.path.isfile(fname) else None |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 9.5 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 9.8 KiB |
After Width: | Height: | Size: 7.1 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 6.6 KiB |
After Width: | Height: | Size: 4.8 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 9.7 KiB |
@ -0,0 +1,81 @@ |
|||||||
|
{ |
||||||
|
|
||||||
|
"allied-minor": { |
||||||
|
} , |
||||||
|
|
||||||
|
"american": { |
||||||
|
} , |
||||||
|
|
||||||
|
"axis-minor": { |
||||||
|
} , |
||||||
|
|
||||||
|
"belgian": { |
||||||
|
"flag": "allied-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"british": { |
||||||
|
} , |
||||||
|
|
||||||
|
"bulgarian": { |
||||||
|
"flag": "axis-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"chinese": { |
||||||
|
} , |
||||||
|
|
||||||
|
"croatian": { |
||||||
|
"flag": "axis-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"danish": { |
||||||
|
"flag": "axis-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"dutch": { |
||||||
|
"flag": "allied-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"finnish": { |
||||||
|
"flag": "axis-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"french": { |
||||||
|
} , |
||||||
|
|
||||||
|
"german": { |
||||||
|
} , |
||||||
|
|
||||||
|
"greek": { |
||||||
|
"flag": "allied-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"hungarian": { |
||||||
|
"flag": "axis-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"italian": { |
||||||
|
} , |
||||||
|
|
||||||
|
"japanese": { |
||||||
|
} , |
||||||
|
|
||||||
|
"polish": { |
||||||
|
"flag": "allied-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"romanian": { |
||||||
|
"flag": "axis-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"russian": { |
||||||
|
} , |
||||||
|
|
||||||
|
"slovakian": { |
||||||
|
"flag": "axis-minor.png" |
||||||
|
} , |
||||||
|
|
||||||
|
"yugoslav": { |
||||||
|
"flag": "allied-minor.png" |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,20 @@ |
|||||||
|
{ |
||||||
|
"german": { |
||||||
|
"_comment_": "this nationality has no flag" |
||||||
|
} , |
||||||
|
|
||||||
|
"russian": { |
||||||
|
"flag": "_missing-file_" |
||||||
|
} , |
||||||
|
|
||||||
|
"american" :{ |
||||||
|
"_comment_": "this nationality has a default flag" |
||||||
|
} , |
||||||
|
|
||||||
|
"japanese" :{ |
||||||
|
"flag": "japanese-flag.gif" |
||||||
|
} , |
||||||
|
|
||||||
|
"allied-minor": { |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
#!/usr/bin/env python3 |
||||||
|
|
||||||
|
import sys |
||||||
|
import os |
||||||
|
import unittest |
||||||
|
|
||||||
|
from _test_case_base import TestCaseBase |
||||||
|
from asl_cards import natinfo |
||||||
|
|
||||||
|
# --------------------------------------------------------------------- |
||||||
|
|
||||||
|
class TestNatInfo( TestCaseBase ) : |
||||||
|
"""Test nationality info.""" |
||||||
|
|
||||||
|
@classmethod |
||||||
|
def setUpClass( cls ) : |
||||||
|
# load the nationality info |
||||||
|
base_dir = os.path.split( __file__ )[ 0 ] |
||||||
|
fname = os.path.join( base_dir , "natinfo-data" ) |
||||||
|
natinfo.load( fname ) |
||||||
|
|
||||||
|
def test_flags( self ) : |
||||||
|
"""Test locating the flag image files for each nationality.""" |
||||||
|
self.assertIsNone( natinfo.get_flag( "xxx" ) ) |
||||||
|
self.assertIsNone( natinfo.get_flag( "german" ) ) |
||||||
|
self.assertTrue( natinfo.get_flag("american").endswith( "/flags/american.png" ) ) |
||||||
|
self.assertTrue( natinfo.get_flag("japanese").endswith( "/flags/japanese-flag.gif" ) ) |
||||||
|
|
||||||
|
# --------------------------------------------------------------------- |
||||||
|
|
||||||
|
if __name__ == "__main__" : |
||||||
|
unittest.main() |