Esempio n. 1
0
    def handle(self, fileName=None, *args, **options):
        if not fileName:
            raise CommandError('Enter a filename')

        fin = open(fileName,'rU')
        dialect = csv.Sniffer().sniff(fin.read(1024))
        fin.seek(0)
	
        
        # Open the file in a normal reader to see if the file is a REDCap data dictionary
        # A REDCap data Dictionary begins with the four necessary columns:
        # Variable/Field Name | Form Name | Field Type | Field Label in any order

        reader = _csv.UnicodeDictReader(fin, fieldnames=header_keys, encoding='latin-1', dialect=dialect)
        header = reader.next()
        
        if header['field_name'] != 'Variable / Field Name' or \
            header['form_name'] != 'Form Name' or \
            header['field_type'] != 'Field Type' or \
            header['field_label'] != 'Field Label':

            sys.stderr.write(fileName + '\n')
            sys.stderr.write(header['field_name'] + header['form_name'] + header['field_type'] + header['field_label'] + '\n')
            sys.stderr.write('Invalid header. File must be a valid REDCap data dictionary.\n')
            sys.stderr.flush() 
            sys.exit(1)
        
        if fileName.find('.json') == -1:
            fileName = djconvert.csv_2_json(self, reader, fileName)
        djconvert.json_2_dj(self, fileName, options['model_filename'])
Esempio n. 2
0
    def handle(self, fileName=None, *args, **options):
        if not fileName:
            raise CommandError("Enter a filename")

        fin = open(fileName, "rU")
        dialect = csv.Sniffer().sniff(fin.read(1024))
        fin.seek(0)

        # Open the file in a normal reader to see if the file is a REDCap data dictionary
        # A REDCap data Dictionary begins with the four necessary columns:
        # Variable/Field Name | Form Name | Field Type | Field Label in any order

        reader = _csv.UnicodeDictReader(fin, fieldnames=header_keys, encoding="latin-1", dialect=dialect)
        header = reader.next()

        if (
            header["field_name"] != "Variable / Field Name"
            or header["form_name"] != "Form Name"
            or header["field_type"] != "Field Type"
            or header["field_label"] != "Field Label"
        ):

            sys.stderr.write(fileName + "\n")
            sys.stderr.write(
                header["field_name"] + header["form_name"] + header["field_type"] + header["field_label"] + "\n"
            )
            sys.stderr.write("Invalid header. File must be a valid REDCap data dictionary.\n")
            sys.stderr.flush()
            sys.exit(1)

        if fileName.find(".json") == -1:
            fileName = djconvert.csv_2_json(self, reader, fileName)
        djconvert.json_2_dj(self, fileName, options["output_filename"])
Esempio n. 3
0
    def handle(self, file_name=None, *args, **options):
        if not file_name:
            raise CommandError('Enter a filename')

        json_filename = options.get('json')
        fin = open(file_name, 'rU')
        dialect = csv.Sniffer().sniff(fin.read(1024))
        fin.seek(0)
        reader = csv.DictReader(fin, fieldnames=header_keys, dialect=dialect)

        reader.next()

        file_name = djconvert.csv_2_json(self, reader, file_name)
        file_name1 = file_name
        djconvert.json_2_dj(self, file_name, options['output_filename'])
        if json_filename:
            os.rename(file_name1, json_filename)
        else:
            os.remove(os.path.join(os.path.dirname(file_name1), file_name1))
Esempio n. 4
0
    def handle(self, file_name=None, *args, **options):
        if not file_name:
            raise CommandError('Enter a filename')

        fin = open(file_name, 'rU')
        dialect = csv.Sniffer().sniff(fin.read(1024))
        fin.seek(0)
        reader = csv.DictReader(fin, fieldnames=header_keys, dialect=dialect)

        reader.next()
        fileName = djconvert.json_2_dj(self, file_name, options['output_filename'])