예제 #1
0
class ServerFactory:
    def __init__(self):
        try:
            self.protocols = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(
                path / 'config' / 'config.json',
                default=True)
            self.plugin_manager = PluginManager(self.configuration_manager)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.Task(self.plugin_manager.get_overrides())
        except Exception as e:
            print("Exception encountered during server startup.")
            print(e)
            loop.stop()
            sys.exit()


    def remove(self, protocol):
        self.protocols.remove(protocol)

    def __call__(self, reader, writer):
        server = StarryPyServer(reader, writer, factory=self)
        self.protocols.append(server)
        print(self.protocols)
예제 #2
0
    def __init__(self, client):
        self.manually_overriden = False

        self.configuration_manager = ConfigurationManager(self.CONFIGURATION_FILENAME)
        self.repost_manager = RepostManager(self.HASHES_FILENAME)
        self.command_processor = CommandProcessor(self)

        self.configuration_manager.load_configuration()

        self.TOKEN = self.configuration_manager.configuration['token']

        self.MASTER_ADMIN_ID = self.configuration_manager.configuration['admin']

        self.ABOUT_MESSAGE = self.configuration_manager.configuration['about']

        self.client = client
예제 #3
0
class KeypadApp:
    # pylint: disable=R0903

    ## __slots__ allow us to explicitly declare data members
    __slots__ = ['_config_mgr', '_logger', '_log_store', '_state_object']

    ## KeypadApp class constructor.
    #  @param self The object pointer.
    def __init__(self):
        ## Instance of a configuration manager class.
        self._config_mgr = None
        self._logger = Logger()
        self._log_store = LogStore()

        self._logger.WriteToConsole = True
        self._logger.ExternalLogger = self
        self._logger.Initialise()

        ## Instance of the keypad state object.
        self._state_object = None

    ## Start the application, this will not exit until both the GUI and the
    #  Twisted reactor have been destroyed.  The only exception is if any
    #  elements of the startup fail (e.g. loading the configuration).
    #  @param self The object pointer.
    def start_app(self):

        self._config_mgr = ConfigurationManager()
        config = self._config_mgr.parse_config_file('configuration.json')

        if not config:
            self._logger.Log(LogType.Error, self._config_mgr.last_error_msg)
            return

        wx_app = wx.App()
        reactor.registerWxApp(wx_app)

        self._state_object = KeypadStateObject(config, self._logger)

        keypad_api_ctrl = KeypadApiController(config, self._state_object,
                                              self._log_store, self._logger)
        api_server = server.Site(keypad_api_ctrl)
        reactor.listenTCP(config.keypadController.networkPort, api_server)

        check_panel_loop = LoopingCall(self._state_object.check_panel)
        check_panel_loop.start(0.01, now=False)

        reactor.run()

    ## Stop the application.
    #  @param self The object pointer.
    def stop_app(self):
        self._logger.Log(LogType.Info,
                         'Stopping keypad controller, cleaning up...')

    def add_log_event(self, current_time, log_level, msg):
        self._log_store.AddLogEvent(current_time, log_level, msg)
예제 #4
0
 def __init__(self):
     try:
         self.protocols = []
         self.configuration_manager = ConfigurationManager()
         self.configuration_manager.load_config(path / 'config' /
                                                'config.json',
                                                default=True)
         self.plugin_manager = PluginManager(self.configuration_manager)
         self.plugin_manager.load_from_path(
             path / self.configuration_manager.config.plugin_path)
         self.plugin_manager.resolve_dependencies()
         self.plugin_manager.activate_all()
         asyncio.Task(self.plugin_manager.get_overrides())
     except Exception as e:
         print("Exception encountered during server startup.")
         print(e)
         loop.stop()
         sys.exit()
예제 #5
0
    def __init__(self, endpoints, text_logger, junit_logger, protocol: str):
        self._endpoints = endpoints
        self._text_logger = text_logger
        self._junit_logger = junit_logger
        self._protocol = protocol
        self._session = None

        self._configure_session()

        self._remove_endpoints_by_keywords(
            ConfigurationManager.get_keywords_for_endpoints_skipping())

        if ConfigurationManager.is_http_fuzzing_allowed():
            self._generate_http_fuzzing()
        self._generate_uri_attributes_fuzzing()
        self._generate_request_body_fuzzing()
        self._generate_request_body_fuzzing(
            add_quotation_marks_into_non_string_primitives=True)
