Beispiel #1
0
def sanitise_filename(name):
    assert is_string(name)

    # strip the parent directory name
    name = os.path.basename(name)

    # replace all non-ASCII or dangerous characters
    if is_bytes(name):
        name = re.sub(br"[^-_.a-zA-Z0-9]", "_", name)

        # convert to unicode string
        name = name.decode("ascii")
    else:
        name = re.sub(r"[^-_.a-zA-Z0-9]", "_", name)

    # dots at the beginning of a file name are "special", remove them
    name = name.lstrip(".")

    # normalise to lower case
    name = name.lower()

    # empty file names are illegal, replace
    if name == "":
        name = u"empty"
    return name
Beispiel #2
0
def sanitise_filename(name):
    assert is_string(name)

    # strip the parent directory name
    name = os.path.basename(name)

    # replace all non-ASCII or dangerous characters
    if is_bytes(name):
        name = re.sub(br"[^-_.a-zA-Z0-9]", "_", name)

        # convert to unicode string
        name = name.decode("ascii")
    else:
        name = re.sub(r"[^-_.a-zA-Z0-9]", "_", name)

    # dots at the beginning of a file name are "special", remove them
    name = name.lstrip(".")

    # normalise to lower case
    name = name.lower()

    # empty file names are illegal, replace
    if name == "":
        name = u"empty"
    return name
Beispiel #3
0
def delete_file(name):
    assert is_string(name)

    path = filename_to_path(name)
    try:
        os.unlink(path)
    except OSError:
        pass
Beispiel #4
0
def delete_file(name):
    assert is_string(name)

    path = filename_to_path(name)
    try:
        os.unlink(path)
    except OSError:
        pass
Beispiel #5
0
def date(date):
    try:
        if is_string(date):
            date = datetime.strptime(date, "%Y-%m-%d")

        if isinstance(date, datetime):
            date = date.date()

    except:
        return jsonify(), 404

    return _create_list(date=date, default_sorting_column='score', default_sorting_order='desc')
Beispiel #6
0
def date(date):
    try:
        if is_string(date):
            date = datetime.strptime(date, "%Y-%m-%d")

        if isinstance(date, datetime):
            date = date.date()

    except:
        return jsonify(), 404

    return _create_list(date=date,
                        default_sorting_column='score',
                        default_sorting_order='desc')
def flight_path(igc_file, max_points=1000, add_elevation=False, qnh=None):
    if isinstance(igc_file, IGCFile):
        path = files.filename_to_path(igc_file.filename)
    elif is_string(igc_file):
        path = igc_file
    else:
        return None

    output = run_flight_path(path, max_points=max_points, qnh=qnh)

    if add_elevation and len(output):
        output = get_elevation(output)

    return list(FlightPathFix(*line) for line in output)
Beispiel #8
0
def add_file(name, f):
    assert is_string(name)

    while True:
        path = filename_to_path(name)
        if not os.access(path, os.F_OK):
            break
        name = next_filename(name)

    dest = open(path, "w")
    shutil.copyfileobj(f, dest)
    dest.close()

    return name
Beispiel #9
0
def flight_path(igc_file, max_points=1000, add_elevation=False, qnh=None):
    if isinstance(igc_file, IGCFile):
        path = files.filename_to_path(igc_file.filename)
    elif is_string(igc_file):
        path = igc_file
    else:
        return None

    output = run_flight_path(path, max_points=max_points, qnh=qnh)

    if add_elevation and len(output):
        output = get_elevation(output)

    return map(lambda line: FlightPathFix(*line), output)
Beispiel #10
0
def add_file(name, f):
    assert is_string(name)

    while True:
        path = filename_to_path(name)
        if not os.access(path, os.F_OK):
            break
        name = next_filename(name)

    dest = open(path, 'w')
    shutil.copyfileobj(f, dest)
    dest.close()

    return name
Beispiel #11
0
def next_filename(name):
    assert is_string(name)

    i = name.rfind(".")

    match = igc_filename_numbers_regex.match(name[:i])

    try:
        number = int(match.group(2))
        return "%s_%i%s" % (match.group(1), number + 1, name[i:])

    except AttributeError:
        pass

    return "%s_1%s" % (name[:i], name[i:])
Beispiel #12
0
def next_filename(name):
    assert is_string(name)

    i = name.rfind('.')

    match = igc_filename_numbers_regex.match(name[:i])

    try:
        number = int(match.group(2))
        return "%s_%i%s" % (match.group(1), number + 1, name[i:])

    except AttributeError:
        pass

    return "%s_1%s" % (name[:i], name[i:])
