-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
PackageProvider.py
200 lines (176 loc) · 7.18 KB
/
PackageProvider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os
from collections import defaultdict
from os.path import relpath, join, basename, isdir, isfile, dirname
import shutil
from ...providers.Provider import Provider
from ...exceptions import InvalidPackageName
from ...utils.location import (
base_path,
config_path,
views_path,
migrations_path,
resources_path,
)
from ...facades import Config
from ...utils.time import migration_timestamp
from ...routes import Route
from ...utils.structures import load
from ...utils.str import modularize, as_filepath
from ...utils.filesystem import make_directory
from ..reserved_names import PACKAGE_RESERVED_NAMES
from ..Package import Package
class PackageProvider(Provider):
vendor_prefix = "vendor"
def __init__(self, application):
self.application = application
# TODO: the default here could be set auto by deciding that its the dirname
# containing the provider !
self.package = Package()
self.default_resources = ["config", "views", "migrations", "assets"]
def register(self):
self.configure()
def boot(self):
pass
# api
def configure(self):
pass
def publish(self, resources, dry=False):
project_root = base_path()
resources_list = resources or self.default_resources
published_resources = defaultdict(lambda: [])
for resource in resources_list:
resource = self.package.resources.get(resource)
if not resource:
continue
for source, dest in resource.files:
if not dry:
make_directory(dest)
shutil.copy(source, dest)
published_resources[resource.key].append(relpath(dest, project_root))
return published_resources
def root(self, relative_dir):
"""Define python package module root path and absolute package root path.
It works when installing the package locally with: pip install . or pip install -e .
and when installing the package from production release with: pip install package-name
"""
# load module provider
provider_module = load(self.__module__)
# get relative module path to package root
relative_module_path = modularize(relative_dir)
self.package.module_root = self.__module__[
0 : self.__module__.find(relative_module_path) + len(relative_module_path)
]
module_root_path = as_filepath(self.package.module_root)
self.package.abs_root = provider_module.__file__[
0 : provider_module.__file__.find(module_root_path) + len(module_root_path)
]
return self
def name(self, name):
if name in PACKAGE_RESERVED_NAMES:
raise InvalidPackageName(
f"{name} is a reserved name. Please choose another name for your package."
)
self.package.name = name
return self
def vendor_name(self, name):
self.package.vendor_name = name
return self
def config(self, config_filepath, publish=False):
# TODO: a name must be specified !
self.package.add_config(config_filepath)
Config.merge_with(self.package.name, self.package.config)
if publish:
self.package.add_publishable_resource(
"config", config_filepath, config_path(f"{self.package.name}.py")
)
return self
def views(self, location, publish=False):
"""Register views location in the project. location must be a folder containinng the views you want to publish."""
self.package.add_views(location)
# register views into project
self.application.make("view").add_namespaced_location(
self.package.name, self.package.views
)
if publish:
location_abs_path = self.package._build_path(location)
for dirpath, _, filenames in os.walk(location_abs_path):
for f in filenames:
# don't add other files than templates
view_abs_path = join(dirpath, f)
_, ext = os.path.splitext(view_abs_path)
if ext != ".html":
continue
self.package.add_publishable_resource(
"views",
view_abs_path,
views_path(
join(
self.vendor_prefix,
self.package.name,
relpath(view_abs_path, location_abs_path),
)
),
)
return self
def commands(self, *commands):
self.application.make("commands").add(*commands)
return self
def presets(self, *presets):
for preset in presets:
self.application.make("presets").add(preset)
return self
def migrations(self, *migrations):
self.package.add_migrations(*migrations)
# use same timestamp for all package migrations
timestamp = migration_timestamp()
for index, migration in enumerate(migrations):
self.package.add_publishable_resource(
"migrations",
migration,
migrations_path(f"{timestamp}{index + 1}_{basename(migration)}"),
)
return self
def routes(self, *routes):
"""Controller locations must have been loaded already !"""
self.package.add_routes(*routes)
for route_group in self.package.routes:
self.application.make("router").add(
Route.group(load(route_group, "ROUTES", []), middleware=["web"])
)
return self
def controllers(self, *controller_locations):
self.package.add_controller_locations(*controller_locations)
Route.add_controller_locations(*self.package.controller_locations)
return self
def assets(self, *assets):
self.package.add_assets(*assets)
for asset_dir_or_file in assets:
abs_path = self.package._build_path(asset_dir_or_file)
if isdir(abs_path):
for dirpath, _, filenames in os.walk(abs_path):
for f in filenames:
asset_abs_path = join(dirpath, f)
self.package.add_publishable_resource(
"assets",
asset_abs_path,
resources_path(
join(
self.vendor_prefix,
self.package.name,
relpath(asset_abs_path, abs_path),
)
),
)
elif isfile(abs_path):
self.package.add_publishable_resource(
"assets",
abs_path,
resources_path(
join(
self.vendor_prefix,
self.package.name,
asset_dir_or_file,
)
),
)
return self