1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | import os |
---|
5 | import shutil |
---|
6 | import sys |
---|
7 | from pkg_resources import resource_filename |
---|
8 | |
---|
9 | from migrate.versioning.base import * |
---|
10 | from migrate.versioning import pathed |
---|
11 | |
---|
12 | |
---|
13 | class Packaged(pathed.Pathed): |
---|
14 | """An object assoc'ed with a Python package""" |
---|
15 | |
---|
16 | def __init__(self, pkg): |
---|
17 | self.pkg = pkg |
---|
18 | path = self._find_path(pkg) |
---|
19 | super(Packaged, self).__init__(path) |
---|
20 | |
---|
21 | @classmethod |
---|
22 | def _find_path(cls, pkg): |
---|
23 | pkg_name, resource_name = pkg.rsplit('.', 1) |
---|
24 | ret = resource_filename(pkg_name, resource_name) |
---|
25 | return ret |
---|
26 | |
---|
27 | class Collection(Packaged): |
---|
28 | """A collection of templates of a specific type""" |
---|
29 | |
---|
30 | _default = None |
---|
31 | |
---|
32 | def get_path(self, file): |
---|
33 | return os.path.join(self.path, str(file)) |
---|
34 | |
---|
35 | def get_pkg(self, file): |
---|
36 | return (self.pkg, str(file)) |
---|
37 | |
---|
38 | class RepositoryCollection(Collection): |
---|
39 | _default = 'default' |
---|
40 | |
---|
41 | class ScriptCollection(Collection): |
---|
42 | _default = 'default.py_tmpl' |
---|
43 | |
---|
44 | class Template(Packaged): |
---|
45 | """Finds the paths/packages of various Migrate templates""" |
---|
46 | |
---|
47 | _repository = 'repository' |
---|
48 | _script = 'script' |
---|
49 | _manage = 'manage.py_tmpl' |
---|
50 | |
---|
51 | def __init__(self, pkg): |
---|
52 | super(Template, self).__init__(pkg) |
---|
53 | self.repository=RepositoryCollection('.'.join((self.pkg, self._repository))) |
---|
54 | self.script=ScriptCollection('.'.join((self.pkg, self._script))) |
---|
55 | |
---|
56 | def get_item(self, attr, filename=None, as_pkg=None, as_str=None): |
---|
57 | item = getattr(self, attr) |
---|
58 | if filename is None: |
---|
59 | filename = getattr(item, '_default') |
---|
60 | if as_pkg: |
---|
61 | ret = item.get_pkg(filename) |
---|
62 | if as_str: |
---|
63 | ret = '.'.join(ret) |
---|
64 | else: |
---|
65 | ret = item.get_path(filename) |
---|
66 | return ret |
---|
67 | |
---|
68 | def get_repository(self, filename=None, as_pkg=None, as_str=None): |
---|
69 | return self.get_item('repository', filename, as_pkg, as_str) |
---|
70 | |
---|
71 | def get_script(self, filename=None, as_pkg=None, as_str=None): |
---|
72 | return self.get_item('script', filename, as_pkg, as_str) |
---|
73 | |
---|
74 | def manage(self, **k): |
---|
75 | return (self.pkg, self._manage) |
---|
76 | |
---|
77 | template_pkg = 'migrate.versioning.templates' |
---|
78 | template = Template(template_pkg) |
---|