waspy 0.9.0

A Python interpreter written in Rust, designed for WebAssembly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Configuration data from a Python project
pub struct ProjectConfig {
    /// Project name
    pub name: String,
    /// Project version
    pub version: String,
    /// Project description
    pub description: Option<String>,
    /// Project author
    pub author: Option<String>,
    /// Dependencies
    pub dependencies: Vec<String>,
    /// Python version requirement
    pub python_requires: Option<String>,
    /// Module initialization code from __init__.py files
    pub init_code: HashMap<String, String>,
    /// Configuration settings
    pub settings: HashMap<String, String>,
    /// Build options
    pub build_options: HashMap<String, String>,
}

impl Default for ProjectConfig {
    fn default() -> Self {
        ProjectConfig {
            name: String::new(),
            version: "0.1.0".to_string(),
            description: None,
            author: None,
            dependencies: Vec::new(),
            python_requires: None,
            init_code: HashMap::new(),
            settings: HashMap::new(),
            build_options: HashMap::new(),
        }
    }
}

impl ProjectConfig {
    /// Create a new empty project configuration
    pub fn new() -> Self {
        ProjectConfig::default()
    }

    /// Extract a value from a Python string assignment
    fn extract_value_from_string(line: &str) -> Option<String> {
        let parts: Vec<&str> = line.split('=').collect();
        if parts.len() < 2 {
            return None;
        }

        let value_part = parts[1].trim();
        if (value_part.starts_with('"') && value_part.ends_with('"'))
            || (value_part.starts_with('\'') && value_part.ends_with('\''))
        {
            // Remove quotes and trailing comma if present
            Some(
                value_part[1..value_part.len() - 1]
                    .trim_end_matches(',')
                    .to_string(),
            )
        } else {
            // For non-string values, just take the value as is
            Some(value_part.trim_end_matches(',').to_string())
        }
    }

