summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2014-12-30 12:05:06 -0500
committerDoug Hellmann <doug@doughellmann.com>2014-12-30 12:05:06 -0500
commit80d5fc4ec2711dde9bd7cf775e9376190c46f9f7 (patch)
treea01daa63292166b8c1c2e07b99c64f1b92cec94c
parentf8b9b8cda0e5e395d5a8d52ba015e050c3a90dbf (diff)
Support project filter with wildcard
Translate glob-style wildcards to ILIKE comparisons for the database.
-rw-r--r--aeromancer/project_filter.py28
1 files changed, 22 insertions, 6 deletions
diff --git a/aeromancer/project_filter.py b/aeromancer/project_filter.py
index f05122c..031b4e2 100644
--- a/aeromancer/project_filter.py
+++ b/aeromancer/project_filter.py
@@ -1,8 +1,10 @@
1import argparse 1import argparse
2 2import logging
3 3
4from aeromancer.db.models import Project 4from aeromancer.db.models import Project
5 5
6LOG = logging.getLogger(__name__)
7
6 8
7class ProjectFilter(object): 9class ProjectFilter(object):
8 """Manage the arguments for filtering queries by project. 10 """Manage the arguments for filtering queries by project.
@@ -18,7 +20,8 @@ class ProjectFilter(object):
18 action='append', 20 action='append',
19 default=[], 21 default=[],
20 dest='projects', 22 dest='projects',
21 help='projects to limit search, by exact name', 23 help=('projects to limit search, '
24 'by exact name or glob-style patterns'),
22 ) 25 )
23 26
24 @classmethod 27 @classmethod
@@ -26,11 +29,24 @@ class ProjectFilter(object):
26 return cls(projects=parsed_args.projects) 29 return cls(projects=parsed_args.projects)
27 30
28 def __init__(self, projects): 31 def __init__(self, projects):
32 self.exact = []
33 self.patterns = []
34 for p in projects:
35 if '*' in p:
36 self.patterns.append(p.replace('*', '%'))
37 else:
38 self.exact.append(p)
29 self.projects = projects 39 self.projects = projects
30 40
31 def update_query(self, query): 41 def update_query(self, query):
32 if self.projects: 42 the_filter = ()
33 query = query.filter( 43 if self.exact:
34 Project.name.in_(self.projects) 44 LOG.debug('filtering on projects in %s', self.exact)
35 ) 45 the_filter += (Project.name.in_(self.exact),)
46 if self.patterns:
47 LOG.debug('filtering on projects matching %s', self.patterns)
48 the_filter += tuple(Project.name.ilike(p)
49 for p in self.patterns)
50 if the_filter:
51 query = query.filter(*the_filter)
36 return query 52 return query