OpenStack Source Explorer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project_filter.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import argparse
  2. import logging
  3. from aeromancer.db.models import Project
  4. LOG = logging.getLogger(__name__)
  5. class ProjectFilter(object):
  6. """Manage the arguments for filtering queries by project.
  7. """
  8. @staticmethod
  9. def add_arguments(parser):
  10. """Given an argparse.ArgumentParser add arguments.
  11. """
  12. grp = parser.add_argument_group('Project Filter')
  13. grp.add_argument(
  14. '--project',
  15. action='append',
  16. default=[],
  17. dest='projects',
  18. help=('projects to limit search, '
  19. 'by exact name or glob-style patterns'),
  20. )
  21. @classmethod
  22. def from_parsed_args(cls, parsed_args):
  23. return cls(projects=parsed_args.projects)
  24. def __init__(self, projects):
  25. self.exact = []
  26. self.patterns = []
  27. for p in projects:
  28. if '*' in p:
  29. self.patterns.append(p.replace('*', '%'))
  30. else:
  31. self.exact.append(p)
  32. self.projects = projects
  33. def update_query(self, query):
  34. the_filter = ()
  35. if self.exact:
  36. LOG.debug('filtering on projects in %s', self.exact)
  37. the_filter += (Project.name.in_(self.exact),)
  38. if self.patterns:
  39. LOG.debug('filtering on projects matching %s', self.patterns)
  40. the_filter += tuple(Project.name.ilike(p)
  41. for p in self.patterns)
  42. if the_filter:
  43. query = query.filter(*the_filter)
  44. return query