def get_option(self, name, section='General'): if not self.config.has_section(section): raise ConfigError(_("Invalid section: %s") % section) elif not self.config.has_option(section, name): raise ConfigError( _("%s does not have option: %s") % (self.filename, name)) return self.config.get(section, name)
def _load_configuration(self, config_file): try: self.config = StoqdriversConfig(config_file) except ConfigError as e: log.info(e) self.config = None else: # This allows overriding in the config file some or all of the # data that was specified through the constructor section_name = BaseDevice.typename_translate_dict[self.device_type] for field in ['brand', 'device', 'model', 'inverted_drawer']: try: setattr(self, field, self.config.get_option(field, section_name)) except ConfigError: # Field not found, ignore pass # At this point, either we have the data needed to initialize the # device or we will need to bail if (not self.model or not self.brand or (self.interface == BaseDevice.INTERFACE_SERIAL and not self.device and not self._port)): raise ConfigError("Device not specified in config or constructor, giving up") name = "stoqdrivers.%s.%s.%s" % (self.device_dirname, self.brand, self.model) try: module = __import__(name, None, None, 'stoqdevices') except ImportError as reason: raise CriticalError("Could not load driver %s %s: %s" % (self.brand.capitalize(), self.model.upper(), reason)) class_name = self.model driver_class = getattr(module, class_name, None) if not driver_class: raise CriticalError("Device driver at %s needs a class called %s" % (name, class_name)) if self.interface == 'serial': if not self._port: self._port = SerialPort(self.device, self._baudrate) self._driver = driver_class(self._port, consts=self._driver_constants) elif self.interface == 'usb': self._driver = driver_class(self.vendor_id, self.product_id) else: raise NotImplementedError('Interface not implemented') log.info("Device class initialized: brand=%s,device=%s,model=%s" % (self.brand, self.device, self.model)) # This check is necessary but ugly because the configuration code # doesn't understand booleans, and we don't want mismatches if self.inverted_drawer in ("True", True): log.info("Inverting drawer check logic") self._driver.inverted_drawer = True self.check_interfaces()
def _parse_bank(self, items): if not 'name' in items: raise ConfigError("There is no bank name defined") coordinates = {} bank_name = items.pop('name') for name in ('value', 'legal_amount', 'legal_amount2', 'city', 'thirdparty', 'year', 'day', 'month'): if not name in items: raise ConfigError("The especification for `%s' was not found" % name) value = items.pop(name) if not ',' in value: raise ConfigError("Invalid value format for `%s'" % name) x, y = value.split(',') x = int(x or 0) y = int(y or 0) if x < 0 or y < 0: raise ConfigError("Negative values aren't allowed") coordinates[name] = (x, y) if items: raise ConfigError("The name(s) %s are invalid, busted " "configuration" % ", ".join(items.keys())) return BankConfiguration(bank_name, coordinates)
def get_banks(self): configfile = self.__module__.split('.')[-2] + '.ini' config = ConfigParser() filename = environ.find_resource("conf", configfile) if not config.read(filename): return None for section in config.sections(): # With this, we'll have a dictionary in this format: # CONFIG_NAME: "Y,X" items = dict(config.items(section)) try: bank = self._parse_bank(items) except ConfigError, errmsg: raise ConfigError("In section `%s' of `%s': %s" % (section, filename, errmsg)) self._banks[int(section)] = bank
def _load_configuration(self, config_file): section_name = BaseDevice.typename_translate_dict[self.device_type] if not self.model or not self.brand or (not self.device and not self._port): self.config = StoqdriversConfig(config_file) if not self.config.has_section(section_name): raise ConfigError(_("There is no section named `%s'!") % section_name) self.brand = self.config.get_option("brand", section_name) self.device = self.config.get_option("device", section_name) self.model = self.config.get_option("model", section_name) name = "stoqdrivers.%s.%s.%s" % (self.device_dirname, self.brand, self.model) try: module = __import__(name, None, None, 'stoqdevices') except ImportError, reason: raise CriticalError("Could not load driver %s %s: %s" % (self.brand.capitalize(), self.model.upper(), reason))
def get_banks(self): configfile = self.__module__.split('.')[-2] + '.ini' config = ConfigParser() resource_name = 'conf/%s' % configfile filename = pkg_resources.resource_filename('stoqdrivers', resource_name) if not config.read(filename): return None for section in config.sections(): # With this, we'll have a dictionary in this format: # CONFIG_NAME: "Y,X" items = dict(config.items(section)) try: bank = self._parse_bank(items) except ConfigError as errmsg: raise ConfigError("In section `%s' of `%s': %s" % (section, filename, errmsg)) self._banks[int(section)] = bank return self._banks
def _load_config(self): # Try to load configuration from: # 1) $HOME/.$domain/$filename # 2) $PREFIX/etc/$domain/$filename # 3) /etc/$filename # This is a bit hackish: # $prefix / lib / $DOMAIN / lib / config.py # -4 -3 -2 -1 0 filename = os.path.abspath(__file__) stripped = filename.split(os.sep)[:-4] self.prefix = os.path.join(os.sep, *stripped) homepath = self.get_homepath() etcpath = os.path.join(self.prefix, 'etc', self.domain) globetcpath = os.path.join(os.sep, 'etc', self.domain) if not (self._open_config(homepath) or self._open_config(etcpath) or self._open_config(globetcpath)): raise ConfigError( _("Config file not found in: `%s', `%s' and " "`%s'") % (homepath, etcpath, globetcpath))
def _load_configuration(self, config_file): section_name = BaseDevice.typename_translate_dict[self.device_type] if (not self.model or not self.brand or (self.interface == BaseDevice.INTERFACE_SERIAL and not self.device and not self._port)): self.config = StoqdriversConfig(config_file) if not self.config.has_section(section_name): raise ConfigError( _("There is no section named `%s'!") % section_name) self.brand = self.config.get_option("brand", section_name) self.device = self.config.get_option("device", section_name) self.model = self.config.get_option("model", section_name) name = "stoqdrivers.%s.%s.%s" % (self.device_dirname, self.brand, self.model) try: module = __import__(name, None, None, 'stoqdevices') except ImportError as reason: raise CriticalError( "Could not load driver %s %s: %s" % (self.brand.capitalize(), self.model.upper(), reason)) class_name = self.model driver_class = getattr(module, class_name, None) if not driver_class: raise CriticalError("Device driver at %s needs a class called %s" % (name, class_name)) if self.interface == 'serial': if not self._port: self._port = SerialPort(self.device, self._baudrate) self._driver = driver_class(self._port, consts=self._driver_constants) elif self.interface == 'usb': self._driver = driver_class(self.vendor_id, self.product_id) else: raise NotImplementedError('Interface not implemented') log.info(("Config data: brand=%s,device=%s,model=%s" % (self.brand, self.device, self.model))) self.check_interfaces()
def set_option(self, name, section='General'): if not self.config.has_section(section): raise ConfigError(_("Invalid section: %s") % section) self.config.set(section, name)