Format the metadata of pypi packages stored in email header format.

    Currently only used as backup on the wheel (compressed) file format.
331827
def format_sdist_header_metadata(data, filename):
    
    description = get_header_description(data)
    config_items = python_version_check(data)
    attrs = dict(config_items)
    name = pop_key(attrs, 'Name', None)

    basename = path.basename(filename)
    if name is None:
        name = basename.split('-')[0]

    package_data = {
        'name': name,
        'summary': pop_key(attrs, 'Summary', None),
        'license': pop_key(attrs, 'License', None),
    }

    release_data = {
        'version': pop_key(attrs, 'Version'),
        'description': pop_key(attrs, 'Description', description),
        'home_page': pop_key(attrs, 'Home-page', None),
    }

    file_data = {
        'basename': basename,
        'attrs': {
            'packagetype': 'sdist',
            'python_version': 'source',
        }
    }

    # Parse multiple keys
    deps = []
    exts = {}
    environments = {}
    for key, val in config_items:
        if key in ['Requires-Dist', 'Requires']:
            name, extras, const, marker, url = parse_specification(val)
            name = norm_package_name(name)
            specs = const.split(',')
            new_specs = []
            for spec in specs:
                pos = [i for i, c in enumerate(spec) if c in '0123456789']
                if pos:
                    pos = pos[0]
                    comp, spec_ = spec[:pos].strip(), spec[pos:].strip()
                    new_specs.append((comp, spec_))

            # TODO: All this is to preserve the format used originally
            # but is this really needed?
            if marker:
                if marker.startswith('extra'):
                    marker = marker.replace('extra', '')
                    marker = marker.replace('==', '').strip()
                    ext = marker.rsplit(' ')[-1]
                    if '"' in ext or "'" in ext:
                        ext = ext[1:-1]

                    if ext not in exts:
                        exts[ext] = [{'name': name, 'specs': new_specs}]
                    else:
                        exts[ext].append({'name': name, 'specs': new_specs})
                else:
                    if marker not in environments:
                        environments[marker] = [{'name': name, 'specs': new_specs}]
                    else:
                        environments[marker].append({'name': name, 'specs': new_specs})
            else:
                deps.append({
                    'name': name,
                    'specs': new_specs,
                })

    deps.sort(key=lambda o: o['name'])

    new_exts = []
    for key, values in exts.items():
        new_exts.append({'name': key, 'depends': values})

    new_environments = []
    for key, values in environments.items():
        new_environments.append({'name': key, 'depends': values})

    file_data.update(dependencies={
        'has_dep_errors': False,
        'depends': deps,
        'extras': new_exts,
        'environments': new_environments,
    })

    return package_data, release_data, file_data
970848
def get_serialize_format(self, mimetype):
		
		format = self.formats.get(mimetype, None)
		if format is None:
			format = formats.get(mimetype, None)
		return format
1134582
def output_format_lock(self, packages, **kwargs):
        
        self._output_config['type'] = PLAIN
        text = ''
        tmp_packages = OrderedDict()
        columns = self._config.get_columns()
        widths = {}
        for _pkg in packages.values():
            _pkg_name = _pkg.package_name
            _params = _pkg.get_params(columns, merged=True, raw=False)
            if _pkg_name not in tmp_packages:
                tmp_packages[_pkg_name] = _params
                comment = 1
                for _col in columns:
                    widths[_col] = max(widths.get(_col, len(_col)), len(str(_params.get(_col, '')))) + comment
                    comment = 0
        comment = 1
        for _col in columns:
            text += '{}{} '.format(_col, ' ' * (widths[_col] - len(_col) - comment))
            comment = 0
        text = '#{}\n'.format(text.strip())
        for _pkg_name in sorted(tmp_packages, key=lambda x: str(x).lower()):
            _pkg = tmp_packages[_pkg_name]
            line = ''
            for _col in columns:
                line += '{}{} '.format(_pkg[_col], ' ' * (widths[_col] - len(str(_pkg[_col]))))
            text += '{}\n'.format(line.strip())
        return text
620129
def as_string(self):
        """"""
        if self.headers_only:
            self.msgobj = self._get_content()

        # We could just use msgobj.as_string() but this is more flexible... we might need it.
        from email.generator import Generator
        fp = StringIO()
        g = Generator(fp, maxheaderlen=60)
        g.flatten(self.msgobj)
        text = fp.getvalue()
        return text
