def _walk_schemas(self, schema_dir): """Walks the `schema_dir` directory and builds a dictionary of schema ``targetNamespace`` values to a list of schema file paths. Because multiple schemas can declare the same ``targetNamespace`` value, the ``value`` portion of the returned dictionary is a ``list``. Note: This method attempts to resolve issues where the same schema exists in two or more locations under `schema_dir` by keeping a record of visited target namespaces and filenames. If the same filename:targetNS (not file path) pair has been visited already, the file is not added to the schemalocation dictionary. Returns: A dictionary of schema ``targetNamespace`` values to a list of schema file paths. """ seen = [] schemalocs = collections.defaultdict(list) for top, _, files in os.walk(schema_dir): for fn in files: if not fn.endswith('.xsd'): continue fp = os.path.abspath(os.path.join(top, fn)) target_ns = utils.get_target_ns(fp) if (target_ns, fn) in seen: continue schemalocs[target_ns].append(fp) seen.append((target_ns, fn)) for ns, loc in self.OVERRIDE_SCHEMALOC.iteritems(): schemalocs[ns] = [loc] return schemalocs
def _walk_schemas(self, schema_dir): """Walks the `schema_dir` directory and builds a dictionary of schema ``targetNamespace`` values to a list of schema file paths. Because multiple schemas can declare the same ``targetNamespace`` value, the ``value`` portion of the returned dictionary is a ``list``. Note: This method attempts to resolve issues where the same schema exists in two or more locations under `schema_dir` by keeping a record of visited target namespaces and filenames. If the same filename:targetNS (not file path) pair has been visited already, the file is not added to the schemalocation dictionary. Returns: A dictionary of schema ``targetNamespace`` values to a list of schema file paths. """ seen = [] schemalocs = collections.defaultdict(list) for top, _, files in os.walk(schema_dir): for fn in files: if not fn.endswith('.xsd'): continue fp = os.path.abspath(os.path.join(top, fn)) target_ns = utils.get_target_ns(fp) if (target_ns, fn) in seen: continue schemalocs[target_ns].append(fp) seen.append((target_ns, fn)) for ns, loc in iteritems(self.OVERRIDE_SCHEMALOC): schemalocs[ns] = [loc] return schemalocs
def test_target_ns(self): sio = StringIO(XML) target_ns = utils.get_target_ns(sio) self.assertEqual(target_ns, TARGET_NS)