comparison rhodecode/lib/dbmigrate/migrate/versioning/template.py @ 833:9753e0907827 beta

added dbmigrate package, added model changes moved out upgrade db command to that package
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 11 Dec 2010 01:54:12 +0100
parents
children 08d2dcd71666
comparison
equal deleted inserted replaced
832:634596f81cfd 833:9753e0907827
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import os
5 import shutil
6 import sys
7
8 from pkg_resources import resource_filename
9
10 from migrate.versioning.config import *
11 from migrate.versioning import pathed
12
13
14 class Collection(pathed.Pathed):
15 """A collection of templates of a specific type"""
16 _mask = None
17
18 def get_path(self, file):
19 return os.path.join(self.path, str(file))
20
21
22 class RepositoryCollection(Collection):
23 _mask = '%s'
24
25 class ScriptCollection(Collection):
26 _mask = '%s.py_tmpl'
27
28 class ManageCollection(Collection):
29 _mask = '%s.py_tmpl'
30
31 class SQLScriptCollection(Collection):
32 _mask = '%s.py_tmpl'
33
34 class Template(pathed.Pathed):
35 """Finds the paths/packages of various Migrate templates.
36
37 :param path: Templates are loaded from migrate package
38 if `path` is not provided.
39 """
40 pkg = 'migrate.versioning.templates'
41 _manage = 'manage.py_tmpl'
42
43 def __new__(cls, path=None):
44 if path is None:
45 path = cls._find_path(cls.pkg)
46 return super(Template, cls).__new__(cls, path)
47
48 def __init__(self, path=None):
49 if path is None:
50 path = Template._find_path(self.pkg)
51 super(Template, self).__init__(path)
52 self.repository = RepositoryCollection(os.path.join(path, 'repository'))
53 self.script = ScriptCollection(os.path.join(path, 'script'))
54 self.manage = ManageCollection(os.path.join(path, 'manage'))
55 self.sql_script = SQLScriptCollection(os.path.join(path, 'sql_script'))
56
57 @classmethod
58 def _find_path(cls, pkg):
59 """Returns absolute path to dotted python package."""
60 tmp_pkg = pkg.rsplit('.', 1)
61
62 if len(tmp_pkg) != 1:
63 return resource_filename(tmp_pkg[0], tmp_pkg[1])
64 else:
65 return resource_filename(tmp_pkg[0], '')
66
67 def _get_item(self, collection, theme=None):
68 """Locates and returns collection.
69
70 :param collection: name of collection to locate
71 :param type_: type of subfolder in collection (defaults to "_default")
72 :returns: (package, source)
73 :rtype: str, str
74 """
75 item = getattr(self, collection)
76 theme_mask = getattr(item, '_mask')
77 theme = theme_mask % (theme or 'default')
78 return item.get_path(theme)
79
80 def get_repository(self, *a, **kw):
81 """Calls self._get_item('repository', *a, **kw)"""
82 return self._get_item('repository', *a, **kw)
83
84 def get_script(self, *a, **kw):
85 """Calls self._get_item('script', *a, **kw)"""
86 return self._get_item('script', *a, **kw)
87
88 def get_sql_script(self, *a, **kw):
89 """Calls self._get_item('sql_script', *a, **kw)"""
90 return self._get_item('sql_script', *a, **kw)
91
92 def get_manage(self, *a, **kw):
93 """Calls self._get_item('manage', *a, **kw)"""
94 return self._get_item('manage', *a, **kw)