Skip to content

Commit

Permalink
format code and add pre commit
Browse files Browse the repository at this point in the history
  • Loading branch information
YadominJinta committed Nov 22, 2024
1 parent 6afd4e5 commit b34abbf
Show file tree
Hide file tree
Showing 18 changed files with 560 additions and 340 deletions.
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: local
hooks:
- id: black
name: black
entry: black --check --diff --color
language: python
files: ".py"
- id: flake8
name: flake8
entry: flake8
language: python
files: ".py"
28 changes: 18 additions & 10 deletions pdf2zh/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import time
import hashlib
import shutil
cache_dir = os.path.join(tempfile.gettempdir(), 'cache')

cache_dir = os.path.join(tempfile.gettempdir(), "cache")
os.makedirs(cache_dir, exist_ok=True)
time_filename = 'update_time'
time_filename = "update_time"
max_cache = 5


Expand All @@ -16,25 +17,30 @@ def deterministic_hash(obj):


def get_dirs():
dirs = [os.path.join(cache_dir, dir) for dir in os.listdir(cache_dir) if os.path.isdir(os.path.join(cache_dir, dir))]
dirs = [
os.path.join(cache_dir, dir)
for dir in os.listdir(cache_dir)
if os.path.isdir(os.path.join(cache_dir, dir))
]
return dirs


def get_time(dir):
try:
timefile = os.path.join(dir, time_filename)
t = float(open(timefile, encoding='utf-8').read())
t = float(open(timefile, encoding="utf-8").read())
return t
except FileNotFoundError:
# handle the error as needed, for now we'll just return a default value
return float('inf') # This ensures that this directory will be the first to be removed if required

return float(
"inf"
) # This ensures that this directory will be the first to be removed if required


def write_time(dir):
timefile = os.path.join(dir, time_filename)
t = time.time()
print(t, file=open(timefile, "w", encoding='utf-8'), end='')
print(t, file=open(timefile, "w", encoding="utf-8"), end="")


def argmin(iterable):
Expand All @@ -44,7 +50,9 @@ def argmin(iterable):
def remove_extra():
dirs = get_dirs()
for dir in dirs:
if not os.path.isdir(dir): # This line might be redundant now, as get_dirs() ensures only directories are returned
if not os.path.isdir(
dir
): # This line might be redundant now, as get_dirs() ensures only directories are returned
os.remove(dir)
try:
get_time(dir)
Expand Down Expand Up @@ -73,11 +81,11 @@ def create_cache(hash_key):
def load_paragraph(hash_key, hash_key_paragraph):
filename = os.path.join(cache_dir, hash_key, hash_key_paragraph)
if os.path.exists(filename):
return open(filename, encoding='utf-8').read()
return open(filename, encoding="utf-8").read()
else:
return None


def write_paragraph(hash_key, hash_key_paragraph, paragraph):
filename = os.path.join(cache_dir, hash_key, hash_key_paragraph)
print(paragraph, file=open(filename, "w", encoding='utf-8'), end='')
print(paragraph, file=open(filename, "w", encoding="utf-8"), end="")
Loading

0 comments on commit b34abbf

Please sign in to comment.