예제 #6
0
class ServerFactory:
    def __init__(self):
        try:
            self.protocols = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(
                path / 'config' / 'config.json',
                default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.Task(self.plugin_manager.get_overrides())
        except Exception as e:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()

    @asyncio.coroutine
    def broadcast(self, messages, *, world="", name="", channel=0,
                  client_id=0):
        for protocol in self.protocols:
            try:
                yield from protocol.send_message(messages,
                                                 world=world,
                                                 name=name,
                                                 channel=channel,
                                                 client_id=client_id)
            except ConnectionError:
                continue

    def remove(self, protocol):
        self.protocols.remove(protocol)

    def __call__(self, reader, writer):
        server = StarryPyServer(reader, writer, factory=self)
        self.protocols.append(server)

    def kill_all(self):
        for protocol in self.protocols:
            protocol.die()
예제 #7
0
    def __init__(self):
        try:
            self.connections = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(path / 'config' /
                                                   'config.json',
                                                   default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.ensure_future(self.plugin_manager.get_overrides())
        except Exception as err:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()
예제 #8
0
    def start_app(self):

        if not os.getenv('PWRCON_CONFIG'):
            self._logger.error('PWRCON_CONFIG environment variable missing!')
            sys.exit(1)

        config_file = os.getenv('PWRCON_CONFIG')

        self._config_mgr = ConfigurationManager()
        config = self._config_mgr.parse_config_file(config_file)

        if not config:
            print(f"ERROR: {self._config_mgr.last_error_msg}")
            return

        wx_app = wx.App()

        main_window = MainWindow(config)
        main_window.Show()

        wx_app.MainLoop()
예제 #9
0
class PowerConsoleApp:
    # pylint: disable=R0903

    ## __slots__ allow us to explicitly declare data members
    __slots__ = ['_config_mgr', '_logger']

    ## KeypadApp class constructor.
    #  @param self The object pointer.
    def __init__(self):
        ## Instance of a configuration manager class.
        self._config_mgr = None

        formatter = logging.Formatter(
            "%(asctime)s [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S")

        ## Instance of a logger.
        self._logger = logging.getLogger('system log')
        console_stream = logging.StreamHandler()
        console_stream.setFormatter(formatter)
        self._logger.setLevel(logging.DEBUG)
        self._logger.addHandler(console_stream)

    ## Start the application, this will not exit until both the GUI and the
    #  Twisted reactor have been destroyed.  The only exception is if any
    #  elements of the startup fail (e.g. loading the configuration).
    #  @param self The object pointer.
    def start_app(self):

        if not os.getenv('PWRCON_CONFIG'):
            self._logger.error('PWRCON_CONFIG environment variable missing!')
            sys.exit(1)

        config_file = os.getenv('PWRCON_CONFIG')

        self._config_mgr = ConfigurationManager()
        config = self._config_mgr.parse_config_file(config_file)

        if not config:
            print(f"ERROR: {self._config_mgr.last_error_msg}")
            return

        wx_app = wx.App()

        main_window = MainWindow(config)
        main_window.Show()

        wx_app.MainLoop()

    ## Stop the application.
    #  @param self The object pointer.
    def stop_app(self):
        self._logger.info('Stopping power console, cleaning up...')
예제 #10
0
    def start_app(self):

        self._config_mgr = ConfigurationManager()
        config = self._config_mgr.parse_config_file('configuration.json')

        if not config:
            self._logger.Log(LogType.Error, self._config_mgr.last_error_msg)
            return

        wx_app = wx.App()
        reactor.registerWxApp(wx_app)

        self._state_object = KeypadStateObject(config, self._logger)

        keypad_api_ctrl = KeypadApiController(config, self._state_object,
                                              self._log_store, self._logger)
        api_server = server.Site(keypad_api_ctrl)
        reactor.listenTCP(config.keypadController.networkPort, api_server)

        check_panel_loop = LoopingCall(self._state_object.check_panel)
        check_panel_loop.start(0.01, now=False)

        reactor.run()
예제 #11
0
 def _generate_single_query_additional_parameter(id_generator,
                                                 uri_parameters, fuzzable,
                                                 parameter_name, required):
     are_non_required_attributes_in_requests = ConfigurationManager.are_non_required_attributes_in_requests(
     )
     if required or are_non_required_attributes_in_requests:
         prefix = "?" if "?" not in s_render().decode('ascii',
                                                      'ignore') else "&"
         name = "URI attribute, default value: " + parameter_name + ", id: " + next(
             id_generator)
         s_http_string(prefix + parameter_name + "=",
                       fuzzable=False,
                       encoding=EncodingTypes.ascii,
                       name=name)
         RequestBuildHelper._append_parameter(parameter_name, id_generator,
                                              uri_parameters, fuzzable)
예제 #12
0
 def __init__(self):
     try:
         self.protocols = []
         self.configuration_manager = ConfigurationManager()
         self.configuration_manager.load_config(
             path / 'config' / 'config.json',
             default=True)
         self.plugin_manager = PluginManager(self.configuration_manager)
         self.plugin_manager.load_from_path(
             path / self.configuration_manager.config.plugin_path)
         self.plugin_manager.resolve_dependencies()
         self.plugin_manager.activate_all()
         asyncio.Task(self.plugin_manager.get_overrides())
     except Exception as e:
         print("Exception encountered during server startup.")
         print(e)
         loop.stop()
         sys.exit()
예제 #13
0
    def __init__(self):
        try:
            self.connections = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(
                path / 'config' / 'config.json',
                default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.ensure_future(self.plugin_manager.get_overrides())
        except Exception as err:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()
예제 #14
0
def report_progress(session, junit_logger):
    if did_fuzzing_already_started(session) > 0:

        if is_fuzzing_hanged(session):
            message = create_hanged_message(session)
            print(message, file=sys.stderr)
            try:
                junit_logger.close_test()
            except:
                pass
            finally:
                os._exit(2)

        if is_fuzzing_still_in_progress(session):
            plan_another_report(session, junit_logger, ConfigurationManager.get_reporting_interval())

        message = create_report_message(session)
        print(message)
    else:
        plan_another_report(session, junit_logger, DID_FUZZING_STARTED_CHECKS_TIME_INTERVAL_IN_SECONDS)
예제 #15
0
    def accept(self) -> None:

        if not self.testConnection():
            return

        self.library.setBackend(self.backend_tab.getBackend())
        self.library.setName(self.name_input.text())

        # resolving naming scheme selection

        indices: List[int] = [
            m.row() for m in self.naming_schemes_list.selectionModel().selectedRows()
        ]

        assert len(indices) == 1

        scheme: NamingScheme = cast(
            List[NamingScheme], ConfigurationManager().namingSchemes
        )[indices[0]]

        self.library.setNamingScheme(scheme)

        return super().accept()
예제 #16
0
def main():
    config_file_path = sys.argv[1]
    endpoints_description = sys.argv[2]
    junit_output = sys.argv[3]
    custom_payloads_path = sys.argv[4] if len(sys.argv) == 5 else None

    with open(config_file_path, 'r') as config_file_pointer:
        ConfigurationManager(config_file_pointer)

    target = ConfigurationManager.config["target"]

    # Load and generate default payloads
    load_default_payloads(target["hostname"])

    # If user specified file with custom payloads, we add them to our mutations
    payloads_loader = PayloadsLoader(target["hostname"])
    payloads_loader.load_payloads(custom_payloads_path,
                                  FuzzPayloads.CUSTOM_PAYLOADS_KEY)

    with open(junit_output, 'w', encoding='utf8') as junit_output_file_pointer:
        with open(FUZZING_LOG_FILE, "w",
                  encoding='utf8') as full_log_file_pointer:
            text_logger = TextLogger(full_log_file_pointer)
            junit_logger = JUnitLogger(junit_output_file_pointer,
                                       test_suite_name_delimiter=":",
                                       hostname=target["hostname"])
            protocol = 'ssl' if target["ssl"] is True else 'tcp'

            with open(endpoints_description,
                      'r') as endpoints_description_file_pointer:
                endpoints = json.loads(
                    endpoints_description_file_pointer.read())

            fuzzer = Fuzzer(endpoints, text_logger, junit_logger, protocol)
            fuzzer.fuzz()
            return fuzzer.was_there_any_failure()
예제 #17
0
 def setup(self):
     self.config = ConfigurationManager()
예제 #18
0
class ServerFactory:
    def __init__(self):
        try:
            self.connections = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(path / 'config' /
                                                   'config.json',
                                                   default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.ensure_future(self.plugin_manager.get_overrides())
        except Exception as err:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()

    @asyncio.coroutine
    def broadcast(self,
                  messages,
                  *,
                  mode=ChatReceiveMode.RADIO_MESSAGE,
                  **kwargs):
        """
        Send a message to all connected clients.

        :param messages: Message(s) to be sent.
        :param mode: Mode bit of message.
        :return: Null.
        """
        for connection in self.connections:
            try:
                yield from connection.send_message(messages, mode=mode)
            except Exception as err:
                logger.exception("Error while trying to broadcast.")
                logger.exception(err)
                continue

    def remove(self, connection):
        """
        Remove a single connection.

        :param connection: Connection to be removed.
        :return: Null.
        """
        self.connections.remove(connection)

    def __call__(self, reader, writer):
        """
        Whenever a client connects, ping the server factory to start
        handling it.

        :param reader: Reader transport socket.
        :param writer: Writer transport socket.
        :return: Null.
        """
        server = StarryPyServer(reader,
                                writer,
                                self.configuration_manager,
                                factory=self)
        self.connections.append(server)
        logger.debug("New connection established.")

    def kill_all(self):
        """
        Drop all connections.

        :return: Null.
        """
        logger.debug("Dropping all connections.")
        for connection in self.connections:
            connection.die()
예제 #19
0
    def deserialize(self, serialized: Dict[str, Any]) -> None:

        b: Type[Backend]
        name: str = serialized.get("backendName", "")

        backend: Optional[Backend] = None

        for b in Backends:
            if b.getName() == name:
                backend = b()
                break

        if not backend:
            raise IOError(
                "backend with name {name} not found".format(name=name))

        ser: Dict[str, Any] = serialized.get("backend", {})

        if not ser:
            raise IOError("no backend data supplied")

        backend.deserialize(ser)

        self._backend = backend
        self._tree.setBackend(self._backend)
        self._uuid = uuid.UUID(serialized.get("uuid", ""))
        self._name = serialized.get("name", "")

        tree: Dict[str, Any] = serialized.get("tree", {})

        if tree:
            self._tree.deserialize(tree)

        books: List[Dict[str, Any]] = serialized.get("books", [])
        book: Dict[str, Any]

        for book in books:

            book_obj: Book = Book()
            book_obj.deserialize(book)

            self._books[book_obj.path.as_posix()] = book_obj

        scheme_s: str = serialized.get("naming_scheme", "")

        if not scheme_s:

            warnings.warn(
                f"library '{self._name}' has no naming scheme. Will use the default naming scheme instead."
            )

            self._naming_scheme = cast(List[NamingScheme],
                                       ConfigurationManager().namingSchemes)[0]

        else:

            scheme_idx: int = cast(List[NamingScheme],
                                   ConfigurationManager().namingSchemes).index(
                                       scheme_s)  # type: ignore

            self._naming_scheme = cast(
                List[NamingScheme],
                ConfigurationManager().namingSchemes)[scheme_idx]
예제 #20
0
import logging
import time

from configuration_manager import ConfigurationManager

conf_manager = ConfigurationManager()
module_params = {}


@conf_manager.observe
def update_configuration(new_configuration):
    print('updating new configuration')
    global module_params
    module_params = new_configuration


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    module_params = conf_manager.configuration()
    print(module_params)
    conf_manager.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        conf_manager.stop()
        conf_manager.join()

예제 #21
0
    def __init__(self):
        try:
            # initiate logger
            self.logger = logging.getLogger()
            self.logger.setLevel(logging.DEBUG)
            self.log_send_store_handler = LogSendStoreHandler(LOG_LOCATION)
            formatter = logging.Formatter(
                '%(asctime)s - %(levelname)s - %(name)s - %(message)s')
            self.log_send_store_handler.setFormatter(formatter)
            self.logger.addHandler(self.log_send_store_handler)
            self.logger.info('Initialising system...')
            job_info_filter = JobInfoFilter()
            logging.getLogger('apscheduler.scheduler').addFilter(
                job_info_filter)
            logging.getLogger('apscheduler.threadpool').addFilter(
                job_info_filter)

            # load local configuration
            self.conf_man = ConfigurationManager(CONFIG_LOCATION)
            self.log_send_store_handler.update_configuration()

            self.scheduler = Scheduler()
            self.scheduler.start()

            self.packet_manager = PacketManager(self.scheduler)

            # initiate network connection
            self.connection = ConnectionManager()

            # add scheduler and connection to log handler
            self.log_send_store_handler.update_configuration(
                scheduler=self.scheduler,
                connection=self.connection)

            # try to connect
            connected_to_internet = self.connection.check_internet_connection()
            connected_to_server = self.connection.check_server_connection()
            if connected_to_internet and connected_to_server:
                self.load_online_configuration_and_initiate_sending_data()
                self.packet_manager.update_time()
                self.packet_manager.initiate_send_packets(self.connection)
            else:
                '''
                if there is no connection:
                    keep checking for a connection
                    temporarily use offline timer and modbus slave
                    configuration
                '''

                if connected_to_internet:
                    self.packet_manager.update_time()
                self.wait_for_connection_to_load_configuration()

            # initiate sensor timers
            self.read_sensor_scheduler = ReadSensorScheduler(
                self.scheduler,
                self.packet_manager)
            self.led_manager = LedManager(self.scheduler)
            self.led_manager.update_led(PinName.powered, LedState.on)
            self.set_up_led_manager_calls()

            # sleep 2 seconds to intialise led of log handler
            sleep(1)
            self.logger.info('Initialisation complete')

            while True:
                sleep(10)
                self.logger.debug('Alive and kicking')
                if self.logger.level is logging.DEBUG:
                    scheduler_jobs = self.scheduler.get_jobs()
                    if len(scheduler_jobs) > 1:
                        self.logger.debug('Current scheduler jobs:')
                        for index, job in enumerate(scheduler_jobs):
                            self.logger.debug(' Job {0}: {1} {2}'.format(
                                index,
                                job.name,
                                job.next_run_time))
                    else:
                        self.logger.debug('No running scheduler jobs')

        except Exception as e:
            self.logger.error(e)
            raise
            self.log_send_store_handler.send_logs_job()
예제 #22
0
class DataLogger:

    """
    In this class the different systems are initialiased:
        - logger and its handlers
        - local configuration
        - connection with internet

    If there is no connection with the internet:
        - A timed job is created that controls internet connection.
        - Logging of data is started with local configuration.
        - If the internet connection is started, and the online\
        configuration differs, the old wrongly logged data will be removed.

    If there is connection with the internet and the server is working:
        - Check if the online configuration differs from the local one. If so,\
        the configuration will be updated.
        - Logging of data is started.
        - Sending of data is started.
        - A timed job is created that checks if the online configuration is\
        updated
        - The management of leds is started.
    """

    def __init__(self):
        try:
            # initiate logger
            self.logger = logging.getLogger()
            self.logger.setLevel(logging.DEBUG)
            self.log_send_store_handler = LogSendStoreHandler(LOG_LOCATION)
            formatter = logging.Formatter(
                '%(asctime)s - %(levelname)s - %(name)s - %(message)s')
            self.log_send_store_handler.setFormatter(formatter)
            self.logger.addHandler(self.log_send_store_handler)
            self.logger.info('Initialising system...')
            job_info_filter = JobInfoFilter()
            logging.getLogger('apscheduler.scheduler').addFilter(
                job_info_filter)
            logging.getLogger('apscheduler.threadpool').addFilter(
                job_info_filter)

            # load local configuration
            self.conf_man = ConfigurationManager(CONFIG_LOCATION)
            self.log_send_store_handler.update_configuration()

            self.scheduler = Scheduler()
            self.scheduler.start()

            self.packet_manager = PacketManager(self.scheduler)

            # initiate network connection
            self.connection = ConnectionManager()

            # add scheduler and connection to log handler
            self.log_send_store_handler.update_configuration(
                scheduler=self.scheduler,
                connection=self.connection)

            # try to connect
            connected_to_internet = self.connection.check_internet_connection()
            connected_to_server = self.connection.check_server_connection()
            if connected_to_internet and connected_to_server:
                self.load_online_configuration_and_initiate_sending_data()
                self.packet_manager.update_time()
                self.packet_manager.initiate_send_packets(self.connection)
            else:
                '''
                if there is no connection:
                    keep checking for a connection
                    temporarily use offline timer and modbus slave
                    configuration
                '''

                if connected_to_internet:
                    self.packet_manager.update_time()
                self.wait_for_connection_to_load_configuration()

            # initiate sensor timers
            self.read_sensor_scheduler = ReadSensorScheduler(
                self.scheduler,
                self.packet_manager)
            self.led_manager = LedManager(self.scheduler)
            self.led_manager.update_led(PinName.powered, LedState.on)
            self.set_up_led_manager_calls()

            # sleep 2 seconds to intialise led of log handler
            sleep(1)
            self.logger.info('Initialisation complete')

            while True:
                sleep(10)
                self.logger.debug('Alive and kicking')
                if self.logger.level is logging.DEBUG:
                    scheduler_jobs = self.scheduler.get_jobs()
                    if len(scheduler_jobs) > 1:
                        self.logger.debug('Current scheduler jobs:')
                        for index, job in enumerate(scheduler_jobs):
                            self.logger.debug(' Job {0}: {1} {2}'.format(
                                index,
                                job.name,
                                job.next_run_time))
                    else:
                        self.logger.debug('No running scheduler jobs')

        except Exception as e:
            self.logger.error(e)
            raise
            self.log_send_store_handler.send_logs_job()

    def load_online_configuration_and_initiate_sending_data(self):
        # check online configuration
        try:
            online_checksum = self.connection.get_configuration_checksum()
            self.logger.info("Checking online configuration..")
            if self.conf_man.is_online_configuration_different(online_checksum):
                self.logger.info(
                    'Online configuration is new, updating configuration..')
                online_configuration = self.connection.get_configuration()
                self.conf_man.validate_json_configuration(online_configuration)
                self.conf_man.save_configuration_local(
                    online_checksum,
                    online_configuration)
                self.packet_manager.remove_all_packets_from_memory()

                # update systems that make use of the configuration
                self.log_send_store_handler.update_configuration(
                    scheduler=self.scheduler,
                    connection=self.connection)
                self.connection.update_configuration()
                try:
                    self.read_sensor_scheduler.update_configuration()
                except:
                    pass
                self.packet_manager.update_configuration()
        except:
            self.logger.warning('Problem updating configuration')
            raise
        try:  # try to remove job
            self.scheduler.unschedule_func(
                self.load_online_configuration_and_initiate_sending_data)
        except:
            pass

        # periodically check changes in configuration
        self.scheduler.add_interval_job(
            self.load_online_configuration_and_initiate_sending_data,
            seconds=configuration.get_time_interval_to_check_online_config())

        self.packet_manager.initiate_send_packets(self.connection)

    def wait_for_connection_to_load_configuration(self):
        if not self.connection.is_connected():
            # no internet connection, start job to check connection
            self.scheduler.add_interval_job(self.try_to_connect_to_internet,
                                            seconds=CHECK_CONNECTION_INTERVAL)
        else:
            self.packet_manager.update_time()
            if not self.connection.check_server_connection():
                # no connection with server, start job to check connection
                self.scheduler.add_interval_job(
                    self.try_to_load_online_configuration,
                    seconds=CHECK_CONNECTION_INTERVAL)

    def try_to_connect_to_internet(self):
        if self.connection.check_internet_connection():
            self.scheduler.unschedule_func(self.try_to_connect_to_internet)

            self.packet_manager.update_time()
            if not self.connection.check_server_connection():
                    # no connection with server, start job to check connection
                self.scheduler.add_interval_job(
                    self.try_to_load_online_configuration,
                    seconds=CHECK_CONNECTION_INTERVAL)
            else:
                self.load_online_configuration_and_initiate_sending_data()

    def try_to_load_online_configuration(self):
        if self.connection.check_server_connection():
            self.load_online_configuration_and_initiate_sending_data()

            self.scheduler.unschedule_func(
                self.try_to_load_online_configuration)

    def set_up_led_manager_calls(self):
        sensor_led_call = LedCall(self.led_manager, PinName.readingsensor)
        connected_led_call = LedCall(self.led_manager, PinName.connected)
        logging_led_call = LedCall(self.led_manager, PinName.logging)

        self.read_sensor_scheduler.set_led_call(sensor_led_call)
        self.connection.set_led_call(connected_led_call)
        self.log_send_store_handler.set_led_call(logging_led_call)
예제 #23
0
# Broadening the base path in order to import remind-me's libraries
Install_Directory = os.path.dirname(os.path.abspath(__file__))
os.chdir(Install_Directory)
sys.path.append(Install_Directory + '/src/python')

# remind-me's libraries imports
from configuration_manager import ConfigurationManager
from data_manager_plyvel import DataManagerPlyvel
from data_manager_pickle import DataManagerPickle
from mordelles_library_api import MordellesLibraryAPI
from xtemplate import Xtemplate
from loans_mailer import LoansMailer

if __name__ == "__main__":

    with ConfigurationManager(Install_Directory + '/preferences.yaml') as cm, \
        DataManagerPickle(cm) as dm,                                          \
        MordellesLibraryAPI(cm) as lib_api,                                   \
        Xtemplate() as formatter,                                             \
        LoansMailer(cm, dm, lib_api, formatter) as mailer:

        # Configure logging facility
        app_logger = logging.getLogger()
        app_logger.setLevel(0)
        # Defines logger file and max size
        handler = logging.handlers.RotatingFileHandler(
            cm.get('configuration.log-directory') + '/' +
            cm.get("configuration.log-file"),
            maxBytes=1000000)
        # Define logger format
        formatter = logging.Formatter(
예제 #24
0
 def __init__(self, config=None, create=None):
     config = config or self.DEFAULT_CONFIG_PATH
     create = create or False
     self.config_manager = ConfigurationManager(os.path.abspath(config),
                                                create)
     self.executor = CommandExecutor(self.config_manager)
예제 #25
0
    def __init__(
        self, backend_tab: Type[BackendTab], library: Library, *args: Any, **kwargs: Any
    ):

        super().__init__(*args, **kwargs)

        self.library = library
        self.name_input_was_edited = False

        self.updated.connect(self.handleUpdated)

        layout = QHBoxLayout(self)

        self.tabs = QTabWidget(self)

        self.general_tab = QWidget(self)

        general_layout: QHBoxLayout = QHBoxLayout(self.general_tab)

        name_label: QLabel = QLabel("Name:", self.general_tab)
        general_layout.addWidget(name_label)

        self.name_input = QLineEdit(self.library.getName(), self.general_tab)
        self.name_input.textChanged.connect(self.handleUpdated)
        self.name_input.textEdited.connect(self.setNameInputWasEdited)
        name_label.setBuddy(self.name_input)
        general_layout.addWidget(self.name_input)

        naming_schemes_label: QLabel = QLabel("Naming scheme:", self.general_tab)
        general_layout.addWidget(naming_schemes_label)

        self.naming_schemes_list = QTableView(self.general_tab)
        self.naming_schemes_list.setTabKeyNavigation(False)
        self.naming_schemes_model = NamingSchemesModel()
        self.naming_schemes_list.setModel(self.naming_schemes_model)
        self.naming_schemes_list.setSelectionMode(QTableView.SingleSelection)
        self.naming_schemes_list.setSelectionBehavior(QTableView.SelectRows)
        self.naming_schemes_list.selectRow(
            0
            if not self.library.getNamingScheme()
            else cast(List[NamingScheme], ConfigurationManager().namingSchemes).index(
                cast(NamingScheme, self.library.getNamingScheme())
            )
        )
        naming_schemes_label.setBuddy(self.naming_schemes_list)
        general_layout.addWidget(self.naming_schemes_list)

        self.general_tab.setLayout(general_layout)

        self.tabs.addTab(self.general_tab, "General")

        self.backend_tab = backend_tab(self, self.library.getBackend())

        self.tabs.addTab(self.backend_tab, "Location")

        self.button_box = QDialogButtonBox(
            cast(
                QDialogButtonBox.StandardButton,
                QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
            ),
            self,
        )
        self.button_box.accepted.connect(self.accept)
        self.button_box.rejected.connect(self.reject)
        layout.addWidget(self.button_box)

        self.setLayout(layout)

        self.updated.emit()
예제 #26
0
class RepostBot:
    HASHES_FILENAME = 'hashes.txt'
    CONFIGURATION_FILENAME = 'settings.json'
    DEFAULT_CHANNEL = 'memes'

    def __init__(self, client):
        self.manually_overriden = False

        self.configuration_manager = ConfigurationManager(self.CONFIGURATION_FILENAME)
        self.repost_manager = RepostManager(self.HASHES_FILENAME)
        self.command_processor = CommandProcessor(self)

        self.configuration_manager.load_configuration()

        self.TOKEN = self.configuration_manager.configuration['token']

        self.MASTER_ADMIN_ID = self.configuration_manager.configuration['admin']

        self.ABOUT_MESSAGE = self.configuration_manager.configuration['about']

        self.client = client

    def run(self):
        self.client.run(self.TOKEN)

    async def on_ready(self):
        print('Logged in as')
        print(self.client.user.name)
        print(self.client.user.id)
        print('------')

    async def on_message(self, message):
        if message.channel.name != self.DEFAULT_CHANNEL:
            return
            
        await self.command_processor.process_command(message)
        if not self.manually_overriden and self.repost_manager.is_repost(message):
            try:
                await self.client.send_message(message.channel, message.author.display_name + ' just reposted a meme.')
                await self.client.delete_message(message)
            except discord.Forbidden:
                await self.client.send_message(message.channel, 'I need permissions to manage messages in order to work.')


    def is_admin(self, author):
        if (author.id == self.MASTER_ADMIN_ID):
            return True

        for role in author.roles:
            if role.permissions.administrator:
                return True

        return False

    async def about(self, message):
        await self.client.send_message(message.channel, self.ABOUT_MESSAGE)

    async def off(self, message):
        if self.is_admin(message.author):
            self.manually_overriden = True
            await self.client.send_message(message.channel, 'Repost control turned off.')

    async def on(self, message):
        if self.is_admin(message.author):
            self.manually_overriden = False
            await self.client.send_message(message.channel, 'Repost control turned on.')
예제 #27
0
class ServerFactory:
    def __init__(self):
        try:
            self.connections = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(
                path / 'config' / 'config.json',
                default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.ensure_future(self.plugin_manager.get_overrides())
        except Exception as err:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()

    @asyncio.coroutine
    def broadcast(self, messages):
        """
        Send a message to all connected clients.

        :param messages: Message(s) to be sent.
        :return: Null.
        """
        for connection in self.connections:
            try:
                yield from connection.send_message(messages)
            except Exception as err:
                logger.exception("Error while trying to broadcast.")
                logger.exception(err)
                continue

    def remove(self, connection):
        """
        Remove a single connection.

        :param connection: Connection to be removed.
        :return: Null.
        """
        self.connections.remove(connection)

    def __call__(self, reader, writer):
        """
        Whenever a client connects, ping the server factory to start
        handling it.

        :param reader: Reader transport socket.
        :param writer: Writer transport socket.
        :return: Null.
        """
        server = StarryPyServer(reader, writer, self.configuration_manager,
                                factory=self)
        self.connections.append(server)
        logger.debug("New connection established.")

    def kill_all(self):
        """
        Drop all connections.

        :return: Null.
        """
        logger.debug("Dropping all connections.")
        for connection in self.connections:
            connection.die()
예제 #28
0
 def get_boolean_payloads():
     payload_folders = ConfigurationManager.get_payloads_folders_for_boolean_json_primitive(
     )
     return FuzzPayloads._get_specific_type_payloads(payload_folders)
예제 #29
0
class TestConfigurationManager:
    def __init__(self):
        self.base_path = utilities.path / 'tests' / 'test_config'
        self.output_path = self.base_path / 'test_outputs' / 'out.json'
        self.trivial_config_path = self.base_path / 'trivial_config.json'
        self.complex_config_path = self.base_path / 'complex_config.json'
        self.merged_path = self.base_path / 'complex_merged_config.json'
        self.config_with_utf_8_path = self.base_path / 'unicode_config.json'
        self.config = None

    def setup(self):
        self.config = ConfigurationManager()

    def test_config_manager_loads_correct_path(self):
        with open(str(str(self.trivial_config_path))) as f:
            raw = f.read()
        self.config.load_config(str(self.trivial_config_path))
        assert_equals(raw, self.config._raw_config)

    def test_config_manager_path_set_on_load(self):
        self.config.load_config(self.trivial_config_path)
        assert_equals(self.config._path, self.trivial_config_path)

    def test_config_manager_load_complex_with_default(self):
        with open(str(self.merged_path)) as f:
            merged = json.load(f)
        self.config.load_config(str(self.complex_config_path), default=True)
        assert_equals(merged, self.config.config)

    def test_config_manager_dot_notation(self):
        complex_config_path = str((self.base_path / 'complex_config.json'))
        self.config.load_config(str(complex_config_path))
        assert_equals(self.config.config.test1.test5.test7.test8,
                      ["test9", "test10"])
        with assert_raises(AttributeError):
            self.config.config.borp

    def test_config_manager_add_dictionary_with_dot_notation(self):
        complex_config_path = str((self.base_path / 'complex_config.json'))
        self.config.load_config(str(complex_config_path))
        self.config.config.testx = {"testy": "testz"}
        assert_equals(self.config.config.testx.testy, "testz")

    def test_config_manager_save_to_path(self):
        self.config.load_config(str(self.complex_config_path))
        self.config.save_config(path=str(self.output_path))
        assert_true(self.output_path.exists(),
                    msg="Output file does not exist.")

    def test_config_manager_save_utf_8(self):
        self.config.load_config(str(self.config_with_utf_8_path))
        self.config.save_config(path=str(self.output_path))
        with self.config_with_utf_8_path.open() as f:
            original = f.read()
        with self.output_path.open() as f:
            saved = f.read()
        assert_equals(original, saved)

    def test_config_manager_with_path_object(self):
        self.config.load_config(self.trivial_config_path)