root/trunk/libffado/support/mixer-qt4/ffado_configuration.py

Revision 1501, 4.3 kB (checked in by ppalmers, 12 years ago)

fix merge issues with the QT4 mixer

Line 
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2008 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 ):
38                 if not os.path.exists( filename ):
39                         return
40                 f = open( filename, "r" )
41
42                 lex = Parser( f )
43                 config = {}
44                 while lex.peaktoken() != lex.eof:
45                         tmp = lex.parsenamedvalue()
46                         if tmp != None:
47                                 config[ tmp[0] ] = tmp[1]
48                 for dev in config["device_definitions"]:
49                         self.addDevice( dev )
50
51         def getDeviceById( self, vendor, model ):
52                 log.debug("DeviceList::getDeviceById( %s, %s )" % (vendor, model ))
53                 for dev in self.devices:
54                         if int("%s" % dev['vendorid'], 0) == int("%s" % vendor, 0) and \
55                            int("%s" % dev['modelid'], 0) == int("%s" % model, 0):
56                                 return dev
57                 tmp = dict()
58                 self.devices.append( tmp )
59                 return tmp
60
61         def addDevice( self, device_dict ):
62                 log.debug("DeviceList::addDevice()")
63                 dev = self.getDeviceById( device_dict['vendorid'], device_dict['modelid'] )
64                 dev.update( device_dict )
65
66 class Parser:
67         def __init__( self, file ):
68                 self.lex = shlex.shlex( file )
69                 self.eof = self.lex.eof
70
71         def peaktoken( self ):
72                 token = self.lex.get_token()
73                 self.lex.push_token( token )
74                 return token
75
76         def parselist( self, level="" ):
77                 token = self.peaktoken()
78                 if token != "(":
79                         return
80                 self.lex.get_token()
81                 log.debug("%sWill parse list" % level)
82                 ret = []
83                 token = self.peaktoken()
84                 while token != ")":
85                         ret.append( self.parsenamedvalue( level+"  " ) )
86                         token = self.peaktoken()
87                 log.debug("%slist is %s" % (level, str(ret)))
88                 self.lex.get_token()
89                 if self.peaktoken() == ",":
90                         log.debug("%sFound a delimiter" % level)
91                         self.lex.get_token()
92                 return ret
93
94         def parsemap( self, level="" ):
95                 token = self.lex.get_token()
96                 if token != "{":
97                         return
98                 log.debug("%sWill parse map" % level)
99                 ret = {}
100                 token = self.peaktoken()
101                 while token != "}":
102                         #self.push_token( token )
103                         tmp = self.parsenamedvalue( level+"  " )
104                         if tmp != None:
105                                 ret[ tmp[0] ] = tmp[1]
106                         token = self.peaktoken()
107                 token = self.lex.get_token()
108                 log.debug("%sMap ended with '%s' and '%s'"% (level,token,self.peaktoken()))
109                 if self.peaktoken() in (",",";"):
110                         log.debug("%sFound a delimiter!" % level)
111                         self.lex.get_token()
112                 return ret
113
114         def parsevalue( self, level="" ):
115                 token = self.lex.get_token()
116                 log.debug("%sparsevalue() called on token '%s'" % (level, token))
117                 self.lex.push_token( token )
118                 if token == "(":
119                         value = self.parselist( level+"  " )
120                 elif token == "{":
121                         value = self.parsemap( level+"  " )
122                 else:
123                         value = self.lex.get_token().replace( "\"", "" )
124                 token = self.peaktoken()
125                 if token == ";":
126                         log.debug("%sFound a delimiter!" % level)
127                         self.lex.get_token()
128                 return value
129
130         def parsenamedvalue( self, level="" ):
131                 token = self.lex.get_token()
132                 log.debug("%sparsenamedvalue() called on token '%s'" % (level, token))
133                 if token == "{":
134                         self.lex.push_token( token )
135                         return self.parsemap( level+"  " )
136                 if len(token) > 0 and token not in ("{","}","(",")",",",";"):
137                         log.debug("%sGot name '%s'" %(level,token))
138                         name = token
139                         token = self.lex.get_token()
140                         if token in ("=",":"):
141                                 #print "%sWill parse value" % level
142                                 value = self.parsevalue( level )
143                                 return (name,value)
144                 log.debug("%sparsenamedvalue() will return None!" % level)
145                 return
146
147 #
148 # Have kind of a test directly included...
149 #
150 if __name__ == "__main__":
151
152         import sys
153
154         file = sys.argv[1]
155
156         log.setLevel(logging.DEBUG)
157         devs = DeviceList( file )
158
159         print devs.devices
160
Note: See TracBrowser for help on using the browser.