south.migration.utils: 55 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/south/migration/utils.py

Stats: 0 executed, 50 missed, 5 excluded, 28 ignored

  1. import sys
  2. from collections import deque
  3. from django.utils.datastructures import SortedDict
  4. from django.db import models
  5. from south import exceptions
  6. class SortedSet(SortedDict):
  7. def __init__(self, data=tuple()):
  8. self.extend(data)
  9. def __str__(self):
  10. return "SortedSet(%s)" % list(self)
  11. def add(self, value):
  12. self[value] = True
  13. def remove(self, value):
  14. del self[value]
  15. def extend(self, iterable):
  16. [self.add(k) for k in iterable]
  17. def get_app_label(app):
  18. """
  19. Returns the _internal_ app label for the given app module.
  20. i.e. for <module django.contrib.auth.models> will return 'auth'
  21. """
  22. return app.__name__.split('.')[-2]
  23. def app_label_to_app_module(app_label):
  24. """
  25. Given the app label, returns the module of the app itself (unlike models.get_app,
  26. which returns the models module)
  27. """
  28. # Get the models module
  29. app = models.get_app(app_label)
  30. module_name = ".".join(app.__name__.split(".")[:-1])
  31. try:
  32. module = sys.modules[module_name]
  33. except KeyError:
  34. __import__(module_name, {}, {}, [''])
  35. module = sys.modules[module_name]
  36. return module
  37. def flatten(*stack):
  38. stack = deque(stack)
  39. while stack:
  40. try:
  41. x = stack[0].next()
  42. except AttributeError:
  43. stack[0] = iter(stack[0])
  44. x = stack[0].next()
  45. except StopIteration:
  46. stack.popleft()
  47. continue
  48. if hasattr(x, '__iter__'):
  49. stack.appendleft(x)
  50. else:
  51. yield x
  52. def _dfs(start, get_children, path):
  53. if start in path:
  54. raise exceptions.CircularDependency(path[path.index(start):] + [start])
  55. path.append(start)
  56. yield start
  57. children = sorted(get_children(start), key=lambda x: str(x))
  58. if children:
  59. # We need to apply all the migrations this one depends on
  60. yield (_dfs(n, get_children, path) for n in children)
  61. path.pop()
  62. def dfs(start, get_children):
  63. return flatten(_dfs(start, get_children, []))
  64. def depends(start, get_children):
  65. result = SortedSet(reversed(list(dfs(start, get_children))))
  66. return list(result)