コード例 #1
0
    def __init__(self,
                 config: JsonDict = None,
                 communicator: python_communicator.PythonCommunicator = None,
                 logger=None,
                 autocheckports=AUTOCHECKPORTS,
                 port_check_time=PORTCHECKTIME,
                 permanently_ignored_ports=None,
                 datalogger: DataLogger = None,
                 start_in_background=False):

        self.ignored_ports = set()
        self.deadports = set()
        self.connected_ports = set()
        self.available_ports = set()
        self.identified_ports = set()

        self.datalogger = DataLogger() if datalogger is None else datalogger

        self.config = JsonDict("portdata.json") if config is None else config

        if permanently_ignored_ports is None:
            permanently_ignored_ports = []

        self.port_check_time = port_check_time
        self.autocheckports = autocheckports
        self.permanently_ignored_ports = set(permanently_ignored_ports)

        self.set_communicator(python_communicator.PythonCommunicator(
        ) if communicator is None else communicator)

        self.logger = logging.getLogger(
            "SerialReader") if logger is None else logger

        if start_in_background:
            self.run_in_background()
コード例 #2
0
 def setUp(self):
     self.temp_dir = tempfile.mkdtemp()
     self.temp_file = os.path.join(self.temp_dir, "d")
     with open(self.temp_file, "wb+") as f:
         f.write(b'{"test":"file"}')
     with open(self.temp_file, "r") as f:
         print(f.read(), self.temp_file)
     self.d = JsonDict(self.temp_file, backup=False)
コード例 #3
0
 def test_empty_key(self):
     dic = {
         "data": {
             "": {
                 "inempty": ["test"],
             }
         }
     }
     self.d.data = dic
     ds = JsonDict(self.d.file)
     print(ds)
コード例 #4
0
    def __init__(self, serial_reader=None, config=None):
        """
        :type serial_reader: SerialReader
        """
        super().__init__()
        self._on_stop_functions = []
        self.data_logger = DataLogger()
        self._data = dict()
        self.running = False
        self.pause = False
        self._ws_targets = set()
        self.logger = logging.getLogger(self.__class__.__name__)
        self.logger.info("start API {}".format(self.__class__.__name__))
        self._serial_reader = None
        self.config = (
            JsonDict(
                os.path.join(
                    os.path.expanduser("~"),
                    ".{}".format(self.__class__.__name__),
                    "portdata.json",
                )
            )
            if config is None
            else config
        )
        self.possible_boards = [[] for board in self.required_boards]
        if (
            self.config.get("api_name", default=self.__class__.__name__)
            != self.__class__.__name__
        ):
            self.config.data = {}
            self.config.put("api_name", value=self.__class__.__name__)
            self.config.put(
                "linked_boards", value=[None for board in self.required_boards]
            )

        self.linked_boards = [None for board in self.required_boards]
        if len(
            self.config.get(
                "linked_boards",
                default=[b.id if b is not None else None for b in self.linked_boards],
            )
        ) != len(self.linked_boards):
            self.config.put(
                "linked_boards", value=[None for board in self.required_boards]
            )

        for board in self.required_boards:
            parseboards.add_board(board)
        self.serial_reader = serial_reader
コード例 #5
0
    def __init__(
        self,
        name="arduinocontrollerapi",
        python_communicator: PythonCommunicator = None,
        websocket_server: SocketServer = None,
        config=None,
        datalogger: DataLogger = None,
        logger=None,
        data_dir=None,
    ):
        self._python_communicator = None
        self.board_data = {}
        self.websocket_server = None
        if websocket_server is not None:
            self.set_websocket_server(websocket_server)
        self.name = name
        self.data = {}
        self.dataupdate = 1
        self.lastupdate = 0
        self.lastsend = dict()
        self.connected_ports = set()
        self.ignored_ports = set()
        self.available_ports = set()
        self.identified_ports = set()

        self.logger = logging.getLogger(
            self.name) if logger is None else -logger

        self.data_dir = (os.path.join(tempfile.gettempdir(), self.name +
                                      "_API")
                         if data_dir is None else data_dir)

        os.makedirs(self.data_dir, exist_ok=True)

        self.config = (JsonDict(file=os.path.join(self.data_dir,
                                                  "config.json"),
                                createfile=True) if config is None else config)
        self.config.autosave = True

        self.sensor_ports = set([])

        self.set_communicator(PythonCommunicator(
        ) if python_communicator is None else python_communicator)

        self.datalogger = (DataLogger(
            autosave_path=self.data_dir) if datalogger is None else datalogger)

        self.running = False
        self.run_thread = None
