def tag_occ(self, tag, data): if isinstance(data, tuple): utils.debbuging(tag) utils.debbuging(data) s = '' elif isinstance(data, dict): s = self.tag_content(tag, self.format_subfields(data)) else: s = self.tag_content(tag, data) return s
def save(self, filename, records): path = os.path.dirname(filename) if not os.path.isdir(path): os.makedirs(path) content = self._format_file(records) if isinstance(content, unicode): content = u_encode(content, 'iso-8859-1') content = content.replace('<PRESERVECIRC/>', '^') try: open(filename, 'w').write(content) except Exception as e: utils.debbuging('saving...') utils.debbuging(e) print(e)
def format_subfields(self, subf_and_value_list): try: first = u'' value = u'' #for k, v in subf_and_value_list.items(): for k in sorted(subf_and_value_list.keys()): v = subf_and_value_list[k] if v is not None and v != '' and len(k) == 1: v = format_value(v) v = change_circ(v) if k in 'abcdefghijklmnopqrstuvwxyz123456789': value += u'^' + k + v elif k in '_': first = v except Exception as e: utils.debbuging('format_subfields') utils.debbuging(e) utils.debbuging(subf_and_value_list) utils.debbuging(first + value) return first + value
def format_value(content): try: if not isinstance(content, unicode): content = content.decode('utf-8') except Exception as e: utils.debbuging('format_value 1:') utils.debbuging(e) utils.debbuging(content) try: content = xml_utils.remove_break_lines_characters(content) except Exception as e: utils.debbuging('format_value: remove_break_lines_characters:') utils.debbuging(e) utils.debbuging(content) try: if '&' in content: content, replace = xml_utils.convert_entities_to_chars(content) except Exception as e: utils.debbuging('format_value: convert_entities_to_chars:') utils.debbuging(e) utils.debbuging(content) try: if not isinstance(content, unicode): content = content.decode('utf-8') except Exception as e: utils.debbuging('format_value: 2:') utils.debbuging(e) utils.debbuging(content) return content.strip()
def tag_content(self, tag, value): r = '' s = value if int(tag) <= 999: if value is not None and value != '': try: tag = '000' + tag tag = tag[-3:] value = format_value(value) #value = change_circ(value) r = '!v' + tag + '!' + value + '\n' except Exception as e: utils.debbuging('tag_content: ') utils.debbuging(e) utils.debbuging(s) utils.debbuging(value) utils.debbuging(type(s)) utils.debbuging(type(value)) return r