コード例 #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
class USBDevice(SerialCommunicator):
    AVAILABLE_QUERIES = {}

    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()

    def set_device_property(self, name, value):
        self.config.put("device", "properties", name, value=value)

    def get_device_property(self, name):
        return self.config.get("device", "properties", name, autosave=False)

    def set_device_status(self, name, value):
        self.config.put("device", "status", name, value=value)

    def get_device_status(self, name):
        return self.config.get("device", "status", name, autosave=False)
コード例 #10
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")
コード例 #11
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())
コード例 #12
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)
コード例 #13
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)))
コード例 #14
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)
コード例 #15
0
class BaseApp:
    DEBUGGING = False
    BASENAME = "BaseApp"
    SNAKE_NAME = BASENAME.lower().replace(" ", "_")
    BASE_DIR = os.path.join(expanduser("~"), "." + SNAKE_NAME)
    app_configs = []
    logging_fmt = (
        "%(asctime)s %(filename)s %(lineno)d %(name)s %(levelname)-8s  %(message)s"
    )

    login_required = True

    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)

    def migrate(self):
        plug_in_django_manage.run(sys.argv[0], "makemigrations")
        plug_in_django_manage.run(sys.argv[0], "migrate")

    def run(self, open_browser=False, open_data_dir=False):
        self.migrate()
        if self.to_migrate:
            self.migrate()
        if open_browser:

            def check_thread():
                import urllib.request

                while (not urllib.request.urlopen("http://localhost:{}".format(
                        plug_in_django_manage.CONFIG.get(
                            "django_settings", "port",
                            default=8000))).getcode() == 200):
                    time.sleep(200)
                import webbrowser

                webbrowser.open(
                    "http://localhost:{}".format(
                        plug_in_django_manage.CONFIG.get("django_settings",
                                                         "port",
                                                         default=8000)),
                    new=2,
                )

            import threading

            threading.Thread(target=check_thread).start()
        if open_data_dir:
            import webbrowser

            if sys.platform == "darwin":
                webbrowser.open(self.BASE_DIR)
            elif sys.platform == "linux2":
                webbrowser.open(self.BASE_DIR)
            elif sys.platform == "win32":
                webbrowser.open(self.BASE_DIR)
            else:
                webbrowser.open(self.BASE_DIR)
        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)),
        )
コード例 #16
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()
コード例 #17
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",
コード例 #18
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)
        ),
    )
コード例 #19
0
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import random
from json_dict import JsonDict
from shutil import copyfile

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

try:
    if not os.path.exists(os.path.join(BASE_DIR, "config.json")):
        copyfile(os.path.join(BASE_DIR, "config_basic.json"),
                 os.path.join(BASE_DIR, "config.json"))
except:
    pass

JCONFIG = JsonDict(os.path.join(BASE_DIR, "config.json"))