    /// Parse a setup.py file
    pub fn parse_setup_py(&mut self, content: &str) -> Result<()> {
        // This is a simplified parser for setup.py files
        for line in content.lines() {
            let line = line.trim();

            if line.starts_with("name=") || line.contains("'name'") || line.contains("\"name\"") {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.name = value;
                }
            } else if line.starts_with("version=")
                || line.contains("'version'")
                || line.contains("\"version\"")
            {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.version = value;
                }
            } else if line.starts_with("description=")
                || line.contains("'description'")
                || line.contains("\"description\"")
            {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.description = Some(value);
                }
            } else if line.starts_with("author=")
                || line.contains("'author'")
                || line.contains("\"author\"")
            {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.author = Some(value);
                }
            } else if line.starts_with("python_requires=")
                || line.contains("'python_requires'")
                || line.contains("\"python_requires\"")
            {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.python_requires = Some(value);
                }
            } else if line.starts_with("install_requires=")
                || line.contains("'install_requires'")
                || line.contains("\"install_requires\"")
            {
                // This is a simplified approach - properly parsing list contents would be more complex
                if line.contains("[") && line.contains("]") {
                    let start = line.find('[').unwrap();
                    let end = line.rfind(']').unwrap();
                    let deps_str = &line[start + 1..end];

                    for dep in deps_str.split(',') {
                        let dep = dep.trim();
                        if !dep.is_empty() {
                            // Remove quotes
                            let clean_dep = dep
                                .trim_start_matches('\'')
                                .trim_end_matches('\'')
                                .trim_start_matches('"')
                                .trim_end_matches('"');
                            if !clean_dep.is_empty() {
                                self.dependencies.push(clean_dep.to_string());
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Parse a __version__.py or __about__.py file
    pub fn parse_version_file(&mut self, content: &str) -> Result<()> {
        for line in content.lines() {
            let line = line.trim();

            if line.starts_with("__version__") {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.version = value;
                }
            } else if line.starts_with("__author__") {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.author = Some(value);
                }
            } else if line.starts_with("__description__") {
                if let Some(value) = Self::extract_value_from_string(line) {
                    self.description = Some(value);
                }
            }
        }

        Ok(())
    }

    /// Parse a pyproject.toml file
    pub fn parse_pyproject_toml(&mut self, content: &str) -> Result<()> {
        let mut in_build_system = false;
        let mut in_project = false;
        let mut in_dependencies = false;

        for line in content.lines() {
            let line = line.trim();

            if line.starts_with("[build-system]") {
                in_build_system = true;
                in_project = false;
                in_dependencies = false;
            } else if line.starts_with("[project]") {
                in_build_system = false;
                in_project = true;
                in_dependencies = false;
            } else if line.starts_with("[project.dependencies]")
                || line.starts_with("[tool.poetry.dependencies]")
            {
                in_build_system = false;
                in_project = false;
                in_dependencies = true;
            } else if line.starts_with("[") {
                // Any other section
                in_build_system = false;
                in_project = false;
                in_dependencies = false;
            } else if !line.is_empty() && !line.starts_with('#') {
                if in_build_system {
                    let parts: Vec<&str> = line.splitn(2, '=').collect();
                    if parts.len() == 2 {
                        let key = parts[0].trim();
                        let value = parts[1].trim();
                        // Remove quotes if present
                        let clean_value = value
                            .trim_start_matches('"')
                            .trim_end_matches('"')
                            .trim_start_matches('\'')
                            .trim_end_matches('\'');
                        self.build_options
                            .insert(key.to_string(), clean_value.to_string());
                    }
                } else if in_project {
                    let parts: Vec<&str> = line.splitn(2, '=').collect();
                    if parts.len() == 2 {
                        let key = parts[0].trim();
                        let value = parts[1].trim();
                        // Remove quotes if present
                        let clean_value = value
                            .trim_start_matches('"')
                            .trim_end_matches('"')
                            .trim_start_matches('\'')
                            .trim_end_matches('\'');

                        match key {
                            "name" => self.name = clean_value.to_string(),
                            "version" => self.version = clean_value.to_string(),
                            "description" => self.description = Some(clean_value.to_string()),
                            "authors" => {
                                // Simple parsing of author list
                                if clean_value.starts_with('[') && clean_value.ends_with(']') {
                                    let authors = clean_value[1..clean_value.len() - 1].trim();
                                    if !authors.is_empty() {
                                        self.author = Some(authors.to_string());
                                    }
                                } else {
                                    self.author = Some(clean_value.to_string());
                                }
                            }
                            "requires-python" => {
                                self.python_requires = Some(clean_value.to_string())
                            }
                            _ => {
                                self.settings
                                    .insert(key.to_string(), clean_value.to_string());
                            }
                        }
                    }
                } else if in_dependencies {
                    let parts: Vec<&str> = line.splitn(2, '=').collect();
                    if parts.len() == 2 {
                        let package = parts[0].trim();
                        let version = parts[1].trim();
                        self.dependencies.push(format!("{package} {version}"));
                    }
                }
            }
        }

        Ok(())
    }

    /// Parse an __init__.py file
    pub fn parse_init_py(&mut self, module_path: &str, content: &str) -> Result<()> {
        // Store the content for later use in compilation
        self.init_code
            .insert(module_path.to_string(), content.to_string());
        Ok(())
    }

    /// Parse a conftest.py file
    pub fn parse_conftest_py(&mut self, _content: &str) -> Result<()> {
        // For now, we just store a marker that we have pytest configuration
        self.settings
            .insert("has_pytest".to_string(), "true".to_string());
        Ok(())
    }
}

/// Load and parse configuration files from a Python project
pub fn load_project_config<P: AsRef<Path>>(project_dir: P) -> Result<ProjectConfig> {
    let project_dir = project_dir.as_ref();
    let mut config = ProjectConfig::new();

    // Try to get project name from directory
    if let Some(dir_name) = project_dir.file_name() {
        config.name = dir_name.to_string_lossy().to_string();
    }

    // Check for setup.py
    let setup_py_path = project_dir.join("setup.py");
    if setup_py_path.exists() && setup_py_path.is_file() {
        match fs::read_to_string(&setup_py_path) {
            Ok(content) => {
                config
                    .parse_setup_py(&content)
                    .context("Failed to parse setup.py")?;
            }
            Err(e) => {
                println!("Warning: Could not read setup.py: {e}");
            }
        }
    }

    // Check for __version__.py or __about__.py in project root
    for version_file in &["__version__.py", "__about__.py"] {
        let version_path = project_dir.join(version_file);
        if version_path.exists() && version_path.is_file() {
            match fs::read_to_string(&version_path) {
                Ok(content) => {
                    config
                        .parse_version_file(&content)
                        .context(format!("Failed to parse {version_file}"))?;
                }
                Err(e) => {
                    println!("Warning: Could not read {version_file}: {e}");
                }
            }
        }
    }

    // Check for pyproject.toml
    let pyproject_path = project_dir.join("pyproject.toml");
    if pyproject_path.exists() && pyproject_path.is_file() {
        match fs::read_to_string(&pyproject_path) {
            Ok(content) => {
                config
                    .parse_pyproject_toml(&content)
                    .context("Failed to parse pyproject.toml")?;
            }
            Err(e) => {
                println!("Warning: Could not read pyproject.toml: {e}");
            }
        }
    }

    // Look for __init__.py files in the project
    find_and_parse_init_files(&mut config, project_dir, "")?;

    // Check for conftest.py (pytest configuration)
    find_conftest_files(&mut config, project_dir)?;

    Ok(config)
}

/// Find and parse all __init__.py files in the project
fn find_and_parse_init_files(
    config: &mut ProjectConfig,
    dir: &Path,
    parent_module: &str,
) -> Result<()> {
    // Skip directories that shouldn't be included
    if should_skip_directory(dir) {
        return Ok(());
    }

    let init_path = dir.join("__init__.py");
    if init_path.exists() && init_path.is_file() {
        match fs::read_to_string(&init_path) {
            Ok(content) => {
                // Calculate module path
                let module_path = if parent_module.is_empty() {
                    dir.file_name()
                        .unwrap_or_default()
                        .to_string_lossy()
                        .to_string()
                } else {
                    format!(
                        "{}.{}",
                        parent_module,
                        dir.file_name().unwrap_or_default().to_string_lossy()
                    )
                };

                config
                    .parse_init_py(&module_path, &content)
                    .context(format!("Failed to parse {}", init_path.display()))?;
            }
            Err(e) => {
                println!("Warning: Could not read {}: {}", init_path.display(), e);
            }
        }
    }

    // Recursively check subdirectories
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            // Calculate the new parent module
            let new_parent = if parent_module.is_empty() {
                dir.file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string()
            } else {
                format!(
                    "{}.{}",
                    parent_module,
                    dir.file_name().unwrap_or_default().to_string_lossy()
                )
            };

            find_and_parse_init_files(config, &path, &new_parent)?;
        }
    }

    Ok(())
}

