1 # -*- coding: utf-8 -*-
6 Utility functions for Sphinx.
8 :copyright: 2007 by Georg Brandl.
9 :license: Python license.
18 def relative_uri(base
, to
):
19 """Return a relative URL from ``base`` to ``to``."""
22 # remove common segments
23 for x
, y
in zip(b2
, t2
):
28 return '../' * (len(b2
)-1) + '/'.join(t2
)
32 """Ensure that a path exists."""
36 if not err
.errno
== 17:
40 def status_iterator(iterable
, colorfunc
=lambda x
: x
, stream
=sys
.stdout
):
41 """Print out each item before yielding it."""
43 print >>stream
, colorfunc(item
),
49 def get_matching_files(dirname
, pattern
, exclude
=()):
50 """Get all files matching a pattern in a directory, recursively."""
51 # dirname is a normalized absolute path.
52 dirname
= path
.normpath(path
.abspath(dirname
))
53 dirlen
= len(dirname
) + 1 # exclude slash
54 for root
, dirs
, files
in os
.walk(dirname
):
58 if not fnmatch
.fnmatch(sfile
, pattern
):
60 qualified_name
= path
.join(root
[dirlen
:], sfile
)
61 if qualified_name
in exclude
:
66 def get_category(filename
):
67 """Get the "category" part of a RST filename."""
68 parts
= filename
.split('/', 1)
74 def shorten_result(text
='', keywords
=[], maxlen
=240, fuzz
=60):
77 text_low
= text
.lower()
80 i
= text_low
.find(k
.lower())
81 if (i
> -1 and i
< beg
) or beg
== -1:
85 for sep
in ('.', ':', ';', '='):
86 eb
= text
.find(sep
, beg
- fuzz
, beg
- 1)
95 msg
= text
[excerpt_beg
:beg
+maxlen
]
98 if beg
< len(text
)-maxlen
:
103 class attrdict(dict):
104 def __getattr__(self
, key
):
106 def __setattr__(self
, key
, val
):
108 def __delattr__(self
, key
):