Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

grep from STDIN python

Posted on 2006-11-30
2
Medium Priority
?
2,846 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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

715 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