'Read all numbering style definitions'

def __call__(self, root, styles, rid_map):

    (XPath, get) = (self.namespace.XPath, self.namespace.get)
    self.rid_map = rid_map
    for npb in XPath(u'./w:numPicBullet[@w:numPicBulletId]')(root):
        npbid = get(npb, u'w:numPicBulletId')
        for idata in XPath(u'descendant::v:imagedata[@r:id]')(npb):
            rid = get(idata, u'r:id')
            self.pic_map[npbid] = rid
    lazy_load = {}
    for an in XPath(u'./w:abstractNum[@w:abstractNumId]')(root):
        an_id = get(an, u'w:abstractNumId')
        nsl = XPath(u'./w:numStyleLink[@w:val]')(an)
        if nsl:
            lazy_load[an_id] = get(nsl[0], u'w:val')
        else:
            nd = NumberingDefinition(self.namespace, an, an_id=an_id)
            self.definitions[an_id] = nd
    def create_instance(n, definition):
        nd = definition.copy()
        start_overrides = {}
        for lo in XPath(u'./w:lvlOverride')(n):
            try:
                ilvl = int(get(lo, u'w:ilvl'))
            except (ValueError, TypeError):
                ilvl = None
            for so in XPath(u'./w:startOverride[@w:val]')(lo):
                try:
                    start_override = int(get(so, u'w:val'))
                except (TypeError, ValueError):
                    pass
                else:
                    start_overrides[ilvl] = start_override
            for lvl in XPath(u'./w:lvl')(lo)[:1]:
                nilvl = get(lvl, u'w:ilvl')
                ilvl = (nilvl if (ilvl is None) else ilvl)
                alvl = nd.levels.get(ilvl, None)
                if (alvl is None):
                    alvl = Level(self.namespace)
                alvl.read_from_xml(lvl, override=True)
        for (ilvl, so) in start_overrides.iteritems():
            try:
                nd.levels[ilvl].start = start_override
            except KeyError:
                pass
        return nd
    next_pass = {}
    for n in XPath(u'./w:num[@w:numId]')(root):
        an_id = None
        num_id = get(n, u'w:numId')
        for an in XPath(u'./w:abstractNumId[@w:val]')(n):
            an_id = get(an, u'w:val')
        d = self.definitions.get(an_id, None)
        if (d is None):
            next_pass[num_id] = (an_id, n)
            continue
        self.instances[num_id] = create_instance(n, d)
    numbering_links = styles.numbering_style_links
    for (an_id, style_link) in lazy_load.iteritems():
        num_id = numbering_links[style_link]
        self.definitions[an_id] = self.instances[num_id].copy()
    for (num_id, (an_id, n)) in next_pass.iteritems():
        d = self.definitions.get(an_id, None)
        if (d is not None):
            self.instances[num_id] = create_instance(n, d)
    for (num_id, d) in self.instances.iteritems():
        self.starts[num_id] = {lvl: d.levels[lvl].start for lvl in d.levels}