Beispiel #13
0
def add_file(name, f):
    assert is_string(name)

    while True:
        path = filename_to_path(name)
        if not os.access(path, os.F_OK):
            break
        name = next_filename(name)

    dest = open(path, "wb")
    shutil.copyfileobj(f, dest)
    dest.close()
    # modtime = os.path.getmtime(path) #bch
    # createtime = os.path.getctime(path) #bch

    # return name,modtime,createtime #bch
    return name
Beispiel #14
0
def read_igc_headers(f):
    """ Read IGC file headers from a file-like object, a list of strings or a
    file if the parameter is a path. """

    if is_string(f):
        try:
            f = open(f, "rb")
        except IOError:
            return None

    igc_headers = dict()

    for i, line in enumerate(f):
        assert is_bytes(line)

        if line.startswith(b"A"):
            length = len(line)

            if length >= 4:
                igc_headers["manufacturer_id"] = line[1:4].decode("ascii")

            if length >= 7:
                igc_headers["logger_id"] = parse_logger_id(line)

        if line.startswith(b"HFDTE"):
            igc_headers["date_utc"] = parse_date(line)

        if line.startswith(b"HFGTY"):
            igc_headers["model"] = parse_pattern(hfgty_re, line)

        if line.startswith(b"HFGID"):
            igc_headers["reg"] = parse_pattern(hfgid_re, line)

        if line.startswith(b"HFCID"):
            igc_headers["cid"] = parse_pattern(hfcid_re, line)

        # don't read more than 100 lines, that should be enough
        if i > 100:
            break

    return igc_headers
Beispiel #15
0
def read_igc_headers(f):
    """ Read IGC file headers from a file-like object, a list of strings or a
    file if the parameter is a path. """

    if is_string(f):
        try:
            f = open(f, 'rb')
        except IOError:
            return None

    igc_headers = dict()

    for i, line in enumerate(f):
        assert is_bytes(line)

        if line.startswith(b'A'):
            length = len(line)

            if length >= 4:
                igc_headers['manufacturer_id'] = line[1:4].decode('ascii')

            if length >= 7:
                igc_headers['logger_id'] = parse_logger_id(line)

        if line.startswith(b'HFDTE'):
            igc_headers['date_utc'] = parse_date(line)

        if line.startswith(b'HFGTY'):
            igc_headers['model'] = parse_pattern(hfgty_re, line)

        if line.startswith(b'HFGID'):
            igc_headers['reg'] = parse_pattern(hfgid_re, line)

        if line.startswith(b'HFCID'):
            igc_headers['cid'] = parse_pattern(hfcid_re, line)

        # don't read more than 100 lines, that should be enough
        if i > 100:
            break

    return igc_headers
Beispiel #16
0
def read_igc_headers(f):
    """ Read IGC file headers from a file-like object, a list of strings or a
    file if the parameter is a path. """

    if is_string(f):
        try:
            f = open(f, "rb")
        except IOError:
            return None

    igc_headers = dict()

    for i, line in enumerate(f):
        assert is_bytes(line)

        if line.startswith(b"A"):
            length = len(line)

            if length >= 4:
                igc_headers["manufacturer_id"] = line[1:4].decode("ascii")

            if length >= 7:
                igc_headers["logger_id"] = parse_logger_id(line)

        if line.startswith(b"HFDTE"):
            igc_headers["date_utc"] = parse_date(line)

        if line.startswith(b"HFGTY"):
            igc_headers["model"] = parse_pattern(hfgty_re, line)

        if line.startswith(b"HFGID"):
            igc_headers["reg"] = parse_pattern(hfgid_re, line)

        if line.startswith(b"HFCID"):
            igc_headers["cid"] = parse_pattern(hfcid_re, line)

        # don't read more than 100 lines, that should be enough
        if i > 100:
            break

    return igc_headers
Beispiel #17
0
def sanitise_filename(name):
    assert is_string(name)

    # strip the parent directory name
    name = os.path.basename(name)

    # replace all non-ASCII or dangerous characters
    name = re.sub(r'[^-_.a-zA-Z0-9]', '_', name)

    # convert to unicode string
    name = unicode(name)

    # dots at the beginning of a file name are "special", remove them
    name = name.lstrip('.')

    # normalise to lower case
    name = name.lower()

    # empty file names are illegal, replace
    if name == '':
        name = 'empty'
    return name
Beispiel #18
0
def open_file(name):
    assert is_string(name)

    return open(filename_to_path(name), "rb")
Beispiel #19
0
def filename_to_path(name):
    assert is_string(name)

    return os.path.join(current_app.config["SKYLINES_FILES_PATH"], name)
Beispiel #20
0
def filename_to_path(name):
    assert is_string(name)

    return os.path.join(current_app.config['SKYLINES_FILES_PATH'], name)
Beispiel #21
0
def open_file(name):
    assert is_string(name)

    return open(filename_to_path(name))