mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2025-12-16 01:30:07 +01:00
refactor reading and writing data to pickle files
This commit is contained in:
@@ -12,7 +12,7 @@ else:
|
||||
|
||||
from inspect import getmro, getclasstree, getdoc, getcomments
|
||||
|
||||
from .utilities import MakeSummary, ChopDescription, WriteSphinxOutput
|
||||
from .utilities import MakeSummary, ChopDescription, WriteSphinxOutput, PickleFile
|
||||
from .utilities import FindControlImages, FormatExternalLink, IsPython3
|
||||
from .constants import object_types, MODULE_TO_ICON, DOXY_2_REST, SPHINXROOT
|
||||
from . import templates
|
||||
@@ -521,25 +521,14 @@ class Library(ParentBase):
|
||||
|
||||
|
||||
def Save(self):
|
||||
|
||||
ParentBase.Save(self)
|
||||
|
||||
class_dict = {}
|
||||
class_dict = self.ClassesToPickle(self, class_dict)
|
||||
|
||||
pickle_file = os.path.join(SPHINXROOT, 'class_summary.lst')
|
||||
|
||||
if os.path.isfile(pickle_file):
|
||||
fid = open(pickle_file, 'rb')
|
||||
items = pickle.load(fid)
|
||||
fid.close()
|
||||
else:
|
||||
items = {}
|
||||
|
||||
items.update(class_dict)
|
||||
fid = open(pickle_file, 'wb')
|
||||
pickle.dump(items, fid)
|
||||
fid.close()
|
||||
with PickleFile(pickle_file) as pf:
|
||||
pf.items.update(class_dict)
|
||||
|
||||
|
||||
class Module(ParentBase):
|
||||
|
||||
@@ -29,7 +29,7 @@ from .librarydescription import Method, Property, Attribute
|
||||
|
||||
from . import inheritance
|
||||
|
||||
from .utilities import IsPython3
|
||||
from .utilities import IsPython3, PickleFile
|
||||
from .constants import object_types, EXCLUDED_ATTRS, MODULE_TO_ICON
|
||||
from .constants import CONSTANT_RE
|
||||
|
||||
@@ -582,13 +582,11 @@ def ToRest(import_name):
|
||||
sphinxDir = os.path.join(phoenixDir(), 'docs', 'sphinx')
|
||||
pickle_file = os.path.join(sphinxDir, 'wx%s.pkl'%import_name)
|
||||
|
||||
fid = open(pickle_file, 'rb')
|
||||
library_class = pickle.load(fid)
|
||||
fid.close()
|
||||
pf = PickleFile(pickle_file)
|
||||
library_class = pf.read()
|
||||
|
||||
fid = open(os.path.join(sphinxDir, 'class_summary.lst'), 'rb')
|
||||
class_summary = pickle.load(fid)
|
||||
fid.close()
|
||||
pf = PickleFile(os.path.join(sphinxDir, 'class_summary.lst'))
|
||||
class_summary = pf.read()
|
||||
|
||||
library_class.Walk(library_class, class_summary)
|
||||
|
||||
@@ -602,8 +600,6 @@ def ModuleHunter(init_name, import_name, version):
|
||||
ToRest(import_name)
|
||||
return
|
||||
|
||||
path = list(sys.path)
|
||||
|
||||
directory, module_name = os.path.split(init_name)
|
||||
path = list(sys.path)
|
||||
|
||||
@@ -660,9 +656,8 @@ def ModuleHunter(init_name, import_name, version):
|
||||
|
||||
sys.path[:] = path # restore
|
||||
|
||||
fid = open(pickle_file, 'wb')
|
||||
pickle.dump(library_class, fid)
|
||||
fid.close()
|
||||
pf = PickleFile(pickle_file)
|
||||
pf.write(library_class)
|
||||
|
||||
ToRest(import_name)
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ else:
|
||||
from buildtools.config import copyIfNewer, writeIfChanged, newer, getVcsRev, textfile_open
|
||||
|
||||
from . import templates
|
||||
from .utilities import Wx2Sphinx
|
||||
from .utilities import Wx2Sphinx, PickleFile
|
||||
from .constants import HTML_REPLACE, TODAY, SPHINXROOT, SECTIONS_EXCLUDE
|
||||
from .constants import CONSTANT_INSTANCES, WIDGETS_IMAGES_ROOT, SPHINX_IMAGES_ROOT
|
||||
|
||||
@@ -100,9 +100,8 @@ def BuildEnumsAndMethods(sphinxDir):
|
||||
4. Some cleanup.
|
||||
"""
|
||||
|
||||
fid = open(os.path.join(sphinxDir, 'class_summary.lst'), 'rb')
|
||||
class_summary = pickle.load(fid)
|
||||
fid.close()
|
||||
pf = PickleFile(os.path.join(sphinxDir, 'class_summary.lst'))
|
||||
class_summary = pf.read()
|
||||
|
||||
unreferenced_classes = {}
|
||||
|
||||
@@ -365,10 +364,9 @@ def ReformatFunctions(file):
|
||||
if not newer(file, text_file):
|
||||
return
|
||||
|
||||
fid = open(file, 'rb')
|
||||
functions = pickle.load(fid)
|
||||
fid.close()
|
||||
|
||||
pf = PickleFile(file)
|
||||
functions = pf.read()
|
||||
|
||||
if local_file.count('.') == 1:
|
||||
# Core functions
|
||||
label = 'Core'
|
||||
@@ -420,11 +418,8 @@ def MakeClassIndex(sphinxDir, file):
|
||||
if not newer(file, text_file):
|
||||
return
|
||||
|
||||
fid = open(file, 'rb')
|
||||
classes = pickle.load(fid)
|
||||
fid.close()
|
||||
|
||||
if local_file.count('.') == 1:
|
||||
pf = PickleFile(file)
|
||||
classes = pf.read()
|
||||
# Core functions
|
||||
label = 'Core'
|
||||
module = ''
|
||||
|
||||
@@ -578,12 +578,10 @@ def WriteSphinxOutput(stream, filename, append=False):
|
||||
text = stream.getvalue()
|
||||
|
||||
mode = 'a' if append else 'w'
|
||||
fid = codecs.open(text_file, mode, encoding='utf-8')
|
||||
if mode == 'w':
|
||||
fid.write('.. include:: headings.inc\n\n')
|
||||
|
||||
fid.write(text)
|
||||
fid.close()
|
||||
with codecs.open(text_file, mode, encoding='utf-8') as fid:
|
||||
if mode == 'w':
|
||||
fid.write('.. include:: headings.inc\n\n')
|
||||
fid.write(text)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------- #
|
||||
@@ -614,6 +612,35 @@ def ChopDescription(text):
|
||||
|
||||
# ----------------------------------------------------------------------- #
|
||||
|
||||
class PickleFile(object):
|
||||
"""
|
||||
A class to help simplify loading and saving data to pickle files.
|
||||
"""
|
||||
def __init__(self, fileName):
|
||||
self.fileName = fileName
|
||||
|
||||
def __enter__(self):
|
||||
self.read()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.write(self.items)
|
||||
|
||||
def read(self):
|
||||
if os.path.isfile(self.fileName):
|
||||
with open(self.fileName, 'rb') as fid:
|
||||
items = pickle.load(fid)
|
||||
else:
|
||||
items = {}
|
||||
self.items = items
|
||||
return items
|
||||
|
||||
def write(self, items):
|
||||
with open(self.fileName, 'wb') as fid:
|
||||
pickle.dump(items, fid)
|
||||
|
||||
# ----------------------------------------------------------------------- #
|
||||
|
||||
def PickleItem(description, current_module, name, kind):
|
||||
"""
|
||||
This function pickles/unpickles a dictionary containing class names as keys
|
||||
@@ -635,18 +662,9 @@ def PickleItem(description, current_module, name, kind):
|
||||
pickle_file = os.path.join(SPHINXROOT, current_module + 'functions.pkl')
|
||||
else:
|
||||
pickle_file = os.path.join(SPHINXROOT, current_module + '1classindex.pkl')
|
||||
|
||||
if os.path.isfile(pickle_file):
|
||||
fid = open(pickle_file, 'rb')
|
||||
items = pickle.load(fid)
|
||||
fid.close()
|
||||
else:
|
||||
items = {}
|
||||
|
||||
items[name] = description
|
||||
fid = open(pickle_file, 'wb')
|
||||
pickle.dump(items, fid)
|
||||
fid.close()
|
||||
with PickleFile(pickle_file) as pf:
|
||||
pf.items[name] = description
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------- #
|
||||
@@ -661,15 +679,6 @@ def PickleClassInfo(class_name, element, short_description):
|
||||
:param string `short_description`: the class short description (if any).
|
||||
"""
|
||||
|
||||
pickle_file = os.path.join(SPHINXROOT, 'class_summary.lst')
|
||||
|
||||
if os.path.isfile(pickle_file):
|
||||
fid = open(pickle_file, 'rb')
|
||||
items = pickle.load(fid)
|
||||
fid.close()
|
||||
else:
|
||||
items = {}
|
||||
|
||||
method_list, bases = [], []
|
||||
for method, description in element.method_list:
|
||||
method_list.append(method)
|
||||
@@ -677,10 +686,9 @@ def PickleClassInfo(class_name, element, short_description):
|
||||
for base in element.bases:
|
||||
bases.append(Wx2Sphinx(base)[1])
|
||||
|
||||
items[class_name] = (method_list, bases, short_description)
|
||||
fid = open(pickle_file, 'wb')
|
||||
pickle.dump(items, fid)
|
||||
fid.close()
|
||||
pickle_file = os.path.join(SPHINXROOT, 'class_summary.lst')
|
||||
with PickleFile(pickle_file) as pf:
|
||||
pf.items[class_name] = (method_list, bases, short_description)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------- #
|
||||
@@ -786,9 +794,8 @@ def FormatContributedSnippets(kind, contrib_snippets):
|
||||
text = '\n' + spacer + '|contributed| **Contributed Examples:**\n\n'
|
||||
|
||||
for indx, snippet in enumerate(contrib_snippets):
|
||||
fid = open(snippet, 'rt')
|
||||
lines = fid.readlines()
|
||||
fid.close()
|
||||
with open(snippet, 'rt') as fid:
|
||||
lines = fid.readlines()
|
||||
user = lines[0].replace('##', '').strip()
|
||||
onlyfile = os.path.split(snippet)[1]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user