-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.py
120 lines (96 loc) · 3.79 KB
/
app.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
# Copyright (c) 2013 Shotgun Software Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Shotgun Software Inc.
"""
App that creates folders on disk from inside of Shotgun.
"""
from tank.platform import Application
import tank
class CreateFolders(Application):
def init_app(self):
deny_permissions = self.get_setting("deny_permissions")
deny_platforms = self.get_setting("deny_platforms")
p = {
"title": "Create Folders",
"deny_permissions": deny_permissions,
"deny_platforms": deny_platforms,
"supports_multiple_selection": True,
}
self.engine.register_command("create_folders", self.create_folders, p)
p = {
"title": "Preview Create Folders",
"deny_permissions": deny_permissions,
"deny_platforms": deny_platforms,
"supports_multiple_selection": True,
}
self.engine.register_command("preview_folders", self.preview_create_folders, p)
def _add_plural(self, word, items):
"""
appends an s if items > 1
"""
if items > 1:
return "%ss" % word
else:
return word
def preview_create_folders(self, entity_type, entity_ids):
if len(entity_ids) == 0:
self.log_info("No entities specified!")
return
paths = []
try:
paths.extend(
self.tank.preview_filesystem_structure(entity_type, entity_ids)
)
except tank.TankError as tank_error:
# tank errors are errors that are expected and intended for the user
self.log_error(tank_error)
except Exception:
# other errors are not expected and probably bugs - here it's useful with a callstack.
self.log_exception("Error when previewing folders!")
else:
# success! report back to user
if len(paths) == 0:
self.log_info("*No folders would be generated on disk for this item!*")
else:
self.log_info(
"*Creating folders would generate %d items on disk:*" % len(paths)
)
self.log_info("")
for p in paths:
self.log_info(p.replace(r"\_", r"\\_"))
self.log_info("")
self.log_info(
"Note that some of these folders may exist on disk already."
)
def create_folders(self, entity_type, entity_ids):
if len(entity_ids) == 0:
self.log_info("No entities specified!")
return
entities_processed = 0
try:
entities_processed = self.tank.create_filesystem_structure(
entity_type, entity_ids
)
except tank.TankError as tank_error:
# tank errors are errors that are expected and intended for the user
self.log_error(tank_error)
except Exception:
# other errors are not expected and probably bugs - here it's useful with a callstack.
self.log_exception("Error when creating folders!")
else:
# report back to user
self.log_info(
"%d %s processed - "
"Processed %d folders on disk."
% (
len(entity_ids),
self._add_plural(entity_type, len(entity_ids)),
entities_processed,
)
)