From 5922cb5aad5e84c86a70d5d2bc87740542d4a744 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Mon, 10 Feb 2014 15:35:51 +0000 Subject: more forgiving type checking in Media --- diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index 5f3315a..c257a14 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -43,19 +43,21 @@ class Media(object): ALL_TYPES = ('media', 'audio', 'video', 'descr', 'camera', 'camera1') - def __init__(self, type_, value=None): + def __init__(self, media_type, value=None): """ - type_ --- a string that indicates the kind of media: + media_type --- a string that indicates the kind of media: media --- image audio --- audio file video --- video descr --- Journal description camera, camera1 --- camera snapshot value --- a file path or a reference to a Sugar datastore object """ - if type_ not in Media.ALL_TYPES: + if media_type == 'image': + media_type = 'media' + if media_type not in Media.ALL_TYPES: raise ValueError("Media.type must be one of " + repr(Media.ALL_TYPES)) - self.type = type_ + self.type = media_type self.value = value def __str__(self): diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 1161560..97b2b60 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -1082,6 +1082,7 @@ class LogoCode: """ Get contents of URL as text or tempfile to image """ if "://" not in url: # no protocol url = "http://" + url # assume HTTP + try: req = urllib2.urlopen(url) except urllib2.HTTPError, e: @@ -1098,14 +1099,15 @@ class LogoCode: self.tw.running_sugar) raise logoerror('#noconnection') - if req.info().getheader("Content-Type")[0:5] == "image": - # it can't be deleted immediately, or else we won't ever access it + mediatype = req.info().getheader('Content-Type') + if mediatype[0:5] in ['image', 'audio', 'video']: tmp = tempfile.NamedTemporaryFile(delete=False) - tmp.write(req.read()) # prepare for writing - tmp.flush() # actually write it - obj = Media('media', value=tmp.name) + tmp.write(req.read()) + tmp.flush() + obj = Media(mediatype[0:5], value=tmp.name) return obj else: + debug_output('Returning req.read()', self.tw.running_sugar) return req.read() def showlist(self, objects): @@ -1133,6 +1135,7 @@ class LogoCode: mediatype = 'image' # camera snapshot elif os_path_exists(obj.value): self.filepath = obj.value + mediatype = obj.type if self.filepath is not None: if movie_media_type(self.filepath): mediatype = 'video' @@ -1140,7 +1143,7 @@ class LogoCode: mediatype = 'audio' elif image_media_type(self.filepath): mediatype = 'image' - else: + elif text_media_type(self.filepath): mediatype = 'text' elif self.tw.running_sugar: from sugar.datastore import datastore -- cgit v0.9.1