[PATCH] templatefilters: add new dirname filter

Martin Geisler mg at lazybytes.net
Fri Apr 24 10:26:02 CDT 2009


Aleix Conchillo Flaqué <aleix at member.fsf.org> writes:

> Martin Geisler wrote:
>> Aleix Conchillo Flaque <aleix at member.fsf.org> writes:
>>
>>> # HG changeset patch
>>> # User Aleix Conchillo Flaque <aleix at member.fsf.org>
>>> # Date 1240561285 -7200
>>> # Node ID 58e9ce722255c65d24a039487b40f1d1ffd61149
>>> # Parent  441dc7becd4305f3884aab2fcf6f8e51cb547aac
>>> templatefilters: add new dirname filter
>>>
>>> diff -r 441dc7becd43 -r 58e9ce722255 mercurial/templatefilters.py
>>> --- a/mercurial/templatefilters.py	Fri Apr 24 00:06:01 2009 -0700
>>> +++ b/mercurial/templatefilters.py	Fri Apr 24 10:21:25 2009 +0200
>>> @@ -157,9 +157,19 @@
>>>      else:
>>>          raise TypeError('cannot encode type %s' % obj.__class__.__name__)
>>>  +def dirname(text):
>>> +    '''Treat the text as path, and return dirname. If no base path is found,
>>> +       return basename. Useful for recursive repos.'''
>>> +    dir = os.path.dirname(text)
>>> +    if dir == "":
>>> +        return os.path.basename(text)
>>> +    else:
>>> +        return dir
>>
>> I think it is a bit misleading to call the filter 'dirname' when it
>> mixes both dirname and basename. Could you add both a dirname and a
>> basename filter and combine them?
>
> Yes, that's true, the name might not be the best one.
>
> What I need is to substract repo from "repo/subrepo1" or from "repo"
> if no subrepos are found (both come from #repo# variable).
> os.path.dirname returns "" for "repo", so I need to use
> os.path.basename.

Right. For os.path.dirname to be used the #repo# variable should be
terminated with a slash:

>>> os.path.dirname("repo/")
'repo'
>>> os.path.dirname(os.path.dirname("repo/subrepo1/"))
'repo'

> This dirname filter is also useful for "repo/subrepo1/subsubrepo/..."
> as you can combine multiple dirname filter to get "repo"
> (#repo|dirname|dirname#).
>
> May be only a name change is neeed?

Maybe calls it stripdir -- it strips a directory level from the string,
if possible.

-- 
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20090424/02bf3192/attachment.pgp 


More information about the Mercurial-devel mailing list