import argparse


from aeromancer.db.models import Project


class ProjectFilter(object):
    """Manage the arguments for filtering queries by project.
    """

    @staticmethod
    def add_arguments(parser):
        """Given an argparse.ArgumentParser add arguments.
        """
        grp = parser.add_argument_group('Project Filter')
        grp.add_argument(
            '--project',
            action='append',
            default=[],
            dest='projects',
            help='projects to limit search, by exact name',
        )

    @classmethod
    def from_parsed_args(cls, parsed_args):
        return cls(projects=parsed_args.projects)

    def __init__(self, projects):
        self.projects = projects

    def update_query(self, query):
        if self.projects:
            query = query.filter(
                Project.name.in_(self.projects)
            )
        return query

@@ -9,16 +9,30 @@
 rgparse%0A
+import logging
 %0A%0Afrom a
@@ -67,16 +67,51 @@
 roject%0A%0A
+LOG = logging.getLogger(__name__)%0A%0A
 %0Aclass P
@@ -531,16 +531,17 @@
    help=
+(
 'project
@@ -563,22 +563,67 @@
 ch, 
-by exact name'
+'%0A                  'by exact name or glob-style patterns')
 ,%0A  
@@ -794,184 +794,727 @@
 elf.
-projects = projects%0A%0A    def update_query(self, query):%0A        if self.projects:%0A            query = query.filter(%0A                Project.name.in_(self.projects)%0A            
+exact = %5B%5D%0A        self.patterns = %5B%5D%0A        for p in projects:%0A            if '*' in p:%0A                self.patterns.append(p.replace('*', '%25'))%0A            else:%0A                self.exact.append(p)%0A        self.projects = projects%0A%0A    def update_query(self, query):%0A        the_filter = ()%0A        if self.exact:%0A            LOG.debug('filtering on projects in %25s', self.exact)%0A            the_filter += (Project.name.in_(self.exact),)%0A        if self.patterns:%0A            LOG.debug('filtering on projects matching %25s', self.patterns)%0A            the_filter += tuple(Project.name.ilike(p)%0A                                for p in self.patterns)%0A        if the_filter:%0A            query = query.filter(*the_filter
 )%0A  

Support project filter with wildcard