move local file write once logic to separate class
This commit is contained in:
@@ -7,7 +7,7 @@ from botlib.cli import Cli, DirType
|
|||||||
from botlib.cron import Cron
|
from botlib.cron import Cron
|
||||||
from botlib.curl import Curl
|
from botlib.curl import Curl
|
||||||
from botlib.feed2list import Feed2List
|
from botlib.feed2list import Feed2List
|
||||||
from botlib.helper import Log, FileTime, StrFormat
|
from botlib.helper import Log, FileTime, StrFormat, FileWrite
|
||||||
from botlib.html2list import HTML2List, MatchGroup
|
from botlib.html2list import HTML2List, MatchGroup
|
||||||
from botlib.oncedb import OnceDB
|
from botlib.oncedb import OnceDB
|
||||||
from botlib.tgclient import TGClient
|
from botlib.tgclient import TGClient
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ class Curl:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def once(dest_dir, fname, urllist, date, desc=None, *,
|
def once(dest_dir, fname, urllist, date=None, *,
|
||||||
override=False, dry_run=False, verbose=False, intro=''):
|
override=False, dry_run=False, verbose=False, intro=''):
|
||||||
did_update = False
|
did_update = False
|
||||||
for url_str in urllist:
|
for url_str in urllist:
|
||||||
@@ -116,22 +116,14 @@ class Curl:
|
|||||||
ext = parts.path.split('.')[-1] or 'unknown'
|
ext = parts.path.split('.')[-1] or 'unknown'
|
||||||
file_path = os.path.join(dest_dir, fname + '.' + ext)
|
file_path = os.path.join(dest_dir, fname + '.' + ext)
|
||||||
if override or not os.path.isfile(file_path):
|
if override or not os.path.isfile(file_path):
|
||||||
if not did_update and verbose and intro:
|
url = parts.geturl()
|
||||||
print(intro)
|
|
||||||
did_update = True
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print(' GET', parts.geturl())
|
if not did_update and intro:
|
||||||
if not dry_run:
|
print(intro)
|
||||||
Curl.file(parts.geturl(), file_path, raise_except=True)
|
print(' GET', url)
|
||||||
FileTime.set(file_path, date)
|
|
||||||
if desc:
|
|
||||||
desc_path = os.path.join(dest_dir, fname + '.txt')
|
|
||||||
if override or not os.path.isfile(desc_path):
|
|
||||||
did_update = True
|
did_update = True
|
||||||
if verbose:
|
|
||||||
print(' –>', desc_path)
|
|
||||||
if not dry_run:
|
if not dry_run:
|
||||||
with open(desc_path, 'w') as f:
|
Curl.file(url, file_path, raise_except=True)
|
||||||
f.write(desc)
|
if date:
|
||||||
FileTime.set(desc_path, date)
|
FileTime.set(file_path, date)
|
||||||
return did_update
|
return did_update
|
||||||
|
|||||||
@@ -71,3 +71,28 @@ class StrFormat:
|
|||||||
text = text.replace('̈', 'e') # replace umlauts e.g., Ä -> Ae
|
text = text.replace('̈', 'e') # replace umlauts e.g., Ä -> Ae
|
||||||
text = text.encode('ASCII', 'ignore')
|
text = text.encode('ASCII', 'ignore')
|
||||||
return ''.join(chr(c) for c in text if chr(c) in StrFormat.fnameChars)
|
return ''.join(chr(c) for c in text if chr(c) in StrFormat.fnameChars)
|
||||||
|
|
||||||
|
|
||||||
|
class FileWrite:
|
||||||
|
@staticmethod
|
||||||
|
def once(dest_dir, fname, date=None, *,
|
||||||
|
override=False, dry_run=False, verbose=False, intro=''):
|
||||||
|
def _decorator(func):
|
||||||
|
path = os.path.join(dest_dir, fname)
|
||||||
|
if os.path.isfile(path) and not override:
|
||||||
|
return
|
||||||
|
content = func()
|
||||||
|
if not content:
|
||||||
|
return
|
||||||
|
if verbose:
|
||||||
|
if intro and not isinstance(intro, bool):
|
||||||
|
print(intro)
|
||||||
|
print(' –>', path)
|
||||||
|
if dry_run:
|
||||||
|
return
|
||||||
|
# write file
|
||||||
|
with open(path, 'w') as f:
|
||||||
|
f.write(content)
|
||||||
|
if date:
|
||||||
|
FileTime.set(path, date)
|
||||||
|
return _decorator
|
||||||
|
|||||||
Reference in New Issue
Block a user