Skip to content

Commit 36a7cb2

Browse files
committed
feat(CLI): global optional parameters+DL tracking
* set globally shared parameters (which includes 'alt') * track if 'alt' is set to 'media' at runtime to do the right thing when outputting the result. There is still an issue to be fixed though Related to #61
1 parent 830529c commit 36a7cb2

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

src/mako/cli/lib/engine.mako

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<%namespace name="util" file="../../lib/util.mako"/>\
22
<%!
3-
from util import (hub_type, mangle_ident, indent_all_but_first_by, activity_rust_type, setter_fn_name)
3+
from util import (hub_type, mangle_ident, indent_all_but_first_by, activity_rust_type, setter_fn_name, ADD_PARAM_FN)
44
from cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, UPLOAD_FLAG, OUTPUT_FLAG, VALUE_ARG,
55
CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG,
66
cmd_ident, call_method_ident, arg_ident, POD_TYPES, flag_ident, ident, JSON_TYPE_RND_MAP)
@@ -128,9 +128,12 @@ self.opt.${cmd_ident(method)} {
128128
<%def name="_method_call_impl(c, resource, method)" buffered="True">\
129129
<%
130130
mc = new_method_context(resource, method, c)
131-
handle_output = mc.response_schema or mc.m.get('supportsMediaDownload', False)
132-
131+
supports_media_download = mc.m.get('supportsMediaDownload', False)
132+
handle_output = mc.response_schema or supports_media_download
133+
track_download_flag = supports_media_download and parameters is not UNDEFINED and 'alt' in parameters
134+
133135
optional_props = [p for p in mc.optional_props if not p.get('skip_example', False)]
136+
optional_prop_names = set(p.name for p in optional_props)
134137
handle_props = optional_props or parameters is not UNDEFINED
135138
%>\
136139
## REQUIRED PARAMETERS
@@ -158,22 +161,51 @@ let ${prop_name}: ${prop_type} = arg_from_str(&${opt_ident}, err, "<${mangle_sub
158161
call_args.append(borrow + arg_name)
159162
# end for each required prop
160163
%>\
164+
% if track_download_flag:
165+
let mut download_mode = false;
166+
% endif
161167
let mut call = self.hub.${mangle_ident(resource)}().${mangle_ident(method)}(${', '.join(call_args)});
162168
% if handle_props:
163169
for parg in ${SOPT + arg_ident(VALUE_ARG)}.iter() {
164170
let (key, value) = parse_kv_arg(&*parg, err);
165171
match key {
166172
% for p in optional_props:
167173
<%
168-
value_unwrap = 'value.unwrap_or("%s")' % JSON_TYPE_RND_MAP[p.type]()
174+
ptype = p.type
175+
if p.type == 'string' and 'Count' in p.name:
176+
ptype = 'int64'
177+
value_unwrap = 'value.unwrap_or("%s")' % JSON_TYPE_RND_MAP[ptype]()
169178
%>\
170179
"${ident(p.name)}" => call = call.${mangle_ident(setter_fn_name(p))}(\
171-
% if p.type != 'string':
180+
% if ptype != 'string':
172181
arg_from_str(${value_unwrap}, err, "${ident(p.name)}", "${p.type}")),
173182
% else:
174183
${value_unwrap}),
175184
% endif # handle conversion
176185
% endfor # each property
186+
% if parameters is not UNDEFINED:
187+
% for pn, p in list((pn, p) for (pn, p) in parameters.iteritems() if pn not in optional_prop_names):
188+
\
189+
% if not loop.first:
190+
|\
191+
% endif
192+
"${ident(pn)}"\
193+
% if not loop.last:
194+
195+
% endif
196+
% endfor # each global parameter
197+
=> {
198+
<%
199+
value_unwrap = 'value.unwrap_or("unset")'
200+
%>\
201+
% if track_download_flag:
202+
if key == "alt" && ${value_unwrap} == "media" {
203+
download_mode = true;
204+
}
205+
% endif
206+
call = call.${ADD_PARAM_FN}(key, ${value_unwrap})
207+
},
208+
% endif # handle global parameters
177209
_ => err.issues.push(CLIError::UnknownParameter(key.to_string())),
178210
}
179211
}
@@ -194,17 +226,30 @@ if dry_run {
194226
match call.${api.terms.action}() {
195227
Err(api_err) => Some(api_err),
196228
% if mc.response_schema:
197-
Ok((response, output_schema)) => {
229+
Ok((mut response, output_schema)) => {
198230
% else:
199231
Ok(mut response) => {
200232
% endif # handle output structure
201233
println!("DEBUG: REMOVE ME {:?}", response);
234+
## We are not generating optimal code, but hope it will still be logically correct.
235+
## If not, we might build the code in python
236+
## TODO: Fix this
237+
% if track_download_flag:
238+
if !download_mode {
239+
% endif
202240
% if mc.response_schema:
203241
serde::json::to_writer(&mut ostream, &output_schema).unwrap();
204-
% elif mc.m.get('supportsMediaDownload', False):
242+
% endif
243+
% if track_download_flag:
244+
} else {
245+
% endif
246+
% if supports_media_download:
205247
## Download is the only option - nothing else matters
206248
io::copy(&mut response, &mut ostream).unwrap();
207249
% endif
250+
% if track_download_flag:
251+
}
252+
% endif
208253
None
209254
}
210255
}

src/mako/cli/main.rs.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</%block>
1313
#![feature(plugin, exit_status)]
1414
#![plugin(docopt_macros)]
15-
#![allow(unused_variables, unused_imports, dead_code)]
15+
#![allow(unused_variables, unused_imports, dead_code, unsed_mut)]
1616

1717
extern crate docopt;
1818
extern crate yup_oauth2 as oauth2;

0 commit comments

Comments
 (0)