Skip to content

Commit d0db886

Browse files
committed
Moves page classes to individual files and into a folder
1 parent f9d652a commit d0db886

File tree

7 files changed

+136
-104
lines changed

7 files changed

+136
-104
lines changed

pagesources/AbstractPage.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from abc import abstractmethod
2+
3+
class AbstractPage(object):
4+
5+
@classmethod
6+
@abstractmethod
7+
def has_page(cls, name: str) -> bool:
8+
pass
9+
10+
@classmethod
11+
@abstractmethod
12+
def get_page(cls, name: str):
13+
pass
14+
15+
@abstractmethod
16+
def description(self) -> str:
17+
pass

pagesources/BashHelpPage.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List, Optional, Tuple, Union, Type
2+
from .AbstractPage import AbstractPage
3+
import subprocess
4+
5+
class BashHelpPage(AbstractPage):
6+
"""`help` documents the built-in commands of bash"""
7+
8+
def __init__(self, name, content: str):
9+
self.page_name = name
10+
self.content = content
11+
12+
def description(self, detailed = False) -> str:
13+
return self.content
14+
15+
@classmethod
16+
def run_help(cls, name: str):
17+
return subprocess.run(["/bin/bash", "-c", '"help -d {name}"'.format(name=name)], capture_output=True)
18+
19+
@classmethod
20+
def has_page(cls, name: str) -> bool:
21+
process = cls.run_help(name)
22+
return process.returncode == 0
23+
24+
@classmethod
25+
def get_page(cls, name: str) -> 'BashHelpPage':
26+
process = cls.run_help(name)
27+
return cls(name, str(process.stdout))

pagesources/FSPathPage.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List, Optional, Tuple, Union, Type
2+
from .AbstractPage import AbstractPage
3+
import json, os
4+
5+
class FSPathPage(AbstractPage):
6+
7+
pages = None
8+
with open("./fspages.json") as pages_file:
9+
pages = json.load(pages_file)
10+
11+
def __init__(self, path):
12+
self.path = path
13+
14+
@classmethod
15+
def is_path(cls, path):
16+
return os.path.exists(path)
17+
18+
@classmethod
19+
def get_page(cls, path) -> Type['FSPathPage']:
20+
return cls(path)
21+
22+
def description(self, detailed = False) -> str:
23+
return FSPathPage.pages.get(self.path, "no page found")

pagesources/TLDRPage.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List, Optional, Tuple, Union, Type
2+
from .AbstractPage import AbstractPage
3+
4+
import tldr
5+
6+
class TLDRPage(AbstractPage):
7+
8+
def __init__(self, name, content: List[str]):
9+
self.page_name = name
10+
self.content = content
11+
12+
@classmethod
13+
def get_page(cls, name: str) -> 'TLDRPage':
14+
return cls(name, tldr.get_page(name))
15+
16+
@classmethod
17+
def has_page(cls, name: str) -> bool:
18+
return tldr.get_page(name) is not False
19+
20+
def description(self, detailed = False) -> str:
21+
description = ""
22+
lines = self.content[2:4] if detailed else self.content[2:3]
23+
for line in lines:
24+
line = line.rstrip().decode('utf-8')
25+
if line[0] == ">":
26+
line = line[1:].lstrip()
27+
description = description + line + "\n"
28+
return description.rstrip()

pagesources/WhatIsPage.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List, Optional, Tuple, Union, Type
2+
from .AbstractPage import AbstractPage
3+
import subprocess
4+
5+
class WhatIsPage(AbstractPage):
6+
"""`whatis` makes use of the man page infrastructure of the OS.
7+
This also covers the output of `apropos`, `info`, and `man`."""
8+
9+
def __init__(self, name, content: str):
10+
self.page_name = name
11+
self.content = content
12+
13+
def description(self, detailed = False) -> str:
14+
return self.content
15+
16+
@classmethod
17+
def run_whatis(cls, name: str):
18+
return subprocess.run(["whatis", name], capture_output=True)
19+
20+
@classmethod
21+
def has_page(cls, name: str) -> bool:
22+
process = cls.run_whatis(name)
23+
return process.returncode == 0
24+
25+
@classmethod
26+
def get_page(cls, name: str) -> 'WhatIsPage':
27+
process = cls.run_whatis(name)
28+
description = process.stdout.decode('utf-8')
29+
description = description.split(" - ")[1].strip()
30+
return cls(name, description)

pagesources/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .BashHelpPage import BashHelpPage
2+
from .TLDRPage import TLDRPage
3+
from .FSPathPage import FSPathPage
4+
from .WhatIsPage import WhatIsPage

wat.py

Lines changed: 7 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,9 @@
22

33
from typing import List, Optional, Tuple, Union, Type
44

5-
import tldr
65
from 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

1069
def create_parser() -> ArgumentParser:
10710
parser = ArgumentParser(prog="wat")
@@ -118,12 +21,12 @@ def parse_arguments() -> List[str]:
11821
return arguments.name_of_this
11922

12023
def 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

Comments
 (0)