Skip to content

Commit

Permalink
Merge pull request #114 from pyonair/dev-latest
Browse files Browse the repository at this point in the history
Dev latest
  • Loading branch information
ossonts authored Apr 28, 2022
2 parents af3e610 + 1c72592 commit 9b8cd7f
Show file tree
Hide file tree
Showing 26 changed files with 503 additions and 301 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Autoformatter friendly flake8 config (all formatting rules disabled)
[flake8]
extend-ignore = E1, E2, E3, E501, W1, W2, W3, W5
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ debug_config.json

# Byte-compiled / optimized / DLL files
__pycache__/

.vscode
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile=black
10 changes: 10 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Autoformatter friendly markdownlint config (all formatting rules disabled)
default: true
blank_lines: false
bullet: false
html: false
indentation: false
line_length: false
spaces: false
url: false
whitespace: false
1 change: 1 addition & 0 deletions .trunk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*out
12 changes: 12 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 0.1
cli:
version: 0.10.1-beta
lint:
enabled:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.analysis.extraPaths": ["./code/lib"]
}
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

## Branches

* main : prodcution
* dev-latest -- branch for all finalised features.
- main : prodcution
- dev-latest -- branch for all finalised features.

## Adding code

1. Branch `dev-latest`
2. Name you branch `feature-*` `bug-*` `performance-*` `refactor-*` `doc-*`
3. Edit your new branch
4. Pull request back to `dev-latest` when completed
5. Expect a code review


2. Name you branch `feature-*` `bug-*` `performance-*` `refactor-*`
3. # Edit your new branch
4. Name you branch `feature-*` `bug-*` `performance-*` `refactor-*` `doc-*`
5. Edit your new branch
6. Pull request back to `dev-latest` when completed
7. Expect 2 code review(s)