コード例 #6
0
    def __init__(self, interpreter, on_connect=None, config=None, **kwargs):
        super().__init__(**kwargs)
        self._interpreter = None
        self.interpreter = interpreter
        if config:
            self.config = config
        else:
            self.config = JsonDict()

        self.send_queue = []
        self.connection_checks = []
        self.connected = False
        if not hasattr(self, "logger"):
            self.logger = logging.getLogger(self.__class__.__name__)
        if not hasattr(self, "on_connect") or on_connect is not None:
            self.on_connect = on_connect
コード例 #7
0
ファイル: manage.py プロジェクト: ajaykhanna123/django_test
def plug_in(appconfig, config=None):
    # plugin app
    global CONFIG
    if CONFIG is None:
        if config is not None:
            CONFIG = config.getsubdict(preamble=["plug_in_django_server"])
        else:
            CONFIG = JsonDict(
                os.path.join(
                    os.path.join(os.path.expanduser("~"),
                                 ".plug_in_django_server"),
                    "plug_in_django_server_config.json",
                ))

    if config is not None:
        appconfig.config = config.getsubdict(preamble=[appconfig.name])
    else:
        if appconfig.config is None:
            appconfig.config = CONFIG.getsubdict(preamble=[appconfig.name])
    logger.info("plug in {} with the config-path:'{}'".format(
        appconfig.name, appconfig.config.file))

    if not hasattr(appconfig, "baseurl"):
        setattr(appconfig, "baseurl", getattr(appconfig, "name"))

    apps = CONFIG.get("django_settings", "apps", "additional", default={})
    if getattr(appconfig, "data_dir", False) is True:
        appconfig.data_dir = os.path.join(os.path.dirname(CONFIG.file),
                                          "{}_data".format(appconfig.name))
        appconfig.data_dir_url = ("/" + appconfig.baseurl +
                                  ("/" if len(appconfig.baseurl) > 0 else "") +
                                  "{}_data".format(appconfig.name))
    else:
        appconfig.data_dir = False

    apps[appconfig.name] = {
        "name": appconfig.name,
        "baseurl": getattr(appconfig, "baseurl"),
        "data_dir": appconfig.data_dir,
    }
    if appconfig.data_dir is not False:
        os.makedirs(appconfig.data_dir, exist_ok=True)

    CONFIG.put("django_settings", "apps", "additional", value=apps)
    CONFIG.save()
コード例 #8
0
    def __init__(
        self,
        auto_check_ports=AUTO_CHECK_PORTS,
        port_check_time=PORT_CHECK_TIME,
        start_in_background=False,
        config: JsonDict = None,
        logger=None,
        permanently_ignored_ports=None,
        # datalogger: DataLogger = None,
    ):
        self.data_targets = set()

        self.ignored_ports = set()
        self.dead_ports = set()
        self.connected_ports = set()
        self.available_ports = set()
        self.identified_ports = set()
        self.running = False
        self.read_thread = None
        # self.datalogger = DataLogger() if datalogger is None else datalogger

        self.config = (
            JsonDict(
                os.path.join(
                    os.path.expanduser("~"), ".arduino_controller", "portdata.json"
                )
            )
            if config is None
            else config
        )

        if permanently_ignored_ports is None:
            permanently_ignored_ports = []

        self.port_check_time = port_check_time
        self.auto_check_ports = auto_check_ports
        self.permanently_ignored_ports = set(permanently_ignored_ports)

        # self.set_communicator(python_communicator.PythonCommunicator() if communicator is None else communicator)

        self.logger = logging.getLogger("SerialReader") if logger is None else logger

        if start_in_background:
            self.run_in_background()
コード例 #9
0
import os
import time

from json_dict import JsonDict
from arduino_controller.controller import ArduinoController

