Skip to content

Commit

Permalink
Implement weight loading and saving
Browse files Browse the repository at this point in the history
Last important feature done \o/
  • Loading branch information
dbr committed Jan 8, 2012
1 parent 05eed32 commit 10ddfd7
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions tabtabtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
license: http://unlicense.org/
"""

import os
import sys

try:
Expand Down Expand Up @@ -116,10 +117,53 @@ def __init__(self, fname = None):
self.weights = {}

def load(self):
raise NotImplementedError("TODO") #TODO: Implement this
if self.fname is None:
return

def _load_internal():
import json
if not os.path.isfile(self.fname):
print "Weight file does not exist"
return
f = open(self.fname)
self.weights = json.load(f)
f.close()

# Catch any errors, print traceback and continue
try:
_load_internal()
except Exception:
print "Error loading node weights"
import traceback
traceback.print_exc()

def save(self):
raise NotImplementedError("TODO") #TODO: Implement this
if self.fname is None:
print "Not saving node weights, no file specified"
return

def _save_internal():
import json
ndir = os.path.dirname(self.fname)
if not os.path.isdir(ndir):
try:
os.makedir(ndir)
except OSError, e:
if e.errno != 17: # errno 17 is "already exists"
raise

f = open(self.fname, "w")
# TODO: Limit number of saved items to some sane number
json.dump(self.weights, fp = f)
f.close()

# Catch any errors, print traceback and continue
try:
_save_internal()
except Exception:
print "Error saving node weights"
import traceback
traceback.print_exc()

def get(self, k, default = 0):
if len(self.weights.values()) == 0:
Expand Down Expand Up @@ -281,8 +325,8 @@ def __init__(self, on_create = None, parent = None, winflags = None):
self.input = TabyLineEdit()

# Node weighting
self.weights = NodeWeights("/tmp/weights.json")

self.weights = NodeWeights(os.path.expanduser("~/.nuke/tabtabtab_weights.json"))
self.weights.load() # save called in close method
try:
import nuke
nodes = find_menu_items(nuke.menu("Nodes"))
Expand Down Expand Up @@ -358,6 +402,7 @@ def close(self):
"""Clear current input when closing
"""
self.input.setText("")
self.weights.save()
super(TabTabTabWidget, self).close()

def create(self):
Expand Down

0 comments on commit 10ddfd7

Please sign in to comment.