| 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 Crossfire |
| 22 |
|
| 23 |
class CFDataFile: |
| 24 |
'''Plain text storage for Crossfire data''' |
| 25 |
|
| 26 |
def __init__(self, datafile_name): |
| 27 |
'''make datafile paths for datafile 'object' |
| 28 |
- these all go in ../var/crossfire/datafiles to keep the local dir clean''' |
| 29 |
self.datafile_name = datafile_name |
| 30 |
self.filename = os.path.join((Crossfire.LocalDirectory()),'datafiles',datafile_name) |
| 31 |
|
| 32 |
def exists(self): |
| 33 |
'''checks for datafile - no need to load it yet''' |
| 34 |
if os.path.isfile(self.filename): |
| 35 |
return 1 |
| 36 |
else: |
| 37 |
return 0 |
| 38 |
|
| 39 |
def make_file(self, header): |
| 40 |
'''creates a datafile, making the column header from a list passed in''' |
| 41 |
try: |
| 42 |
file = open(self.filename,'wt') |
| 43 |
except: |
| 44 |
Crossfire.Log(Crossfire.LogError, "Can't create datafile %s" % self.datafile_name) |
| 45 |
else: |
| 46 |
temp = [] |
| 47 |
for item in header: |
| 48 |
temp.append(str(item)) |
| 49 |
contents = '#|%s\n' %('|'.join(temp)) |
| 50 |
file.write(contents) |
| 51 |
file.close() |
| 52 |
Crossfire.Log(Crossfire.LogInfo, "New datafile created: %s" % self.datafile_name) |
| 53 |
|
| 54 |
def getData(self): |
| 55 |
'''Gets the formatted file as a dictionary |
| 56 |
The # key contains the column headers for the file and indicates the 'primary' key''' |
| 57 |
try: |
| 58 |
file = open(self.filename,'rt') |
| 59 |
except: |
| 60 |
raise Exception('Unable to read %s' % self.filename) |
| 61 |
else: |
| 62 |
temp = file.read().split('\n') |
| 63 |
file.close() |
| 64 |
contents = temp[:-1] |
| 65 |
DF = {} |
| 66 |
templist = [] |
| 67 |
for list in contents: |
| 68 |
templist = list.split('|') |
| 69 |
DF[templist[0]] = templist[1:] |
| 70 |
return DF |
| 71 |
|
| 72 |
def putData(self, dic): |
| 73 |
'''Writes dictionary to formatted file - uses | character as a delimiter''' |
| 74 |
try: |
| 75 |
file = open(self.filename,'wt') |
| 76 |
except: |
| 77 |
raise Exception('Unable to open %s for writing' % self.datafile_name) |
| 78 |
else: |
| 79 |
header = dic['#'] |
| 80 |
del dic['#'] |
| 81 |
index = list(dic.keys()) |
| 82 |
index.sort() |
| 83 |
contents = '#|%s\n' %('|'.join(header)) |
| 84 |
file.write(contents) |
| 85 |
for entry in index: |
| 86 |
tmp = [] |
| 87 |
stringlist = dic[entry] |
| 88 |
for item in stringlist: |
| 89 |
tmp.append(str(item)) |
| 90 |
temp = '%s|%s\n' %(entry, ('|'.join(tmp))) |
| 91 |
file.write(temp) |
| 92 |
file.close() |
| 93 |
|
| 94 |
class CFData: |
| 95 |
'''CFData Object is basically a dictionary parsed from the datafile - |
| 96 |
serves to pass back and forth a dictionary containing the datafile header |
| 97 |
and the desired record to the caller - easier to read the ''' |
| 98 |
|
| 99 |
def __init__(self, filename, header): |
| 100 |
self.header = header |
| 101 |
self.datafile = CFDataFile(filename) |
| 102 |
|
| 103 |
if self.datafile.exists(): |
| 104 |
self.datadb = self.datafile.getData() |
| 105 |
if self.datadb['#'] != self.header: |
| 106 |
# see if header in calling object matches header in file |
| 107 |
# raise an alert but do nothing yet - |
| 108 |
# indicates possible upgrade of caller, will flesh this out later |
| 109 |
raise Exception('Header does not match! You may need to fix the object or the datafile.') |
| 110 |
else: |
| 111 |
self.datafile.make_file(self.header) |
| 112 |
self.datadb = self.datafile.getData() |
| 113 |
|
| 114 |
def remove_record(self, name): |
| 115 |
if name in self.datadb: |
| 116 |
del self.datadb[name] |
| 117 |
self.datafile.putData(self.datadb) |
| 118 |
return 1 |
| 119 |
else: |
| 120 |
return 0 |
| 121 |
|
| 122 |
def exist(self, name): |
| 123 |
'''checks if a record exists given the primary key as "name"''' |
| 124 |
if name in self.datadb: |
| 125 |
return 1 |
| 126 |
else: |
| 127 |
return 0 |
| 128 |
|
| 129 |
def get_record(self, name): |
| 130 |
'''returns a small dictionary of the header and the record by "name"''' |
| 131 |
if self.exist(name): |
| 132 |
record = {} |
| 133 |
for header, item in zip(self.header,self.datadb[name]): |
| 134 |
record[header]=item |
| 135 |
record['#'] = name |
| 136 |
return record |
| 137 |
else: |
| 138 |
return 0 |
| 139 |
|
| 140 |
def put_record(self, record): |
| 141 |
'''adds an line entry to the datafile''' |
| 142 |
name = record['#'] |
| 143 |
del record['#'] |
| 144 |
temp = [] |
| 145 |
for item in self.header: |
| 146 |
temp.append(record[item]) |
| 147 |
self.datadb[name]=temp |
| 148 |
self.datafile.putData(self.datadb) |
| 149 |
|
| 150 |
def get_keys(self): |
| 151 |
'''returns a sorted list of the primary keys (usually names) in the datafile''' |
| 152 |
keys = list(self.datadb.keys()) |
| 153 |
keys.remove('#') |
| 154 |
keys.sort() |
| 155 |
return keys |