if __name__ == "__main__":
    import argparse

    p = argparse.ArgumentParser()
    p.add_argument('-c', '--config', dest="config",
                   default=os.path.join(os.path.expanduser("~"), ".ArduinoController", "controller_config.json"))

    kwargs = vars(p.parse_args())

    kwargs['config'] = JsonDict(kwargs['config'])

    logging_fmt = (
        "%(asctime)s %(filename)s %(lineno)d %(name)s %(levelname)-8s  %(message)s"
    )
    logging.basicConfig(level=logging.DEBUG, format=logging_fmt, datefmt="(%H:%M:%S)")

    try:
        import coloredlogs

        coloredlogs.install(level="DEBUG", fmt=logging_fmt)
    except:
        pass

    ac = ArduinoController(**kwargs, start_in_background=True)
    while ac._running:
コード例 #10
0
def main():
    os.makedirs(BASE_DIR, exist_ok=True)
    config = JsonDict(os.path.join(BASE_DIR, SNAKE_NAME + "_config.json"))

    logging_fmt = (
        "%(asctime)s %(filename)s %(lineno)d %(name)s %(levelname)-8s  %(message)s"
    )
    logging.basicConfig(
        level=config.get(
            "basic",
            "logging",
            "level",
            default=logging.DEBUG if DEBUGGING else logging.INFO,
        ),
        format=logging_fmt,
        datefmt="(%H:%M:%S)",
    )

    rotating_handler = RotatingFileHandler(
        os.path.join(BASE_DIR, "log.log"),
        maxBytes=config.get("basic", "logging", "max_bytes", default=2 ** 19),
        backupCount=config.get("basic", "logging", "backup_count", default=10),
    )
    rotating_handler.setFormatter(logging.Formatter(logging_fmt))
    logging.getLogger("").addHandler(rotating_handler)

    logger = logging.getLogger(BASENAME)

    coloredlogs.install(level="DEBUG", fmt=logging_fmt)

    logger.info("Use basedir: " + os.path.abspath(BASE_DIR))

    #    board_collection.

    # plugin to django
    plug_in_django_manage.plug_in(DjangoArduinoControllerConfig, config)

    plug_in_django_manage.CONFIG.put("django_settings", "apps", "channels", value=True)

    # set site parameters
    plug_in_django_manage.CONFIG.put("public", "site", "title", value=BASENAME)
    plug_in_django_manage.CONFIG.put("django_settings", "DEBUG", value=DEBUGGING)
    plug_in_django_manage.CONFIG.put("django_settings", "BASE_DIR", value=BASE_DIR)

    # login required
    plug_in_django_manage.CONFIG.put(
        "django_settings",
        "manual",
        "add_to_list",
        "MIDDLEWARE",
        value=[
            #     "global_login_required.GlobalLoginRequiredMiddleware"
        ],
    )
    # if login is required accounds neet to be public
    plug_in_django_manage.CONFIG.put(
        "django_settings",
        "manual",
        "add_to_list",
        "PUBLIC_PATHS",
        value=[r"^/accounts/.*"],
    )

    plug_in_django_manage.run(
        sys.argv[0],
        "runserver",
        "--noreload",
        "0.0.0.0:"
        + str(
            plug_in_django_manage.CONFIG.get("django_settings", "port", default=8000)
        ),
    )
コード例 #11
0
ファイル: settings.py プロジェクト: JulianKimmig/chemDB
import datetime
from django.utils.timezone import make_aware
from json_dict import JsonDict
import random

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!

if os.path.exists("/django_settings.json"):
    config = JsonDict("/django_settings.json")
else:
    config = JsonDict("django_settings.json")

SECRET_KEY = config.get(
    'base_settings',
    'SECRET_KEY',
    default="".join([
        random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)")
        for i in range(50)
    ]))
