Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2870
  • Last Modified:

grep from STDIN python

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
ircpamanager
Asked:
ircpamanager
1 Solution
 
RichieHindleCommented:
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
 
ircpamanagerAuthor Commented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now