56 def __lt__(self, other): |
56 def __lt__(self, other): |
57 return self.repo_uid < other.repo_uid |
57 return self.repo_uid < other.repo_uid |
58 |
58 |
59 def __repr__(self): |
59 def __repr__(self): |
60 return "CenRepRfsRecord(repo_uid=%s, key_uids=%r)" % (self.repo_uid, self.key_uids) |
60 return "CenRepRfsRecord(repo_uid=%s, key_uids=%r)" % (self.repo_uid, self.key_uids) |
61 |
|
62 |
61 |
63 class CrmlTxtWriter(object): |
62 class CrmlTxtWriter(object): |
64 """ |
63 """ |
65 Writer class for generating CenRep .txt files based on a CRML model. |
64 Writer class for generating CenRep .txt files based on a CRML model. |
66 """ |
65 """ |
67 |
66 |
68 def __init__(self, configuration, log): |
67 def __init__(self, context, log): |
69 self.configuration = configuration |
68 self.context = context |
70 self.log = log |
69 self.log = log |
71 |
70 |
72 def get_cenrep_txt_data(self, repository): |
71 def get_cenrep_txt_data(self, repository, changed_refs=None): |
73 """ |
72 """ |
74 Return the text data for the CenRep txt generated based on the given |
73 Return the text data for the CenRep txt generated based on the given |
75 CRML repository model. |
74 CRML repository model. |
|
75 @param changed_refs: List of changed refs. If this is passed, only a delta |
|
76 CenRep txt file is generated (i.e. containing only the changed settings). |
|
77 If None, the whole CenRep file is generated normally. |
76 @return: Text data for the CenRep text file. |
78 @return: Text data for the CenRep text file. |
77 """ |
79 """ |
|
80 delta_cenrep = changed_refs is not None |
78 data = [] |
81 data = [] |
79 |
82 |
80 # Generate header lines |
83 # Generate header lines |
81 data.extend(self.get_header_lines(repository)) |
84 data.extend(self.get_header_lines(repository, delta_cenrep)) |
82 |
85 |
83 self._check_repository_attrs(repository) |
86 self._check_repository_attrs(repository) |
84 |
87 |
85 # Generate CenRep entries for all keys |
88 # Generate CenRep entries for all keys |
86 cenrep_entries = [] |
89 cenrep_entries = [] |
87 for key in repository.keys: |
90 for key in repository.keys: |
|
91 # If generating a delta CenRep file, ignore keys that don't |
|
92 # use any of the changed settings |
|
93 if delta_cenrep: |
|
94 if not plugin.uses_ref(changed_refs, key.get_refs()): |
|
95 continue |
88 cenrep_entries.extend(self.get_cenrep_entries(key)) |
96 cenrep_entries.extend(self.get_cenrep_entries(key)) |
89 |
97 |
90 # Generate entry lines based on the entries |
98 # Generate entry lines based on the entries |
91 cenrep_entries.sort() |
99 cenrep_entries.sort() |
92 for entry in cenrep_entries: |
100 for entry in cenrep_entries: |
93 data.append(self.get_cenrep_entry_line(entry)) |
101 data.append(self.get_cenrep_entry_line(entry, delta_cenrep)) |
94 |
102 |
95 data.append('') |
103 data.append('') |
96 |
104 |
97 # Remove Nones from the line list |
105 # Remove Nones from the line list |
98 data = filter(lambda val: val is not None, data) |
106 data = filter(lambda val: val is not None, data) |
177 # Feature not found in the configuration |
185 # Feature not found in the configuration |
178 return None |
186 return None |
179 |
187 |
180 return feature.get_value(attr='rfs') |
188 return feature.get_value(attr='rfs') |
181 |
189 |
182 def get_header_lines(self, repository): |
190 def get_header_lines(self, repository, delta_cenrep=False): |
183 """ |
191 """ |
184 Return a list of lines to be written in the header section of the CenRep text file. |
192 Return a list of lines to be written in the header section of the CenRep text file. |
|
193 @param delta_cenrep: If True, only the data needed for a delta CenRep |
|
194 file is returned. |
185 """ |
195 """ |
186 data = ['cenrep', |
196 data = ['cenrep', |
187 'version %s' % repository.version] |
197 'version %s' % repository.version] |
188 |
198 |
|
199 # Owner seems to be required even for delta CenReps |
189 if repository.owner: |
200 if repository.owner: |
190 data.append('[owner]') |
201 data.append('[owner]') |
191 data.append(repository.owner) |
202 data.append(repository.owner) |
192 |
203 |
193 data.append('[defaultmeta]') |
204 data.append('[defaultmeta]') |
194 data.append(' %d' % _get_metadata(repository.backup)) |
205 if not delta_cenrep: |
195 for key in repository.keys: |
206 data.append(' %d' % _get_metadata(repository.backup)) |
196 data.append(self.get_defaultmeta_line(key)) |
207 for key in repository.keys: |
|
208 data.append(self.get_defaultmeta_line(key)) |
197 |
209 |
198 data.append('[platsec]') |
210 data.append('[platsec]') |
199 acc_text = self.get_access_line(repository.access) |
211 if not delta_cenrep: |
200 if acc_text: acc_text = ' ' + acc_text |
212 acc_text = self.get_access_line(repository.access) |
201 data.append(acc_text) |
213 if acc_text: acc_text = ' ' + acc_text |
202 for key in repository.keys: |
214 data.append(acc_text) |
|
215 for key in repository.keys: |
203 data.append(self.get_platsec_line(key, repository)) |
216 data.append(self.get_platsec_line(key, repository)) |
204 |
|
205 |
217 |
206 data.append('[Main]') |
218 data.append('[Main]') |
207 return data |
219 return data |
208 |
220 |
209 def get_cenrep_entries(self, key): |
221 def get_cenrep_entries(self, key): |
367 return "%s %s%s" % (key.first_int, |
379 return "%s %s%s" % (key.first_int, |
368 key.last_int, |
380 key.last_int, |
369 acc_text) |
381 acc_text) |
370 |
382 |
371 |
383 |
372 def get_cenrep_entry_line(self, entry): |
384 def get_cenrep_entry_line(self, entry, delta_cenrep=False): |
373 """ |
385 """ |
374 Return the text line for a CenRepEntry object. |
386 Return the text line for a CenRepEntry object. |
|
387 @param delta_cenrep: If True, only the data needed for a delta CenRep |
|
388 file is returned. |
375 """ |
389 """ |
376 value = None |
390 value = None |
377 if entry.crml_type in ('string', 'string8'): |
391 if entry.crml_type in ('string', 'string8'): |
378 if entry.confml_type is None: |
392 if entry.confml_type is None: |
379 pass |
393 pass |
402 if value is None: |
416 if value is None: |
403 value = unicode(entry.value) |
417 value = unicode(entry.value) |
404 |
418 |
405 self._check_value(entry, value) |
419 self._check_value(entry, value) |
406 |
420 |
407 acc_text = self.get_access_line(entry.access) |
421 if delta_cenrep: |
408 if acc_text: acc_text = ' ' + acc_text |
422 return '%s %s %s' % (_translate_key_uid(entry.int), |
409 |
423 entry.crml_type, |
410 return '%s %s %s %d%s' % (_translate_key_uid(entry.int), |
424 value) |
411 entry.crml_type, |
425 else: |
412 value, |
426 acc_text = self.get_access_line(entry.access) |
413 entry.metadata, |
427 if acc_text: acc_text = ' ' + acc_text |
414 acc_text) |
428 |
|
429 return '%s %s %s %d%s' % (_translate_key_uid(entry.int), |
|
430 entry.crml_type, |
|
431 value, |
|
432 entry.metadata, |
|
433 acc_text) |
|
434 |
415 def _check_value(self, entry, value): |
435 def _check_value(self, entry, value): |
416 """ |
436 """ |
417 Check that the given value is valid for the given CenRep entry, |
437 Check that the given value is valid for the given CenRep entry, |
418 and log a warning if it is not. |
438 and log a warning if it is not. |
419 """ |
439 """ |
474 data.append('%s=%s' % (varname, _translate_capability_string(val))) |
494 data.append('%s=%s' % (varname, _translate_capability_string(val))) |
475 |
495 |
476 return ' '.join(data) |
496 return ' '.join(data) |
477 |
497 |
478 def _get_feature(self, ref): |
498 def _get_feature(self, ref): |
479 return self.configuration.get_default_view().get_feature(ref) |
499 return self.context.configuration.get_default_view().get_feature(ref) |
480 |
500 |
481 @classmethod |
501 @classmethod |
482 def get_index(cls,firstInt,firstIndex,indexBits,seqIndex, subIndex): |
502 def get_index(cls,firstInt,firstIndex,indexBits,seqIndex, subIndex): |
483 """ |
503 """ |
484 @param firstIndex: The first value available in the keyrange |
504 @param firstIndex: The first value available in the keyrange |