24 '''Plain text storage for Crossfire data'''
27 '''make datafile paths for datafile 'object'
28 - these all go in ../var/crossfire/datafiles to keep the local dir clean'''
30 self.
filename = os.path.join((Crossfire.LocalDirectory()),
'datafiles',datafile_name)
33 '''checks for datafile - no need to load it yet'''
40 '''creates a datafile, making the column header from a list passed in'''
44 Crossfire.Log(Crossfire.LogError,
"Can't create datafile %s" % self.
datafile_name)
48 temp.append(
str(item))
49 contents =
'#|%s\n' %(
'|'.join(temp))
52 Crossfire.Log(Crossfire.LogInfo,
"New datafile created: %s" % self.
datafile_name)
55 '''Gets the formatted file as a dictionary
56 The # key contains the column headers for the file and indicates the 'primary' key'''
60 raise Exception(
'Unable to read %s' % self.
filename)
62 temp = file.read().
split(
'\n')
68 templist = list.split(
'|')
69 DF[templist[0]] = templist[1:]
73 '''Writes dictionary to formatted file - uses | character as a delimiter'''
77 raise Exception(
'Unable to open %s for writing' % self.
datafile_name)
81 index =
list(dic.keys())
83 contents =
'#|%s\n' %(
'|'.join(header))
87 stringlist = dic[entry]
88 for item
in stringlist:
90 temp =
'%s|%s\n' %(entry, (
'|'.join(tmp)))
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 '''
109 raise Exception(
'Header does not match! You may need to fix the object or the datafile.')
123 '''checks if a record exists given the primary key as "name"'''
130 '''returns a small dictionary of the header and the record by "name"'''
141 '''adds an line entry to the datafile'''
146 temp.append(record[item])
151 '''returns a sorted list of the primary keys (usually names) in the datafile'''