#'l5!r&kcwz5r+#%**eq8j*6p-6@nw&0h)1uf&9c^27sw)9_s^-='

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config.get('base_settings', 'DEBUG', default=False)
コード例 #12
0
def main():
    # main data dir
    os.makedirs(BASE_DIR, exist_ok=True)

    config = JsonDict(os.path.join(BASE_DIR, SNAKE_NAME + "_config.json"))

    # create basic logger
    logging_fmt = "%(asctime)s %(filename)s %(lineno)d %(name)s %(levelname)-8s  %(message)s"
    logging.basicConfig(
        level=config.get("basic", "logging", "level", default=logging.DEBUG),
        format=logging_fmt,
        datefmt="(%H:%M:%S)",
    )

    rotating_handler = RotatingFileHandler(os.path.join(BASE_DIR, 'log.log'),
                                           maxBytes=config.get("basic",
                                                               "logging",
                                                               "max_bytes",
                                                               default=2**19),
                                           backupCount=config.get(
                                               "basic",
                                               "logging",
                                               "backup_count",
                                               default=10))
    rotating_handler.setFormatter(logging.Formatter(logging_fmt))
    logging.getLogger('').addHandler(rotating_handler)
    # logger.addHandler(logging.StreamHandler())

    logger = logging.getLogger(BASENAME)

    coloredlogs.install(level='DEBUG', fmt=logging_fmt)

    logger.info("Use basedir: " + os.path.abspath(BASE_DIR))

    # set server logger.
    from multi_purpose_arduino_controller.controll_server import manage as controll_server_manage
    # controll_server_manage.logger = logger

    # set_server_config
    controll_server_manage.CONFIG = config.getsubdict(
        preamble=['controll_server'])

    controll_server_manage.CONFIG.put("public",
                                      "site",
                                      "title",
                                      value=BASENAME)

    if config.get("basic", "mode", default=DEFAULT_MODE) == DEVELOPMENT_MODE:
        controll_server_manage.CONFIG.put("django_settings",
                                          "debug",
                                          value=True)
    else:
        controll_server_manage.CONFIG.put("django_settings",
                                          "debug",
                                          value=False)

    apps = [
        "custom_accounts_django",
        "multi_purpose_arduino_controller.controll_server.board_controller",
        "django_websocket_server", "column_switcher.django_app"
    ]
    # apps.append("serverapps.board_creator")
    controll_server_manage.CONFIG.put("django_settings",
                                      "apps",
                                      "additional",
                                      value=apps)

    # create pending migrations if in development mode
    if config.get("basic", "mode", default=DEFAULT_MODE) == DEVELOPMENT_MODE:
        controll_server_manage.run(sys.argv[0], "makemigrations")
        controll_server_manage.run(sys.argv[0], "migrate")

        from django.contrib.auth.models import User
        superusers = User.objects.filter(is_superuser=True)
        if len(superusers) == 0:
            print("No Superuser specified")
            unsername = input("Enter Username: "******"Mail: ")
            pass1 = input("Password: "******"django_settings",
        "static_files",
        "dirs",
        value=[
            os.path.abspath(
                os.path.join(os.path.dirname(__file__), "apis", "static")),
            # socketserver_instance.get_www_data_path(),
            # socketserver.TEMPDIR
        ],
    )

    python_communicator = PythonCommunicator()

    # websocket server
    socketserver_instance = websocket_server.connect_to_first_free_port(
        data_dir=BASE_DIR,
        logger=logging.getLogger("websocket_server"),
        start_in_background=True,
        startport=config.get("websocket", "startport", default=9001),
        disable_encryption=config.get("websocket",
                                      "security",
                                      "disable_encryption",
                                      default=False))

    # parse for boards
    parse_path_for_boards(os.path.join(os.path.dirname(__file__), "boards"))

    board_controller_api = BoardControllerAPI(
        python_communicator=python_communicator,
        websocket_server=socketserver_instance,
        data_dir=os.path.join(BASE_DIR, "BoardControllerAPI"))

    column_switcher_api = ColumnSwitcherAPI(
        python_communicator=python_communicator,
        websocket_server=socketserver_instance,
        data_dir=os.path.join(BASE_DIR, "ColumnSwitcherAPI"))

    # SerialReader
    sr = serialreader.SerialReader(communicator=python_communicator,
                                   start_in_background=True,
                                   config=config.getsubdict(["portdata"]))

    # starts django
    controll_server_manage.run(
        sys.argv[0], "runserver", "--noreload", "0.0.0.0:" + str(
            config.get(
                "controll_server", "django_settings", "port", default=8000)))
コード例 #13
0
 def test_double_open_file(self):
     d2 = JsonDict(self.d.file, backup=False)
     d2.put("second_entry", value=1)
     assert len(d2.data.keys()) == len(self.d.data.keys())
コード例 #14
0
import logging
import os
import threading
import time

from arduino_controller import serialreader
from arduino_controller.parseboards import parse_path_for_boards, BOARDS
from arduinocontrollserver.arduinocontrollserver import ArduinoControllServer
from json_dict import JsonDict
from websocket_communication_server.socketserver import connect_to_first_free_port

