Skip to content

Commit

Permalink
Feature: update rules.toml automatically if it has not been modified
Browse files Browse the repository at this point in the history
  • Loading branch information
URenko committed Oct 24, 2024
1 parent c7af052 commit 13392f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
10 changes: 10 additions & 0 deletions accesser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ async def main():
global context, cert_store, cert_lock, DNSresolver
print(f"Accesser v{__version__} Copyright (C) 2018-2024 URenko")
setting.parse_args()

if setting.rules_update_case in ('old', 'missing'):
logger.warning("Updated rules.toml because it is %s.", setting.rules_update_case)
elif setting.rules_update_case == 'modified':
logger.warning("You've already modified rules.toml, so it won't be updated automatically!")
else:
logger.debug("rules.toml status: %s", setting.rules_update_case)

if any(_keys in setting._config for _keys in setting._rules):
logger.warning("Some sections of config.toml overlap with rules.toml, config.toml has higher priority, but this may make rule updates ineffective.")

DNSresolver = dns.asyncresolver.Resolver(configure=False)
DNSresolver.cache = dns.resolver.LRUCache()
Expand Down
22 changes: 18 additions & 4 deletions accesser/utils/setting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
import shutil
import shutil, os, filecmp
try:
import tomllib
except ModuleNotFoundError:
Expand All @@ -8,15 +8,29 @@

basepath = Path(__file__).parent.parent

if not Path('rules.toml').exists():
if Path('rules.toml').exists():
if Path('rules.toml').stat().st_mtime_ns > 0:
rules_update_case = 'modified'
else:
if filecmp.cmp('rules.toml', basepath / 'rules.toml'):
rules_update_case = 'holding'
else:
rules_update_case = 'old'
else:
rules_update_case = 'missing'
if rules_update_case in ('old', 'missing'):
shutil.copyfile(basepath / 'rules.toml', 'rules.toml')
os.utime('rules.toml', ns=(0, 0))
with open('rules.toml', 'rb') as f:
config = tomllib.load(f)
_rules = tomllib.load(f)
config = _rules.copy()

if not Path('config.toml').exists():
shutil.copyfile(basepath / 'config.toml', 'config.toml')
with open('config.toml', 'rb') as f:
config |= tomllib.load(f)
_config = tomllib.load(f)

config |= _config

def parse_args():
parser = argparse.ArgumentParser()
Expand Down

0 comments on commit 13392f4

Please sign in to comment.