tools.py 854 Bytes
Newer Older
1
import oyaml as yaml
mmassaviol's avatar
mmassaviol committed
2
import shutil
3
4
5

def read_yaml(filepath):
    try:
jlopez's avatar
Update    
jlopez committed
6
        with open(filepath, 'rb') as file:
mmassaviol's avatar
mmassaviol committed
7
            data = yaml.load(file, Loader=yaml.FullLoader)
8
9
10
11
12
13
14
            return data
    except IOError as e:
        print("Error in file opening:", e)
    except yaml.YAMLError as exc:
        print("Error in yaml loading:", exc)

def write_yaml(filepath,data):
mmassaviol's avatar
mmassaviol committed
15
16
    noalias_dumper = yaml.dumper.SafeDumper
    noalias_dumper.ignore_aliases = lambda self, data: True
17
18
    try:
        with open(filepath, 'w') as file:
mmassaviol's avatar
mmassaviol committed
19
            yaml.dump(data, file, default_flow_style=False, Dumper=noalias_dumper)
20
    except IOError as e:
mmassaviol's avatar
mmassaviol committed
21
22
23
24
25
26
27
28
        print("Error in file opening:", e)

def copy_dir(src,dst):
    try:
        shutil.copytree(src,dst)
    except FileExistsError:
        shutil.rmtree(dst, ignore_errors=True)
        shutil.copytree(src,dst)