Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--TurtleArt/talogo.py22
-rw-r--r--plugins/turtle_blocks_extras/turtle_blocks_extras.py12
2 files changed, 34 insertions, 0 deletions
diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py
index ba76085..11e98f7 100644
--- a/TurtleArt/talogo.py
+++ b/TurtleArt/talogo.py
@@ -29,6 +29,8 @@ from operator import isNumberType
import os
from os.path import exists as os_path_exists
from UserDict import UserDict
+import urllib2
+import tempfile
try:
from sugar.graphics import style
@@ -1060,6 +1062,26 @@ class LogoCode:
gobject.idle_add(self.tw.send_event, event)
os.remove(tmp_file)
+ def get_from_url(self, url):
+ """ 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:
+ debug_output("Couldn't open %s" % (url), self.tw.running_sugar)
+ raise logoerror(url)
+
+ if req.info().getheader("Content-Type")[0:5] == "image":
+ # it can't be deleted immediately, or else we won't ever access it
+ tmp = tempfile.NamedTemporaryFile(delete=False)
+ tmp.write(req.read()) # prepare for writing
+ tmp.flush() # actually write it
+ obj = Media('media', value=tmp.name)
+ return obj
+ else:
+ return req.read()
+
def showlist(self, objects):
""" Display list of media objects """
x = (self.tw.turtles.get_active_turtle().get_xy()[0] /
diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
index df0d1ff..acb5aea 100644
--- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py
+++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
@@ -862,6 +862,18 @@ module found in the Journal'))
special_name=_('top'),
help_string=_('top of a collapsed stack'))
+ palette.add_block('getfromurl',
+ style='number-style-1arg',
+ label=_('get data from URL'),
+ default=\
+'http://wiki.sugarlabs.org/images/2/2c/Logo_alt_3.svg',
+ prim_name='getfromurl',
+ help_string=\
+_('gets a text string or image from a URL'))
+ self.tw.lc.def_prim('getfromurl', 1,
+ Primitive(self.tw.lc.get_from_url,
+ arg_descs=[ArgSlot(TYPE_STRING)]))
+
def _portfolio_palette(self):
palette = make_palette('portfolio',