23
23
unicode_literals )
24
24
25
25
from matplotlib .externals import six
26
- from matplotlib .externals .six .moves import cPickle as pickle
27
26
28
27
"""
29
28
KNOWN ISSUES
47
46
see license/LICENSE_TTFQUERY.
48
47
"""
49
48
49
+ import json
50
50
import os , sys , warnings
51
51
try :
52
52
set
@@ -947,23 +947,43 @@ def ttfdict_to_fnames(d):
947
947
fnames .append (fname )
948
948
return fnames
949
949
950
- def pickle_dump (data , filename ):
951
- """
952
- Equivalent to pickle.dump(data, open(filename, 'w'))
953
- but closes the file to prevent filehandle leakage.
954
- """
955
- with open (filename , 'wb' ) as fh :
956
- pickle .dump (data , fh )
950
+ class JSONEncoder (json .JSONEncoder ):
951
+ def default (self , o ):
952
+ if isinstance (o , FontManager ):
953
+ return dict (o .__dict__ , _class = 'FontManager' )
954
+ elif isinstance (o , FontEntry ):
955
+ return dict (o .__dict__ , _class = 'FontEntry' )
956
+ else :
957
+ return super (JSONEncoder , self ).default (o )
958
+
959
+ def _json_decode (o ):
960
+ cls = o .pop ('_class' , None )
961
+ if cls is None :
962
+ return o
963
+ elif cls == 'FontManager' :
964
+ r = FontManager .__new__ (FontManager )
965
+ r .__dict__ .update (o )
966
+ return r
967
+ elif cls == 'FontEntry' :
968
+ r = FontEntry .__new__ (FontEntry )
969
+ r .__dict__ .update (o )
970
+ return r
971
+ else :
972
+ raise ValueError ("don't know how to deserialize _class=%s" % cls )
957
973
958
- def pickle_load (filename ):
959
- """
960
- Equivalent to pickle.load(open(filename, 'r'))
961
- but closes the file to prevent filehandle leakage.
962
- """
963
- with open (filename , 'rb' ) as fh :
964
- data = pickle .load (fh )
965
- return data
974
+ def json_dump (data , filename ):
975
+ """Dumps a data structure as JSON in the named file.
976
+ Handles FontManager and its fields."""
977
+
978
+ with open (filename , 'w' ) as fh :
979
+ json .dump (data , fh , cls = JSONEncoder , indent = 2 )
966
980
981
+ def json_load (filename ):
982
+ """Loads a data structure as JSON from the named file.
983
+ Handles FontManager and its fields."""
984
+
985
+ with open (filename , 'r' ) as fh :
986
+ return json .load (fh , object_hook = _json_decode )
967
987
968
988
class TempCache (object ):
969
989
"""
@@ -1388,10 +1408,7 @@ def findfont(prop, fontext='ttf'):
1388
1408
if not 'TRAVIS' in os .environ :
1389
1409
cachedir = get_cachedir ()
1390
1410
if cachedir is not None :
1391
- if six .PY3 :
1392
- _fmcache = os .path .join (cachedir , 'fontList.py3k.cache' )
1393
- else :
1394
- _fmcache = os .path .join (cachedir , 'fontList.cache' )
1411
+ _fmcache = os .path .join (cachedir , 'fontList.json' )
1395
1412
1396
1413
fontManager = None
1397
1414
@@ -1404,12 +1421,12 @@ def _rebuild():
1404
1421
global fontManager
1405
1422
fontManager = FontManager ()
1406
1423
if _fmcache :
1407
- pickle_dump (fontManager , _fmcache )
1424
+ json_dump (fontManager , _fmcache )
1408
1425
verbose .report ("generated new fontManager" )
1409
1426
1410
1427
if _fmcache :
1411
1428
try :
1412
- fontManager = pickle_load (_fmcache )
1429
+ fontManager = json_load (_fmcache )
1413
1430
if (not hasattr (fontManager , '_version' ) or
1414
1431
fontManager ._version != FontManager .__version__ ):
1415
1432
_rebuild ()
0 commit comments