Added recipe editing pages.
[cookbook.git] / cookbook / cookbook.py
index de3a84b278869a2f58876ec0fef12d9b9bc27fa7..b95e576b2d113f2c0b52c1df2adac06c1b565eb5 100644 (file)
@@ -149,7 +149,7 @@ class Amount (object):
         if len(self.alternatives) > 0:
             ret.append('(%s)' % ', '.join(
                     [unicode(a) for a in self.alternatives]))
-        return ' '.join(ret)
+        return ' '.join([x for x in ret if x != None])
 
     def to_yaml(self):
         d = {}
@@ -202,8 +202,8 @@ class Ingredient (object):
 
     def __unicode__(self):
         if self.note == None:
-            return '%s %s' % (unicode(self.amount), self.name)
-        return '%s %s, %s' % (unicode(self.amount), self.name, self.note)
+            return '%s %s' % (unicode(self.amount or ''), self.name)
+        return '%s %s, %s' % (unicode(self.amount or ''), self.name, self.note)
 
     def to_yaml(self):
         d = {}
@@ -439,6 +439,7 @@ class Recipe (object):
         self.source = source
         self.url = url
         self.tags = tags
+        self.path = None
 
     def clean_name(self, ascii=False):
         name = self.name
@@ -492,13 +493,17 @@ class Recipe (object):
         self.directions = Directions()
         self.directions.from_yaml(dict.get('directions', None))
 
-    def save(self, stream):
+    def save(self, stream, path=None):
         yaml.dump(self.to_yaml(), stream,
                   default_flow_style=False, allow_unicode=True, width=78)
+        if path != None:
+            self.path = path
 
-    def load(self, stream):
+    def load(self, stream, path=None):
         dict = yaml.load(stream)
         self.from_yaml(dict)
+        if path != None:
+            self.path = path
 
 
 class Cookbook (list):
@@ -507,41 +512,47 @@ class Cookbook (list):
     """
     def __init__(self, name="Mom's cookbook", *args, **kwargs):
         self.name = name
+        self.save_dir = u'recipe'
         super(Cookbook, self).__init__(*args, **kwargs)
 
-    def save(self, dir='recipe'):
-        if not os.path.isdir(dir):
-            os.mkdir(dir)
+    def save(self):
+        if not os.path.isdir(self.save_dir):
+            os.mkdir(self.save_dir)
         paths = []
         for recipe in self:
-            base_path = recipe.clean_name()
-            path = base_path
-            i = 2
-            while path in paths:
-                path = '%s_%d' % (base_path, i)
-                i += 1
-            for x in ['large']:
-                if x in path:
-                    print paths[-4:]
-            paths.append(path)
-            with open(os.path.join(dir, path), 'w') as f:
-                recipe.save(f)
-
-    def load(self, dir='recipe'):
-        for path in os.listdir(dir):
+            p = self._free_path(recipe, paths)
+            paths.append(p)
+            with open(p, 'w') as f:
+                recipe.save(f, p)
+
+    def load(self):
+        for path in os.listdir(self.save_dir):
             r = Recipe()
-            p = os.path.join(dir, path)
+            p = os.path.join(self.save_dir, path)
             with open(p, 'r') as f:
-                r.load(f)
-            r.path = p
+                r.load(f, p)
             self.append(r)
 
     def make_index(self):
         self.index = {}
+        paths = [recipe.path for recipe in self]
         for recipe in self:
             self.index[recipe.name] = recipe
             self.index[recipe.clean_name()] = recipe
             self.index[recipe.clean_name(ascii=True)] = recipe
+            if recipe.path == None:
+                p = self._free_path(recipe, paths)
+                paths.append(p)
+                recipe.path = p
+
+    def _free_path(self, recipe, paths):
+        base_path = os.path.join(self.save_dir, recipe.clean_name())
+        path = base_path
+        i = 2
+        while path in paths:
+            path = '%s_%d' % (base_path, i)
+            i += 1
+        return path
 
     def tags(self):
         """List all tags used in this cookbook.