summaryrefslogtreecommitdiff
path: root/cloudinit/user_data.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-06-22 20:39:21 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-06-22 20:39:21 -0700
commit7a8744f218873367746eab63ebe29dc5bf8e4249 (patch)
tree7497ceaf590c0b190e63d7281d11b754fbe0bf2e /cloudinit/user_data.py
parent73e59752a8d776b12d03a5e9dd9d5f8b9823f66c (diff)
downloadvyos-cloud-init-7a8744f218873367746eab63ebe29dc5bf8e4249.tar.gz
vyos-cloud-init-7a8744f218873367746eab63ebe29dc5bf8e4249.zip
Constant adding for content type, allow the part filename to be over-ridden easily
Diffstat (limited to 'cloudinit/user_data.py')
-rw-r--r--cloudinit/user_data.py27
1 files changed, 17 insertions, 10 deletions
diff --git a/cloudinit/user_data.py b/cloudinit/user_data.py
index 4babb8e5..91841bb8 100644
--- a/cloudinit/user_data.py
+++ b/cloudinit/user_data.py
@@ -39,6 +39,9 @@ NOT_MULTIPART_TYPE = handlers.NOT_MULTIPART_TYPE
PART_FN_TPL = handlers.PART_FN_TPL
OCTET_TYPE = handlers.OCTET_TYPE
+# Saves typing errors
+CONTENT_TYPE = 'Content-Type'
+
# Various special content types that cause special actions
TYPE_NEEDED = ["text/plain", "text/x-not-multipart"]
INCLUDE_TYPES = ['text/x-include-url', 'text/x-include-once-url']
@@ -87,10 +90,10 @@ class UserDataProcessor(object):
self._explode_archive(payload, append_msg)
continue
- if 'Content-Type' in base_msg:
- base_msg.replace_header('Content-Type', ctype)
+ if CONTENT_TYPE in base_msg:
+ base_msg.replace_header(CONTENT_TYPE, ctype)
else:
- base_msg['Content-Type'] = ctype
+ base_msg[CONTENT_TYPE] = ctype
self._attach_part(append_msg, part)
@@ -173,8 +176,8 @@ class UserDataProcessor(object):
msg.set_payload(content)
if 'filename' in ent:
- msg.add_header('Content-Disposition', 'attachment',
- filename=ent['filename'])
+ msg.add_header('Content-Disposition',
+ 'attachment', filename=ent['filename'])
for header in list(ent.keys()):
if header in ('content', 'filename', 'type'):
@@ -201,17 +204,21 @@ class UserDataProcessor(object):
outer_msg.replace_header(ATTACHMENT_FIELD, str(fetched_count))
return fetched_count
+ def _part_filename(self, unnamed_part, count):
+ return PART_FN_TPL % (count + 1)
+
def _attach_part(self, outer_msg, part):
"""
Attach an part to an outer message. outermsg must be a MIMEMultipart.
Modifies a header in the message to keep track of number of attachments.
"""
- cur = self._multi_part_count(outer_msg)
+ cur_c = self._multi_part_count(outer_msg)
if not part.get_filename():
- fn = PART_FN_TPL % (cur + 1)
- part.add_header('Content-Disposition', 'attachment', filename=fn)
+ fn = self._part_filename(part, cur_c)
+ part.add_header('Content-Disposition',
+ 'attachment', filename=fn)
outer_msg.attach(part)
- self._multi_part_count(outer_msg, cur + 1)
+ self._multi_part_count(outer_msg, cur_c + 1)
# Coverts a raw string into a mime message
@@ -229,7 +236,7 @@ def convert_string(raw_data, headers=None):
else:
msg[key] = val
else:
- mtype = headers.get("Content-Type", NOT_MULTIPART_TYPE)
+ mtype = headers.get(CONTENT_TYPE, NOT_MULTIPART_TYPE)
maintype, subtype = mtype.split("/", 1)
msg = MIMEBase(maintype, subtype, *headers)
msg.set_payload(data)