root/trunk/libffado/support/mixer-qt4/ffado/configuration.py

Revision 1702, 5.3 kB (checked in by arnonym, 11 years ago)

Change whitespaces.

And don't crash when a configuration-file doesn't have a "device_definitions" section.

  • Property svn:mergeinfo set to
Line 
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2008-2009 by Arnold Krille
4 #
5 # This file is part of FFADO
6 # FFADO = Free Firewire (pro-)audio drivers for linux
7 #
8 # FFADO is based upon FreeBoB.
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23
24 import re, os
25
26 import shlex
27
28 import logging
29 log = logging.getLogger('configparser')
30
31 class DeviceList:
32     def __init__( self, filename="" ):
33         self.devices = list()
34         if not filename == "":
35             self.updateFromFile( filename )
36
37     def updateFromFile( self, filename, must_exist=False):
38         filename = os.path.expanduser(filename)
39         log.debug("DeviceList::updateFromFile(%s)" % filename)
40         if not os.path.exists( filename ):
41             if must_exist:
42                 log.error("cannot open file")
43             return
44         f = open( filename, "r" )
45
46         lex = Parser( f )
47         config = {}
48         while lex.peaktoken() != lex.eof:
49             tmp = lex.parsenamedvalue()
50             if tmp != None:
51                 config[ tmp[0] ] = tmp[1]
52         if config.has_key("device_definitions"):
53             for dev in config["device_definitions"]:
54                 self.addDevice( dev )
55
56     def getDeviceById( self, vendor, model ):
57         log.debug("DeviceList::getDeviceById( %s, %s )" % (vendor, model ))
58         for dev in self.devices:
59             if int("%s" % dev['vendorid'], 0) == int("%s" % vendor, 0) and \
60                int("%s" % dev['modelid'], 0) == int("%s" % model, 0):
61                 return dev
62         tmp = dict()
63         self.devices.append( tmp )
64         return tmp
65
66     def addDevice( self, device_dict ):
67         log.debug("DeviceList::addDevice()")
68         dev = self.getDeviceById( device_dict['vendorid'], device_dict['modelid'] )
69         dev.update( device_dict )
70
71 class Parser:
72     def __init__( self, file ):
73         self.lex = shlex.shlex( file )
74         self.eof = self.lex.eof
75
76     def peaktoken( self ):
77         token = self.lex.get_token()
78         self.lex.push_token( token )
79         return token
80
81     def parselist( self, level="" ):
82         token = self.peaktoken()
83         if token != "(":
84             return
85         self.lex.get_token()
86         log.debug("%sWill parse list" % level)
87         ret = []
88         token = self.peaktoken()
89         while token != ")":
90             ret.append( self.parsenamedvalue( level+"  " ) )
91             token = self.peaktoken()
92         log.debug("%slist is %s" % (level, str(ret)))
93         self.lex.get_token()
94         if self.peaktoken() == ",":
95             log.debug("%sFound a delimiter" % level)
96             self.lex.get_token()
97         return ret
98
99     def parsemap( self, level="" ):
100         token = self.lex.get_token()
101         if token != "{":
102             return
103         log.debug("%sWill parse map" % level)
104         ret = {}
105         token = self.peaktoken()
106         while token != "}":
107             #self.push_token( token )
108             tmp = self.parsenamedvalue( level+"  " )
109             if tmp != None:
110                 ret[ tmp[0] ] = tmp[1]
111             token = self.peaktoken()
112         token = self.lex.get_token()
113         log.debug("%sMap ended with '%s' and '%s'"% (level,token,self.peaktoken()))
114         if self.peaktoken() in (",",";"):
115             log.debug("%sFound a delimiter!" % level)
116             self.lex.get_token()
117         return ret
118
119     def parsevalue( self, level="" ):
120         token = self.lex.get_token()
121         log.debug("%sparsevalue() called on token '%s'" % (level, token))
122         self.lex.push_token( token )
123         if token == "(":
124             value = self.parselist( level+"  " )
125         elif token == "{":
126             value = self.parsemap( level+"  " )
127         else:
128             value = self.lex.get_token().replace( "\"", "" )
129         token = self.peaktoken()
130         if token == ";":
131             log.debug("%sFound a delimiter!" % level)
132             self.lex.get_token()
133         return value
134
135     def parsenamedvalue( self, level="" ):
136         token = self.lex.get_token()
137         log.debug("%sparsenamedvalue() called on token '%s'" % (level, token))
138         if token == "{":
139             self.lex.push_token( token )
140             return self.parsemap( level+"  " )
141         if len(token) > 0 and token not in ("{","}","(",")",",",";"):
142             log.debug("%sGot name '%s'" %(level,token))
143             name = token
144             token = self.lex.get_token()
145             if token in ("=",":"):
146                 #print "%sWill parse value" % level
147                 value = self.parsevalue( level )
148                 return (name,value)
149         log.debug("%sparsenamedvalue() will return None!" % level)
150         return
151
152 #
153 # Have kind of a test directly included...
154 #
155 if __name__ == "__main__":
156
157     import sys
158
159     file = sys.argv[1]
160
161     log.setLevel(logging.DEBUG)
162     devs = DeviceList( file )
163
164     print devs.devices
165
166 # vim: et
Note: See TracBrowser for help on using the browser.