/// Find and parse all conftest.py files in the project
fn find_conftest_files(config: &mut ProjectConfig, dir: &Path) -> Result<()> {
    // Skip directories that shouldn't be included
    if should_skip_directory(dir) {
        return Ok(());
    }

    let conftest_path = dir.join("conftest.py");
    if conftest_path.exists() && conftest_path.is_file() {
        match fs::read_to_string(&conftest_path) {
            Ok(content) => {
                config
                    .parse_conftest_py(&content)
                    .context(format!("Failed to parse {}", conftest_path.display()))?;
            }
            Err(e) => {
                println!("Warning: Could not read {}: {}", conftest_path.display(), e);
            }
        }
    }

    // Recursively check subdirectories
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() && !should_skip_directory(&path) {
            find_conftest_files(config, &path)?;
        }
    }

    Ok(())
}

/// Check if a directory should be skipped during scanning
fn should_skip_directory(dir: &Path) -> bool {
    if let Some(dir_name) = dir.file_name() {
        let dir_name = dir_name.to_string_lossy();
        return dir_name.starts_with("__pycache__") || // Python cache
            dir_name.starts_with('.') ||           // Hidden directories
            dir_name == "venv" ||                  // Common virtual environment name
            dir_name.starts_with("env") ||         // Another common virtual environment name  
            dir_name == "node_modules" ||          // JavaScript dependencies
            dir_name.contains("site-packages") ||  // Installed packages
            dir_name == "dist" ||                  // Distribution directory
            dir_name == "build"; // Build directory
    }
    false
}

/// Check if a file is a configuration file
pub fn is_config_file(filename: &str) -> bool {
    let filename_lower = filename.to_lowercase();
    filename_lower == "setup.py"
        || filename_lower == "__init__.py"
        || filename_lower == "__about__.py"
        || filename_lower == "__version__.py"
        || filename_lower == "pyproject.toml"
        || filename_lower == "conftest.py"
}

/// Extract module initialization code for a specific module
pub fn get_module_init_code<'a>(config: &'a ProjectConfig, module_name: &str) -> Option<&'a str> {
    config.init_code.get(module_name).map(|s| s.as_str())
}

/// Extract all Python files from project with config awareness
pub fn collect_python_files_with_config<P: AsRef<Path>>(
    project_dir: P,
    config: &ProjectConfig,
) -> Result<HashMap<String, (PathBuf, String)>> {
    let project_dir = project_dir.as_ref();
    let mut files = HashMap::new();

    collect_python_files_recursive(project_dir, project_dir, &mut files, config)?;

    Ok(files)
}

/// Recursively collect Python files from a directory with config awareness
fn collect_python_files_recursive(
    root_dir: &Path,
    current_dir: &Path,
    files: &mut HashMap<String, (PathBuf, String)>,
    _config: &ProjectConfig,
) -> Result<()> {
    for entry in fs::read_dir(current_dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            // Skip directories that shouldn't be included
            if should_skip_directory(&path) {
                continue;
            }

            // Recursively scan subdirectory
            collect_python_files_recursive(root_dir, &path, files, _config)?;
        } else if path.is_file() && path.extension().is_some_and(|ext| ext == "py") {
            // Skip configuration files
            if let Some(file_name) = path.file_name() {
                let file_name = file_name.to_string_lossy();
                if is_config_file(&file_name) {
                    // Skip known config files
                    continue;
                }
            }

            // Read file content
            match fs::read_to_string(&path) {
                Ok(content) => {
                    // Calculate module path
                    let rel_path = path
                        .strip_prefix(root_dir)
                        .unwrap_or(&path)
                        .to_string_lossy()
                        .to_string();

                    // Add the file to our collection
                    files.insert(rel_path.clone(), (path.clone(), content));
                }
                Err(e) => {
                    println!("Warning: Failed to read {}: {}", path.display(), e);
                }
            }
        }
    }

    Ok(())
}