Solved

grep from STDIN python

Posted on 2006-11-30
2
2,826 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
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 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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

630 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