mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-05-13 08:26:56 +00:00
Start work on themes kitten
This commit is contained in:
parent
13e59df1f8
commit
050eb5660d
2 changed files with 51 additions and 0 deletions
0
kittens/themes/__init__.py
Normal file
0
kittens/themes/__init__.py
Normal file
51
kittens/themes/collection.py
Normal file
51
kittens/themes/collection.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPLv3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import datetime
|
||||
import http
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import zipfile
|
||||
from contextlib import suppress
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from kitty.constants import cache_dir
|
||||
|
||||
|
||||
def fetch_themes(
|
||||
name: str = 'kitty-themes',
|
||||
url: str = 'https://codeload.github.com/kovidgoyal/kitty-themes/zip/master'
|
||||
) -> str:
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
|
||||
class Metadata:
|
||||
def __init__(self) -> None:
|
||||
self.etag = ''
|
||||
self.timestamp = now
|
||||
|
||||
dest_path = os.path.join(cache_dir(), f'{name}.zip')
|
||||
m = Metadata()
|
||||
with suppress(Exception), zipfile.ZipFile(dest_path, 'r') as zf:
|
||||
q = json.loads(zf.comment)
|
||||
m.etag = str(q.get('etag') or '')
|
||||
m.timestamp = datetime.datetime.fromisoformat(q['timestamp'])
|
||||
if (now - m.timestamp).days < 1:
|
||||
return dest_path
|
||||
rq = Request(url)
|
||||
if m.etag:
|
||||
rq.add_header('If-None-Match', m.etag)
|
||||
try:
|
||||
res = urlopen(rq)
|
||||
except HTTPError as e:
|
||||
if m.etag and e.code == http.HTTPStatus.NOT_MODIFIED:
|
||||
return dest_path
|
||||
raise
|
||||
m.etag = res.headers.get('etag') or ''
|
||||
with open(dest_path, 'wb') as f:
|
||||
shutil.copyfileobj(res, f)
|
||||
with zipfile.ZipFile(dest_path, 'a') as zf:
|
||||
zf.comment = json.dumps({'etag': m.etag, 'timestamp': m.timestamp.isoformat()}).encode('utf-8')
|
||||
return dest_path
|
||||
Loading…
Add table
Add a link
Reference in a new issue