def filter_anonymous_name(bundle_subclass, metadata): ''' If the user left an anonymous name for this bundle, wipe it out and let the bundle subclass's constructor choose a name instead. ''' anonymous_name = MetadataDefaults.get_anonymous_name(bundle_subclass) if metadata.get('name') == anonymous_name: metadata['name'] = None return metadata
def fill_missing_metadata(bundle_subclass, args, initial_metadata): ''' Return metadata for bundles by filling in the missing metadata with default values. args: Namespace object created from attributes parsed out of the command line. See `argparse` for more information ''' # Fill in default values for all unsupplied metadata keys. new_initial_metadata = {} for spec in bundle_subclass.get_user_defined_metadata(): new_initial_metadata[spec.key] = initial_metadata.get(spec.key) if not new_initial_metadata[spec.key]: default = MetadataDefaults.get_default(spec, bundle_subclass, args) new_initial_metadata[spec.key] = default return new_initial_metadata
def request_missing_data(bundle_subclass, args, initial_metadata=None): ''' For any metadata arguments that were not supplied through the command line, pop up an editor and request that data from the user. ''' if not initial_metadata: initial_metadata = { spec.key: getattr(args, metadata_key_to_argument(spec.key,)) for spec in bundle_subclass.get_user_defined_metadata() } # A special-case: if the user specified all required metadata on the command # line, do NOT show the editor. This allows for programmatic bundle creation. if not any(value is None for value in initial_metadata.values()): return initial_metadata # Fill in default values for all unsupplied metadata keys. for spec in bundle_subclass.get_user_defined_metadata(): if not initial_metadata[spec.key]: default = MetadataDefaults.get_default(spec, bundle_subclass, args) initial_metadata[spec.key] = default # If the --auto flag was used, skip showing the editor. if getattr(args, 'auto', False): return filter_anonymous_name(bundle_subclass, initial_metadata) # Construct a form template with the required keys, prefilled with the # command-line metadata options. template_lines = [] for spec in bundle_subclass.get_user_defined_metadata(): initial_value = initial_metadata.get(spec.key) or '' if spec.type == set: initial_value = ' '.join(initial_value or []) template_lines.append('%s: %s' % (spec.key.title(), initial_value)) bundle_type = bundle_subclass.BUNDLE_TYPE template_lines.append(os.linesep.join([ '# Record metadata for the new %s, then save and quit.' % (bundle_type,), '# Leave the name blank to cancel the upload.', ])) template = (os.linesep + os.linesep).join(template_lines) # Show the form to the user in their editor of choice and parse the result. editor = os.environ.get('EDITOR', 'notepad' if sys.platform == 'win32' else 'vim') tempfile_name = '' with tempfile.NamedTemporaryFile(suffix='.sh', delete=False) as form: form.write(template) form.flush() tempfile_name = form.name if os.path.isfile(tempfile_name): subprocess.call([editor, tempfile_name]) with open(tempfile_name, 'rb') as form: form_result = form.readlines() os.remove(tempfile_name) return parse_metadata_form(bundle_subclass, form_result)
def request_missing_metadata(bundle_subclass, args, initial_metadata=None): ''' For any metadata arguments that were not supplied through the command line, pop up an editor and request that data from the user. ''' if not initial_metadata: initial_metadata = { spec.key: getattr(args, metadata_key_to_argument(spec.key,)) for spec in bundle_subclass.get_user_defined_metadata() } # Fill in default values for all unsupplied metadata keys. new_initial_metadata = {} for spec in bundle_subclass.get_user_defined_metadata(): new_initial_metadata[spec.key] = initial_metadata.get(spec.key) if not new_initial_metadata[spec.key]: default = MetadataDefaults.get_default(spec, bundle_subclass, args) new_initial_metadata[spec.key] = default initial_metadata = new_initial_metadata # If args.edit doesn't exist (when doing 'cl edit'), then we want to show # the editor. if not getattr(args, 'edit', True): return initial_metadata # Construct a form template with the required keys, prefilled with the # command-line metadata options. template_lines = [] bundle_type = bundle_subclass.BUNDLE_TYPE template_lines.append(os.linesep.join([ '// Enter metadata for the new %s bundle, then save and quit.' % (bundle_type,), '// To cancel the upload, delete the name.', ])) for spec in bundle_subclass.get_user_defined_metadata(): initial_value = initial_metadata.get(spec.key) or '' if spec.type == list: initial_value = ' '.join(initial_value or []) template_lines.append('') template_lines.append('// %s' % spec.description) template_lines.append('%s: %s' % (spec.key, initial_value)) template = os.linesep.join(template_lines) # Show the form to the user in their editor of choice and parse the result. form_result = editor_util.open_and_edit(suffix='.c', template=template) return parse_metadata_form(bundle_subclass, form_result)
def fill_missing_metadata(bundle_subclass, args, initial_metadata): ''' Return metadata for bundles by filling in the missing metadata with default values. args: Namespace object created from attributes parsed out of the command line. See `argparse` for more information ''' # Fill in default values for all unsupplied metadata keys. new_initial_metadata = {} for spec in bundle_subclass.get_user_defined_metadata(): new_initial_metadata[spec.key] = initial_metadata.get(spec.key) if not new_initial_metadata[spec.key]: default = MetadataDefaults.get_default(spec, bundle_subclass, args) new_initial_metadata[spec.key] = default final_value = new_initial_metadata[spec.key] is_unicode_string = isinstance( final_value, str) and unicode_util.contains_unicode(final_value) is_unicode_list = isinstance(final_value, list) and any( unicode_util.contains_unicode(v) for v in final_value) if is_unicode_string or is_unicode_list: raise UsageError('Metadata cannot contain unicode: %s = %s' % (spec.key, final_value)) return new_initial_metadata
def fill_missing_metadata(bundle_subclass, args, initial_metadata): ''' Return metadata for bundles by filling in the missing metadata with default values. args: Namespace object created from attributes parsed out of the command line. See `argparse` for more information ''' # Fill in default values for all unsupplied metadata keys. new_initial_metadata = {} for spec in bundle_subclass.get_user_defined_metadata(): new_initial_metadata[spec.key] = initial_metadata.get(spec.key) if not new_initial_metadata[spec.key]: default = MetadataDefaults.get_default(spec, bundle_subclass, args) new_initial_metadata[spec.key] = default final_value = new_initial_metadata[spec.key] is_unicode_string = isinstance(final_value, basestring) and unicode_util.contains_unicode( final_value ) is_unicode_list = isinstance(final_value, list) and any( unicode_util.contains_unicode(v) for v in final_value ) if is_unicode_string or is_unicode_list: raise UsageError('Metadata cannot contain unicode: %s = %s' % (spec.key, final_value)) return new_initial_metadata