mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2026-01-05 11:30:06 +01:00
Add a generator that updates a persistent mapping of item (functions, classes, etc.) to the module names they are a member of. Remove the NO_MODULE dictionary as we're tracking all the item --> module names now. Consolidate the 2 removeWxPrefix implementations to just one. Fix taking out too much space in the class index when removing the :ref: for unknown items, which caused ReST problems. Use pyName if it is set When renaming classes we also need to change the className in the method objects Properly deal with nested classes
126 lines
3.4 KiB
Python
126 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# ---------------------------------------------------------------------------
|
|
# Name: etgtools/map_generator.py
|
|
# Author: Robin Dunn
|
|
#
|
|
# Created: 20-May-2016
|
|
# Copyright: (c) 2016 by Total Control Software
|
|
# License: wxWindows License
|
|
# ---------------------------------------------------------------------------
|
|
|
|
"""
|
|
This module provides a class that manages a persistent mapping, currently just
|
|
for mapping module item names to the name of their module, so other phases of
|
|
the code generation can find things that may not have been seen yet. This
|
|
class can easily be adapted to other purposes however, if the need arises.
|
|
"""
|
|
|
|
# Standard library imports
|
|
import os.path as op
|
|
import json
|
|
|
|
|
|
# Phoenix imports
|
|
from .generators import textfile_open
|
|
from sphinxtools.constants import SPHINXROOT
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class ItemModuleMap(object):
|
|
"""
|
|
A persistent (across builds) mapping. It manages the data in a
|
|
dictionary, and also has a few methods to make the object quack a little
|
|
like a real dictionary.
|
|
"""
|
|
|
|
# This is the Borg pattern, so all instances of this class actually share
|
|
# the same data attributes
|
|
__shared_state = dict(_haveReadData=False,
|
|
_items=dict())
|
|
|
|
def __init__(self):
|
|
self.__dict__ = self.__shared_state # Borg part 2
|
|
self.fileName = op.join(SPHINXROOT, 'itemToModuleMap.json')
|
|
|
|
|
|
@property
|
|
def items(self):
|
|
# lazy load the items on first use
|
|
if not self._haveReadData:
|
|
self.read()
|
|
return self._items
|
|
|
|
|
|
# Methods for reading/writing the data from/to persistent storage.
|
|
def read(self):
|
|
if op.isfile(self.fileName):
|
|
with textfile_open(self.fileName, 'rt') as fid:
|
|
items = json.load(fid)
|
|
# TODO: catch JSON exception...
|
|
if items is None:
|
|
items = dict()
|
|
else:
|
|
items = dict()
|
|
|
|
self._items.clear()
|
|
self._items.update(items)
|
|
self._haveReadData = True
|
|
|
|
|
|
def flush(self):
|
|
with textfile_open(self.fileName, 'wt') as fid:
|
|
# Dump the data to a file in json, using a format that minimizes
|
|
# excess whitespace.
|
|
json.dump(self._items, fid, sort_keys=True,
|
|
indent=0, separators=(',', ':'))
|
|
|
|
|
|
def reset(self):
|
|
self._haveReadData = False,
|
|
self._items.clear()
|
|
|
|
|
|
def get_module(self, name):
|
|
return self.items.get(name)
|
|
|
|
def get_fullname(self, name):
|
|
module = self.items.get(name)
|
|
if not module:
|
|
import mydbstub
|
|
return name
|
|
return module + name
|
|
|
|
|
|
|
|
# Methods for a dictionary Facade, for convenience
|
|
def get(self, key, default=None):
|
|
return self.items.get(key, default)
|
|
|
|
def clear(self):
|
|
self.items.clear()
|
|
|
|
def __len__(self):
|
|
return len(self.items)
|
|
|
|
def __getitem__(self, key):
|
|
if key in self.items:
|
|
return self.items[key]
|
|
raise KeyError(key)
|
|
|
|
def __setitem__(self, key, item):
|
|
self.items[key] = item
|
|
|
|
def __delitem__(self, key):
|
|
del self.items[key]
|
|
|
|
def __iter__(self):
|
|
return iter(self.items)
|
|
|
|
def __contains__(self, key):
|
|
return key in self.items
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|