Skip to content

Commit

Permalink
Use importlib.context.as_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Dec 7, 2023
1 parent 42f9dc1 commit 1db8226
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 25 deletions.
33 changes: 13 additions & 20 deletions htmd/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import importlib
# Need to import importlib.resources explicitly
import importlib.resources
from importlib.resources import as_file, files
import os
import sys

Expand Down Expand Up @@ -32,47 +31,41 @@ def start(all_templates):
if all_templates:
copy_missing_templates()
else:
resource_path = importlib.resources.path('htmd.example_site.templates', '_layout.html')
with resource_path as path:
with as_file(files('htmd.example_site.templates') / '_layout.html') as file:
copy_file(
path,
file,
os.path.join('templates/', '_layout.html')
)

create_directory('static/')
resource_path = importlib.resources.path('htmd.example_site.static', '_reset.css')
with resource_path as path:
with as_file(files('htmd.example_site.static') / '_reset.css') as file:
copy_file(
path,
file,
os.path.join('static/', '_reset.css')
)
resource_path = importlib.resources.path('htmd.example_site.static', 'style.css')
with resource_path as path:
with as_file(files('htmd.example_site.static') / 'style.css') as file:
copy_file(
path,
file,
os.path.join('static/', 'style.css'),
)

create_directory('pages/')
resource_path = importlib.resources.path('htmd.example_site.pages', 'about.html')
with resource_path as path:
with as_file(files('htmd.example_site.pages') / 'about.html') as file:
copy_file(
path,
file,
os.path.join('pages/', 'about.html'),
)

create_directory('posts/')
resource_path = importlib.resources.path('htmd.example_site.posts', 'example.md')
with resource_path as path:
with as_file(files('htmd.example_site.posts') / 'example.md') as file:
copy_file(
path,
file,
os.path.join('posts/', 'example.md'),
)

resource_path = importlib.resources.path('htmd.example_site', 'config.py')
with resource_path as path:
with as_file(files('htmd.example_site') / 'config.py') as file:
copy_file(
path,
file,
'config.py',
)
click.echo('Add the site name and edit settings in config.py')
Expand Down
Empty file removed htmd/example_site/__init__.py
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
9 changes: 4 additions & 5 deletions htmd/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib.resources
import importlib
from importlib.resources import as_file, files
import os
import shutil

Expand Down Expand Up @@ -80,11 +81,9 @@ def combine_and_minify_js(static_folder):
def copy_missing_templates():
template_dir = importlib.resources.contents('htmd.example_site.templates')
for template_file in sorted(template_dir):
if template_file in ('__init__.py', '__pycache__'):
# __init__.py is that this directory for importlib.resources to work
if template_file in ('__pycache__',):
continue
resource_path = importlib.resources.path('htmd.example_site.templates', template_file)
with resource_path as path:
with as_file(files('htmd.example_site.templates') / template_file) as path:
copy_file(
path,
os.path.join('templates', template_file)
Expand Down
1 change: 1 addition & 0 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_start():
with runner.isolated_filesystem():
result = runner.invoke(start)
assert result.exit_code == 0
print(result.output)
assert result.output == expected_output


Expand Down

0 comments on commit 1db8226

Please sign in to comment.