Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Himmelbauer P authored and Roberto Himmelbauer P committed Apr 17, 2020
0 parents commit eab8660
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Compiled python modules.
*.pyc

# Setuptools distribution folder.
/dist/

# Python egg metadata, regenerated from source files by setuptools.
/*.egg-info
Empty file added LICENSE
Empty file.
Empty file added README.md
Empty file.
Empty file added egrep/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions egrep/egrep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import subprocess
from os import path


class EgrepErrors(Exception):
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None

def __str__(self):
if self.message:
return f"EGREP Exception Raised: {self.message}"
else:
return "EGREP Exception Raised"

class egrep:
ERROR_INVALID_NUMBER_OF_ARGUMENTS = "Invalid number of arguments. Need at least two, eg:\nEGREP(\"regex\",\"file-path\")"
ERROR_FILE_DOES_NOT_EXIST_AT = "File does not exist at: "

EGREP = "egrep "

MINIMUM_ARGUMENTS = 2

def __init__(self, *args):
try:
if(self.validateArguments(*args)):
self.command = self.EGREP + " ".join(args)

except EgrepErrors as error:
print(error)

def execute(self):
command = subprocess.Popen([self.command], stdout=subprocess.PIPE,shell=True)
(command_output, error) = command.communicate()
if error:
raise EgrepErrors(error)
else:
for word in self.createOutputList(command_output):
print(word)

def createOutputList(self, command_output):
return str(command_output)[1:].split("\\n")

def validateArguments(self, *args):
self.isValidLength(*args)
self.isValidFilePath(args[-1])
return True

def isValidLength(self, *args):
if len(args) < self.MINIMUM_ARGUMENTS:
raise EgrepErrors(self.ERROR_INVALID_NUMBER_OF_ARGUMENTS)

def isValidFilePath(self, filePath):
if path.exists(filePath) == False:
raise EgrepErrors(self.ERROR_FILE_DOES_NOT_EXIST_AT + filePath)

18 changes: 18 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from setuptools import setup

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name='egrep',
version='0.0.1',
author='Roberto Himmelbauer',
author_email='robertoh89@gmail.com',
description='A simple library for exectuing egrep linux commando to files',
long_description=long_description,
long_description_content_type="text/markdown",
url='https://github.com/sean-mcclure/datapeek_py',
license='MIT',
packages=['egrep'],
zip_safe=False
)
Empty file added tests
Empty file.

0 comments on commit eab8660

Please sign in to comment.