706516
def pack(mtype, request, rid=None):
    ''''''
    envelope = wire.Envelope() 
    if rid is not None:
        envelope.id = rid
    envelope.type = mtype
    envelope.message = request.SerializeToString()
    data = envelope.SerializeToString()
    data = encoder._VarintBytes(len(data)) + data
    return data
498695
def _format_info(data):
    '''
    
    '''
    return {'name': data.gr_name,
            'gid': data.gr_gid,
            'passwd': data.gr_passwd,
            'members': data.gr_mem}
1306670
def write_pkg_file(self, file):
    
    version = self.get_metadata_version()

    if six.PY2:
        def write_field(key, value):
            file.write("%s: %s\n" % (key, self._encode_field(value)))
    else:
        def write_field(key, value):
            file.write("%s: %s\n" % (key, value))

    write_field('Metadata-Version', str(version))
    write_field('Name', self.get_name())
    write_field('Version', self.get_version())
    write_field('Summary', self.get_description())
    write_field('Home-page', self.get_url())

    if version < StrictVersion('1.2'):
        write_field('Author', self.get_contact())
        write_field('Author-email', self.get_contact_email())
    else:
        optional_fields = (
            ('Author', 'author'),
            ('Author-email', 'author_email'),
            ('Maintainer', 'maintainer'),
            ('Maintainer-email', 'maintainer_email'),
        )

        for field, attr in optional_fields:
            attr_val = getattr(self, attr)

            if attr_val is not None:
                write_field(field, attr_val)

    write_field('License', self.get_license())
    if self.download_url:
        write_field('Download-URL', self.download_url)
    for project_url in self.project_urls.items():
        write_field('Project-URL',  '%s, %s' % project_url)

    long_desc = rfc822_escape(self.get_long_description())
    write_field('Description', long_desc)

    keywords = ','.join(self.get_keywords())
    if keywords:
        write_field('Keywords', keywords)

    if version >= StrictVersion('1.2'):
        for platform in self.get_platforms():
            write_field('Platform', platform)
    else:
        self._write_list(file, 'Platform', self.get_platforms())

    self._write_list(file, 'Classifier', self.get_classifiers())

    # PEP 314
    self._write_list(file, 'Requires', self.get_requires())
    self._write_list(file, 'Provides', self.get_provides())
    self._write_list(file, 'Obsoletes', self.get_obsoletes())

    # Setuptools specific for PEP 345
    if hasattr(self, 'python_requires'):
        write_field('Requires-Python', self.python_requires)

    # PEP 566
    if self.long_description_content_type:
        write_field(
            'Description-Content-Type',
            self.long_description_content_type
        )
    if self.provides_extras:
        for extra in self.provides_extras:
            write_field('Provides-Extra', extra)
3740
def _encode_message_header(cls, client_id, correlation_id, request_key,
                               api_version=0):
        
        return (struct.pack('>hhih',
                            request_key,          # ApiKey
                            api_version,          # ApiVersion
                            correlation_id,       # CorrelationId
                            len(client_id)) +     # ClientId size
                client_id)
1118674
def write_backup_meta_data(self):
        """"""
        auto_backup_meta_file = os.path.join(self._tmp_storage_path, FILE_NAME_AUTO_BACKUP)
        storage.storage_utils.write_dict_to_json(self.meta, auto_backup_meta_file)
1082721
def split_metadata_params(headers):
    

    params = {}
    metadata = {}
    for header_name in headers:
        if header_name.lower() in header_mapping:
            params[header_mapping[header_name.lower()]] = headers[header_name]
        else:
            metadata[header_name] = headers[header_name]
    return metadata, params
150280
def _format_python2_or_3(self):
        
        pb_files = set()
        with open(self.source, 'r', buffering=1) as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                _, _, proto = row
                pb_files.add('riak/pb/{0}_pb2.py'.format(proto))

        for im in sorted(pb_files):
            with open(im, 'r', buffering=1) as pbfile:
                contents = 'from six import *\n' + pbfile.read()
                contents = re.sub(r'riak_pb2',
                                  r'riak.pb.riak_pb2',
                                  contents)
            # Look for this pattern in the protoc-generated file:
            #
            # class RpbCounterGetResp(_message.Message):
            #    __metaclass__ = _reflection.GeneratedProtocolMessageType
            #
            # and convert it to:
            #
            # @add_metaclass(_reflection.GeneratedProtocolMessageType)
            # class RpbCounterGetResp(_message.Message):
            contents = re.sub(
                r'class\s+(\S+)\((\S+)\):\s*\n'
                '\s+__metaclass__\s+=\s+(\S+)\s*\n',
                r'@add_metaclass(\3)\nclass \1(\2):\n', contents)

            with open(im, 'w', buffering=1) as pbfile:
                pbfile.write(contents)
