예제 #1
0
 def __init__(self, directories):
     self.name = 'unknown'
     self.version = 0
     self.author = ['anonymous']
     self.description = 'This module is undocumented.'
     self.detailed_description = 'This module is undocumented.'
     self.options = Options(directories)
     self.advanced_options = Options(directories)
예제 #2
0
	def __init__(self, stdout = None):
		self.modules = { }
		self.__package__ = '.'.join(self.__module__.split('.')[:-1])
		package_path = __import__(self.__package__, None, None, ['__path__']).__path__[0]	# that's some python black magic trickery for you
		if stdout == None:
			stdout = sys.stdout
		self.stdout = stdout
		
		self.directories = Namespace()
		self.directories.user_data = os.path.expanduser('~') + os.sep + '.termineter' + os.sep
		self.directories.modules_path = package_path + os.sep + 'modules' + os.sep
		self.directories.data_path = package_path + os.sep + 'data' + os.sep
		if not os.path.isdir(self.directories.data_path):
			self.logger.critical('path to data not found')
			raise FrameworkConfigurationError('path to data not found')
		if not os.path.isdir(self.directories.user_data):
			os.mkdir(self.directories.user_data)
		
		self.serial_connection = None
		self.__serial_connected__ = False
		
		# setup logging stuff
		self.logger = logging.getLogger(self.__package__ + '.' + self.__class__.__name__.lower())
		main_file_handler = logging.handlers.RotatingFileHandler(self.directories.user_data + self.__package__ + '.log', maxBytes = 262144, backupCount = 5)
		main_file_handler.setLevel(logging.DEBUG)
		main_file_handler.setFormatter(logging.Formatter("%(asctime)s %(name)-50s %(levelname)-10s %(message)s"))
		logging.getLogger('').addHandler(main_file_handler)
		
		# setup and configure options
		# Whether or not these are 'required' is really enforced by the individual
		# modules get_missing_options method and by which options they require based
		# on their respective types.  See framework/templates.py for more info.
		self.options = Options(self.directories)
		self.options.addBoolean('USECOLOR', 'enable color on the console interface', default = False)
		self.options.addString('CONNECTION', 'serial connection string')
		self.options.addString('USERNAME', 'serial username', default = '0000')
		self.options.addInteger('USERID', 'serial userid', default = 0)
		self.options.addString('PASSWORD', 'serial c12.18 password', default = '00000000000000000000')
		self.options.addBoolean('PASSWORDHEX', 'if the password is in hex', default = True)
		self.advanced_options = AdvancedOptions(self.directories)
		self.advanced_options.addInteger('BAUDRATE', 'serial connection baud rate', default = 9600)
		self.advanced_options.addInteger('BYTESIZE', 'serial connection byte size', default = serial.EIGHTBITS)
		self.advanced_options.addBoolean('CACHETBLS', 'cache certain read-only tables', default = True)
		self.advanced_options.setCallback('CACHETBLS', self.__optCallbackSetTableCachePolicy__)
		self.advanced_options.addInteger('STOPBITS', 'serial connection stop bits', default = serial.STOPBITS_ONE)
		self.advanced_options.addInteger('NBRPKTS', 'c12.18 maximum packets for reassembly', default = 2)
		self.advanced_options.addInteger('PKTSIZE', 'c12.18 maximum packet size', default = 512)
		if sys.platform.startswith('linux'):
			self.options.setOption('USECOLOR', 'True')
		
		# check and configure rfcat stuff
		self.rfcat_available = False
		try:
			import rflib
			self.logger.info('the rfcat library is available')
			self.rfcat_available = True
		except ImportError:
			self.logger.info('the rfcat library is not available, it can be found at https://code.google.com/p/rfcat/')
			pass
		if self.rfcat_available:
			# init the values to be used
			self.rfcat_connection = None
			self.__rfcat_connected__ = False
			self.is_rfcat_connected = lambda: self.__rfcat_connected__
			# self.options.addInteger('RFCATIDX', 'the rfcat device to use', default = 0)
		
		# start loading modules
		modules_path = self.directories.modules_path
		self.logger.debug('searching for modules in: ' + modules_path)
		self.current_module = None
		if not os.path.isdir(modules_path):
			self.logger.critical('path to modules not found')
			raise FrameworkConfigurationError('path to modules not found')
		for module_path in FileWalker(modules_path, absolute_path = True, skip_dirs = True):
			module_path = module_path.replace(os.path.sep, '/')
			if not module_path.endswith('.py'):
				continue
			module_path = module_path[len(modules_path):-3]
			module_name = module_path.split(os.path.sep)[-1]
			if module_name.startswith('__'):
				continue
			if module_name.lower() != module_name:
				continue
			if module_path.startswith('rfcat') and not self.rfcat_available:
				self.logger.debug('skipping module: ' + module_path + ' because rfcat is not available')
				continue
			# looks good, proceed to load
			self.logger.debug('loading module: ' + module_path)
			try:
				module_instance = self.import_module(module_path)
			except FrameworkRuntimeError:
				continue
			if not isinstance(module_instance, module_template):
				self.logger.error('module: ' + module_path + ' is not derived from the module_template class')
				continue
			# if isinstance(module_instance, rfcat_module_template) and not self.rfcat_available:
			# 	self.logger.debug('skipping module: ' + module_path + ' because rfcat is not available')
			#	continue
			if not hasattr(module_instance, 'run'):
				self.logger.critical('module: ' + module_path + ' has no run() method')
				raise FrameworkRuntimeError('module: ' + module_path + ' has no run() method')
			if not isinstance(module_instance.options, Options) or not isinstance(module_instance.advanced_options, Options):
				self.logger.critical('module: ' + module_path + ' options and advanced_options must be Options instances')
				raise FrameworkRuntimeError('options and advanced_options must be Options instances')
			module_instance.name = module_name
			module_instance.path = module_path
			self.modules[module_path] = module_instance
		self.logger.info('successfully loaded ' + str(len(self.modules)) + ' modules into the framework')
		return
