-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_data.py
More file actions
54 lines (45 loc) · 2.01 KB
/
Copy pathfilter_data.py
File metadata and controls
54 lines (45 loc) · 2.01 KB
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
import os
import csv
import yaml
def copy_csv_file(src_file, dest_file):
filtered_rows = []
with open(src_file, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
filtered_rows = [item for item in reader if ('tag' in item and 'macromanagers' in item['tag']) or ('tags' in item and 'macromanagers' in item['tags'])]
if filtered_rows:
with open(dest_file, 'w', encoding='utf-8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=reader.fieldnames)
writer.writeheader()
writer.writerows(filtered_rows)
return True
return False
def copy_yaml_file(src_file, dest_file):
with open(src_file, 'r', encoding='utf-8') as f:
try:
data = yaml.safe_load(f)
if not isinstance(data, list):
return False
filtered_data = [item for item in data if ('tag' in item and 'macromanagers' in item['tag']) or ('tags' in item and 'macromanagers' in item['tags'])]
if filtered_data:
with open(dest_file, 'w', encoding='utf-8') as f:
yaml.dump(filtered_data, f, allow_unicode=True)
return True
except:
return False
return False
def copy_data_files(src_dir, dest_dir, files_to_check=[]):
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for root, _, files in os.walk(src_dir):
rel_path = os.path.relpath(root, src_dir)
for file in files:
src_file = os.path.join(root, file)
dest_subdir = os.path.join(dest_dir, rel_path)
if not os.path.exists(dest_subdir):
os.makedirs(dest_subdir)
dest_file = os.path.join(dest_subdir, file)
if file in files_to_check:
if file.endswith('.csv'):
os.replace(src_file, dest_file)
elif file.endswith(('.yaml', '.yml')):
os.replace(src_file, dest_file)