MCPcopy Create free account
hub / github.com/canonical/cloud-init / convert_string

Function convert_string

cloudinit/user_data.py:365–387  ·  view source on GitHub ↗

convert a string (more likely bytes) or a message into a mime message.

(raw_data, content_type=NOT_MULTIPART_TYPE)

Source from the content-addressed store, hash-verified

363
364# Converts a raw string into a mime message
365def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE):
366 """convert a string (more likely bytes) or a message into
367 a mime message."""
368 if not raw_data:
369 raw_data = b""
370
371 def create_binmsg(data, content_type):
372 maintype, subtype = content_type.split("/", 1)
373 msg = MIMEBase(maintype, subtype)
374 msg.set_payload(data)
375 return msg
376
377 if isinstance(raw_data, str):
378 bdata = raw_data.encode("utf-8")
379 else:
380 bdata = raw_data
381 bdata = util.decomp_gzip(bdata, decode=False)
382 if b"mime-version:" in bdata[0:4096].lower():
383 msg = util.message_from_string(bdata.decode("utf-8"))
384 else:
385 msg = create_binmsg(bdata, content_type)
386
387 return msg

Callers 2

processMethod · 0.85
_do_includeMethod · 0.85

Calls 1

create_binmsgFunction · 0.85

Tested by

no test coverage detected