Skip to content

Commit

Permalink
made relate_dependencies handle non-ascii distilled files under py2 a…
Browse files Browse the repository at this point in the history
…gain, with modified testcase
  • Loading branch information
staffanm committed Jan 17, 2016
1 parent 7762a4a commit dac7f2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
58 changes: 29 additions & 29 deletions ferenda/documentrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,35 +1762,35 @@ def relate_dependencies(self, basefile, repos=[]):
with util.logtime(self.log.debug,
"%(basefile)s: Registered %(deps)s dependencies (%(elapsed).3f sec)",
values):
with self.store.open_distilled(basefile) as fp:
g = Graph().parse(fp, format="xml")
subjects = set([s for s, p, o in g])
for (s, p, o) in g:
# the graph for a single doc can describe
# multiple, linked, resources. Don't attempt to
# find basefiles for these resources, even if they
# occur as objects in the graphs as well.
if p == RDF.type:
continue
if o in subjects:
continue
# for each URIRef in graph
if isinstance(o, URIRef):
# in order to minimize calls to
# basefile_from_uri(), it'd be nice if the
# order of repos was dynamically altered
# according to MRU (most recently used)
for repo in repos:
# find out if any docrepo can handle it
dep_basefile = repo.basefile_from_uri(str(o))
if dep_basefile and (
(repo != self) or
(dep_basefile != basefile)):
# if so, add to that repo's dependencyfile
pp = self.store.parsed_path(basefile)
res = repo.add_dependency(dep_basefile, pp)
values['deps'] += 1
break
distilled = util.readfile(self.store.distilled_path(basefile), encoding="utf-8")
g = Graph().parse(data=distilled, format="xml")
subjects = set([s for s, p, o in g])
for (s, p, o) in g:
# the graph for a single doc can describe
# multiple, linked, resources. Don't attempt to
# find basefiles for these resources, even if they
# occur as objects in the graphs as well.
if p == RDF.type:
continue
if o in subjects:
continue
# for each URIRef in graph
if isinstance(o, URIRef):
# in order to minimize calls to
# basefile_from_uri(), it'd be nice if the
# order of repos was dynamically altered
# according to MRU (most recently used)
for repo in repos:
# find out if any docrepo can handle it
dep_basefile = repo.basefile_from_uri(str(o))
if dep_basefile and (
(repo != self) or
(dep_basefile != basefile)):
# if so, add to that repo's dependencyfile
pp = self.store.parsed_path(basefile)
res = repo.add_dependency(dep_basefile, pp)
values['deps'] += 1
break
return values['deps']

def add_dependency(self, basefile, dependencyfile):
Expand Down
7 changes: 4 additions & 3 deletions test/testDocRepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,9 @@ def test_relate_all_teardown(self, mock_store):
<dcterms:updates rdf:resource="http://localhost:8000/res/base/res-a"/>
<dcterms:references rdf:resource="http://localhost:8000/res/other/res-b"/>
<rdf:seeAlso rdf:resource="http://localhost:8000/somewhere/else"/>
<dcterms:title>Sacré bleu!</dcterms:title>
</bibo:Document>
</rdf:RDF>"""
</rdf:RDF>"""

def test_relate(self):
# the helper methods are called separately. this test only
Expand Down Expand Up @@ -828,8 +829,8 @@ class OtherRepo(DocumentRepository):
self.assertEqual(util.readfile(otherrepo.store.dependencies_path("res-b")),
dependencyfile)
# 4.3 no other deps files exists in datadir
self.assertEqual(2,
len(list(util.list_dirs(self.datadir, '.txt'))))
self.assertEqual(2,
len(list(util.list_dirs(self.datadir, '.txt'))))

# 5. Finally, create a basefile with a complicated name
# (KFD-normalized latin-1 name,which yields characters outside
Expand Down

0 comments on commit dac7f2c

Please sign in to comment.