def extract_cif(infile, folder, nodes_export_subfolder="nodes", aiida_export_subfolder="aiida", silent=False): """ Extract the nodes to be imported from a TCOD CIF file. TCOD CIFs, exported by AiiDA, may contain an importable subset of AiiDA database, which can be imported. This function prepares SandboxFolder with files required for import. :param infile: file path :param folder: a SandboxFolder, used to extract the file tree :param nodes_export_subfolder: name of the subfolder for AiiDA nodes :param aiida_export_subfolder: name of the subfolder for AiiDA data inside the TCOD CIF internal file tree :param silent: suppress debug print """ import os import urllib2 import CifFile from aiida.common.exceptions import ValidationError from aiida.common.utils import md5_file, sha1_file from aiida.tools.dbexporters.tcod import decode_textfield values = CifFile.ReadCif(infile) values = values[values.keys()[0]] # taking the first datablock in CIF for i in range(0,len(values['_tcod_file_id'])-1): name = values['_tcod_file_name'][i] if not name.startswith(aiida_export_subfolder+os.sep): continue dest_path = os.path.relpath(name,aiida_export_subfolder) if name.endswith(os.sep): if not os.path.exists(folder.get_abs_path(dest_path)): folder.get_subfolder(folder.get_abs_path(dest_path),create=True) continue contents = values['_tcod_file_contents'][i] if contents == '?' or contents == '.': uri = values['_tcod_file_uri'][i] if uri is not None and uri != '?' and uri != '.': contents = urllib2.urlopen(uri).read() encoding = values['_tcod_file_content_encoding'][i] if encoding == '.': encoding = None contents = decode_textfield(contents,encoding) if os.path.dirname(dest_path) != '': folder.get_subfolder(os.path.dirname(dest_path)+os.sep,create=True) with open(folder.get_abs_path(dest_path),'w') as f: f.write(contents) f.flush() md5 = values['_tcod_file_md5sum'][i] if md5 is not None: if md5_file(folder.get_abs_path(dest_path)) != md5: raise ValidationError("MD5 sum for extracted file '{}' is " "different from given in the CIF " "file".format(dest_path)) sha1 = values['_tcod_file_sha1sum'][i] if sha1 is not None: if sha1_file(folder.get_abs_path(dest_path)) != sha1: raise ValidationError("SHA1 sum for extracted file '{}' is " "different from given in the CIF " "file".format(dest_path))
def check_gzip_base64(self, text): from aiida.tools.dbexporters.tcod import ( encode_textfield_gzip_base64, decode_textfield_gzip_base64) encoded = encode_textfield_gzip_base64(text) decoded = decode_textfield_gzip_base64(encoded) decoded_universal = decode_textfield(encoded, 'gzip+base64') self.assertEquals(text, decoded) self.assertEquals(text, decoded_universal)
def check_base64(self, inp, out): from aiida.tools.dbexporters.tcod import (encode_textfield_base64, decode_textfield_base64) encoded = encode_textfield_base64(inp) decoded = decode_textfield_base64(out) decoded_universal = decode_textfield(out, 'base64') self.assertEquals(encoded, out) self.assertEquals(decoded, inp) self.assertEquals(decoded_universal, inp)
def check_ncr(self, inp, out): from aiida.tools.dbexporters.tcod import (encode_textfield_ncr, decode_textfield_ncr) encoded = encode_textfield_ncr(inp) decoded = decode_textfield_ncr(out) decoded_universal = decode_textfield(out, 'ncr') self.assertEquals(encoded, out) self.assertEquals(decoded, inp) self.assertEquals(decoded_universal, inp)
def test_quoted_printable(self, inp, out): from aiida.tools.dbexporters.tcod import (encode_textfield_quoted_printable, decode_textfield_quoted_printable) encoded = encode_textfield_quoted_printable(inp) decoded = decode_textfield_quoted_printable(out) decoded_universal = decode_textfield(out, 'quoted-printable') self.assertEquals(encoded, out) self.assertEquals(decoded, inp) self.assertEquals(decoded_universal, inp)