[https://www.pyonair.org](https://www.pyonair.org)

103 changes: 61 additions & 42 deletions code/lib/Configuration.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
import strings as s
#from ubinascii import hexlify
#from machine import unique_id
#from network import LoRa
import os

import strings as s
import ujson
from Constants import *

#TODO: memory hog, sort this class - static?
# from ubinascii import hexlify
# from machine import unique_id
# from network import LoRa
from Constants import (
CONFIG_FILE_DIRECTORY,
CONFIG_FILE_FULL_NAME,
CONFIG_FILE_NAME,
DEBUG_CONFIG_FILE_DIRECTORY,
DEBUG_CONFIG_FILE_FULL_NAME,
DEBUG_CONFIG_FILE_NAME,
DEFAULT_CONFIG,
)

# import imp

# TODO: memory hog, sort this class - static?


class ConfigurationException(Exception):
"""
Exception to be thrown if Exception occurs in configuration
"""
#TODO: why?

# TODO: why?
pass


# Configuration
class Configuration:
def __init__(self, logger):
self.logger = logger
self.configuration = {}
# RM

def __init__(self,logger):
self.logger= logger
self.configuration = {}
#RM

self.read_configuration()
print("CONFIG READ########################################")

# Configuration Accessor/Getter
def get_config(self, keys=None):

"""
If keys are given, return corresponding values in a list, if one key is given, return the corresponding value,
if no arguments are given, return the config dictionary
:param keys: keys in configuration dictionary
:type keys: None, list of keys or a single key
:return: values in configuration dictionary
:rtype: dict, list of any, any
"""
#TODO: Warn if key not in dict
# TODO: Warn if key not in dict
if keys is None:
return self.configuration
if isinstance(keys, list):
return list(self.configuration[k] for k in keys if k in self.configuration)
#TODO remove this multi option
# TODO remove this multi option
else:
return self.configuration[keys]

Expand All @@ -52,7 +68,7 @@ def set_config(self, new_config):
:param new_config: set of new configuration key-value pairs
:type new_config: dict
"""

self.configuration.update(new_config)

# Saves keys and preferences to sd card
Expand All @@ -64,42 +80,45 @@ def save_config(self, new_config):
"""
self.set_config(new_config)

with open(s.root_path + CONFIG_FILE_NAME, 'w') as f: # save credentials to sd card
with open(
s.root_path + CONFIG_FILE_NAME, "w"
) as f: # save credentials to sd card

f.write(ujson.dumps(self.configuration))

# Reads and returns keys and preferences from sd card
def read_configuration(self):
"""
Read config file on SD card and load it to the configuration dict
"""
#set to default
# set to default
self.configuration = DEFAULT_CONFIG


#if no config file
# if no config file
if CONFIG_FILE_NAME not in os.listdir(CONFIG_FILE_DIRECTORY):
with open(CONFIG_FILE_FULL_NAME, 'w') as f: # create new config file
with open(CONFIG_FILE_FULL_NAME, "w") as f: # create new config file
f.write(ujson.dumps(self.configuration))
#self.set_config(str(self.configuration))
# self.set_config(str(self.configuration))

#load config
# load config
if CONFIG_FILE_NAME in os.listdir(CONFIG_FILE_DIRECTORY):
with open(CONFIG_FILE_FULL_NAME, 'r') as f:
with open(CONFIG_FILE_FULL_NAME, "r") as f:
self.set_config(ujson.loads(f.read()))
self.logger.debug(str(self.configuration))

# check for debug force overwrite.

#check for debug force overwrite.


#Override Preferences - DEVELOPER USE ONLY - keep all overwrites here
# Override Preferences - DEVELOPER USE ONLY - keep all overwrites here
if DEBUG_CONFIG_FILE_NAME in os.listdir(DEBUG_CONFIG_FILE_DIRECTORY):
self.logger.warning("Overriding configuration with the content of debug_config.json")

with open(DEBUG_CONFIG_FILE_FULL_NAME, 'r') as f:
self.set_config(ujson.loads(f.read()))
self.logger.warning("Configuration changed to: " + str(self.configuration))
self.logger.warning(
"Overriding configuration with the content of debug_config.json"
)

with open(DEBUG_CONFIG_FILE_FULL_NAME, "r") as f:
self.set_config(ujson.loads(f.read()))
self.logger.warning(
"Configuration changed to: " + str(self.configuration)
)

# Returns True if the configuration file is complete
def is_complete(self, logger):
Expand Down Expand Up @@ -132,25 +151,25 @@ def reset_configuration(self, logger):
:type logger: LoggerFactory object
"""
try:
self.logger.warning("This wont work ") #TODO: this method needs sorting
self.logger.warning("This wont work ") # TODO: this method needs sorting
self.configuration.clear() # clear configuration
self.set_config(DEFAULT_CONFIG) # set configuration to default

#disable Lora configs -- use pybytes
#self.set_config({"device_id": hexlify(unique_id()).upper().decode("utf-8")}) # set new device_id
# disable Lora configs -- use pybytes
# self.set_config({"device_id": hexlify(unique_id()).upper().decode("utf-8")}) # set new device_id

#lora = LoRa(mode=LoRa.LORAWAN)
#self.set_config({"device_eui": hexlify(lora.mac()).upper().decode('utf-8')}) # set new device_EUI
#del lora
# lora = LoRa(mode=LoRa.LORAWAN)
# self.set_config({"device_eui": hexlify(lora.mac()).upper().decode('utf-8')}) # set new device_EUI
# del lora

logger.info('Configurations were reset')
logger.warning('Please configure your device!')
logger.info("Configurations were reset")
logger.warning("Please configure your device!")
except Exception as e:
logger.exception('Failed to reset configurations')
logger.exception("Failed to reset configurations")
raise ConfigurationException(str(e))

def getConfigDictionary(self):
# TODO: that is a mess
# global configuration dictionary
#config = Configuration()
# config = Configuration()
return self.configuration
Loading

0 comments on commit 9b8cd7f

Please sign in to comment.