#!/usr/bin/env python3.9 # -*- coding: utf-8 -*- """ enjoy5.2.2 的 Python 3.9+ 实现 ============================================= This is a Python 3.9+ implementation of the JFinal Template Engine (Enjoy). Project Structure ----------------- py_enjoy/ ├── __init__.py # Main package initialization ├── kit/ # Utility classes │ ├── StrKit.py # String utilities │ ├── HashKit.py # Hash and encryption utilities │ ├── Kv.py # Key-Value dictionary │ ├── Okv.py # Ordered Key-Value dictionary │ ├── Prop.py # Properties file handler │ ├── PropKit.py # Properties file management │ ├── TypeKit.py # Type conversion utilities │ ├── TimeKit.py # Date and time utilities │ ├── Func.py # Lambda function utilities │ ├── ReflectKit.py # Reflection utilities │ ├── JavaKeyword.py # Java keyword detection │ └── SyncWriteMap.py # Thread-safe dictionary │ ├── proxy/ # Dynamic proxy classes │ ├── ProxyClass.py # Proxy class information │ ├── ProxyClassLoader.py # Proxy class loader │ └── ProxyCompiler.py # Proxy class compiler │ └── template/ # Template engine core ├── Engine.py # Main template engine ├── Template.py # Template rendering ├── Env.py # Template environment ├── EngineConfig.py # Engine configuration ├── Directive.py # Base directive class ├── TemplateException.py # Exception handling │ ├── expr/ # Expression parsing │ └── ast/ │ ├── Expr.py # Expression base class │ ├── Const.py # Constant expressions │ ├── Compare.py # Comparison expressions │ └── Arith.py # Arithmetic expressions │ └── stat/ # Statement parsing ├── Scope.py # Execution scope └── ast/ └── Stat.py # Statement base classes Features -------- 1. **Template Engine** - String template rendering - File-based template loading - Template caching and hot-reloading - Shared functions and objects 2. **Expression Language** - Arithmetic operations (+, -, *, /, %) - Comparison operations (==, !=, >, <, >=, <=) - Logical operations (&&, ||, !) - Ternary expressions (condition ? true : false) - Variable access and assignment 3. **Directives** - #if/#else/#end - Conditional statements - #for - Loop statements - #define - Template functions - #include - Template inclusion - #set - Variable assignment - Custom directive support 4. **Utility Classes** - String manipulation (StrKit) - Hash functions (HashKit) - Type conversions (TypeKit) - Date/time handling (TimeKit) - Properties file management (PropKit) - Key-Value containers (Kv, Okv) Requirements ------------ - Python 3.9.0 or higher - No external dependencies Installation ------------ ```bash # Clone or download the project cd py_enjoy # No installation required, just add to PYTHONPATH import sys sys.path.append('/path/to/py_enjoy') ``` Quick Start ----------- ```python from py_enjoy.template.Engine import Engine from py_enjoy.kit.Kv import Kv # Get the template engine engine = Engine.use() # Create a simple template template_content = "Hello, #(name)! Today is #(date)." template = engine.get_template_by_string(template_content) # Render with data data = Kv.of("name", "World").set("date", "2024-01-01") result = template.render_to_string(data) print(result) # Output: Hello, World! Today is 2024-01-01. ``` Advanced Usage -------------- ### Template with Logic ```python template_content = """ #for(item in items) #(item_index + 1). #(item.name) - #(item.price) #if(item.is_last) Total: #(total_price) #end #end """ data = Kv.of("items", [ {"name": "Apple", "price": 1.5, "is_last": False}, {"name": "Banana", "price": 0.75, "is_last": True} ]).set("total_price", 2.25) ``` ### Custom Directives ```python from py_enjoy.template.Directive import Directive class UpperDirective(Directive): def exec(self, env, scope, writer): value = self.expr_list.eval(scope) if value: writer.write(str(value).upper()) ``` Performance Considerations -------------------------- 1. **Template Caching**: Enable template caching in production ```python engine.cache_string_template = True ``` 2. **Dev Mode**: Disable in production ```python engine.dev_mode = False ``` 3. **Buffer Size**: Adjust for large outputs ```python engine.set_buffer_size(8192) ``` Testing ------- ```bash python test.py ``` License ------- This project is licensed under the Apache License 2.0. See the LICENSE file for details. Credits ------- Original Java implementation: James Zhan 詹波 (jfinal@126.com) Python port: Automated conversion """ __version__ = "5.2.2" __author__ = "James Zhan 詹波 (original), Python port by hello@miw.cn" __license__ = "Apache License 2.0"