Skip to content

Commit

Permalink
Merge pull request #147 from Victor-IX/use-dateparser
Browse files Browse the repository at this point in the history
add dateparser functionality
  • Loading branch information
zeptofine authored Oct 27, 2024
2 parents 4a3d4f6 + 72103d9 commit c95a4df
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
53 changes: 51 additions & 2 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
"semver>=3.0.2",
"zstandard>=0.23.0",
"send2trash>=1.8.3",
"dateparser>=1.2.0",
]
requires-python = ">=3.9, <3.11"
readme = "README.md"
Expand Down
16 changes: 0 additions & 16 deletions source/modules/_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import platform
import sys
from functools import cache
from locale import LC_ALL, getdefaultlocale, setlocale
from pathlib import Path
from subprocess import DEVNULL, PIPE, STDOUT, Popen, call, check_call, check_output
from tempfile import NamedTemporaryFile
Expand Down Expand Up @@ -43,21 +42,6 @@ def get_platform_full():
return f"{get_platform()}-{platform.release()}"


def set_locale():
platform = get_platform()

if platform == "Windows":
setlocale(LC_ALL, "eng_usa")
elif platform in {"Linux", "macOS"}:
setlocale(LC_ALL, "en_US.UTF-8")


default_locale = getdefaultlocale(("LC_ALL",))[0]


def reset_locale():
setlocale(LC_ALL, default_locale)


def show_windows_help(parser: argparse.ArgumentParser):
with (
Expand Down
13 changes: 8 additions & 5 deletions source/modules/build_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from functools import cache
from pathlib import Path

from modules._platform import _check_output, _popen, get_platform, reset_locale, set_locale
import dateparser
from modules._platform import _check_output, _popen, get_platform
from modules.bl_api_manager import lts_blender_version
from modules.settings import (
get_bash_arguments,
Expand Down Expand Up @@ -203,7 +204,10 @@ def from_dict(cls, link: str, blinfo: dict):
try:
dt = datetime.fromisoformat(blinfo["commit_time"])
except ValueError: # old file version compatibility
dt = datetime.strptime(blinfo["commit_time"], "%d-%b-%y-%H:%M").astimezone()
try:
dt = datetime.strptime(blinfo["commit_time"], "%d-%b-%y-%H:%M").astimezone()
except Exception:
dt = dateparser.parse(blinfo["commit_time"]).astimezone()
return cls(
link,
blinfo["subversion"],
Expand Down Expand Up @@ -250,7 +254,7 @@ def __lt__(self, other: BuildInfo):


def fill_blender_info(exe: Path, info: BuildInfo | None = None) -> tuple[datetime, str, str, str]:
set_locale()

version = _check_output([exe.as_posix(), "-v"]).decode("UTF-8")
build_hash = ""
subversion = ""
Expand All @@ -267,7 +271,7 @@ def fill_blender_info(exe: Path, info: BuildInfo | None = None) -> tuple[datetim
"%Y-%m-%d %H:%M",
).astimezone()
except Exception:
strptime = datetime.now().astimezone()
strptime = dateparser.parse(f"{cdate[1].rstrip()} {ctime[1].rstrip()}")
else:
strptime = datetime.now().astimezone()
else:
Expand All @@ -284,7 +288,6 @@ def fill_blender_info(exe: Path, info: BuildInfo | None = None) -> tuple[datetim
s = version.splitlines()[0].strip()
custom_name, subversion = s.rsplit(" ", 1)

reset_locale()

return (
strptime,
Expand Down
10 changes: 4 additions & 6 deletions source/threads/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from typing import TYPE_CHECKING
from urllib.parse import urljoin

import dateparser
import distro
from bs4 import BeautifulSoup, SoupStrainer
from modules._platform import get_architecture, get_platform, reset_locale, set_locale, stable_cache_path
from modules._platform import get_architecture, get_platform, stable_cache_path
from modules.bl_api_manager import (
dropdown_blender_version,
lts_blender_version,
Expand Down Expand Up @@ -217,7 +218,6 @@ def get_api_data_manager(self):
self.manager.manager.clear()

def get_download_links(self):
set_locale()

scrapers = []
if self.scrape_stable:
Expand All @@ -227,7 +227,6 @@ def get_download_links(self):
for build in chain(*scrapers):
self.links.emit(build)

reset_locale()

def scrape_automated_releases(self):
base_fmt = "https://builder.blender.org/download/{}/?format=json&v=1"
Expand Down Expand Up @@ -362,8 +361,7 @@ def new_blender_build(self, tag, url, branch_type):
branch = "daily"
subversion = subversion.replace(prerelease=build_var)

commit_time = datetime.strptime(info["last-modified"], "%a, %d %b %Y %H:%M:%S %Z").astimezone()

commit_time = dateparser.parse(info["last-modified"]).astimezone()
r.release_conn()
r.close()
return BuildInfo(link, str(subversion), build_hash, commit_time, branch)
Expand Down Expand Up @@ -409,7 +407,7 @@ def scrap_stable_releases(self):
if date_sibling:
date_str = " ".join(date_sibling.strip().split()[:2])
with contextlib.suppress(ValueError):
modified_date = datetime.strptime(date_str, "%d-%b-%Y %H:%M").astimezone(tz=timezone.utc)
modified_date = dateparser.parse(date_str).astimezone(tz=timezone.utc)
if ver not in self.cache:
logger.debug(f"Creating new folder for version {ver}")
folder = self.cache.new_build(ver)
Expand Down

0 comments on commit c95a4df

Please sign in to comment.