Skip to content

Commit c941026

Browse files
authored
Merge pull request Python-World#481 from Alex108-lab/master
Creation of a new project, cat command
2 parents 16201d0 + a00e036 commit c941026

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

projects/cat_command/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Cat Command
2+
3+
Cat command implementation using python
4+
5+
## Requirements
6+
Not necessary, only python libraries are used
7+
8+
# Run program
9+
On linux you can use
10+
``` ./cat.py [path] ```
11+
12+
Or
13+
```python ./cat.py [path]```
14+
15+
Example
16+
```./cat.py ./test_cat.txt```
17+
18+
## Author Name
19+
20+
[Alexander Monterrosa](https://github.com/Alex108-lab)
21+

projects/cat_command/cat.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/python
2+
3+
import argparse
4+
from pathlib import Path
5+
from sys import stderr, stdout
6+
import os
7+
8+
class CatError(Exception):
9+
pass
10+
11+
class Logger:
12+
def __init__(self, verbosity=False):
13+
self.verbose = verbosity
14+
15+
def error(self, message):
16+
print(f'ERROR: {message}')
17+
18+
logger = Logger()
19+
20+
'''
21+
Read the selected text file
22+
23+
Example:
24+
your/path/file.txt
25+
'''
26+
def readFile(src: Path):
27+
28+
'''
29+
if the given path is a directory
30+
ERROR the path is a directory
31+
'''
32+
if src.is_dir():
33+
34+
logger.error(f'The path {src}: is a directory')
35+
36+
else:
37+
38+
with open(src, 'r') as f:
39+
for lines in f:
40+
print(lines, end='')
41+
42+
def cli() -> argparse.Namespace:
43+
parser = argparse.ArgumentParser(
44+
prog='cat',
45+
description='cat command implementation in python',
46+
epilog='Example: your/path/file.txt'
47+
)
48+
49+
parser.add_argument(
50+
'source',
51+
type=Path,
52+
help='Source file'
53+
)
54+
55+
return parser.parse_args()
56+
57+
def main():
58+
59+
args = cli()
60+
61+
try:
62+
63+
readFile(args.source)
64+
65+
except CatError as e:
66+
67+
logger.error(e)
68+
69+
exit(1)
70+
71+
except KeyboardInterrupt:
72+
73+
logger.error('\nInterrupt')
74+
75+
'''
76+
Start the program
77+
'''
78+
if __name__ == '__main__':
79+
main()

projects/cat_command/test_cat.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam consequat elit vel pretium pellentesque. Vivamus commodo leo sed lorem auctor elementum. Maecenas ut erat ut velit maximus luctus. Vestibulum varius justo et mauris tristique pharetra rutrum porta nulla. Nam porttitor lobortis posuere. Aenean erat nisl, aliquam id molestie in, luctus quis mauris. Donec fermentum vel quam in consectetur. Aliquam nec mauris quis tellus faucibus fermentum. Suspendisse ac maximus sem. Fusce feugiat non dui non gravida. Ut ac eleifend tellus. Vivamus consectetur finibus nisi. Etiam id odio vitae arcu aliquam tincidunt nec sit amet quam.

0 commit comments

Comments
 (0)