Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implementing jinja templater #1222

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- id: setup-python
uses: actions/setup-python@v5
- name: Install dependencies
run: make python_install
- name: Add site-packages to PYTHONPATH
run: echo "PYTHONPATH=$(python -c 'import site; print(site.getsitepackages()[0])'):\$PYTHONPATH" >> $GITHUB_ENV
- name: Run tests in cli
run: cargo test --manifest-path ./crates/cli/Cargo.toml
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@nextest
- name: Run all tests apart from cli
run: cargo test --all --all-features --exclude sqruff
- name: Run tests in cli
run: cargo test --manifest-path ./crates/cli/Cargo.toml
- name: Check for diffs
run: git diff --quiet || exit 1
benchmark:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ name = "ui_json"
harness = false

[features]
python = ["sqruff-lib/python"]
python = ["sqruff-lib/python", "pyo3"]
codegen-docs = ["clap-markdown", "minijinja", "serde", "python"]

[dependencies]
Expand All @@ -69,6 +69,7 @@ strum_macros.workspace = true
clap = { version = "4", features = ["derive"] }
console = "0.15.8"
ignore = "0.4.23"
pyo3 = { version = "0.23.3", features = ["auto-initialize"], optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
mimalloc = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["maturin>=1.0"]
requires = ["maturin>=1.8"]
build-backend = "maturin"

[project]
Expand Down
9 changes: 7 additions & 2 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ pub(crate) struct Cli {

#[derive(Debug, Subcommand)]
pub(crate) enum Commands {
#[command(name = "lint", about = "lint files")]
#[command(name = "lint", about = "Lint files")]
Lint(LintArgs),
#[command(name = "fix", about = "fix files")]
#[command(name = "fix", about = "Fix files")]
Fix(FixArgs),
#[command(name = "lsp", about = "Run an LSP server")]
Lsp,
#[command(
name = "info",
about = "Print information about sqruff and the current environment"
)]
Info,
}

#[derive(Debug, Parser)]
Expand Down
60 changes: 60 additions & 0 deletions crates/cli/src/commands_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
pub(crate) fn info() {
// Print information about the current environment
println!("Rust Version: {}", env!("CARGO_PKG_VERSION"));

// Print information about python
#[cfg(feature = "python")]
{
use pyo3::prelude::*;
use pyo3::types::{PyList, PyMapping, PyString};

// Acquire the Global Interpreter Lock (GIL) and open a Python context
Python::with_gil(|py| {
// Import the sys module
let sys = py.import("sys").unwrap();

// Get some attributes from sys
let version = sys.getattr("version").unwrap();
let version = version.downcast().unwrap();
let executable = sys.getattr("executable").unwrap();
let executable = executable.downcast().unwrap();
let prefix = sys.getattr("prefix").unwrap();
let prefix = prefix.downcast().unwrap();
let base_prefix = sys.getattr("base_prefix").unwrap();
let base_prefix = base_prefix.downcast().unwrap();
// Print them out or do whatever you want with them
println!("Python Version: {}", version.to_str().unwrap());
println!("Executable: {}", executable.to_str().unwrap());
println!("Prefix: {}", prefix.to_str().unwrap());
println!("Base Prefix: {}", base_prefix.to_str().unwrap());

let sys_path = sys.getattr("path").unwrap();
let sys_path = sys_path.downcast::<PyList>().unwrap();
println!("sys.path:");
for p in sys_path.iter() {
println!(" {}", p);
}

// If you need to check environment variables (e.g. VIRTUAL_ENV),
// you can import "os" and query os.environ:
let os = py.import("os").unwrap();
let environ = os.getattr("environ").unwrap();
let environ = environ.downcast::<PyMapping>().unwrap();
// If VIRTUAL_ENV is set, you can get it like:
if let Ok(environ) = environ.get_item("VIRTUAL_ENV") {
// if string
let virtual_env = environ.downcast::<PyString>();
if let Ok(virtual_env) = virtual_env {
println!("VIRTUAL_ENV: {}", virtual_env);
} else {
println!("VIRTUAL_ENV not set.");
}
} else {
println!("VIRTUAL_ENV not set.");
}

Ok::<_, String>(())
})
.unwrap();
}
}
5 changes: 5 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::docs::codegen_docs;

