Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions synapse/utils/proto.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from google.protobuf.json_format import Parse
from google.protobuf.json_format import Parse, ParseError
from synapse.api.device_pb2 import DeviceConfiguration
from synapse.api.app_pb2 import AppManifest
import synapse as syn
Expand All @@ -8,20 +8,35 @@ def load_device_config(path_to_json, console):
# We support either a manifest or a device configuration.
# First, try to load a device configuration
try:
json_text = open(path_to_json, "r").read()
with open(path_to_json, "r") as f:
json_text = f.read()
except FileNotFoundError:
raise ValueError(f"File not found: {path_to_json}")
except PermissionError:
raise ValueError(f"Permission denied reading: {path_to_json}")
except Exception as e:
raise ValueError(f"Failed to read {path_to_json}: {e}")

errors = []
try:
cfg_proto = Parse(json_text, DeviceConfiguration())
return syn.Config.from_proto(cfg_proto)
except Exception:
pass
except ParseError as e:
errors.append(f"DeviceConfiguration: {str(e)}")
except Exception as e:
errors.append(f"DeviceConfiguration: {type(e).__name__}: {str(e)}")

# We couldn't load a device configuration, so try to load a manifest
try:
json_text = open(path_to_json, "r").read()
manifest_proto = Parse(json_text, AppManifest())
return syn.Config.from_proto(manifest_proto.device_config)
except ParseError as e:
errors.append(f"AppManifest: {str(e)}")
except Exception:
raise ValueError(
f"Could not parse {path_to_json} as either device configuration or manifest"
)
errors.append(f"AppManifest: {type(e).__name__}: {str(e)}")

return None
# Only reached here when we've failed to parse
error_msg = f"Could not parse {path_to_json} as either format:\n"
error_msg += "\n".join(f" - {error}" for error in errors)
raise ValueError(error_msg)