from os.path import expanduser
from ..config import load_config

class NamedQueries:

    section_name = 'named queries'

    def __init__(self, filename):
        self.filename = expanduser(filename)
        self.config = load_config(self.filename)

    def list(self):
        if self.config.has_section(self.section_name):
            return self.config.options(self.section_name)
        return []

    def get(self, name):
        if not self.config.has_section(self.section_name):
            return None
        return self.config.get(self.section_name, name)

    def save(self, name, query):
        if not self.config.has_section(self.section_name):
            self.config.add_section(self.section_name)
        self.config.set(self.section_name, name, query)
        self._write()

    def delete(self, name):
        self.config.remove_option(self.section_name, name)
        self._write()

    def _write(self):
        with open(self.filename, 'w') as f:
            self.config.write(f)



namedqueries = NamedQueries('~/.pgclirc')

@@ -153,53 +153,8 @@
 e):%0A
-        self.filename = expanduser(filename)%0A
     
@@ -183,21 +183,16 @@
 _config(
-self.
 filename
@@ -218,67 +218,8 @@
 f):%0A
-        if self.config.has_section(self.section_name):%0A    
     
@@ -245,15 +245,11 @@
 fig.
-options
+get
 (sel
@@ -266,27 +266,13 @@
 name
-)%0A        return
+,
  %5B%5D
+)
 %0A%0A  
@@ -294,38 +294,38 @@
  name):%0A        
-if not
+return
  self.config.has
@@ -317,35 +317,27 @@
 self.config.
-has_section
+get
 (self.sectio
@@ -346,88 +346,27 @@
 name
-):%0A            return None%0A        return self.config.get(self.section_
+, %7B%7D).get(
 name, 
-nam
+Non
 e)%0A%0A
@@ -413,54 +413,44 @@
  if 
-not 
 self.
-config.has_section(self.section_name)
+section_name not in self.config
 :%0A  
@@ -474,21 +474,9 @@
 nfig
-.add_section(
+%5B
 self
@@ -484,25 +484,30 @@
 section_name
-)
+%5D = %7B%7D
 %0A        sel
@@ -518,13 +518,9 @@
 nfig
-.set(
+%5B
 self
@@ -532,30 +532,31 @@
 ion_name
-, 
+%5D%5B
 name
-,
+%5D =
  query
-)
 %0A       
@@ -553,33 +553,39 @@
 ry%0A        self.
-_
+config.
 write()%0A%0A    def
@@ -617,34 +617,68 @@
     
-self.config.remove_option(
+if self.section_name in self.config and name in self.config%5B
 self
@@ -690,23 +690,18 @@
 ion_name
-, name)
+%5D:
 %0A       
@@ -705,88 +705,52 @@
     
-self._write()%0A%0A    def _write(self):%0A        with open(self.filename, 'w') as f:
+    del self.config%5Bself.section_name%5D%5Bname%5D
 %0A   
@@ -780,11 +780,9 @@
 ite(
-f)%0A
+)
 %0A%0A%0An

use configobj