diff --git a/lib/ulib/support/cgilsxml.py b/lib/ulib/support/cgilsxml.py index b74b567..2b90bd6 100755 --- a/lib/ulib/support/cgilsxml.py +++ b/lib/ulib/support/cgilsxml.py @@ -179,7 +179,7 @@ class File: else: self.vars[name] = value def __repr__(self): return 'File<%s>' % (self.pf) -def find_files(basedir, filters): +def find_files(basedir, filters, spec_method='all'): basedir = path.abspath(basedir) files = [] deep_filters = [filter for filter in filters if filter.re_spec is not None and filter.deep_scan] @@ -190,11 +190,21 @@ def find_files(basedir, filters): # exception se produit try: file = File(dirpath, filename) except: continue - matched = False - allowed = False + matched1 = matched = False + allowed1 = allowed = False for filter in deep_filters: - matched = filter.match_fill(file, basedir) or matched - allowed = filter.match_allow(file) or allowed + if spec_method == 'all' or \ + (spec_method == 'first' and not matched): + matched1 = filter.match_fill(file, basedir) + else: + matched1 = False + matched = matched1 or matched + if spec_method == 'all' or \ + (spec_method == 'first' and matched and not allowed): + allowed1 = filter.match_allow(file) + else: + allowed1 = False + allowed = allowed1 or allowed if matched: pass elif allowed: file.dontlist = True else: continue @@ -207,11 +217,21 @@ def find_files(basedir, filters): try: file = File(basedir, name) except: continue if not file.isfile(): continue - matched = False - allowed = False + matched1 = matched = False + allowed1 = allowed = False for filter in simple_filters: - matched = filter.match_fill(file) or matched - allowed = filter.match_allow(file) or allowed + if spec_method == 'all' or \ + (spec_method == 'first' and not matched): + matched1 = filter.match_fill(file, basedir) + else: + matched1 = False + matched = matched1 or matched + if spec_method == 'all' or \ + (spec_method == 'first' and matched and not allowed): + allowed1 = filter.match_allow(file) + else: + allowed1 = False + allowed = allowed1 or allowed if matched: pass elif allowed: file.dontlist = True else: continue @@ -411,6 +431,7 @@ def run_cgilsxml(): charset = 'utf-8' default_content = ContentInfos() default_filter = Filter(r'(.*)') + env = dict(filters=[], filter=None) def add_spec(option, opt, value, parser, *args, **kw): if env['filter'] is not None: env['filters'].append(env['filter']) env['filter'] = Filter(spec=value) @@ -420,16 +441,16 @@ def run_cgilsxml(): def allow_spec(option, opt, value, parser, *args, **kw): if env['filter'] is None: env['filter'] = default_filter env['filter'].allow_spec(value) - def add_var(option, opt_str, value, parser, *args, **kw): + def add_var(option, opt, value, parser, *args, **kw): if env['filter'] is None: env['filter'] = default_filter env['filter'].add_var(value) - def set_group(option, opt_str, value, parser, *args, **kw): + def set_group(option, opt, value, parser, *args, **kw): if env['filter'] is None: env['filter'] = default_filter env['filter'].set_group(value) - def set_break_on(option, opt_str, value, parser, *args, **kw): + def set_break_on(option, opt, value, parser, *args, **kw): if env['filter'] is None: env['filter'] = default_filter env['filter'].set_break_on(value) - def set_content_disposition(option, opt_str, value, parser, *args, **kw): + def set_content_disposition(option, opt, value, parser, *args, **kw): if value in ('attachment', 'inline'): pass elif value == 'none': value = None else: @@ -441,15 +462,16 @@ def run_cgilsxml(): sys.exit(1) if env['filter'] is None: default_content.disposition = value else: env['filter'].force_disposition = value - def set_content_type(option, opt_str, value, parser, *args, **kw): + def set_content_type(option, opt, value, parser, *args, **kw): if env['filter'] is None: default_content.type = value else: env['filter'].force_type = value - def set_content_charset(option, opt_str, value, parser, *args, **kw): + def set_content_charset(option, opt, value, parser, *args, **kw): if env['filter'] is None: default_content.charset = value else: env['filter'].force_charset = value from optparse import OptionParser OP = OptionParser(usage=u"\n\t%prog [options] /path/to/dir", description=__doc__) + OP.set_defaults(spec_method='all') OP.add_option('-e', '--spec', dest='spec', action='callback', callback=add_spec, type='string', help=u"Spécifier l'expression régulière permettant de sélectionner les fichiers à lister. " + u"La correspondance est tentée sur le nom du fichier avec la fonction match(). " @@ -460,6 +482,10 @@ def run_cgilsxml(): + u"\n Il est possible de spécifier cette option plusieurs fois." + u"\n Note: TOUTES les expressions régulières sont testées par rapport au nom du fichier, et pour celles qui correspondent, les variables correspondantes sont définies. " + u"Il faut donc ordonner les expressions régulières de la plus générale à la plus spécifique, contrairement à ce qui se fait d'habitude.") + OP.add_option('--all-specs', dest='spec_method', action='store_const', const='all', + help=u"Avec l'option -e, tester toutes les expressions régulières dans l'ordre de définition. C'est le comportement par défaut.") + OP.add_option('--first-spec', dest='spec_method', action='store_const', const='first', + help=u"Avec l'option -e, arrêter à la première expression régulière qui correspond") OP.add_option('--glob', dest='pattern', action='callback', callback=add_glob, type='string', help=u"Comme --spec, mais en utilisant les patterns du shell *, ?, [seq], [!seq]. " + u"Si l'expression contient un caractère '/', la correspondance se fait sur le chemin complet du fichier avec le module glob. " @@ -533,7 +559,6 @@ def run_cgilsxml(): + u"Il est possible de spécifier plusieurs paramètres en les séparant par des virgules.") OP.add_option('-p', '--prefix', dest="prefix", help=u"Ajouter les paramètres supplémentaires spécifiés à /result/env/query_string.") - env = dict(filters=[], filter=None) o, args = OP.parse_args() filters = env['filters'] filter = env['filter'] @@ -577,7 +602,7 @@ def run_cgilsxml(): files = [] for basedir in args: - files.extend(find_files(basedir, filters)) + files.extend(find_files(basedir, filters, o.spec_method)) sortfunc = build_sortfunc(sortby) files = sort_files(files, sortfunc) fgroups = build_fgroups(files)