Esempio n. 1
0
def sml_import(xmlfile, user):
    filename = xmlfile.filename
    tree = objectify.parse(xmlfile).getroot()
    move = parse_move(tree)
    move.source = os.path.abspath(filename)
    move.import_module = __name__
    device = parse_device(tree)
    persistent_device = Device.query.filter_by(serial_number=device.serial_number).scalar()
    if persistent_device:
        if not persistent_device.name:
            flash("update device name to '%s'" % device.name)
            persistent_device.name = device.name
        else:
            assert device.name == persistent_device.name
        device = persistent_device
    else:
        db.session.add(device)

    if Move.query.filter_by(user=user, date_time=move.date_time, device=device).scalar():
        flash("%s at %s already exists" % (move.activity, move.date_time), 'warning')
    else:
        move.user = user
        move.device = device
        db.session.add(move)

        samples = tree.DeviceLog.Samples.iterchildren()
        for sample in parse_samples(samples, move):
            db.session.add(sample)
        postprocess_move(move)
        db.session.commit()
        return move
Esempio n. 2
0
def sml_import(xmlfile, user, request_form):
    filename = xmlfile.name
    tree = objectify.parse(xmlfile).getroot()
    move = parse_move(tree)
    move.source = os.path.abspath(filename)
    move.import_module = __name__
    device = parse_device(tree)
    persistent_device = Device.query.filter_by(serial_number=device.serial_number).scalar()
    if persistent_device:
        if not persistent_device.name:
            flash("update device name to '%s'" % device.name)
            persistent_device.name = device.name
        else:
            assert device.name == persistent_device.name
        device = persistent_device
    else:
        db.session.add(device)

    if Move.query.filter_by(user=user, date_time=move.date_time, device=device).scalar():
        flash("%s at %s already exists" % (move.activity, move.date_time), 'warning')
    else:
        move.user = user
        move.device = device
        db.session.add(move)

        samples = tree.DeviceLog.Samples.iterchildren()
        for sample in parse_samples(samples, move):
            db.session.add(sample)

        postprocess_move(move)

        db.session.commit()
        return move
Esempio n. 3
0
def old_xml_import(xmlfile, user, request_form):
    filename = xmlfile.filename
    data = xmlfile.readlines()

    if isinstance(data[0], bytes):
        data = [str(x.decode('utf-8')) for x in data]

    data[0] = data[0] + "<sml>"
    data.append("</sml>")

    filematch = re.match(
        r'log-([A-F0-9]{16})-\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}-\d+\.xml',
        filename)
    if not filematch:
        flash("illegal filename: '%s'" % filename, 'error')
        return

    serial_number = filematch.group(1)

    tree = objectify.fromstring("\n".join(data).encode('utf-8'))
    move = parse_move(tree)
    move.source = filename
    move.import_module = __name__

    device = Device.query.filter_by(serial_number=serial_number).scalar()
    if not device:
        device = Device()
        device.serial_number = serial_number
        db.session.add(device)

    if Move.query.filter_by(user=user, date_time=move.date_time,
                            device=device).scalar():
        flash("%s at %s already exists" % (move.activity, move.date_time),
              'warning')
    else:
        move.user = user
        move.device = device
        db.session.add(move)

        for sample in parse_samples(tree.Samples.iterchildren(), move):
            db.session.add(sample)

        postprocess_move(move)

        db.session.commit()
        return move
Esempio n. 4
0
def old_xml_import(xmlfile, user, request_form):
        filename = xmlfile.filename
        data = xmlfile.readlines()

        if isinstance(data[0], bytes):
            data = [str(x.decode('utf-8')) for x in data]

        data[0] = data[0] + "<sml>"
        data.append("</sml>")

        filematch = re.match(r'log-([A-F0-9]{16})-\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}-\d+\.xml', filename)
        if not filematch:
            flash("illegal filename: '%s'" % filename, 'error')
            return

        serial_number = filematch.group(1)

        tree = objectify.fromstring("\n".join(data).encode('utf-8'))
        move = parse_move(tree)
        move.source = filename
        move.import_module = __name__

        device = Device.query.filter_by(serial_number=serial_number).scalar()
        if not device:
            device = Device()
            device.serial_number = serial_number
            db.session.add(device)

        if Move.query.filter_by(user=user, date_time=move.date_time, device=device).scalar():
            flash("%s at %s already exists" % (move.activity, move.date_time), 'warning')
        else:
            move.user = user
            move.device = device
            db.session.add(move)

            for sample in parse_samples(tree.Samples.iterchildren(), move):
                db.session.add(sample)

            postprocess_move(move)

            db.session.commit()
            return move