Add manifest to manifest
[gpytage.git] / save.py
blob5502c1458cca916161f55bb9977cff20e13b9532
1 #!/usr/bin/env python
3 # GPytage save.py module
5 ############################################################################
6 # Copyright (C) 2008 by Kenneth Prugh #
7 # ken69267@gmail.com #
8 # #
9 # This program is free software; you can redistribute it and#or modify #
10 # it under the terms of the GNU General Public License as published by #
11 # the Free Software Foundation under version 2 of the license. #
12 # #
13 # This program is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU General Public License for more details. #
17 # #
18 # You should have received a copy of the GNU General Public License #
19 # along with this program; if not, write to the #
20 # Free Software Foundation, Inc., #
21 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
22 ############################################################################
24 import os.path
25 import sys
27 from helper import reload
28 from config import get_config_path
29 from window import title, window, createMessageDialog
30 import gtk
31 from leftpanel import leftview
34 class SaveFile:
35 def __init__(self):
36 self.errors = []
38 def save(self):
39 import datastore
40 lists = datastore.lists
41 for name,store in lists.iteritems():
42 try:
43 if name == store[0][3]:#I'm a main file
44 file = name
45 data = []
46 parent = None
47 for row in store:
48 datarow = self.assemblerow(row)
49 data.append(datarow)
50 self.savefile(parent, file, data)
51 model = leftview.get_model()
52 model.foreach(self.findMatch, name)
53 else: #we have a subfile
54 parent = store[0][3] #main dir
55 file = name #sub file
56 data = []
57 for row in store:
58 datarow = self.assemblerow(row)
59 data.append(datarow)
60 self.savefile(parent, file, data)
61 model = leftview.get_model()
62 model.foreach(self.findMatch, name)
63 except IndexError:
64 #when a file is "blank" and a save is attempted it fails here. This should be the only case...
65 print name,store
66 model = leftview.get_model()
67 model.foreach(self.findMatch, name)
68 piter = model.iter_parent(self.fiter)
69 if piter:
70 #has parent
71 parent = model.get_value(piter, 0)
72 else:
73 #no parent
74 parent = None
75 data = "\n"
76 self.savefile(parent, name, data)
78 title("GPytage")
79 if self.errors != []:
80 #spawn dialog
81 err = ',\n'.join(self.errors)
82 message = "The following files failed to save:\n\n%s. \n\nPossible causes may include insufficient privileges to write to these files." %err
83 createMessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Error Saving...", message)
85 def assemblerow(self, child):
86 """ Assemble data columns for saving """
87 try:
88 len(child[0])
89 text1 = child[0]
90 except:
91 text1 = ""
92 try:
93 len(child[1])
94 text2 = child[1]
95 except:
96 text2 = ""
97 datarow = text1 + " " + text2 + '\n'
98 return datarow
100 def savefile(self, parent, file, data):
101 """ Write data to file """
102 config_path = get_config_path()
103 if parent is None: #main file
104 try:
105 f=open(config_path + file, 'w')
106 for row in data:
107 f.write(row)
108 f.close
109 except IOError:
110 self.errors.append("%s%s" % (config_path, file))
111 return
112 else: #subfile
113 try:
114 f=open(config_path + parent + '/' + file, 'w')
115 for row in data:
116 f.write(row)
117 f.close
118 except IOError:
119 self.errors.append("%s%s/%s" %(config_path, parent, file))
120 return
122 def findMatch(self, model, path, iter, user_data):
123 if model.get_value(iter, 0).strip('*') == user_data:
124 model.set_value(iter, 0, user_data)
125 self.fiter = iter