Skip to content
Browse files

initial add

  • Loading branch information...
0 parents commit 7d554d228e13c83ee97d0baabdf51dd50f83f900 @sbaer committed Jul 1, 2011
Showing with 238 additions and 0 deletions.
  1. +8 −0 app.yaml
  2. +11 −0 index.yaml
  3. +67 −0 main.py
  4. +152 −0 tests/appenginesettings.py
8 app.yaml
@@ -0,0 +1,8 @@
+application: rhinosettings
+version: 1
+runtime: python
+api_version: 1
+
+handlers:
+- url: .*
+ script: main.py
11 index.yaml
@@ -0,0 +1,11 @@
+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run. If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED"). If you want to manage some indexes
+# manually, move them above the marker line. The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
67 main.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+
+from google.appengine.ext import webapp
+from google.appengine.ext.webapp import util
+from google.appengine.ext import db
+# appengine still uses python 2.5, when it upgrades we can use the built-in json module
+from django.utils import simplejson as json
+
+
+# Our database record class. Other things we may want to save in this data structure are
+# user information, version number
+class RhinoAppSettings(db.Model):
+ name = db.StringProperty(required=True) # the "scheme" name
+ settings = db.TextProperty() # JSON style string of settings
+
+
+
+# called with http://rhinosettings.appspot.com/*
+class MainHandler(webapp.RequestHandler):
+ def get(self):
+ scheme = self.request.path[1:]
+ if scheme:
+ settings = db.GqlQuery('SELECT * FROM RhinoAppSettings WHERE name = :1', scheme)
+ item = settings.get()
+ if item:
+ self.response.out.write(item.settings)
+ else:
+ settings = db.GqlQuery('SELECT * FROM RhinoAppSettings')
+ if settings.count()>0:
+ schemes = []
+ for setting in settings:
+ schemes.append(setting.name)
+ d = {"schemes":schemes}
+ output = json.dumps(d)
+ self.response.out.write(output)
+ else:
+ self.response.out.write("no settings")
+
+ def post(self):
+ posted_data = self.request.body
+ data = json.loads(posted_data)
+ # the data needs to have a name string and a settings dictionary
+ _name = data['name']
+ _settings = data['settings']
+ if _settings:
+ _settings = json.dumps(_settings) #convert _settings back into json
+ if _name and _settings:
+ # see if the item is already in the datastore. If we implemented
+ # versioning, I would just add a new record with an updated version
+ # number
+ query_rc = db.GqlQuery("SELECT * FROM RhinoAppSettings WHERE name = :1", _name)
+ item = query_rc.get() #just grab the first one and update it
+ if item:
+ item.settings = _settings
+ item.put()
+ else:
+ saved = RhinoAppSettings(name=_name, settings=_settings)
+ saved.put()
+
+
+def main():
+ application = webapp.WSGIApplication([('/.*', MainHandler)], debug=True)
+ util.run_wsgi_app(application)
+
+
+if __name__ == '__main__':
+ main()
152 tests/appenginesettings.py
@@ -0,0 +1,152 @@
+#rhinosettings google app to store/retrieve application settings in the 'cloud'
+import sys, json, urllib, urllib2, inspect
+import rhinoscriptsyntax as rs
+import System, Rhino
+
+appengine_url = "http://rhinosettings.appspot.com/"
+# for local testing
+#appengine_url = "http://localhost:8080/"
+
+def cloudrestore(id):
+ """Retrieve settings from database and update the
+ the current application settings by these values
+ Parameters:
+ id: name of the settings scheme in the database
+ """
+ f = urllib.urlopen(appengine_url + id)
+ received_data = f.read()
+ f.close()
+ if not received_data: return
+ data = json.loads(received_data)
+
+ states = {}
+ states["AppearanceSettings"] = Rhino.ApplicationSettings.AppearanceSettings.GetDefaultState()
+ states["EdgeAnalysisSettings"] = Rhino.ApplicationSettings.EdgeAnalysisSettings.GetDefaultState()
+ states["ModelAidSettings"] = Rhino.ApplicationSettings.ModelAidSettings.GetDefaultState()
+ states["ViewSettings"] = Rhino.ApplicationSettings.ViewSettings.GetDefaultState()
+ states["SmartTrackSettings"] = Rhino.ApplicationSettings.SmartTrackSettings.GetDefaultState()
+ aliases = Rhino.ApplicationSettings.CommandAliasList.GetDefaults()
+
+ def restoresetting(state, name, setting):
+ try:
+ s = "state." + name
+ if name.endswith("Color"):
+ s += "=System.Drawing.ColorTranslator.FromHtml(\"" + setting + "\")"
+ else:
+ if type(setting) is str: s += "='" + setting + "'"
+ else: s+= "=" + str(setting)
+ exec(s)
+ except:
+ print sys.exc_info()
+ for setting, val in data.items():
+ if type(val) is dict:
+ if states.has_key(setting):
+ state = states[setting]
+ for k,v in val.items(): restoresetting(state, k, v)
+ if setting=="CommandAliasList":
+ aliases.Clear()
+ for k,v in val.items(): aliases.Add(k,v)
+ Rhino.ApplicationSettings.CommandAliasList.Clear()
+ for kv in aliases:
+ Rhino.ApplicationSettings.CommandAliasList.Add(kv.Key,kv.Value)
+ Rhino.ApplicationSettings.AppearanceSettings.UpdateFromState( states["AppearanceSettings"] )
+ Rhino.ApplicationSettings.EdgeAnalysisSettings.UpdateFromState( states["EdgeAnalysisSettings"] )
+ Rhino.ApplicationSettings.ModelAidSettings.UpdateFromState( states["ModelAidSettings"] )
+ Rhino.ApplicationSettings.ViewSettings.UpdateFromState( states["ViewSettings"] )
+ Rhino.ApplicationSettings.SmartTrackSettings.UpdateFromState( states["SmartTrackSettings"] )
+ rs.Redraw()
+
+
+def cloudsave(id):
+ """Save settings to the cloud.
+ parameters:
+ id: Name of scheme to save in database (always converted to lower case)
+ """
+ def getnondefaults(section, defaults):
+ dict = {}
+ members = inspect.getmembers(defaults, (lambda x: not inspect.isbuiltin(x)) )
+ for name, val in members:
+ if name.startswith('_'): continue
+ s = "Rhino.ApplicationSettings."+section+"." + name
+ sameasdefault = eval( "defaults."+name+"=="+s )
+ if not sameasdefault:
+ print " saving: ", s
+ item = eval(s)
+ if type(item) is System.Drawing.Color:
+ item = System.Drawing.ColorTranslator.ToHtml(item)
+ dict[name] = item
+ return dict
+
+ data = {}
+
+ defaults = Rhino.ApplicationSettings.AppearanceSettings.GetDefaultState()
+ subdict = getnondefaults("AppearanceSettings", defaults)
+ if subdict: data["AppearanceSettings"] = subdict
+
+ defaults = Rhino.ApplicationSettings.EdgeAnalysisSettings.GetDefaultState()
+ subdict = getnondefaults("EdgeAnalysisSettings", defaults)
+ if subdict: data["EdgeAnalysisSettings"] = subdict
+
+ defaults = Rhino.ApplicationSettings.ModelAidSettings.GetDefaultState()
+ subdict = getnondefaults("ModelAidSettings", defaults)
+ if subdict: data["ModelAidSettings"] = subdict
+
+ defaults = Rhino.ApplicationSettings.ViewSettings.GetDefaultState()
+ subdict = getnondefaults("ViewSettings", defaults)
+ if subdict: data["ViewSettings"] = subdict
+
+ defaults = Rhino.ApplicationSettings.SmartTrackSettings.GetDefaultState()
+ subdict = getnondefaults("SmartTrackSettings", defaults)
+ if subdict: data["SmartTrackSettings"] = subdict
+
+ if not Rhino.ApplicationSettings.CommandAliasList.IsDefault():
+ print " saving: Rhino.ApplicationSettings.CommandAliasList"
+ subdict = {}
+ aliases = Rhino.ApplicationSettings.CommandAliasList.ToDictionary()
+ for kv in aliases: subdict[kv.Key] = kv.Value
+ data["CommandAliasList"] = subdict
+
+ if data:
+ jsondata = {"name":id, "settings":data}
+ j = json.dumps(jsondata)
+ req = urllib2.Request(appengine_url, j, {"content-type":"application/json"})
+ stream = urllib2.urlopen(req)
+ response = stream.read()
+ stream.close()
+ return response
+
+
+def getinput():
+ gs = Rhino.Input.Custom.GetString()
+ gs.SetCommandPrompt("Scheme Name")
+ direction = Rhino.Input.Custom.OptionToggle(True, "Save", "Restore")
+ gs.AddOptionToggle("Direction", direction)
+ op_list = gs.AddOption("List")
+ while( True ):
+ if gs.Get()==Rhino.Input.GetResult.Option:
+ if gs.OptionIndex() == op_list:
+ f = urllib.urlopen(appengine_url)
+ received_data = f.read()
+ f.close()
+ if received_data:
+ schemes = json.loads(received_data)["schemes"]
+ for scheme in schemes: print scheme
+ continue
+ break
+ if gs.CommandResult()!=Rhino.Commands.Result.Success:
+ return
+ restore = direction.CurrentValue
+ name = gs.StringResult()
+ return restore, name
+
+if __name__ == "__main__":
+ print "Rhino Appearance Settings"
+ input = getinput()
+ if input:
+ restore, name = input
+ if restore:
+ print "- Restoring settings saved in cloud"
+ cloudrestore(name)
+ else:
+ print "- Saving non-default settings to cloud"
+ cloudsave(name)

0 comments on commit 7d554d2

Please sign in to comment.
Something went wrong with that request. Please try again.