replace use of __dict__ with dir()/xdir()

This commit is contained in:
Sebastien Bourdeauducq 2013-11-02 16:03:47 +01:00
parent 892c12bff5
commit f658802ff8
10 changed files with 27 additions and 12 deletions

View file

@ -1,5 +1,4 @@
from operator import itemgetter from migen.util.misc import xdir
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.bus import csr from migen.bus import csr
from migen.bank.description import * from migen.bank.description import *
@ -69,7 +68,7 @@ class BankArray(Module):
def scan(self, ifargs, ifkwargs): def scan(self, ifargs, ifkwargs):
self.banks = [] self.banks = []
self.srams = [] self.srams = []
for name, obj in sorted(self.source.__dict__.items(), key=itemgetter(0)): for name, obj in xdir(self.source, True):
if hasattr(obj, "get_csrs"): if hasattr(obj, "get_csrs"):
csrs = obj.get_csrs() csrs = obj.get_csrs()
else: else:

View file

@ -1,3 +1,4 @@
from migen.util.misc import xdir
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.fhdl.tracer import get_obj_var_name from migen.fhdl.tracer import get_obj_var_name
@ -107,7 +108,7 @@ class AutoCSR:
except AttributeError: except AttributeError:
prefixed = self.__prefixed = set() prefixed = self.__prefixed = set()
r = [] r = []
for k, v in self.__dict__.items(): for k, v in xdir(self, True):
if k not in exclude: if k not in exclude:
if isinstance(v, Memory): if isinstance(v, Memory):
r.append(v) r.append(v)
@ -127,7 +128,7 @@ class AutoCSR:
except AttributeError: except AttributeError:
prefixed = self.__prefixed = set() prefixed = self.__prefixed = set()
r = [] r = []
for k, v in self.__dict__.items(): for k, v in xdir(self, True):
if k not in exclude: if k not in exclude:
if isinstance(v, _CSRBase): if isinstance(v, _CSRBase):
r.append(v) r.append(v)

View file

@ -1,3 +1,4 @@
from migen.util.misc import xdir
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.bank.description import * from migen.bank.description import *
from migen.genlib.misc import optree from migen.genlib.misc import optree
@ -46,7 +47,7 @@ class EventManager(Module, AutoCSR):
self.irq = Signal() self.irq = Signal()
def do_finalize(self): def do_finalize(self):
sources_u = [v for v in self.__dict__.values() if isinstance(v, _EventSource)] sources_u = [v for k, v in xdir(self, True) if isinstance(v, _EventSource)]
sources = sorted(sources_u, key=lambda x: x.huid) sources = sorted(sources_u, key=lambda x: x.huid)
n = len(sources) n = len(sources)
self.status = CSR(n) self.status = CSR(n)

View file

@ -20,6 +20,9 @@ class ModuleDecorator:
self.transform_fragment(f) self.transform_fragment(f)
return f return f
def __dir__(self):
return dir(self._md_decorated)
class DecorateModule: class DecorateModule:
def __init__(self, decorator, *dec_args, **dec_kwargs): def __init__(self, decorator, *dec_args, **dec_kwargs):
self.decorator = decorator self.decorator = decorator

View file

@ -12,7 +12,7 @@ class _Node:
self.children = OrderedDict() self.children = OrderedDict()
def _display_tree(filename, tree): def _display_tree(filename, tree):
from migen.graph.treeviz import RenderNode from migen.util.treeviz import RenderNode
def _to_render_node(name, node): def _to_render_node(name, node):
children = [_to_render_node(k, v) for k, v in node.children.items()] children = [_to_render_node(k, v) for k, v in node.children.items()]

View file

@ -1,3 +1,4 @@
from migen.util.misc import xdir
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.genlib.misc import optree from migen.genlib.misc import optree
from migen.genlib.record import * from migen.genlib.record import *
@ -32,7 +33,7 @@ def get_endpoints(obj, filt=_Endpoint):
if hasattr(obj, "get_endpoints") and callable(obj.get_endpoints): if hasattr(obj, "get_endpoints") and callable(obj.get_endpoints):
return obj.get_endpoints(filt) return obj.get_endpoints(filt)
r = dict() r = dict()
for k, v in obj.__dict__.items(): for k, v in xdir(obj, True):
if isinstance(v, filt): if isinstance(v, filt):
r[k] = v r[k] = v
return r return r

View file

@ -1,3 +1,4 @@
from migen.util.misc import xdir
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.flow.actor import * from migen.flow.actor import *
from migen.flow.actor import _Endpoint from migen.flow.actor import _Endpoint
@ -10,13 +11,13 @@ class UnifiedIOObject(Module):
def do_finalize(self): def do_finalize(self):
if self.get_dataflow(): if self.get_dataflow():
self.busy = Signal() self.busy = Signal()
self.specials += set(v for v in self.__dict__.values() if isinstance(v, Memory)) self.specials += set(v for k, v in xdir(self, True) if isinstance(v, Memory))
def get_dataflow(self): def get_dataflow(self):
return dict((k, v) for k, v in self.__dict__.items() if isinstance(v, _Endpoint)) return dict((k, v) for k, v in xdir(self, True) if isinstance(v, _Endpoint))
def get_buses(self): def get_buses(self):
return dict((k, v) for k, v in self.__dict__.items() if isinstance(v, (wishbone.Interface, Memory))) return dict((k, v) for k, v in xdir(self, True) if isinstance(v, (wishbone.Interface, Memory)))
(_WAIT_COMPLETE, _WAIT_POLL) = range(2) (_WAIT_COMPLETE, _WAIT_POLL) = range(2)

View file

@ -2,6 +2,7 @@ import inspect
import ast import ast
from collections import OrderedDict from collections import OrderedDict
from migen.util.misc import xdir
from migen.fhdl.structure import * from migen.fhdl.structure import *
from migen.fhdl.visit import TransformModule from migen.fhdl.visit import TransformModule
from migen.fhdl.specials import Memory from migen.fhdl.specials import Memory
@ -293,7 +294,8 @@ class Pytholite(UnifiedIOObject):
if self.get_dataflow(): if self.get_dataflow():
self.busy.reset = 1 self.busy.reset = 1
self.memory_ports = dict() self.memory_ports = dict()
for mem in self.__dict__.values(): for key in xdir(self):
mem = getattr(self, key)
if isinstance(mem, Memory): if isinstance(mem, Memory):
port = mem.get_port(write_capable=True, we_granularity=8) port = mem.get_port(write_capable=True, we_granularity=8)
self.specials += port self.specials += port

7
migen/util/misc.py Normal file
View file

@ -0,0 +1,7 @@
def xdir(obj, return_values=False):
for attr in dir(obj):
if attr[:2] != "__" and attr[-2:] != "__":
if return_values:
yield attr, getattr(obj, attr)
else:
yield attr