1- use std:: process:: Command ;
2- use std:: path:: Path ;
3-
41/// Classifies Python imports as standard library or third-party/project-specific.
52pub struct ImportClassifier {
6- /// List of standard library module names.
7- stdlib_modules : Vec < String >
83}
94
105impl ImportClassifier {
116 /// Creates a new `ImportClassifier` and initializes the
127 /// list of standard library modules.
138 pub fn new ( ) -> Self {
14- let stdlib_modules: Vec < String > = [
15- "__phello__" , "_pyrepl" , "asyncio" , "collections" , "compression" ,
16- "concurrent" , "ctypes" , "curses" , "dbm" , "email" , "encodings" ,
17- "ensurepip" , "html" , "http" , "idlelib" , "importlib" , "json" ,
18- "logging" , "multiprocessing" , "pathlib" , "profiling" , "pydoc_data" ,
19- "re" , "site-packages" , "sqlite3" , "string" , "sysconfig" , "test" ,
20- "tkinter" , "tomllib" , "turtledemo" , "unittest" , "urllib" , "venv" ,
21- "wsgiref" , "xml" , "xmlrpc" , "zipfile" , "zoneinfo" , "__future__" , "__hello__" ,
22- "_aix_support" , "_android_support" , "_apple_support" , "_ast_unparse" ,
23- "_collections_abc" , "_colorize" , "_compat_pickle" , "_ios_support" , "_markupbase" ,
24- "_opcode_metadata" , "_osx_support" , "_py_abc" , "_py_warnings" , "_pydatetime" ,
25- "_pydecimal" , "_pyio" , "_pylong" , "_sitebuiltins" , "_strptime" , "_threading_local" ,
26- "_weakrefset" , "abc" , "annotationlib" , "antigravity" , "argparse" , "ast" , "base64" ,
27- "bdb" , "bisect" , "bz2" , "cProfile" , "calendar" , "cmd" , "code" , "codecs" ,
28- "codeop" , "colorsys" , "compileall" , "configparser" , "contextlib" , "contextvars" ,
29- "copy" , "copyreg" , "csv" , "dataclasses" , "datetime" , "decimal" , "difflib" ,
30- "dis" , "doctest" , "enum" , "filecmp" , "fileinput" , "fnmatch" , "fractions" ,
31- "ftplib" , "functools" , "genericpath" , "getopt" , "getpass" , "gettext" , "glob" ,
32- "graphlib" , "gzip" , "hashlib" , "heapq" , "hmac" , "imaplib" , "inspect" , "io" ,
33- "ipaddress" , "keyword" , "linecache" , "locale" , "lzma" , "mailbox" , "mimetypes" ,
34- "modulefinder" , "netrc" , "ntpath" , "nturl2path" , "numbers" , "opcode" , "operator" ,
35- "optparse" , "os" , "pdb" , "pickle" , "pickletools" , "pkgutil" , "platform" , "plistlib" ,
36- "poplib" , "posixpath" , "pprint" , "profile" , "pstats" , "pty" , "py_compile" , "pyclbr" ,
37- "pydoc" , "queue" , "quopri" , "random" , "reprlib" , "rlcompleter" , "runpy" , "sched" ,
38- "secrets" , "selectors" , "shelve" , "shlex" , "shutil" , "signal" , "site" , "smtplib" ,
39- "socket" , "socketserver" , "ssl" , "stat" , "statistics" , "stringprep" , "struct" ,
40- "subprocess" , "symtable" , "tabnanny" , "tarfile" , "tempfile" , "textwrap" , "this" ,
41- "threading" , "timeit" , "token" , "tokenize" , "trace" , "traceback" , "tracemalloc" ,
42- "tty" , "turtle" , "types" , "typing" , "uuid" , "warnings" , "wave" , "weakref" ,
43- "webbrowser" , "zipapp" , "zipimport"
44- ] . iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ;
45-
46- Self { stdlib_modules}
9+ Self { }
4710 }
4811
4912 /// Returns `true` if the import is project level.
@@ -59,38 +22,17 @@ impl ImportClassifier {
5922 /// assert_eq!(classifier.is_eligible(&"infra.db.modules".to_string()), true);
6023 /// assert_eq!(classifier.is_eligible(&"os".to_string()), false);
6124 /// ```
62- pub fn is_eligible ( & self , import : & String ) -> bool {
63- // Checks is import from standard
64- // library or not
65- true
66- // if self.stdlib_modules.contains(import) {
67- // return false;
68- // } else if self.is_third_party(import) {
69- // return false;
70- // } else {
71- // return true;
72- // }
73- }
25+ pub fn is_eligible (
26+ & self ,
27+ import : & String ,
28+ root_dirs : & Vec < String > ,
29+ ) -> bool {
7430
75- // fn is_third_party(&self, import: &str) -> bool {
76- // let site_packages = self.get_site_packages();
77- // for sp in site_packages {
78- // let path = Path::new(&sp).join(import);
79- // println!("{}", path.to_string_lossy());
80- // if path.exists() {
81- // return true;
82- // }
83- // }
84- // false
85- // }
86-
87- // fn get_site_packages(&self) -> Vec<String> {
88- // let output = Command::new("python3")
89- // .args(&["-c", "import site; print('\\n'.join(site.getsitepackages()))"])
90- // .output()
91- // .expect("Failed to run python3");
92-
93- // let stdout = String::from_utf8_lossy(&output.stdout);
94- // stdout.lines().map(|s| s.to_string()).collect()
95- // }
31+ for dir in root_dirs {
32+ if import. starts_with ( dir) {
33+ return true ;
34+ }
35+ }
36+ return false ;
37+ }
9638}
0 commit comments