Commits

Rob Lanphier committed f4c4b74

Respecting schema key order. Using alpha key sort on json saving for now

Comments (0)

Files changed (3)

 Things to get done before version 0.1
 
 On deck:
-*  Respect key order in json and schema files
+*  Implement support for optional/required elements
 
 Roadmap
 0.1.0:
-*  Implement support for optional/required elements
 *  Remove hardcoded 20 character label column
 *  Remove hardcoded 13 character "Add field" button width
 *  Put "Add field" over in the data area
+*  Change "add fields" to "add items" for sequences
 *  Disable the "delete" option when a field that can't be deleted is selected
-*  Highlight all children when deleting
+**  Ensure that "delete" over "add fields" area doesn't cause crash
+*  Add alias for editing address books
+*  Fix crashing problem with deleting whole address entries in address book
 
 0.1.1:
 *  Implement ability to edit arbitrary JSON
+*  Highlight all children when deleting
+*  Preserve key order in json files when saving
 
 0.2.0:
 *  Update to urwid 0.9.9 mainloop
 *  Adapt browse.py from urwid into reusable TreeWalker component, and use that
 *  Implement ability to delete child nodes
 *  Don't render "Add field" row if there's no fields to add
-
+*  Use schema key order in editor

jsonwidget/jsonbase.py

         """virtual function"""
         pass
 
+    def sort_keys(self, keys):
+        ordermap = self._get_key_order()
+        def keycmp(a, b):
+            try:
+                ai = ordermap.index(a)
+            except:
+                ai = len(keys)
+            try:
+                bi = ordermap.index(b)
+            except:
+                bi = len(keys)
+            return cmp(ai, bi)
+        return sorted(keys, cmp=keycmp)
+
     def get_child_keys(self):
         if isinstance(self.children, dict):
             keys = self.children.keys()
-            ordermap = self._get_key_order()
-            def keycmp(a, b):
-                try:
-                    ai = ordermap.index(a)
-                except:
-                    ai = len(keys)
-                try:
-                    bi = ordermap.index(b)
-                except:
-                    bi = len(keys)
-                return cmp(ai, bi)
-            keys.sort(cmp=keycmp)
-            return keys
+            return self.sort_keys(keys)
         elif isinstance(self.children, list):
             return range(len(self.children))
         else:

jsonwidget/jsonnode.py

             self.filename = fd.name
         else:
             fd = open(self.filename, 'w+')
-        json.dump(self.get_data(), fd, indent=4)
+        json.dump(self.get_data(), fd, indent=4, sort_keys=True)
         self.savededitcount = self.editcount
 
     def is_type_match(self, schemanode):
         return self.children[key]
 
     def _get_key_order(self):
-        """virtual function"""
-        try:
-            ordermap = self.ordermap['keys']
-        except AttributeError:
-            ordermap = self.schemanode._get_key_order()
+        ordermap = self.schemanode._get_key_order()
         return ordermap
 
     def get_available_keys(self):
             schemakeys = set(self.schemanode.get_child_keys())
             jsonkeys = set(self.get_child_keys())
             unusedkeys = schemakeys.difference(jsonkeys)
-            return list(unusedkeys)
+            sortedkeys = self.sort_keys(list(unusedkeys))
+            return sortedkeys
         elif(self.schemanode.get_type() == 'seq'):
             return [len(self.children)]
         else: