import ConfigParser
import os
import pyaudio

PROG = 'soundmeter'
USER_DIR = os.path.join(os.path.expanduser('~'), '.' + PROG)
USER_LOGFILE = os.path.join(USER_DIR, 'log')
USER_CONFIG = os.path.join(USER_DIR, 'config')
USER_SCRIPT = os.path.join(USER_DIR, 'trigger.sh')

config = ConfigParser.ConfigParser()
config.read(os.environ.get('SOUNDMETER_TEST_CONFIG') or USER_CONFIG)
items = {}

if config.has_section(PROG):
    items = dict(config.items(PROG))
    for name in items:
        try:
            if name in ['frames_per_buffer', 'format', 'channels', 'rate']:
                items[name] = int(items[name])
            if name in ['audio_segment_length']:
                items[name] = float(items[name])
        except:
            items[name] = None
FRAMES_PER_BUFFER = items.get('frames_per_buffer') or 2048
FORMAT = items.get('format') or pyaudio.paInt16
CHANNELS = items.get('channels') or 2
RATE = items.get('rate') or 44100
AUDIO_SEGMENT_LENGTH = items.get('audio_segment_length') or 0.5

import ConfigParser
import os
import pyaudio

PROG = 'soundmeter'
USER_DIR = os.path.join(os.path.expanduser('~'), '.' + PROG)
USER_LOGFILE = os.path.join(USER_DIR, 'log')
USER_CONFIG = os.path.join(USER_DIR, 'config')
USER_SCRIPT = os.path.join(USER_DIR, 'trigger.sh')

config = ConfigParser.ConfigParser()
config.read(os.environ.get('SOUNDMETER_TEST_CONFIG') or USER_CONFIG)
items = {}

if config.has_section(PROG):
    items = dict(config.items(PROG))
    for name in items:
        try:
            if name in ['frames_per_buffer', 'format', 'channels', 'rate']:
                items[name] = int(items[name])
            elif name in ['audio_segment_length']:
                items[name] = float(items[name])
            else:
                raise Exception('Unknown name "%s" in config' % name)
        except ValueError:
            raise Exception('Invalid value to "%s" in config' % name)

FRAMES_PER_BUFFER = items.get('frames_per_buffer') or 2048
FORMAT = items.get('format') or pyaudio.paInt16
CHANNELS = items.get('channels') or 2
RATE = items.get('rate') or 44100
AUDIO_SEGMENT_LENGTH = items.get('audio_segment_length') or 0.5

Modify exception handling to local config names
Modify exception handling to local config names

  import ConfigParser
  import os
  import pyaudio
  
  PROG = 'soundmeter'
  USER_DIR = os.path.join(os.path.expanduser('~'), '.' + PROG)
  USER_LOGFILE = os.path.join(USER_DIR, 'log')
  USER_CONFIG = os.path.join(USER_DIR, 'config')
  USER_SCRIPT = os.path.join(USER_DIR, 'trigger.sh')
  
  config = ConfigParser.ConfigParser()
  config.read(os.environ.get('SOUNDMETER_TEST_CONFIG') or USER_CONFIG)
  items = {}
  
  if config.has_section(PROG):
      items = dict(config.items(PROG))
      for name in items:
          try:
              if name in ['frames_per_buffer', 'format', 'channels', 'rate']:
                  items[name] = int(items[name])
-             if name in ['audio_segment_length']:
+             elif name in ['audio_segment_length']:
                  items[name] = float(items[name])
-         except:
-             items[name] = None
+             else:
+                 raise Exception('Unknown name "%s" in config' % name)
+         except ValueError:
+             raise Exception('Invalid value to "%s" in config' % name)
+ 
  FRAMES_PER_BUFFER = items.get('frames_per_buffer') or 2048
  FORMAT = items.get('format') or pyaudio.paInt16
  CHANNELS = items.get('channels') or 2
  RATE = items.get('rate') or 44100
  AUDIO_SEGMENT_LENGTH = items.get('audio_segment_length') or 0.5
  
Modify exception handling to local config names
## Code Before:
import ConfigParser
import os
import pyaudio

PROG = 'soundmeter'
USER_DIR = os.path.join(os.path.expanduser('~'), '.' + PROG)
USER_LOGFILE = os.path.join(USER_DIR, 'log')
USER_CONFIG = os.path.join(USER_DIR, 'config')
USER_SCRIPT = os.path.join(USER_DIR, 'trigger.sh')

config = ConfigParser.ConfigParser()
config.read(os.environ.get('SOUNDMETER_TEST_CONFIG') or USER_CONFIG)
items = {}

if config.has_section(PROG):
    items = dict(config.items(PROG))
    for name in items:
        try:
            if name in ['frames_per_buffer', 'format', 'channels', 'rate']:
                items[name] = int(items[name])
            if name in ['audio_segment_length']:
                items[name] = float(items[name])
        except:
            items[name] = None
FRAMES_PER_BUFFER = items.get('frames_per_buffer') or 2048
FORMAT = items.get('format') or pyaudio.paInt16
CHANNELS = items.get('channels') or 2
RATE = items.get('rate') or 44100
AUDIO_SEGMENT_LENGTH = items.get('audio_segment_length') or 0.5

## Instruction:
Modify exception handling to local config names
## Code After:
import ConfigParser
import os
import pyaudio

PROG = 'soundmeter'
USER_DIR = os.path.join(os.path.expanduser('~'), '.' + PROG)
USER_LOGFILE = os.path.join(USER_DIR, 'log')
USER_CONFIG = os.path.join(USER_DIR, 'config')
USER_SCRIPT = os.path.join(USER_DIR, 'trigger.sh')

config = ConfigParser.ConfigParser()
config.read(os.environ.get('SOUNDMETER_TEST_CONFIG') or USER_CONFIG)
items = {}

if config.has_section(PROG):
    items = dict(config.items(PROG))
    for name in items:
        try:
            if name in ['frames_per_buffer', 'format', 'channels', 'rate']:
                items[name] = int(items[name])
            elif name in ['audio_segment_length']:
                items[name] = float(items[name])
            else:
                raise Exception('Unknown name "%s" in config' % name)
        except ValueError:
            raise Exception('Invalid value to "%s" in config' % name)

FRAMES_PER_BUFFER = items.get('frames_per_buffer') or 2048
FORMAT = items.get('format') or pyaudio.paInt16
CHANNELS = items.get('channels') or 2
RATE = items.get('rate') or 44100
AUDIO_SEGMENT_LENGTH = items.get('audio_segment_length') or 0.5
