forked from flinteger/dnss-blocklists
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
37 lines (28 loc) · 962 Bytes
/
util.py
File metadata and controls
37 lines (28 loc) · 962 Bytes
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from typing import Set
def load_domains(file: str) -> Set:
domains = set()
if not os.path.exists(file):
return domains
with open(file) as f:
for line in f.readlines():
line = line.strip()
if len(line) == 0 or line.isspace() or line.startswith("#"):
# skip empty or comment line
continue
domains.add(line)
return domains
def write_domains(domains: Set, file: str, reload: bool = True):
if reload and os.path.exists(file):
# reload exist file to avoid any change outside current process.
with open(file) as f:
for line in f.readlines():
line = line.strip()
domains.add(line)
with open(file, 'w') as f:
domains_list = list(domains)
domains_list.sort()
f.write('\n'.join(domains_list))
f.write('\n')