コード例 #1
0
ファイル: wordclock.py プロジェクト: plotaBot/rpi_wordclock
    def __init__(self):
        """
        Initializations, executed at every startup of the wordclock
        """
        # Get path of the directory where this file is stored
        self.basePath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

        # Get wordclock configuration from config-file
        pathToConfigFile = self.basePath + '/wordclock_config/wordclock_config.cfg'
        if not os.path.exists(pathToConfigFile):
            pathToConfigFileExample = self.basePath + '/wordclock_config/wordclock_config.example.cfg'
            if not os.path.exists(pathToConfigFileExample):
                print('Error: No config-file available!')
                print('  Expected ' + pathToConfigFile + ' or ' + pathToConfigFileExample)
                raise Exception('Missing config-file')
            copyfile(pathToConfigFileExample, pathToConfigFile)
            print('Warning: No config-file specified! Was created from example-config!')
        print('Parsing ' + pathToConfigFile)
        self.config = ConfigParser.ConfigParser()
        self.config.read(pathToConfigFile)

        # Add to the loaded configuration the current base path to provide it
        # to other classes/plugins for further usage
        self.config.set('wordclock', 'base_path', self.basePath)

        self.developer_mode_active = self.config.getboolean('wordclock', 'developer_mode')

        # Create object to interact with the wordclock using the interface of your choice
        self.wci = wci.event_handler()

        if not self.developer_mode_active:
            import wordclock_interfaces.gpio_interface as wcigpio
            self.gpio = wcigpio.gpio_interface(self.config, self.wci)

        # Create object to display any content on the wordclock display
        # Its implementation depends on your (individual) wordclock layout/wiring
        self.wcd = wcd.wordclock_display(self.config, self.wci)

        # Define path to general icons (not plugin-specific)
        self.pathToGeneralIcons = os.path.join(self.basePath, 'icons', self.wcd.dispRes())

        # Assemble path to plugin directory
        plugin_dir = os.path.join(self.basePath, 'wordclock_plugins')

        # Assemble list of all available plugins
        plugins = (plugin for plugin in os.listdir(plugin_dir) if os.path.isdir(os.path.join(plugin_dir, plugin)))

        # Import plugins, which can be operated by the wordclock:
        index = 0  # A helper variable (only incremented on successful import)
        self.plugins = []
        for plugin in plugins:
            # Check the config-file, whether to activate or deactivate the plugin
            try:
                if not self.config.getboolean('plugin_' + plugin, 'activate'):
                    print('Skipping plugin ' + plugin + ' since it is set to activate=false in the config-file.')
                    continue
            except:
                print('  INFO: No activate-flag set for plugin ' + plugin + ' within the config-file. Will be imported.')

            try:
                # Perform a minimal (!) validity check
                # Check, if plugin is valid (if the plugin.py is provided)
                if not os.path.isfile(os.path.join(plugin_dir, plugin, 'plugin.py')):
                    raise
                self.plugins.append(import_module('wordclock_plugins.' + plugin + '.plugin').plugin(self.config))
                # Search for default plugin to display the time
                if plugin == 'time_default':
                    print('  Selected "' + plugin + '" as default plugin')
                    self.default_plugin = index
                print('Imported plugin ' + str(index) + ': "' + plugin + '".')
                index += 1
            except:
                print('Failed to import plugin ' + plugin + '!')

        # Create object to interact with the wordclock using the interface of your choice
        self.plugin_index = 0
        self.wciweb = wciweb.web_interface(self)
