pgconsole/config.py
changeset 10 f3a1b9792cc9
child 11 bc69eca59041
equal deleted inserted replaced
9:2fcc8ef0b97d 10:f3a1b9792cc9
       
     1 from lxml import etree
       
     2 from lxml import objectify
       
     3 
       
     4 class Config:
       
     5     def __init__(self):
       
     6         self.tree = None
       
     7         self.fname = None
       
     8 
       
     9 
       
    10     def new(self, fname=None):
       
    11         E = objectify.E
       
    12         self.root = E.psqlconsole(
       
    13             E.servers,
       
    14             E.editor(
       
    15                 E.nodes(
       
    16                     E.node('-- Type your query here', name='Untitled', type='text'),
       
    17                     selected='Untitled'
       
    18                 )
       
    19             ),
       
    20             E.window(
       
    21                 E.size(
       
    22                     E.width(800),
       
    23                     E.height(600)
       
    24                 ),
       
    25                 E.dividers(
       
    26                     E.verticaldivider(300),
       
    27                     E.horizontaldivider(500),
       
    28                     E.editordivider(-1)
       
    29                 )
       
    30             )
       
    31         )
       
    32         self.tree = etree.ElementTree(self.root)
       
    33         self.servers = self.root.servers
       
    34         self.fname = fname
       
    35 
       
    36 
       
    37     def load(self, fname=None):
       
    38         if fname:
       
    39             self.fname = fname
       
    40         self.tree = objectify.parse(self.fname)
       
    41         self.root = self.tree.getroot()
       
    42         self.servers = self.root.servers
       
    43 
       
    44 
       
    45     def save(self, fname=None):
       
    46         if fname:
       
    47             self.fname = fname
       
    48         self.tree.write(
       
    49             self.fname,
       
    50             encoding='utf-8',
       
    51             xml_declaration=True,
       
    52             pretty_print=True,
       
    53             compression=6)
       
    54 
       
    55 
       
    56     def add_server(self, name, host, port, user, password):
       
    57         e = etree.SubElement(self.servers, 'server')
       
    58         e.name = name
       
    59         e.host = host
       
    60         e.port = port
       
    61         e.user = user
       
    62         e.password = password
       
    63         return e
       
    64 
       
    65 
       
    66 cfg = Config()
       
    67 
       
    68 
       
    69 if __name__ == '__main__':
       
    70     cfg.load('../psqlconsole.xml.gz')
       
    71     print cfg.root.servers.server[0].host.text
       
    72     cfg.save()
       
    73