ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/python/CFDataFile.py
Revision: 1.1.1.1 (vendor branch)
Committed: Sun Feb 5 00:02:08 2006 UTC (18 years, 3 months ago) by root
Content type: text/x-python
Branch: UPSTREAM, MAIN
CVS Tags: post_fixaltar, last_stable, post_fixaltar2, rel-2_82, rel-2_81, rel-2_80, pre_coinconvert, UPSTREAM_2006_03_15, rel-3_0, rel-2_6, rel-2_7, rel-2_4, rel-2_5, rel-2_2, rel-2_0, rel-2_1, rel-2_72, rel-2_73, rel-2_71, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_54, rel-2_55, rel-2_56, rel-2_79, UPSTREAM_2006_02_01, rel-2_53, pre_material_cfarch_normalize_run, rel-2_32, pre_fixconverter, post_coinconvert, pre_fixaltar2, pre_map_rename, UPSTREAM_2006_02_22, rel-2_90, rel-2_92, rel-2_93, rel-2_78, post_fixconverter, pre_fixaltar, rel-2_61, rel-2_43, rel-2_42, rel-2_41, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
Initial Import

File Contents

# Content
1 # CFDataFile.py - CFData classes
2 #
3 # Copyright (C) 2004 Todd Mitchell
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19
20 import os
21 import string
22 import Crossfire
23
24 class CFDataFile:
25 '''Plain text storage for Crossfire data'''
26
27 def __init__(self, datafile_name):
28 '''make datafile paths for datafile 'object'
29 - these all go in ../var/crossfire/datafiles to keep the local dir clean'''
30 self.datafile_name = datafile_name
31 self.filename = os.path.join((Crossfire.LocalDirectory()),'datafiles',datafile_name)
32
33 def exists(self):
34 '''checks for datafile - no need to load it yet'''
35 if os.path.isfile(self.filename):
36 return 1
37 else:
38 return 0
39
40 def make_file(self, header):
41 '''creates a datafile, making the column header from a list passed in'''
42 try:
43 file = open(self.filename,'wb')
44 except:
45 print "Can't create datafile %s" % self.datafile_name
46 else:
47 temp = []
48 for item in header:
49 temp.append(str(item))
50 contents = '#|%s\n' %(string.join(temp,'|'))
51 file.write(contents)
52 file.close()
53 print "New datafile created: %s" % self.datafile_name
54
55 def getData(self):
56 '''Gets the formatted file as a dictionary
57 The # key contains the column headers for the file and indicates the 'primary' key'''
58 try:
59 file = open(self.filename,'rb')
60 except:
61 raise 'Unable to read %s' % self.filename
62 else:
63 temp = file.read().split('\n')
64 file.close()
65 contents = temp[:-1]
66 DF = {}
67 templist = []
68 for list in contents:
69 templist = list.split('|')
70 DF[templist[0]] = templist[1:]
71 return DF
72
73 def putData(self, dic):
74 '''Writes dictionary to formatted file - uses | character as a delimiter'''
75 try:
76 file = open(self.filename,'w')
77 except:
78 raise 'Unable to open %s for writing' % self.datafile_name
79 else:
80 header = dic['#']
81 del dic['#']
82 index = dic.keys()
83 index.sort()
84 contents = '#|%s\n' %(string.join(header,'|'))
85 file.write(contents)
86 for entry in index:
87 tmp = []
88 stringlist = dic[entry]
89 for item in stringlist:
90 tmp.append(str(item))
91 temp = '%s|%s\n' %(entry, (string.join(tmp,'|')))
92 file.write(temp)
93 file.close()
94
95 class CFData:
96 '''CFData Object is basically a dictionary parsed from the datafile -
97 serves to pass back and forth a dictionary containing the datafile header
98 and the desired record to the caller - easier to read the '''
99
100 def __init__(self, filename, header):
101 self.header = header
102 self.datafile = CFDataFile(filename)
103
104 if self.datafile.exists():
105 self.datadb = self.datafile.getData()
106 if self.datadb['#'] != self.header:
107 # see if header in calling object matches header in file
108 # raise an alert but do nothing yet -
109 # indicates possible upgrade of caller, will flesh this out later
110 raise 'Header does not match! You may need to fix the object or the datafile.'
111 else:
112 self.datafile.make_file(self.header)
113 self.datadb = self.datafile.getData()
114
115 def remove_record(self, name):
116 if self.datadb.has_key(name):
117 del self.datadb[name]
118 self.datafile.putData(self.datadb)
119 return 1
120 else:
121 return 0
122
123 def exist(self, name):
124 '''checks if a record exists given the primary key as "name"'''
125 if self.datadb.has_key(name):
126 return 1
127 else:
128 return 0
129
130 def get_record(self, name):
131 '''returns a small dictionary of the header and the record by "name"'''
132 if self.exist(name):
133 record = {}
134 for header, item in zip(self.header,self.datadb[name]):
135 record[header]=item
136 record['#'] = name
137 return record
138 else:
139 return 0
140
141 def put_record(self, record):
142 '''adds an line entry to the datafile'''
143 name = record['#']
144 del record['#']
145 temp = []
146 for item in self.header:
147 temp.append(record[item])
148 self.datadb[name]=temp
149 self.datafile.putData(self.datadb)
150
151 def get_keys(self):
152 '''returns a sorted list of the primary keys (usually names) in the datafile'''
153 keys = self.datadb.keys()
154 keys.remove('#')
155 keys.sort()
156 return keys
157