[PATCH] templatefilters: add new dirname filter

Aleix Conchillo Flaqué aleix at member.fsf.org
Fri Apr 24 08:50:03 CDT 2009


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.

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?

Aleix



More information about the Mercurial-devel mailing list