cnlls.py 1.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from cnl_library import CNLParser

from collections import defaultdict, deque


def get_begin(cnl_file):
    return cnl_file.get_general_header()["Date"][1]

def are_close(cnl_file1, cnl_file2):
    t1 = get_begin(cnl_file1)
    t2 = get_begin(cnl_file2)

    return abs(t1 - t2) < 2


def find_match(cnl_file, list_of_files):
    for f in list_of_files:
        if ( are_close(cnl_file, f) ):
            list_of_files.remove(f)
            return f

    return None


## MAIN ##
if __name__ == "__main__":
    import sys

    filenames = sorted( sys.argv[1:] )
    cnl_files = defaultdict(deque)

    ## Parse files and store them in a dict (of lists) according to their hostname.
    for filename in filenames:
        cnl_file = CNLParser(filename)
        hostname = cnl_file.get_hostname()
        cnl_files[hostname].append( cnl_file )


    ## Match.
    hostnames = sorted( cnl_files.keys() )
    left_files = cnl_files[hostnames[0]]
    right_files = cnl_files[hostnames[1]]

    for left_file in left_files:
        matching_file = find_match(left_file, right_files)

        if ( matching_file ):
            print( "{}  {}".format(left_file.filename, matching_file.filename) )
        else:
            print( left_file.filename )


    ## Print left over right files.
    if ( len(right_files) > 0 ):
        print()
        for f in right_files:
            print( f.filename )