コード例 #2
0
    def __init__(self):
        """
        Initializations, executed at every startup of the wordclock
        """
        # Get path of the directory where this file is stored
        self.basePath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

        # Get wordclock configuration from config-file
        pathToConfigFile = self.basePath + '/wordclock_config/wordclock_config.cfg'
        if not os.path.exists(pathToConfigFile):
            pathToConfigFileExample = self.basePath + '/wordclock_config/wordclock_config.example.cfg'
            if not os.path.exists(pathToConfigFileExample):
                print('Error: No config-file available!')
                print('  Expected ' + pathToConfigFile + ' or ' + pathToConfigFileExample)
                raise Exception('Missing config-file')
            copyfile(pathToConfigFileExample, pathToConfigFile)
            print('Warning: No config-file specified! Was created from example-config!')
        print('Parsing ' + pathToConfigFile)
        self.config = ConfigParser.ConfigParser()
        self.config.read(pathToConfigFile)

        # Add to the loaded configuration the current base path to provide it
        # to other classes/plugins for further usage
        self.config.set('wordclock', 'base_path', self.basePath)

        self.developer_mode_active = self.config.getboolean('wordclock', 'developer_mode')

        # Create object to interact with the wordclock using the interface of your choice
        self.wci = wci.event_handler()

        if not self.developer_mode_active:
            import wordclock_interfaces.gpio_interface as wcigpio
            self.gpio = wcigpio.gpio_interface(self.config, self.wci)

        # Create object to display any content on the wordclock display
        # Its implementation depends on your (individual) wordclock layout/wiring
        self.wcd = wcd.wordclock_display(self.config, self.wci)

        # Define path to general icons (not plugin-specific)
        self.pathToGeneralIcons = os.path.join(self.basePath, 'icons', self.wcd.dispRes())

        # Assemble path to plugin directory
        plugin_dir = os.path.join(self.basePath, 'wordclock_plugins')

        # Assemble list of all available plugins
        pluginstemp = (plugin for plugin in os.listdir(plugin_dir) if os.path.isdir(os.path.join(plugin_dir, plugin)))

        #print('--- before cleaning ---')
        #print(pluginstemp)

        plugins = []

        for plugin in pluginstemp:
            if not(plugin.startswith('_')):
                plugins.append(plugin)

        pluginstemp = None

        #print('-- after cleaning ---')
        #print(plugins)

        # Import plugins, which can be operated by the wordclock:
        index = 0  # A helper variable (only incremented on successful import)
        self.plugins = []
        for plugin in plugins:
            # Check the config-file, whether to activate or deactivate the plugin
            try:
                if not self.config.getboolean('plugin_' + plugin, 'activate'):
                    print('Skipping plugin ' + plugin + ' since it is set to activate=false in the config-file.')
                    continue
                else:
                    print('Plugin ' + plugin + ' is set to ACTIVE in config file.')
            except (ValueError, KeyError, NameError):
                print(
                    '  INFO: No activate-flag set for plugin ' + plugin + ' within the config-file. Will be imported.')

            try:
                # Perform a minimal (!) validity check
                # Check, if plugin is valid (if the plugin.py is provided)
                if not os.path.isfile(os.path.join(plugin_dir, plugin, 'plugin.py')):
                    print('validity check 1 for plugin ' + plugin + ' failed :-( - plugin.py not found.')
                    raise
                else:
                    pass
                    print('validity check 1 for plugin ' + plugin + ' succeeded :-) - plugin.py found')
                print('now appending plugin ' + plugin + ' to list')
                self.plugins.append(import_module('wordclock_plugins.' + plugin + '.plugin').plugin(self.config))
                # Search for default plugin to display the time
                #if plugin == 'time_as_words_german':
                if plugin == 'time_default':
                #if plugin == 'rainbow':
                    print('  Selected "' + plugin + '" as default plugin')
                    self.default_plugin = index
                print('Imported plugin ' + str(index) + ': "' + plugin + '".')
                index += 1
            except:
                print('Failed to import plugin ' + plugin + '!')
                #raise

        # Create object to interact with the wordclock using the interface of your choice
        self.plugin_index = 0
        self.wciweb = wciweb.web_interface(self)
コード例 #3
0
ファイル: wordclock.py プロジェクト: ruedli/rpi_wordclock
    def __init__(self):
        """
        Initializations, executed at every startup of the wordclock
        """
        # Get path of the directory where this file is stored
        self.basePath = os.path.dirname(
            os.path.abspath(inspect.getfile(inspect.currentframe())))

        self.currentGitHash = subprocess.check_output(
            ["git", "describe", "--tags"], cwd=self.basePath).strip().decode()
        logging.info("Software version: " + self.currentGitHash)

        self.config = loadConfig(self.basePath)

        # Create object to interact with the wordclock using the interface of your choice
        self.wci = wci.event_handler()

        self.developer_mode_active = self.config.getboolean(
            'wordclock', 'developer_mode')

        if not self.developer_mode_active:
            import wordclock_interfaces.gpio_interface as wcigpio
            self.gpio = wcigpio.gpio_interface(self.config, self.wci)

        # Create object to display any content on the wordclock display
        # Its implementation depends on your (individual) wordclock layout/wiring
        self.wcd = wcd.wordclock_display(self.config, self.wci)

        # Define path to general icons (not plugin-specific)
        self.pathToGeneralIcons = os.path.join(self.basePath, 'icons',
                                               self.wcd.dispRes())

        # Assemble path to plugin directory
        plugin_dir = os.path.join(self.basePath, 'wordclock_plugins')

        # Assemble list of all available plugins
        plugins = (plugin for plugin in os.listdir(plugin_dir)
                   if os.path.isdir(os.path.join(plugin_dir, plugin)))

        # Import plugins, which can be operated by the wordclock:
        index = 0  # A helper variable (only incremented on successful import)
        self.plugins = []
        for plugin in plugins:
            # Check the config-file, whether to activate or deactivate the plugin
            try:
                if not self.config.getboolean('plugin_' + plugin, 'activate'):
                    logging.info(
                        'Skipping plugin ' + plugin +
                        ' since it is set to activate=false in the config-file.'
                    )
                    continue
            except:
                logging.debug('No activate-flag set for plugin ' + plugin +
                              ' within the config-file. Will be imported.')

            try:
                # Perform a minimal (!) validity check
                # Check, if plugin is valid (if the plugin.py is provided)
                if not os.path.isfile(
                        os.path.join(plugin_dir, plugin, 'plugin.py')):
                    raise
                self.plugins.append(
                    import_module('wordclock_plugins.' + plugin +
                                  '.plugin').plugin(self.config))
                # Search for default plugin to display the time
                if plugin == 'time_default':
                    logging.info('Selected "' + plugin + '" as default plugin')
                    self.default_plugin = index
                logging.info('Imported plugin ' + str(index) + ': "' + plugin +
                             '".')
                index += 1
            except:
                logging.warning('Failed to import plugin ' + plugin + '!')

        # Create object to interact with the wordclock using the interface of your choice
        self.plugin_index = 0
        self.wciweb = wciweb.web_interface(self)