-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirman.py
74 lines (49 loc) · 1.9 KB
/
dirman.py
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
#! /usr/bin/env/python3
#@author -> GEM-404 | EPHANTUS MACHARIA
"""
This module is like the terminal ls command, but for
python3 modules and classes... Whenever I wanna see what
a certain module has, or what to use in it, I will be using
this pyls command to see how it contains.
The module is not complete since I know nothing of The
argparse module and wanna implement it some time to come...
This will help parse user's arguments when using it as a command...
"""
# import argparse
import importlib
import sys
from typing import Union
def get_dirs(package) -> Union[str, list]:
if '.' in package:
pack, sub_pack = package.split('.')
package = importlib.import_module(name=pack, package=sub_pack)
else:
try:
package = importlib.import_module(package)
except ModuleNotFoundError:
return f"{package} not in Python3 library or not yet installed"
dir_list = dir(package)
filterd_dirs: list = [directory for directory in dir_list
if not directory.startswith('__')]
if len(filterd_dirs) < 1:
return f"{package} has no sub-packages"
return filterd_dirs
def get_dir_starters(args: list, char: str) -> list:
return [directory for directory in args if directory.startswith(char)]
# check whether the second argument given is a single letter
def check_sec_args(args: list):
char = sys.argv[2]
if len(char) == 1 and char.isalpha:
return get_dir_starters(args=args, char=char)
def check_present_args(package: str, sub_package: str) -> str:
"""Checks whether a sub_module is present in a module"""
pack_list = get_dirs(package)
checker = sub_package in pack_list
if checker:
return f"{sub_package} in {package}"
return f"{checker}... {sub_package} not present in {package}"
def main():
package = sys.argv[1]
print(get_dirs(package))
if __name__ == '__main__':
main()