chiark / gitweb /
Merge pull request #601 from CapnBry/reloadscene
[cura.git] / Cura / util / youmagine.py
index 1451178a37bf585d8d1003120cb24bc0006cd80d..e1df083624bc64203f211c953d3b5cab3d22da16 100644 (file)
@@ -1,11 +1,19 @@
-from __future__ import absolute_import
+"""
+YouMagine communication module.
+This module handles all communication with the YouMagine API.
+"""
 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
 
 import json
 import httplib as httpclient
 import urllib
+import textwrap
 
 class httpUploadDataStream(object):
+       """
+       For http uploads we need a readable/writable datasteam to use with the httpclient.HTTPSConnection class.
+       This is used to facilitate file uploads towards Youmagine.
+       """
        def __init__(self, progressCallback):
                self._dataList = []
                self._totalLength = 0
@@ -35,6 +43,10 @@ class httpUploadDataStream(object):
                return self._totalLength
 
 class Youmagine(object):
+       """
+       Youmagine connection object. Has various functions to communicate with Youmagine.
+       These functions are blocking and thus this class should be used from a thread.
+       """
        def __init__(self, authToken, progressCallback = None):
                self._hostUrl = 'api.youmagine.com'
                self._viewUrl = 'www.youmagine.com'
@@ -54,11 +66,15 @@ class Youmagine(object):
                        ('Jewelry', 7),
                        ('Maker/DIY', 8),
                        ('Miniatures', 9),
+                       ('Toys', 10),
+                       ('3D printer parts and enhancements', 11),
                        ('Other', 1),
                ]
                self._licenses = [
-                       ('Creative Commons - Share Alike', 'cc'),
-                       ('Creative Commons - Attribution-NonCommercial-ShareAlike', 'ccnc'),
+                       ('Creative Commons - Attribution Share Alike', 'ccbysa'),
+                       ('Creative Commons - Attribution Non-Commercial ShareAlike', 'ccbyncsa'),
+                       ('Creative Commons - Attribution No Derivatives', 'ccbynd'),
+                       ('Creative Commons - Attribution Non-Commercial No Derivatives', 'ccbyncsa'),
                        ('GPLv3', 'gplv3'),
                ]
 
@@ -103,7 +119,13 @@ class Youmagine(object):
                return True
 
        def createDesign(self, name, description, category, license):
-               res = self._request('POST', '/designs.json', {'design[name]': name, 'design[excerpt]': description, 'design[design_category_id]': filter(lambda n: n[0] == category, self._categories)[0][1], 'design[license]': filter(lambda n: n[0] == license, self._licenses)[0][1]})
+               excerpt = description
+               description = ''
+               if len(excerpt) >= 300:
+                       lines = textwrap.wrap(excerpt, 300)
+                       excerpt = lines[0]
+                       description = '\n'.join(lines[1:])
+               res = self._request('POST', '/designs.json', {'design[name]': name, 'design[excerpt]': excerpt, 'design[description]': description, 'design[design_category_id]': filter(lambda n: n[0] == category, self._categories)[0][1], 'design[license]': filter(lambda n: n[0] == license, self._licenses)[0][1]})
                if 'id' in res:
                        return res['id']
                print res
@@ -190,8 +212,11 @@ class Youmagine(object):
                return {'error': error}
 
 
-#Fake Youmagine class to test without internet
 class FakeYoumagine(Youmagine):
+       """
+       Fake Youmagine class to test without internet, acts the same as the YouMagine class, but without going to the internet.
+       Assists in testing UI features.
+       """
        def __init__(self, authToken, callback):
                super(FakeYoumagine, self).__init__(authToken)
                self._authUrl = 'file:///C:/Models/output.html'
@@ -233,11 +258,3 @@ class FakeYoumagine(Youmagine):
 
        def _request(self, method, url, postData = None, files = None):
                print "Err: Tried to do request: %s %s" % (method, url)
-
-def main():
-       ym = Youmagine('j3rY9kQF62ptuZF7vqbR')
-       if not ym.isAuthorized():
-               print "Failed to authorize"
-               return
-       for design in ym.listDesigns():
-               print design['name']