1068828
def format_file_params(files):
        '''
            
        '''
        files_payload = {}
        if files:
            for idx, filename in enumerate(files):
                files_payload["file[" + str(idx) + "]"] = open(filename, 'rb')
        return files_payload
951605
def _set_metadata(self, value):
        '''
        
        '''
        if value is None:
            setattr(self, '_PMMail__metadata', {})
        elif isinstance(value, dict):
            for k, v in value.items():
                if (not isinstance(k, str) and not isinstance(k, int)) \
                    or (not isinstance(v, str) and not isinstance(v, int)):
                    raise TypeError('Metadata keys and values can only be strings or integers')
            setattr(self, '_PMMail__metadata', value)
        else:
            raise TypeError('Metadata must be a dictionary of key-value pairs')
1363278
def zip_result(zip_binary: bytes,
               extraheaders: TYPE_WSGI_RESPONSE_HEADERS = None,
               filename: str = None) -> WSGI_TUPLE_TYPE:
    
    extraheaders = extraheaders or []
    if filename:
        extraheaders.append(
            ('content-disposition', 'inline; filename="{}"'.format(filename))
        )
    contenttype = 'application/zip'
    if filename:
        contenttype += '; filename="{}"'.format(filename)
    return contenttype, extraheaders, zip_binary
145536
def _format_info(self):
		
		info = 'GNTP/%s %s' % (
			self.info.get('version'),
			self.info.get('messagetype'),
		)
		if self.info.get('encryptionAlgorithmID', None):
			info += ' %s:%s' % (
				self.info.get('encryptionAlgorithmID'),
				self.info.get('ivValue'),
			)
		else:
			info += ' NONE'

		if self.info.get('keyHashAlgorithmID', None):
			info += ' %s:%s.%s' % (
				self.info.get('keyHashAlgorithmID'),
				self.info.get('keyHash'),
				self.info.get('salt')
			)

		return info
1294925
def save_metadata(hparams):
  """"""
  output_dir = os.path.expanduser(FLAGS.output_dir)
  if not tf.gfile.Exists(output_dir):
    tf.gfile.MakeDirs(output_dir)

  # Save FLAGS in txt file
  if hasattr(FLAGS, "flags_into_string"):
    flags_str = FLAGS.flags_into_string()
    t2t_flags_str = "\n".join([
        "--%s=%s" % (f.name, f.value)
        for f in FLAGS.flags_by_module_dict()["tensor2tensor.utils.flags"]
    ])
  else:
    flags_dict = FLAGS.__dict__["__flags"]
    flags_str = "\n".join(
        ["--%s=%s" % (name, str(f)) for (name, f) in flags_dict.items()])
    t2t_flags_str = None

  flags_txt = os.path.join(output_dir, "flags.txt")
  with tf.gfile.Open(flags_txt, "w") as f:
    f.write(flags_str)

  if t2t_flags_str:
    t2t_flags_txt = os.path.join(output_dir, "flags_t2t.txt")
    with tf.gfile.Open(t2t_flags_txt, "w") as f:
      f.write(t2t_flags_str)

  # Save hparams as hparams.json
  new_hparams = hparams_lib.copy_hparams(hparams)
  # Modality class is not JSON serializable so remove.
  new_hparams.del_hparam("modality")

  hparams_fname = os.path.join(output_dir, "hparams.json")
  with tf.gfile.Open(hparams_fname, "w") as f:
    f.write(new_hparams.to_json(indent=0, sort_keys=True))
746000
def pack_metadata(self) -> List[Tuple[str, Any]]:
        
        metadata = [(k, v) for k, v in self._invocation_metadata.items()
                    if k != LOG_FIELDS_KEY_META]
        metadata.append((LOG_FIELDS_KEY_META, self._log_fields.dumps()))
        return metadata