Skip to content

Commit

Permalink
feat: python support
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Apr 21, 2024
1 parent a945140 commit b994ed1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/cargo-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
git clone https://github.com/typescript-eslint/typescript-eslint --depth 128
# golang
git clone https://github.com/gin-gonic/gin --depth 128
# python
git clone https://github.com/tiangolo/fastapi --depth 128
# execute bin
cp ./target/debug/gossiphs ./gossiphs
Expand All @@ -52,3 +54,7 @@ jobs:
cd gin
time ../gossiphs interactive --dry
cd ..
cd fastapi
time ../gossiphs interactive --dry
cd ..
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tree-sitter-rust = "0.20.4"
petgraph = "0.6.4"
tree-sitter-typescript = "0.20.5"
tree-sitter-go = "0.20.0"
tree-sitter-python = "0.20.4"
serde = { version = "1.0.197", features = ["derive"] }
indicatif = "0.17.8"
inquire = "0.7.4"
Expand Down
41 changes: 41 additions & 0 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum Extractor {
Rust,
TypeScript,
Go,
Python,
}

impl Extractor {
Expand All @@ -24,6 +25,10 @@ impl Extractor {
let lang = &tree_sitter_go::language();
self._extract(f, s, lang)
}
Extractor::Python => {
let lang = &tree_sitter_python::language();
self._extract(f, s, lang)
}
};
}

Expand Down Expand Up @@ -227,4 +232,40 @@ func injectV1Group(v1group *gin.RouterGroup) {
info!("symbol: {:?} {:?}", each.name, each.kind);
})
}

#[test]
fn extract_python() {
let symbols = Extractor::Python.extract(
&String::from("abc"),
&String::from(
r#"
def normal_fff(self, env_config: EnvConfig):
pass
class BaseStep(object):
def apply(self, env_config: EnvConfig, result: ResultContext):
raise NotImplementedError
def name(self) -> str:
raise NotImplementedError
def config_name(self) -> str:
return self.name().replace("-", "_")
def get_mod_config(self, env_config: EnvConfig):
return getattr(
env_config._repo_config.modules,
self.config_name(),
)
def enabled(self, env_config: EnvConfig) -> bool:
mod_config = self.get_mod_config(env_config)
return mod_config.enabled
"#,
),
);
symbols.iter().for_each(|each| {
info!("symbol: {:?}", each);
})
}
}
9 changes: 9 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ impl Graph {
};
file_contexts.push(file_context);
}
"py" => {
let symbols = Extractor::Python.extract(each_file, file_content);
let file_context = FileContext {
// use the relative path as key
path: each_file.clone(),
symbols,
};
file_contexts.push(file_context);
}
_ => {}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ pub fn get_rule(extractor_type: &Extractor) -> Rule {
export_grammar: r#"
(function_declaration name: (identifier) @exported_symbol)
(method_declaration name: (field_identifier) @exported_symbol)
"#,
},

Extractor::Python => Rule {
import_grammar: r#"
(identifier) @variable_name
"#,
export_grammar: r#"
(function_definition name: (identifier) @exported_symbol)
(class_definition name: (identifier) @exported_symbol)
"#,
},
}
Expand Down

0 comments on commit b994ed1

Please sign in to comment.