- Timestamp:
- 13 Feb 2011, 01:27:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/applicants/jambtables/jambtables.py
r5722 r5743 38 38 IJAMBDataTable, IJAMBDataRoot 39 39 ) 40 41 #: The header fields required for a valid JAMB table CSV file.42 JAMB_DATA_HEADERS = [43 'firstname', 'lastname', 'middlenames', 'screening_type',44 'screening_venue', 'reg_no', 'sex', 'course1', 'jamb_state',45 'screening_date', 'jamb_lga', 'fst_sit_fname', 'date_of_birth']46 47 def filter_data(datadict):48 """Filter items whose key contains 'ignore'49 50 The function will remove all fields whose name start with51 ``ignore``. All data will be convertetd to unicode except52 ``data_of_birth``, which is turned into a `dateteime` object.53 """54 keys = datadict.keys()55 for key in keys:56 if not 'ignore' in key:57 continue58 del datadict[key]59 for name in JAMB_DATA_HEADERS:60 datadict[name] = unicode(datadict[name])61 datadict['date_of_birth'] = datetime.strptime(62 datadict['date_of_birth'],63 '%d/%m/%Y'64 ).date()65 66 return datadict67 68 69 class JAMBDataTable(grok.Model):70 """A data table that contains JAMB data.71 72 JAMB data tables are plain but fast as they store nearly no data73 inside the ZODB. All data is held on-disk in CSV tables.74 75 As a consequence these tables are read-only.76 """77 #: Implement :class:`IJAMBDataTable`78 grok.implements(IJAMBDataTable)79 80 #: The datetime when data was imported.81 import_datetime = None82 83 #: The username of the person that initiated the last import.84 importer_username = None85 86 def __init__(self):87 super(JAMBDataTable, self).__init__()88 self._datafile_path = None89 self._data_len = 090 self._temporary = False91 return92 93 def __del__(self):94 self.clear()95 96 def __iter__(self):97 reader = None98 if self._datafile_path is not None:99 reader = csv.DictReader(open(self._datafile_path, 'rb'))100 if reader is None:101 raise StopIteration102 for line in reader:103 data_dict = filter_data(line)104 yield data_dict105 106 def keys(self):107 """Get iterator over all registration numbers stored in table.108 """109 for item in self:110 yield item['reg_no']111 112 def items(self):113 """Get tuples of registration number and datasets for each entry in114 data table.115 """116 for item in self:117 yield (item['reg_no'], item)118 119 def clear(self):120 """Remove all existing entries.121 122 Unsets also the :attr:`import_datetime` and123 :attr:`importer_username` attributes.124 """125 self.import_datetime = None126 self.importer_username = None127 if self._datafile_path is None:128 return129 if self._temporary:130 if not os.path.exists(self._datafile_path):131 return132 shutil.rmtree(os.path.dirname(self._datafile_path))133 self._datafile_path = None134 135 def importFromCSV(self, filepath, username=None):136 """Importing data from a CSV file means to copy the source to a safe137 location.138 139 If the username is set, it will be stored as well in140 :attr:`importer_username`.141 """142 self.clear()143 self.import_datetime = datetime.now()144 self.importer_username = None145 if username is not None:146 self.importer_username = unicode(username)147 self._copyDataFile(filepath)148 149 def _copyDataFile(self, path):150 """Copy file in path to the JAMBData storage.151 152 See :meth:`_getJAMBTableStorage`.153 """154 storage = self._getJAMBTableStorage()155 self._datafile_path = os.path.join(156 storage, os.path.basename(path)157 )158 shutil.copy2(path, self._datafile_path)159 return160 161 def _getJAMBTableStorage(self):162 """Get a path to store copies of datatables.163 164 We normally store data in a ``jambdata`` subdir of datacenter,165 but if none exists, we create a temporary dir and set166 `temporary` to ``True``.167 168 Any not existent directory is created.169 170 Note, that temporary dirs will be deleted when the171 JAMBDataTable object is destroyed.172 173 Returns absolute path to the JAMB data storage.174 """175 site = grok.getSite()176 if site is None:177 jambtable_storage = tempfile.mkdtemp()178 self._temporary = True179 else:180 datacenter = site['datacenter']181 jambtable_storage = os.path.join(datacenter.storage, 'jambdata')182 if not os.path.isdir(jambtable_storage):183 os.mkdir(jambtable_storage)184 return os.path.abspath(jambtable_storage)185 40 186 41 class JAMBDataRoot(grok.Container):
Note: See TracChangeset for help on using the changeset viewer.