# 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!
SECRET_KEY = JCONFIG.get(
    "settings",
    "server",
    "secret_key",
    default=''.join(random.SystemRandom().choice(
        'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
                    for i in range(50)))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = JCONFIG.get("settings", "server", "debug", default=True)
コード例 #20
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:
コード例 #21
0
class SerialReader:
    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()

    def reactivate_port(self, port=None):
        if port is None:
            return
        try:
            self.ignored_ports.remove(port)
        except:
            pass

        try:
            self.deadports.remove(port)
        except:
            pass

    def deactivate_port(self, port=None):
        if port is None:
            return
        self.ignored_ports.add(port)
        self._communicator.cmd_out(targets=[port], cmd="stop_read")

    def run_in_background(self):
        threading.Thread(target=self.read_forever).start()

    def set_communicator(self,
                         communicator: python_communicator.PythonCommunicator):
        self._communicator = communicator
        self._communicator.add_node("serialreader", self)

    def get_communicator(self):
        return self._communicator

    communicator = property(get_communicator, set_communicator)

    def sendports(self, data_target=None):
        try:
            self._communicator.cmd_out(
                cmd="set_ports",
                sender="serialreader",
                targets=(None if data_target is None else
                         (data_target if isinstance(data_target, list) else
                          [data_target])),
                available_ports=list(self.available_ports),
                ignored_ports=list(self.ignored_ports
                                   | self.permanently_ignored_ports),
                connected_ports=[sp.port for sp in self.connected_ports],
                identified_ports=[sp.port for sp in self.identified_ports],
            )
        except python_communicator.TargetNotFoundException:
            pass

    get_ports = sendports

    def read_forever(self):
        while 1:
            if self.autocheckports:
                self.available_ports, self.ignored_ports = serialdetector.get_avalable_serial_ports(
                    ignore=self.ignored_ports | self.permanently_ignored_ports
                    | set([sp.port for sp in self.connected_ports]))
                self.deadports = self.available_ports.intersection(
                    self.deadports)
                newports = self.available_ports - (
                    self.ignored_ports | self.deadports
                    | self.permanently_ignored_ports)
                self.logger.debug(
                    "available Ports: " + str(self.available_ports) +
                    "; new Ports: " + str(newports) + "; ignored Ports: " +
                    str(self.ignored_ports | self.permanently_ignored_ports) +
                    "; ignored Ports: " +
                    str([sp.port for sp in self.connected_ports]) +
                    "; ignored Ports: " +
                    str([sp.port for sp in self.identified_ports]), )

                self.sendports()
                for port in newports.copy():
                    try:
                        self.open_port(port)
                    except Exception as e:
                        self.logger.exception(e)
                        pass
            time.sleep(self.port_check_time)

    def open_port(self, port):
        try:
            self.available_ports.remove(port)
        except:
            pass
        try:
            self.ignored_ports.remove(port)
        except:
            pass
        try:
            self.permanently_ignored_ports.remove(port)
        except:
            pass

        from ..serialport import SerialPort
        t = threading.Thread(
            target=SerialPort,
            kwargs={
                **{
                    "communicator":
                    self._communicator,
                    "serialreader":
                    self,
                    "config":
                    self.config,
                    "port":
                    port,
                    "baudrate":
                    self.config.get("portdata", port, "baud", default=9600),
                },
                # **kwargs,
            },
        )
        t.start()
コード例 #22
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))
コード例 #23
0
class DictTest(unittest.TestCase):
    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)

    def tearDown(self):
        os.remove(self.temp_file)
        os.rmdir(self.temp_dir)

    def test_create_dict(self):
        assert list(self.d.data.keys()) == ["test"]
        assert self.d.get("test") == "file"

    def test_result_is_dict_to_json_dict(self):
        self.d.put("test", value={})
        assert isinstance(self.d.get("test"), JsonSubDict)

    def test_create_subdict(self):
        print("A")
        print(self.d._file_size)
        sub1 = self.d.getsubdict("testsub", "dict")
        print("B")
        sub2 = self.d.getsubdict(["testsub", "dict"])
        assert len(self.d.data.keys()) == 2
        assert len(self.d["testsub"].keys()) == 1

    def test_data_is_dict(self):
        sd = self.d.getsubdict("testsub", "dict")
        assert isinstance(self.d.data, dict)
        assert isinstance(sd.data, dict), (type(sd.data), sd.data)

    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())

    def test_list(self):
        self.d.put("list", value=[0, 1, 2])
        l: json_dict.JsonList = self.d.get("list")
        l[1] = 10
        l.reverse()
        assert all([l[i] == k for i, k in enumerate([2, 10, 0])]), str(l)
        assert l.pop(-1) == 0
        assert all([l[i] == k for i, k in enumerate([2, 10])]), str(l)

    def test_time(self):
        t = time.time()
        json_dict.VERBOSE = False
        d = 1000
        for i in range(d):
            self.d.put("second_entry",
                       value=self.d.get("second_entry", default=0) + 1)
        print(time.time() - t)
        assert self.d.get("second_entry", default=1) == d
        json_dict.VERBOSE = True

    def test_empty_key(self):
        dic = {
            "data": {
                "": {
                    "inempty": ["test"],
                }
            }
        }
        self.d.data = dic
        ds = JsonDict(self.d.file)
        print(ds)