mod commands;
mod commands_fix;
mod commands_info;
mod commands_lint;
#[cfg(feature = "codegen-docs")]
mod docs;
Expand Down Expand Up @@ -93,6 +94,10 @@ fn main() {
sqruff_lsp::run();
0
}
Commands::Info => {
commands_info::info();
0
}
};

std::process::exit(status_code);
Expand Down
6 changes: 5 additions & 1 deletion crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ doctest = false
name = "rules"
harness = false

[[test]]
name = "templaters"
harness = false

[[bench]]
name = "parsing"
harness = false
Expand All @@ -34,7 +38,7 @@ name = "depth_map"
harness = false

[features]
python = ["pyo3"]
python = ["pyo3", "sqruff-lib-core/serde"]

[dependencies]
sqruff-lib-core.workspace = true
Expand Down
22 changes: 12 additions & 10 deletions crates/lib/src/core/linter/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,7 @@ impl Linter {
) -> Linter {
let templater: &'static dyn Templater = match templater {
Some(templater) => templater,
None => {
let templater_name = config.get("templater", "core").as_string();
match templater_name {
Some(name) => match TEMPLATERS.into_iter().find(|t| t.name() == name) {
Some(t) => t,
None => panic!("Unknown templater: {}", name),
},
None => &RawTemplater,
}
}
None => Linter::get_templater(&config),
};
Linter {
config,
Expand All @@ -73,6 +64,17 @@ impl Linter {
}
}

pub fn get_templater(config: &FluffConfig) -> &'static dyn Templater {
let templater_name = config.get("templater", "core").as_string();
match templater_name {
Some(name) => match TEMPLATERS.into_iter().find(|t| t.name() == name) {
Some(t) => t,
None => panic!("Unknown templater: {}", name),
},
None => &RawTemplater,
}
}

/// Lint strings directly.
pub fn lint_string_wrapped(
&mut self,
Expand Down
16 changes: 14 additions & 2 deletions crates/lib/src/templaters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,35 @@ use crate::core::config::FluffConfig;
use crate::templaters::placeholder::PlaceholderTemplater;
use crate::templaters::raw::RawTemplater;

#[cfg(feature = "python")]
use crate::templaters::jinja::JinjaTemplater;
#[cfg(feature = "python")]
use crate::templaters::python::PythonTemplater;

#[cfg(feature = "python")]
pub mod jinja;
pub mod placeholder;
#[cfg(feature = "python")]
pub mod python;
#[cfg(feature = "python")]
pub mod python_shared;
pub mod raw;

pub static RAW_TEMPLATER: RawTemplater = RawTemplater;
pub static PLACEHOLDER_TEMPLATER: PlaceholderTemplater = PlaceholderTemplater;
#[cfg(feature = "python")]
pub static PYTHON_TEMPLATER: PythonTemplater = PythonTemplater;
#[cfg(feature = "python")]
pub static JINJA_TEMPLATER: JinjaTemplater = JinjaTemplater;

// templaters returns all the templaters that are available in the library
#[cfg(feature = "python")]
pub static TEMPLATERS: [&'static dyn Templater; 3] =
[&RAW_TEMPLATER, &PLACEHOLDER_TEMPLATER, &PYTHON_TEMPLATER];
pub static TEMPLATERS: [&'static dyn Templater; 4] = [
&RAW_TEMPLATER,
&PLACEHOLDER_TEMPLATER,
&PYTHON_TEMPLATER,
&JINJA_TEMPLATER,
];

#[cfg(not(feature = "python"))]
pub static TEMPLATERS: [&'static dyn Templater; 2] = [&RAW_TEMPLATER, &PLACEHOLDER_TEMPLATER];
Expand Down
1 change: 0 additions & 1 deletion crates/lib/src/templaters/__init__.py

This file was deleted.

Loading
Loading