Solved

grep from STDIN python

Posted on 2006-11-30
2
2,777 Views
Last Modified: 2012-08-13
Hello everyone,
I have been trying to grep certain lines out of STDIN. Basically this is a little app that email is piped to. It works fine, the onlt thing is I get the whole email. I need only to strip out from and subject. Here is what I have so far
#!/usr/bin/python

import wx
import sys
import os
email = sys.stdin.read()
email_info = os.popen('grep -i \"^[subject:|from:]\" %s' %email)<--------------------------this is where i run into problems!
class EvoTray(wx.TaskBarIcon):
        def __init__(self, frame):
                wx.TaskBarIcon.__init__(self)

                self.frame = frame
                self.SetIcon(wx.Icon('/home/administrator/Desktop/env.png',wx.BITMAP_TYPE_PNG), 'EvoTray')
                self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=1)
                self.Bind(wx.EVT_MENU, self.OnTaskBarDeactivate, id=2)
                self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=3)

        def CreatePopupMenu(self):
                menu = wx.Menu()
                menu.Append(1, 'Show')
                menu.Append(2, 'Hide')
                menu.Append(3, 'Close')
                return menu

        def OnTaskBarClose(self, event):
                self.frame.Close()

        def OnTaskBarActivate(self, event):
                if not self.frame.IsShown():
                        self.frame.Show()

        def OnTaskBarDeactivate(self, event):
                if self.frame.IsShown():
                        self.frame.Hide()

class MyFrame(wx.Frame):
        def __init__(self, parent, id, title):
                wx.Frame.__init__(self, parent, id, title, (-1, -1),
(290, 280))
                self.txt_ctrl = wx.TextCtrl(self,-1,email_info,style=wx.TE_MULTILINE|wx.TE_BESTWRAP)
                self.tskic = EvoTray(self)
                self.Centre()
                self.Bind(wx.EVT_CLOSE, self.OnClose)

        def OnClose(self, event):
                self.tskic.Destroy()
                self.Destroy()

class MyApp(wx.App):
        def OnInit(self):
                frame = MyFrame(None, -1, 'EvoMailNotification')
                frame.Show(True)
                self.SetTopWindow(frame)
                return True

app = MyApp(0)
app.MainLoop()


thanks in advance.
0
Comment
Question by:ircpamanager
2 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 18046834
Why not use Python to strip out the pieces you need?  Here's a full working example (with an example email hard-coded for testing purposes):

import wx
import sys
import os
import re

def get_subject_and_from(email_text):
    return '\n'.join(re.findall(r'(?m)^(Subject:.*|^From:.*)', email_text))

email = """\
Subject: Hello
From: me
To: you

Hello!\n"""  # Replace this with "email = sys.stdin.read()" to read from STDIN

email_info = get_subject_and_from(email)
class EvoTray(wx.TaskBarIcon):
        def __init__(self, frame):
                wx.TaskBarIcon.__init__(self)

                self.frame = frame
                self.SetIcon(wx.Icon('/home/administrator/Desktop/env.png',wx.BITMAP_TYPE_PNG), 'EvoTray')
                self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=1)
                self.Bind(wx.EVT_MENU, self.OnTaskBarDeactivate, id=2)
                self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=3)

        def CreatePopupMenu(self):
                menu = wx.Menu()
                menu.Append(1, 'Show')
                menu.Append(2, 'Hide')
                menu.Append(3, 'Close')
                return menu

        def OnTaskBarClose(self, event):
                self.frame.Close()

        def OnTaskBarActivate(self, event):
                if not self.frame.IsShown():
                        self.frame.Show()

        def OnTaskBarDeactivate(self, event):
                if self.frame.IsShown():
                        self.frame.Hide()

class MyFrame(wx.Frame):
        def __init__(self, parent, id, title):
                wx.Frame.__init__(self, parent, id, title, (-1, -1),
(290, 280))
                self.txt_ctrl = wx.TextCtrl(self,-1,email_info,style=wx.TE_MULTILINE|wx.TE_BESTWRAP)
                self.tskic = EvoTray(self)
                self.Centre()
                self.Bind(wx.EVT_CLOSE, self.OnClose)

        def OnClose(self, event):
                self.tskic.Destroy()
                self.Destroy()

class MyApp(wx.App):
        def OnInit(self):
                frame = MyFrame(None, -1, 'EvoMailNotification')
                frame.Show(True)
                self.SetTopWindow(frame)
                return True

app = MyApp(0)
app.MainLoop()
0
 
LVL 5

Author Comment

by:ircpamanager
ID: 18046964
I could not figure that out for the life of me, I did not know of module re. as you can tell ne to Python. Thanks alot.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now