/
shared.py
195 lines (145 loc) · 6.38 KB
/
shared.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
"""Shared options or arguments for cli commands."""
import click
from grow.common import rc_config
CFG = rc_config.RC_CONFIG.prefixed('grow.shared')
def pod_path_argument(func):
"""Argument for pod path."""
return click.argument('pod_path', default='.')(func)
def deployment_option(config):
"""Option for changing env based on deployment name."""
shared_default = CFG.get('deployment', None)
config_default = config.get('deployment', shared_default)
def _decorator(func):
return click.option(
'--deployment', default=config_default,
help='Name of the deployment config to use.')(func)
return _decorator
def exclude_path_option(func):
"""Option for excluding a path for extraction."""
return click.option(
'--exclude-path',
type=str, multiple=True,
help='Which paths to exclude from the generated messages file.')(func)
def force_untranslated_option(config):
"""Option for forcing untranslated string deployment."""
shared_default = CFG.get('force-untranslated', False)
config_default = config.get('force-untranslated', shared_default)
def _decorator(func):
return click.option(
'--force-untranslated', 'force_untranslated',
default=config_default, is_flag=True,
help='Whether to force untranslated strings to be uploaded.')(func)
return _decorator
def include_header_option(config):
"""Option for preserving catalog header."""
shared_default = CFG.get('include-header', False)
config_default = config.get('include-header', shared_default)
def _decorator(func):
return click.option(
'--include-header',
default=config_default, is_flag=True,
help='Whether to preserve headers at the beginning of catalogs.')(func)
return _decorator
def include_obsolete_option(config, default_value=None):
"""Option for including obsolete translations."""
shared_default = CFG.get('include-obsolete', default_value)
config_default = config.get('include-obsolete', shared_default)
help_text = ('Whether to include obsolete messages. If false, obsolete'
' messages will be removed from the catalog template.')
if default_value is False:
help_text = '{} {}'.format(
help_text, 'By default, Grow cleans obsolete messages from the catalog template.')
def _decorator(func):
return click.option(
'--include-obsolete/--no-include-obsolete',
default=config_default, is_flag=True, help=help_text)(func)
return _decorator
def locale_option(help_text=None, multiple=True):
"""Option for providing locale(s)."""
if help_text is None:
help_text = 'Locale to target.'
def _decorator(func):
return click.option(
'--locale', type=str, multiple=multiple, help=help_text)(func)
return _decorator
def localized_option(config):
"""Option for localizing catalogs."""
shared_default = CFG.get('localized', None)
config_default = config.get('localized', shared_default)
def _decorator(func):
return click.option(
'--localized/--no-localized',
is_flag=True, default=config_default,
help='Whether to create localized message catalogs. Use this'
' option if content varies by locale.')(func)
return _decorator
def out_dir_option(config, help_text=None):
"""Option for output directory."""
shared_default = CFG.get('out-dir', None)
config_default = config.get('out-dir', shared_default)
if help_text is None:
help_text = 'Directory to output files.'
def _decorator(func):
return click.option(
'--out-dir', '--out_dir',
type=str, default=config_default, help=help_text)(func)
return _decorator
def path_option(func):
"""Option for specifying a path for extraction."""
return click.option(
'--path',
type=str, multiple=True,
help='Which paths to extract strings from. By default, all paths'
' are extracted. This option is useful if you\'d like to'
' generate a partial messages file representing just a'
' specific set of files.')(func)
def preprocess_option(config):
"""Option for running preprocessors."""
shared_default = CFG.get('preprocess', True)
config_default = config.get('preprocess', shared_default)
def _decorator(func):
return click.option(
'--preprocess/--no-preprocess', '-p/-np',
is_flag=True, default=config_default,
help='Whether to run preprocessors.')(func)
return _decorator
def routes_file_option(help_text=None):
"""Option for providing a routes file instead of pulling from content."""
if help_text is None:
help_text = 'Use routes file to load routes instead of loading from files.'
def _decorator(func):
return click.option(
'--routes-file', '--routes_file', type=str, default=None,
help=help_text)(func)
return _decorator
def service_option(func):
"""Option for configuring the transltor service to use."""
return click.option('--service', '-s', type=str,
help='Name of the translator service to use. This option is'
' only required if more than one service is configured.')(func)
def shard_option(func):
"""Option for configuring the current shard."""
return click.option(
'--shard', type=int,
help='Current index of shard being used for sharding.')(func)
def shards_option(func):
"""Option for configuring the shard count."""
return click.option(
'--shards', type=int,
help='Number of shards being used for sharding.')(func)
def threaded_option(config):
"""Option for using threading when rendering."""
shared_default = CFG.get('threaded', True)
config_default = config.get('threaded', shared_default)
def _decorator(func):
return click.option(
'--threaded/--no-threaded', '-t/-nt', is_flag=True,
default=config_default,
help='Use threading during rendering pipeline.')(func)
return _decorator
def work_dir_option(func):
"""Option for working directory."""
help_text = 'Directory to pull working files from.'
return click.option(
'--work-dir', '--work_dir', type=str, default=None,
help=help_text)(func)