Skip to content

Commit

Permalink
Fixes to hardware config editing/display (#2765)
Browse files Browse the repository at this point in the history
**If name not specified when loading config then 'save' raises exception**

For example, `Basic_Ota` sample doesn't have its `name` field set in `ota.hw`:

- Run `make hwconfig-edit`
- Click 'save'

So ensure we provide a default name based on filename.


**Ensure all sample .hw files have names**

For consistency.


**Remove 'unused_before' and 'unused_after' fields from `make hwconfig` output**

These are only used for map building so shouldn't be visible.
Example:

```
        "phy_init": {
            "device": "spiFlash",
            "address": "0x000fc000",
            "size": "4K",
            "type": "data",
            "subtype": "phy",
            "readonly": false,
            "encrypted": false,
            "filename": "$(FLASH_INIT_DATA)",
            "unused_before": 0, ///<
            "unused_after": 0   ///< Don't show
        },
```


**Don't set name, comment fields from option data**

For example, `Basic_Blink` sample uses default config. Running `make hwconfig` gives:

```
{
    "name": "Standard config with single ROM",
    "comment": "Should work with any Esp8266 variant",
    "arch": "Esp8266",
    "options": [],
```

If we run `make hwconfig HWCONFIG_OPTS=4m` we get this:

```
{
    "name": "",
    "comment": "",
    "arch": "Esp8266",
    "options": [
        "4m"
    ],
```

The `name` and `comment` have been wiped out, should be as before.


**Fix use of `not in` and `is not` operators**

e.g. `x not in [...]` preferred to `not x in [...]`
  • Loading branch information
mikee47 committed Apr 15, 2024
1 parent bb5074a commit cf1e532
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Sming/Components/Storage/Tools/hwconfig/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def json_save(data, filename):
def to_json(obj):
return json.dumps(obj, indent=4)

def get_basename_no_ext(filename):
basename = os.path.basename(filename)
return os.path.splitext(basename)[0]

def lookup_keyword(t, keywords):
for k, v in keywords.items():
Expand Down
9 changes: 5 additions & 4 deletions Sming/Components/Storage/Tools/hwconfig/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def __init__(self):
self.options = []
self.option_library = load_option_library()
self.base_config = None
self.name = ''
self.comment = ''

def __str__(self):
return "'%s' for %s" % (self.name, self.arch)
Expand Down Expand Up @@ -119,6 +121,8 @@ def load(self, name):
self.depends.append(filename)
data = json_load(filename)
self.parse_dict(data)
self.name = data.get('name', '')
self.comment = data.get('comment', '')

def parse_options(self, options):
"""Apply any specified options.
Expand Down Expand Up @@ -164,10 +168,7 @@ def parse_dict(self, data):
elif k != 'name' and k != 'comment':
raise InputError("Unknown config key '%s'" % k)

self.name = data.get('name', '')
self.comment = data.get('comment', '')

if not partitions is None:
if partitions:
self.partitions.parse_dict(partitions, self.devices)

def dict(self):
Expand Down
23 changes: 16 additions & 7 deletions Sming/Components/Storage/Tools/hwconfig/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def read_property(obj, name):

def get_dict_value(dict, key, default):
"""Read dictionary value, creating one if it doesn't exist."""
if not key in dict:
if key not in dict:
dict[key] = default
return dict[key]

Expand Down Expand Up @@ -370,7 +370,7 @@ def init(self):
self.array = {} # dictionary for array element variables
self.row = 0
keys = self.schema['properties'].keys()
if not 'name' in keys:
if 'name' not in keys:
self.addControl('name')
for k in keys:
self.addControl(k)
Expand Down Expand Up @@ -1150,16 +1150,23 @@ def fileOpen():
self.loadConfig(filename)

def fileSave():
filename = self.json['name']
filename = self._filename
if not filename:
filename = self.json.get('name')
if not filename:
filename = get_basename_no_ext(filename)
filename = filedialog.asksaveasfilename(
title='Save profile to file',
filetypes=hwFilter,
initialfile=filename,
initialdir=os.getcwd())
if len(filename) != 0 and checkProfilePath(filename):
if filename and checkProfilePath(filename):
ext = os.path.splitext(filename)[1]
if ext != HW_EXT:
filename += HW_EXT
if not self.json.get('name'):
self.json['name'] = get_basename_no_ext(filename)
self.reload()
json_save(self.json, filename)

# Toolbar
Expand Down Expand Up @@ -1268,10 +1275,11 @@ def loadConfig(self, filename):
self.json['base_config'] = config_name
else:
self.json = json_load(filename)
self._filename = os.path.basename(filename)

options = get_dict_value(self.json, 'options', [])
for opt in configVars.get('HWCONFIG_OPTS', '').replace(' ', '').split():
if not opt in options:
if opt not in options:
options.append(opt)

self.reload()
Expand All @@ -1280,7 +1288,7 @@ def loadConfig(self, filename):

def updateWindowTitle(self):
name = self.json.get('name', None)
if name is None:
if not name:
name = '(new)'
else:
name = '"' + name + '"'
Expand All @@ -1290,8 +1298,9 @@ def reset(self):
self.tree.clear()
self.map.clear()
self.status.set('')
self._filename = ''
self.json = OrderedDict()
self.json['name'] = 'New Profile'
self.json['name'] = ''
self.json['base_config'] = 'standard'
self.reload()
self.updateWindowTitle()
Expand Down
4 changes: 2 additions & 2 deletions Sming/Components/Storage/Tools/hwconfig/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def dict(self):
res[k] = stringnum(self.type_str())
elif k == 'subtype':
res[k] = stringnum(self.subtype_str())
elif v is not None and k != 'name':
elif v is not None and k not in ['name', 'unused_before', 'unused_after']:
res[k] = v
return res

Expand Down Expand Up @@ -583,7 +583,7 @@ def add_unused(table, device, address, last_end):
# Devices with no defined partitions
pdevs = set(p.device for p in partitions)
for dev in config.devices:
if not dev in pdevs:
if dev not in pdevs:
add_unused(partitions, dev, dev.size, -1)

partitions.sort()
Expand Down
1 change: 1 addition & 0 deletions samples/Basic_IFS/basic_ifs_Esp32.hw
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "Basic IFS sample (ESP32)",
"base_config": "basic_ifs",
"arch": "Esp32",
"partitions": {
Expand Down
1 change: 1 addition & 0 deletions samples/Basic_IFS/basic_ifs_Esp8266.hw
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "Basic IFS (ESP8266)",
"base_config": "basic_ifs",
"arch": "Esp8266",
"partitions": {
Expand Down
1 change: 1 addition & 0 deletions samples/Basic_IFS/basic_ifs_Host.hw
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "Basic IFS (HOST)",
"base_config": "basic_ifs",
"arch": "Host",
"partitions": {
Expand Down
4 changes: 2 additions & 2 deletions samples/Basic_IFS/basic_ifs_Rp2040.hw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Rp2040 config",
"arch": "Rp2040",
"name": "Basic IFS sample (RP2040)",
"base_config": "basic_ifs",
"arch": "Rp2040",
"options": [
"2m",
"cyw43_fw"
Expand Down
1 change: 1 addition & 0 deletions samples/Basic_Ota/ota.hw
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "Basic OTA sample",
"base_config": "spiffs-two-roms",
"partitions": {
"rom0": {
Expand Down
2 changes: 1 addition & 1 deletion samples/FtpServer_Files/ftpserver-esp32.hw
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "FTP Server sample",
"name": "FTP Server sample (ESP32)",
"base_config": "ftpserver",
"partitions": {
"factory": {
Expand Down
2 changes: 1 addition & 1 deletion samples/FtpServer_Files/ftpserver-esp8266.hw
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "FTP Server sample",
"name": "FTP Server sample (ESP8266)",
"base_config": "ftpserver",
"partitions": {
"rom0": {
Expand Down

0 comments on commit cf1e532

Please sign in to comment.