Skip to content

Commit

Permalink
remove external dependencies to logger package
Browse files Browse the repository at this point in the history
  • Loading branch information
brainelectronics committed May 14, 2023
1 parent b4454be commit ec4c79d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 31 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exclude =
libs_external
sdist_upip.py
setup.py
nextion/ulogging.py

# Provide a comma-separated list of glob patterns to add to the list of excluded ones.
# extend-exclude =
Expand Down
24 changes: 0 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,6 @@ cp examples/basic/main.py /pyboard
cp examples/boot.py /pyboard
```

### Install additional MicroPython packages

To use this package with the provided [`boot.py`](examples/boot.py) and one of
the `main.py` files of an [example subfolder](examples/), the additional
module `ulogging` is required.

Either install the required package(s) using `upip` as follows after
connecting to a WiFi network:

```python
# network connection already established

import upip
upip.install('micropython-ulogging')
```

or copy it manually to the MicroPython board using e.g. `rshell`:

```bash
mkdir /pyboard/lib

cp -r libs_external/* /pyboard/lib
```

## Usage

Use one of the [examples](examples/) to get started. Read also the
Expand Down
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$"
-->

## Released
## [0.15.1] - 2023-05-13
## [0.15.1] - 2023-05-14
### Fixed
- Remove yet unavailable files from `package.json`, see #33
- Add `ulogging` module to package to remove all external dependencies

## [0.15.0] - 2023-05-13
### Added
Expand Down
2 changes: 1 addition & 1 deletion nextion/nextion_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
# system packages
from machine import UART
from time import ticks_diff, ticks_ms
import ulogging as logging

# custom packages
from .typing import Optional
from . import const as Const
from . import ulogging as logging


class NexHardwareError(Exception):
Expand Down
94 changes: 94 additions & 0 deletions nextion/ulogging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import sys

CRITICAL = 50
ERROR = 40
WARNING = 30
INFO = 20
DEBUG = 10
NOTSET = 0

_level_dict = {
CRITICAL: "CRIT",
ERROR: "ERROR",
WARNING: "WARN",
INFO: "INFO",
DEBUG: "DEBUG",
}

_stream = sys.stderr

class Logger:

level = NOTSET

def __init__(self, name):
self.name = name

def _level_str(self, level):
l = _level_dict.get(level)
if l is not None:
return l
return "LVL%s" % level

def setLevel(self, level):
self.level = level

def isEnabledFor(self, level):
return level >= (self.level or _level)

def log(self, level, msg, *args):
if level >= (self.level or _level):
_stream.write("%s:%s:" % (self._level_str(level), self.name))
if not args:
print(msg, file=_stream)
else:
print(msg % args, file=_stream)

def debug(self, msg, *args):
self.log(DEBUG, msg, *args)

def info(self, msg, *args):
self.log(INFO, msg, *args)

def warning(self, msg, *args):
self.log(WARNING, msg, *args)

def error(self, msg, *args):
self.log(ERROR, msg, *args)

def critical(self, msg, *args):
self.log(CRITICAL, msg, *args)

def exc(self, e, msg, *args):
self.log(ERROR, msg, *args)
sys.print_exception(e, _stream)

def exception(self, msg, *args):
self.exc(sys.exc_info()[1], msg, *args)


_level = INFO
_loggers = {}

def getLogger(name):
if name in _loggers:
return _loggers[name]
l = Logger(name)
_loggers[name] = l
return l

def info(msg, *args):
getLogger(None).info(msg, *args)

def debug(msg, *args):
getLogger(None).debug(msg, *args)

def basicConfig(level=INFO, filename=None, stream=None, format=None):
global _level, _stream
_level = level
if stream:
_stream = stream
if filename is not None:
print("logging.basicConfig: filename arg is not supported")
if format is not None:
print("logging.basicConfig: format arg is not supported")
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@
"nextion/typing.py",
"github:brainelectronics/micropython-nextion/nextion/typing.py"
],
[
"nextion/ulogging.py",
"github:brainelectronics/micropython-nextion/nextion/ulogging.py"
],
[
"nextion/version.py",
"github:brainelectronics/micropython-nextion/nextion/version.py"
]
],
"deps": [
"micropython-ulogging"
],
"version": "0.15.0"
"deps": [],
"version": "0.15.1"
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
license='MIT',
cmdclass={'sdist': sdist_upip.sdist},
packages=['nextion'],
install_requires=['micropython-ulogging']
install_requires=[]
)

0 comments on commit ec4c79d

Please sign in to comment.