Skip to content

Commit

Permalink
Merge pull request #403 from malthe/issue-402-posix-path
Browse files Browse the repository at this point in the history
Fix loading file from non zip-compressed package
  • Loading branch information
malthe authored Dec 23, 2023
2 parents 3af0102 + 395a20b commit db6186c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changes
=======

In next release ...

- Fix an issue where `auto_reload` is enabled and a file is loaded
from a package that wasn't zip-compressed.
(`#402 <https://github.com/malthe/chameleon/issues/402>`_)


4.4.2 (2023-12-18)
------------------

Expand Down
11 changes: 10 additions & 1 deletion src/chameleon/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import logging
import os
import pathlib
import sys
import tempfile

Expand Down Expand Up @@ -341,12 +342,20 @@ def cook_check(self):
body = self.read()
log.debug("cooking %r (%d bytes)..." % (self.filename, len(body)))
self.cook(body)
return True

return False

def mtime(self):
filename = self.filename
if self.package_name is not None:
with import_package_resource(self.package_name) as path:
filename = path.joinpath(self.filename).at
joined = path.joinpath(filename)
if isinstance(joined, pathlib.Path):
return joined.stat().st_mtime

# Assume this is a zip-like path object:
filename = joined.at
timetuple = path.root.getinfo(filename).date_time
return datetime.datetime(*timetuple).timestamp()
try:
Expand Down
13 changes: 11 additions & 2 deletions src/chameleon/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ def find_files(self, ext):

class ZopePageTemplatesTest(RenderTestCase):
@property
def from_string(body):
def from_string(self):
from chameleon.zpt.template import PageTemplate
return partial(PageTemplate, keep_source=True)

@property
def from_file(body):
def from_file(self):
from chameleon.zpt.template import PageTemplateFile
return partial(PageTemplateFile, keep_source=True)

Expand Down Expand Up @@ -335,6 +335,15 @@ def __html__():
else:
self.fail("expected error")

def test_package_name_cook_check(self):
template = self.from_file(
"__init__.py",
package_name="setuptools",
auto_reload=True
)
assert template.cook_check()
assert not template.cook_check()

def test_custom_encoding_for_str_or_bytes_in_content(self):
string = '<div>Тест${text}</div>'
try:
Expand Down

0 comments on commit db6186c

Please sign in to comment.