22
33from typing import List , Optional , Tuple , Union , Type
44
5- import tldr
65from argparse import ArgumentParser
7- import os
8- import json
9- import subprocess
106
11- class WhatIsHelpPage (object ):
12- """`whatis` makes use of the man page infrastructure of the OS.
13- This also covers the output of `apropos`, `info`, and `man`."""
14-
15- def __init__ (self , name , content : str ):
16- self .page_name = name
17- self .content = content
18-
19- def description (self , detailed = False ) -> str :
20- return self .content
21-
22- @staticmethod
23- def run_whatis (name : str ):
24- return subprocess .run (["whatis" , name ], capture_output = True )
25-
26- @staticmethod
27- def has_page (name : str ) -> bool :
28- process = WhatIsHelpPage .run_whatis (name )
29- return process .returncode == 0
30-
31- @staticmethod
32- def get_page (name : str ) -> 'WhatIsHelpPage' :
33- process = WhatIsHelpPage .run_whatis (name )
34- description = process .stdout .decode ('utf-8' )
35- description = description .split (" - " )[1 ].strip ()
36- return WhatIsHelpPage (name , description )
37-
38- class OSHelpPage (object ):
39- """`help` documents the built-in commands of bash"""
40-
41- def __init__ (self , name , content : str ):
42- self .page_name = name
43- self .content = content
44-
45- def description (self , detailed = False ) -> str :
46- return self .content
47-
48- @staticmethod
49- def run_help (name : str ):
50- return subprocess .run (["/bin/bash" , "-c" , '"help -d {name}"' .format (name = name )], capture_output = True )
51-
52- @staticmethod
53- def has_page (name : str ) -> bool :
54- process = OSHelpPage .run_help (name )
55- return process .returncode == 0
56-
57- @staticmethod
58- def get_page (name : str ) -> 'OSHelpPage' :
59- process = OSHelpPage .run_help (name )
60- return OSHelpPage (name , str (process .stdout ))
61-
62- class TLDRPage (object ):
63-
64- def __init__ (self , name , content : List [str ]):
65- self .page_name = name
66- self .content = content
67-
68- @staticmethod
69- def get_page (name : str ) -> 'TLDRPage' :
70- return TLDRPage (name , tldr .get_page (name ))
71-
72- @staticmethod
73- def has_page (name : str ) -> bool :
74- return tldr .get_page (name ) is not False
75-
76- def description (self , detailed = False ) -> str :
77- description = ""
78- lines = self .content [2 :4 ] if detailed else self .content [2 :3 ]
79- for line in lines :
80- line = line .rstrip ().decode ('utf-8' )
81- if line [0 ] == ">" :
82- line = line [1 :].lstrip ()
83- description = description + line + "\n "
84- return description .rstrip ()
85-
86- class FileOrFolderPage (object ):
87-
88- pages = None
89- with open ("./fspages.json" ) as pages_file :
90- pages = json .load (pages_file )
91-
92- def __init__ (self , path ):
93- self .path = path
94-
95- @staticmethod
96- def is_path (path ):
97- return os .path .exists (path )
98-
99- @staticmethod
100- def get_page (path ) -> Type ['FileOrFolderPage' ]:
101- return FileOrFolderPage (path )
102-
103- def description (self , detailed = False ) -> str :
104- return FileOrFolderPage .pages .get (self .path , "no page found" )
7+ from pagesources import BashHelpPage , FSPathPage , TLDRPage , WhatIsPage
1058
1069def create_parser () -> ArgumentParser :
10710 parser = ArgumentParser (prog = "wat" )
@@ -118,12 +21,12 @@ def parse_arguments() -> List[str]:
11821 return arguments .name_of_this
11922
12023def lookup_page (name : str ) -> str :
121- if FileOrFolderPage .is_path (name ):
122- return FileOrFolderPage .get_page (name )
123- elif WhatIsHelpPage .has_page (name ):
124- return WhatIsHelpPage .get_page (name )
125- elif OSHelpPage .has_page (name ):
126- return OSHelpPage .get_page (name )
24+ if FSPathPage .is_path (name ):
25+ return FSPathPage .get_page (name )
26+ elif WhatIsPage .has_page (name ):
27+ return WhatIsPage .get_page (name )
28+ elif BashHelpPage .has_page (name ):
29+ return BashHelpPage .get_page (name )
12730 elif TLDRPage .has_page (name ):
12831 return TLDRPage .get_page (name )
12932 else :
0 commit comments