-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathfile.py
More file actions
74 lines (60 loc) · 2.2 KB
/
Copy pathfile.py
File metadata and controls
74 lines (60 loc) · 2.2 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
from __future__ import print_function
import os, sys
class File():
#
# Constructor
#
def __init__(self):
self.file_lines = []
self.__file_input_name = ""
self.__complete_content = ""
self.__file_input_stdin = None
# Read STDIN and the contents of the file
self.parse_stdin()
if self.__file_input_name is not "" or self.__file_input_stdin is not None:
self.read_content_of_file()
else:
exit("Something went wrong parsing your input file. Exiting...")
if len(self.file_lines) == 0:
exit("Nothing was read in to memory from the file. Exiting...")
#
# Parse STDIN if any was presented when the program was run
#
def parse_stdin(self):
# Use sys.argv or sys.stdin to get the file input from the command line
if len(sys.argv) > 1 and sys.argv[1] is not None:
# Recognize that sys.argv has a file argument in it
self.__file_input_name = sys.argv[1]
elif not sys.stdin.isatty():
# isatty() makes sure stdin has data http://man7.org/linux/man-pages/man3/isatty.3.html
# Recognize that there is a file object in sys.stdin
self.__file_input_stdin = sys.stdin
else:
# Display the error to the console
print("OOPs! There was an issue locating your input file")
exit("Please provide a valid filename or file as a CLI argument")
#
# The the contents of the file that was presented
#
def read_content_of_file(self):
# -------- Attempt to open the file and read contents --------
# Open file, read contents into a list to be parsed
try:
# Open the file and read the contents into a
if self.__file_input_stdin is None:
with open(self.__file_input_name, 'r') as file_obj:
self.__complete_content = file_obj.read()
else:
self.__complete_content = self.__file_input_stdin.read()
# Explode or split the string contents to a list
self.file_lines = self.__complete_content.split("\n")
except ValueError:
# Write the raised error
error_raised = "Error loading file: " + str(sys.exc_info()[0])
# Display the error to the console
print(error_raised)
exit("This program needs an input file to continue. Exiting...")