access abook addressbook with curses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabian Braennstroem

    access abook addressbook with curses

    Hi,

    I want to get access to my abook address file with python.
    Does anyone have some python lines to achive this using
    curses? If not, maybe anybody has small python program doing
    it with a gui!?

    Greetings!
    Fabian

  • Ben C

    #2
    Re: access abook addressbook with curses

    On 2006-08-05, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
    Hi,
    >
    I want to get access to my abook address file with python.
    Does anyone have some python lines to achive this using
    curses? If not, maybe anybody has small python program doing
    it with a gui!?
    You can just parse the abook addressbook with the ConfigParser, try
    this:

    import os
    from ConfigParser import *

    abook = ConfigParser()
    abook.read(os.e nviron["HOME"] + "/.abook/addressbook")

    for s in abook.sections( ):
    print abook.items(s)

    Comment

    • Fabian Braennstroem

      #3
      Re: access abook addressbook with curses

      Hi Ben,

      * Ben C <spamspam@spam. eggswrote:
      On 2006-08-05, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
      >Hi,
      >>
      >I want to get access to my abook address file with python.
      >Does anyone have some python lines to achive this using
      >curses? If not, maybe anybody has small python program doing
      >it with a gui!?
      >
      You can just parse the abook addressbook with the ConfigParser, try
      this:
      >
      import os
      from ConfigParser import *
      >
      abook = ConfigParser()
      abook.read(os.e nviron["HOME"] + "/.abook/addressbook")
      >
      for s in abook.sections( ):
      print abook.items(s)
      Thanks! I found a different example too:

      import ConfigParser
      import string

      config = ConfigParser.Co nfigParser()

      config.read("/home/fab/.abook/addressbook")

      # print summary
      print
      for number in [2,200]:
      print string.upper(co nfig.get(str(nu mber), "email"))
      print string.upper(co nfig.get(str(nu mber), "name"))
      print string.upper(co nfig.get(str(nu mber), "city"))
      print string.upper(co nfig.get(str(nu mber), "address"))

      but the problem seems to be that abook does not write every
      field, so I get an exception when there is a field missing:

      Traceback (most recent call last):
      File "configpars er-example-1.py", line 13, in ?
      print string.upper(co nfig.get(str(nu mber), "city"))
      File "/usr/lib/python2.4/ConfigParser.py ", line 520, in get
      raise NoOptionError(o ption, section)
      ConfigParser.No OptionError: No option 'city' in section: '2'

      Section 2 looks like:

      [2]
      name=Andrs Gzi
      email=anes.oi@i k.e
      nick=oz

      Is there a workaround?


      Greetings!
      Fabian

      Comment

      • Ben C

        #4
        Re: access abook addressbook with curses

        On 2006-08-06, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
        Hi Ben,
        >
        * Ben C <spamspam@spam. eggswrote:
        >On 2006-08-05, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
        >>Hi,
        >>>
        >>I want to get access to my abook address file with python.
        >>Does anyone have some python lines to achive this using
        >>curses? If not, maybe anybody has small python program doing
        >>it with a gui!?
        >>
        >You can just parse the abook addressbook with the ConfigParser, try
        >this:
        >>
        >import os
        >from ConfigParser import *
        >>
        >abook = ConfigParser()
        >abook.read(os. environ["HOME"] + "/.abook/addressbook")
        >>
        >for s in abook.sections( ):
        > print abook.items(s)
        >
        Thanks! I found a different example too:
        >
        import ConfigParser
        import string
        >
        config = ConfigParser.Co nfigParser()
        >
        config.read("/home/fab/.abook/addressbook")
        >
        # print summary
        print
        for number in [2,200]:
        print string.upper(co nfig.get(str(nu mber), "email"))
        print string.upper(co nfig.get(str(nu mber), "name"))
        print string.upper(co nfig.get(str(nu mber), "city"))
        print string.upper(co nfig.get(str(nu mber), "address"))
        >
        but the problem seems to be that abook does not write every
        field, so I get an exception when there is a field missing:
        >
        Traceback (most recent call last):
        File "configpars er-example-1.py", line 13, in ?
        print string.upper(co nfig.get(str(nu mber), "city"))
        File "/usr/lib/python2.4/ConfigParser.py ", line 520, in get
        raise NoOptionError(o ption, section)
        ConfigParser.No OptionError: No option 'city' in section: '2'
        >
        Section 2 looks like:
        >
        [2]
        name=Andrs Gzi
        email=anes.oi@i k.e
        nick=oz
        >
        Is there a workaround?
        You can construct the parser with a dictionary of defaults:

        config = ConfigParser.Co nfigParser({"ci ty" : "unknown",
        "zip" : "unknown"})

        that kind of thing.

        Or catch the exceptions. Or use config.options( "2") to see what options
        exist in section 2 before you try to read them.

        Comment

        • Fabian Braennstroem

          #5
          Re: access abook addressbook with curses

          Hi Ben,

          * Ben C <spamspam@spam. eggswrote:
          On 2006-08-06, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
          >Hi Ben,
          >>
          >* Ben C <spamspam@spam. eggswrote:
          >>On 2006-08-05, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
          >>>Hi,
          >>>>
          >>>I want to get access to my abook address file with python.
          >>>Does anyone have some python lines to achive this using
          >>>curses? If not, maybe anybody has small python program doing
          >>>it with a gui!?
          >>>
          >>You can just parse the abook addressbook with the ConfigParser, try
          >>this:
          >>>
          >>import os
          >>from ConfigParser import *
          >>>
          >>abook = ConfigParser()
          >>abook.read(os .environ["HOME"] + "/.abook/addressbook")
          >>>
          >>for s in abook.sections( ):
          >> print abook.items(s)
          >>
          >Thanks! I found a different example too:
          >>
          >import ConfigParser
          >import string
          >>
          >config = ConfigParser.Co nfigParser()
          >>
          >config.read( "/home/fab/.abook/addressbook")
          >>
          ># print summary
          >print
          >for number in [2,200]:
          > print string.upper(co nfig.get(str(nu mber), "email"))
          > print string.upper(co nfig.get(str(nu mber), "name"))
          > print string.upper(co nfig.get(str(nu mber), "city"))
          > print string.upper(co nfig.get(str(nu mber), "address"))
          >>
          >but the problem seems to be that abook does not write every
          >field, so I get an exception when there is a field missing:
          >>
          >Traceback (most recent call last):
          > File "configpars er-example-1.py", line 13, in ?
          > print string.upper(co nfig.get(str(nu mber), "city"))
          > File "/usr/lib/python2.4/ConfigParser.py ", line 520, in get
          > raise NoOptionError(o ption, section)
          >ConfigParser.N oOptionError: No option 'city' in section: '2'
          >>
          >Section 2 looks like:
          >>
          >[2]
          >name=Andrs Gzi
          >email=anes.oi@ ik.e
          >nick=oz
          >>
          >Is there a workaround?
          >
          You can construct the parser with a dictionary of defaults:
          >
          config = ConfigParser.Co nfigParser({"ci ty" : "unknown",
          "zip" : "unknown"})
          >
          that kind of thing.
          >
          Or catch the exceptions. Or use config.options( "2") to see what options
          exist in section 2 before you try to read them.
          Thanks! I will try it out!

          Greetings!
          Fabian

          Comment

          • Ben C

            #6
            Re: access abook addressbook with curses

            On 2006-08-08, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
            Hi Ben,
            >
            * Ben C <spamspam@spam. eggswrote:
            >On 2006-08-06, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
            >>Hi Ben,
            >>>
            >>* Ben C <spamspam@spam. eggswrote:
            >>>On 2006-08-05, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
            >>>>Hi,
            >>>>>
            >>>>I want to get access to my abook address file with python.
            >>>>Does anyone have some python lines to achive this using
            >>>>curses? If not, maybe anybody has small python program doing
            >>>>it with a gui!?
            >>>>
            >>>You can just parse the abook addressbook with the ConfigParser, try
            >>>this:
            >>>>
            >>>import os
            >>>from ConfigParser import *
            >>>>
            >>>abook = ConfigParser()
            >>>abook.read(o s.environ["HOME"] + "/.abook/addressbook")
            >>>>
            >>>for s in abook.sections( ):
            >>> print abook.items(s)
            >>>
            >>Thanks! I found a different example too:
            >>>
            >>import ConfigParser
            >>import string
            >>>
            >>config = ConfigParser.Co nfigParser()
            >>>
            >>config.read ("/home/fab/.abook/addressbook")
            >>>
            >># print summary
            >>print
            >>for number in [2,200]:
            >> print string.upper(co nfig.get(str(nu mber), "email"))
            >> print string.upper(co nfig.get(str(nu mber), "name"))
            >> print string.upper(co nfig.get(str(nu mber), "city"))
            >> print string.upper(co nfig.get(str(nu mber), "address"))
            >>>
            >>but the problem seems to be that abook does not write every
            >>field, so I get an exception when there is a field missing:
            >>>
            >>Traceback (most recent call last):
            >> File "configpars er-example-1.py", line 13, in ?
            >> print string.upper(co nfig.get(str(nu mber), "city"))
            >> File "/usr/lib/python2.4/ConfigParser.py ", line 520, in get
            >> raise NoOptionError(o ption, section)
            >>ConfigParser. NoOptionError: No option 'city' in section: '2'
            >>>
            >>Section 2 looks like:
            >>>
            >>[2]
            >>name=Andrs Gzi
            >>email=anes.oi @ik.e
            >>nick=oz
            >>>
            >>Is there a workaround?
            >>
            >You can construct the parser with a dictionary of defaults:
            >>
            > config = ConfigParser.Co nfigParser({"ci ty" : "unknown",
            > "zip" : "unknown"})
            >>
            >that kind of thing.
            >>
            >Or catch the exceptions. Or use config.options( "2") to see what options
            >exist in section 2 before you try to read them.
            >
            Thanks! I will try it out!
            Looking at the bigger picture here, though, I may be giving you the
            wrong advice. Mutt just invokes abook to get the addresses I think,
            that's why you put

            set query_command=" abook --mutt-query '%s'"

            So you could do the same (if what you're trying to do is write a mutt
            clone in Python):

            import subprocess

            name = "Andrs"
            subprocess.Pope n("abook --mutt-query " + name,
            stdout=subproce ss.PIPE, shell=True).com municate()[0]

            The difference is that this "leverages" abook to do the search, not just
            to store the data, which is a logical approach.

            On the other hand, this way, you require that abook is installed on the
            machine, which is no good if the object of the exercise is a portable
            Python-only solution.

            Comment

            • Fabian Braennstroem

              #7
              Re: access abook addressbook with curses

              Hi Ben,
              * Ben C <spamspam@spam. eggswrote:
              On 2006-08-08, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
              >Hi Ben,
              >>
              >* Ben C <spamspam@spam. eggswrote:
              >>On 2006-08-06, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
              >>>Hi Ben,
              >>>>
              >>>* Ben C <spamspam@spam. eggswrote:
              >>>>On 2006-08-05, Fabian Braennstroem <f.braennstroem @gmx.dewrote:
              >>>>>Hi,
              >>>>>>
              >>>>>I want to get access to my abook address file with python.
              >>>>>Does anyone have some python lines to achive this using
              >>>>>curses? If not, maybe anybody has small python program doing
              >>>>>it with a gui!?
              >>>>>
              >>>>You can just parse the abook addressbook with the ConfigParser, try
              >>>>this:
              >>>>>
              >>>>import os
              >>>>from ConfigParser import *
              >>>>>
              >>>>abook = ConfigParser()
              >>>>abook.read( os.environ["HOME"] + "/.abook/addressbook")
              >>>>>
              >>>>for s in abook.sections( ):
              >>>> print abook.items(s)
              >>>>
              >>>Thanks! I found a different example too:
              >>>>
              >>>import ConfigParser
              >>>import string
              >>>>
              >>>config = ConfigParser.Co nfigParser()
              >>>>
              >>>config.read( "/home/fab/.abook/addressbook")
              >>>>
              >>># print summary
              >>>print
              >>>for number in [2,200]:
              >>> print string.upper(co nfig.get(str(nu mber), "email"))
              >>> print string.upper(co nfig.get(str(nu mber), "name"))
              >>> print string.upper(co nfig.get(str(nu mber), "city"))
              >>> print string.upper(co nfig.get(str(nu mber), "address"))
              >>>>
              >>>but the problem seems to be that abook does not write every
              >>>field, so I get an exception when there is a field missing:
              >>>>
              >>>Traceback (most recent call last):
              >>> File "configpars er-example-1.py", line 13, in ?
              >>> print string.upper(co nfig.get(str(nu mber), "city"))
              >>> File "/usr/lib/python2.4/ConfigParser.py ", line 520, in get
              >>> raise NoOptionError(o ption, section)
              >>>ConfigParser .NoOptionError: No option 'city' in section: '2'
              >>>>
              >>>Section 2 looks like:
              >>>>
              >>>[2]
              >>>name=Andrs Gzi
              >>>email=anes.o i@ik.e
              >>>nick=oz
              >>>>
              >>>Is there a workaround?
              >>>
              >>You can construct the parser with a dictionary of defaults:
              >>>
              >> config = ConfigParser.Co nfigParser({"ci ty" : "unknown",
              >> "zip" : "unknown"})
              >>>
              >>that kind of thing.
              >>>
              >>Or catch the exceptions. Or use config.options( "2") to see what options
              >>exist in section 2 before you try to read them.
              >>
              >Thanks! I will try it out!
              >
              Looking at the bigger picture here, though, I may be giving you the
              wrong advice. Mutt just invokes abook to get the addresses I think,
              that's why you put
              >
              set query_command=" abook --mutt-query '%s'"
              >
              So you could do the same (if what you're trying to do is write a mutt
              clone in Python):
              >
              import subprocess
              >
              name = "Andrs"
              subprocess.Pope n("abook --mutt-query " + name,
              stdout=subproce ss.PIPE, shell=True).com municate()[0]
              >
              The difference is that this "leverages" abook to do the search, not just
              to store the data, which is a logical approach.
              >
              On the other hand, this way, you require that abook is installed on the
              machine, which is no good if the object of the exercise is a portable
              Python-only solution.
              The biggest problem is a missing and not allowed
              abook installation. But thanks for your tips!

              Greetings!
              Fabian

              Comment

              Working...