Solved

grep from STDIN python

Posted on 2006-11-30
2
2,794 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Hue issue with import 12 77
Is my Java RTS game window portable to Python? 7 99
Python algorithm 20 151
Python multiple IF statements 4 83
Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

831 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