예제 #3
0
    def __init__(self):
        self.modules = {}
        self.__serial_connected__ = False
        self.__package__ = '.'.join(self.__module__.split('.')[:-1])
        package_path = __import__(
            self.__package__, None, None,
            ['__path__'
             ]).__path__[0]  # that's some python black magic trickery for you

        self.directories = Namespace()
        self.directories.user_data = os.path.expanduser(
            '~') + os.sep + '.termineter' + os.sep
        self.directories.modules_path = package_path + os.sep + 'modules' + os.sep
        self.directories.data_path = package_path + os.sep + 'data' + os.sep
        if not os.path.isdir(self.directories.user_data):
            os.mkdir(self.directories.user_data)
        self.serial_connection = None
        self.logger = logging.getLogger(self.__package__ + '.core')
        main_file_handler = logging.handlers.RotatingFileHandler(
            self.directories.user_data + self.__package__ + '.log',
            maxBytes=262144,
            backupCount=5)
        main_file_handler.setLevel(logging.DEBUG)
        main_file_handler.setFormatter(
            logging.Formatter(
                "%(asctime)s %(name)-50s %(levelname)-10s %(message)s"))
        logging.getLogger('').addHandler(main_file_handler)
        self.options = Options(self.directories)
        self.options.addBoolean('USECOLOR',
                                'enable color on the console interface',
                                default=False)
        self.options.addString('CONNECTION', 'serial connection string', True)
        self.options.addString('USERNAME', 'serial username', default='0000')
        self.options.addInteger('USERID', 'serial userid', default=0)
        self.options.addString('PASSWORD',
                               'serial c12.18 password',
                               default='00000000000000000000')
        self.options.addBoolean('PASSWORDHEX',
                                'if the password is in hex',
                                default=True)
        self.advanced_options = Options(self.directories)
        self.advanced_options.addInteger('BAUDRATE',
                                         'serial connection baud rate',
                                         default=9600)
        self.advanced_options.addInteger('BYTESIZE',
                                         'serial connection byte size',
                                         default=serial.EIGHTBITS)
        self.advanced_options.addInteger('STOPBITS',
                                         'serial connection stop bits',
                                         default=serial.STOPBITS_ONE)
        if sys.platform.startswith('linux'):
            self.options.setOption('USECOLOR', 'True')
        if not os.path.isdir(self.directories.data_path):
            self.logger.critical('path to data not found')
            raise FrameworkConfigurationError('path to data not found')
        modules_path = self.directories.modules_path
        self.logger.debug('searching for modules in: ' + modules_path)
        self.current_module = None
        if not os.path.isdir(modules_path):
            self.logger.critical('path to modules not found')
            raise FrameworkConfigurationError('path to modules not found')
        all_modules = os.listdir(modules_path)
        loadable_modules = os.listdir(modules_path)
        for module in all_modules:  # get rid of ones we don't want to load
            if not module.endswith('.py'):
                loadable_modules.remove(module)
                continue
            if module.startswith('__'):
                loadable_modules.remove(module)
                continue
            if module.lower() != module:
                loadable_modules.remove(module)  # only lower case names please
        del all_modules

        for module_name in loadable_modules:
            module_name = module_name[:-3]
            self.logger.debug('loading module: ' + module_name)
            module = __import__(self.__package__ + '.modules.' + module_name,
                                None, None, ['Module'])
            module_instance = module.Module(self.directories)
            if not isinstance(module_instance, module_template):
                self.logger.error(
                    'module: ' + module_name +
                    ' is not derived from the module_template class')
                continue
            if not hasattr(module_instance, 'run'):
                self.logger.critical('module: ' + module_name +
                                     ' has no run() function')
                raise Exception('module: ' + module_name +
                                ' has no run() function')
            if not isinstance(module_instance.options,
                              Options) or not isinstance(
                                  module_instance.advanced_options, Options):
                self.logger.critical(
                    'module: ' + module_name +
                    ' options and advanced_options must be Options instances')
                raise Exception(
                    'options and advanced_options must be Options instances')
            module_instance.name = module_name
            self.modules[module_name] = module_instance
        self.logger.info('successfully loaded ' + str(len(self.modules)) +
                         ' modules into the framework')