from column_automation import ColumnAutomate

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    serialportconfig = JsonDict("portdata.json")
    serialportconfig.autosave = True

    socketserver = connect_to_first_free_port()
    threading.Thread(target=socketserver.run_forever).start(
    )  # runs server forever in background

    server = ArduinoControllServer(
        port=80,
        socketport=socketserver.port,
        www_data=os.path.join(os.path.dirname(__file__), "www-data"),
    )
    threading.Thread(
        target=server.start).start()  # runs server forever in background

    server.deploy(socketserver.get_www_data_path(), "websocketserver")
コード例 #15
0
 def __init__(self, port=None, auto_port=True, interpreter=None, **kwargs):
     SerialCommunicator.__init__(
         self, port=port, auto_port=auto_port, interpreter=interpreter, **kwargs
     )
     if not hasattr(self, "config"):
         self.config = JsonDict()
コード例 #16
0
from django.conf import settings
from json_dict import JsonDict

preamble = ""
#if manage.py is called directly
if len(__name__.split(".")) == 2:
    from manage import logger, CONFIG
else:
    from ..manage import logger, CONFIG
    preamble = __name__.replace(".controll_server.settings", ".")

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

if CONFIG is None:
    CONFIG = JsonDict(os.path.join(BASE_DIR, "serverconfig.json"))

SECRET_KEY = CONFIG.get(
    "django_settings",
    "security",
    "key",
    default="".join(random.SystemRandom().choice(
        "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)")
                    for i in range(50)),
)

DEBUG = CONFIG.get("django_settings", "debug", default=False)

ALLOWED_HOSTS = CONFIG.get("django_settings",
                           "security",
                           "allowed_hosts",
コード例 #17
0
 def db(self, tablename):
     """Return a JsonDict for a specific 'tablename' i.e. a single json file."""
     assert tablename in DB_NAMES
     return JsonDict(__path__=self._db_json_path(tablename))
コード例 #18
0
    def __init__(self):
        self.to_migrate = False
        if not os.path.exists(self.BASE_DIR):
            self.to_migrate = True
        os.makedirs(self.BASE_DIR, exist_ok=True)
        self.config = JsonDict(
            os.path.join(self.BASE_DIR, self.SNAKE_NAME + "_config.json"))

        logging.basicConfig(
            level=self.config.get(
                "basic",
                "logging",
                "level",
                default=logging.DEBUG if self.DEBUGGING else logging.INFO,
            ),
            format=self.logging_fmt,
            datefmt="(%H:%M:%S)",
        )

        rotating_handler = RotatingFileHandler(
            os.path.join(self.BASE_DIR, "log.log"),
            maxBytes=self.config.get("basic",
                                     "logging",
                                     "max_bytes",
                                     default=2**19),
            backupCount=self.config.get("basic",
                                        "logging",
                                        "backup_count",
                                        default=10),
        )

        rotating_handler.setFormatter(logging.Formatter(self.logging_fmt))
        logging.getLogger("").addHandler(rotating_handler)

        logger = logging.getLogger(self.BASENAME)

        coloredlogs.install(level="DEBUG", fmt=self.logging_fmt)

        logger.info("Use basedir: " + os.path.abspath(self.BASE_DIR))

        # plugin to django
        plug_in_django_manage.plug_in(DjangoArduinoControllerConfig,
                                      self.config)
        plug_in_django_manage.CONFIG.put("django_settings",
                                         "apps",
                                         "channels",
                                         value=True)

        # set site parameters
        plug_in_django_manage.CONFIG.put("public",
                                         "site",
                                         "title",
                                         value=self.BASENAME)
        plug_in_django_manage.CONFIG.put("django_settings",
                                         "DEBUG",
                                         value=self.DEBUGGING)
        plug_in_django_manage.CONFIG.put("django_settings",
                                         "BASE_DIR",
                                         value=self.BASE_DIR)

        if self.login_required:
            # login required
            plug_in_django_manage.CONFIG.put(
                "django_settings",
                "manual",
                "add_to_list",
                "MIDDLEWARE",
                value=[
                    #     "global_login_required.GlobalLoginRequiredMiddleware"
                ],
            )
            # if login is required accounds neet to be public
            plug_in_django_manage.CONFIG.put(
                "django_settings",
                "manual",
                "add_to_list",
                "PUBLIC_PATHS",
                value=[r"^/accounts/.*"],
            )

        for app_config in self.app_configs:
            plug_in_django_manage.plug_in(app_config, self.config)