Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extended FSWTabDict class #377

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions ait/core/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
def expand_config_paths(
config, prefix=None, datetime=None, pathvars=None, parameter_key="", *keys
):
"""Updates all relative configuration paths in dictionary config,
"""
Updates all relative configuration paths in dictionary config,
which contain a key in keys, by prepending prefix.

If keys is omitted, it defaults to 'directory', 'file',
Expand Down Expand Up @@ -377,7 +378,6 @@ def reload(self, filename=None, data=None):
self._datetime,
merge(self._config, self._pathvars),
)

else:
self._config = {}

Expand Down
65 changes: 50 additions & 15 deletions ait/core/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

The ait.core.cmd module provides commands and command dictionaries.
Dictionaries contain command and argument definitions.

"""

import os
Expand All @@ -43,6 +44,7 @@ class ArgDefn(json.SlotSerializer, object):

A fixed argument (fixed=True) defines a fixed bit pattern in that
argument's byte position(s).

"""

__slots__ = [
Expand Down Expand Up @@ -104,8 +106,10 @@ def startbit(self):
return self.slice().start % 2 * 8

def decode(self, bytes):
"""Decodes the given bytes according to this AIT Argument
"""
Decodes the given bytes according to this AIT Argument
Definition.

"""
value = self.type.decode(bytes)
if self._enum is not None:
Expand All @@ -116,8 +120,10 @@ def decode(self, bytes):
return value

def encode(self, value):
"""Encodes the given value according to this AIT Argument
"""
Encodes the given value according to this AIT Argument
Definition.

"""
if not self.type:
return bytearray()
Expand All @@ -128,10 +134,12 @@ def encode(self, value):


def slice(self, offset=0):
"""Returns a Python slice object (e.g. for array indexing) indicating
"""
Returns a Python slice object (e.g. for array indexing) indicating
the start and stop byte position of this Command argument. The
start and stop positions may be translated by the optional byte
offset.

"""
if type(self.bytes) is int:
start = self.bytes
Expand All @@ -143,9 +151,11 @@ def slice(self, offset=0):
return slice(start + offset, stop + offset)

def validate(self, value, messages=None):
"""Returns True if the given Argument value is valid, False otherwise.
"""
Returns True if the given Argument value is valid, False otherwise.
Validation error messages are appended to an optional messages
array.

"""
valid = True
primitive = value
Expand Down Expand Up @@ -176,15 +186,19 @@ def log(msg):


class Cmd(object):
"""Cmd - Command
"""
Cmd - Command

Commands reference their Command Definition and may contain arguments.

"""

def __init__(self, defn, *args, **kwargs):
"""Creates a new AIT Command based on the given command
"""
Creates a new AIT Command based on the given command
definition and command arguments. A Command may be created
with either positional or keyword arguments, but not both.

"""
self.defn = defn

Expand Down Expand Up @@ -234,7 +248,8 @@ def argdefns(self):
return self.defn.argdefns

def encode(self, pad=106):
"""Encodes this AIT command to binary.
"""
Encodes this AIT command to binary.

If pad is specified, it indicates the maximum size of the encoded
command in bytes. If the encoded command is less than pad, the
Expand All @@ -244,6 +259,7 @@ def encode(self, pad=106):
(128 bytes) with 11 words (22 bytes) of CCSDS overhead (SSP
52050J, Section 3.2.3.4). This leaves 53 words (106 bytes) for
the command itself.

"""
try:
opcode = struct.pack(">H", self.defn.opcode)
Expand Down Expand Up @@ -272,20 +288,24 @@ def encode(self, pad=106):
return encoded

def validate(self, messages=None):
"""Returns True if the given Command is valid, False otherwise.
"""
Returns True if the given Command is valid, False otherwise.
Validation error messages are appended to an optional messages
array.

"""
return self.defn.validate(self, messages)


class CmdDefn(json.SlotSerializer, object):
"""CmdDefn - Command Definition
"""
mdDefn - Command Definition

Command Definitions encapsulate all information required to define a
single command. This includes the command name, its opcode,
subsystem, description and a list of argument definitions. Name and
opcode are required. All others are optional.

"""

__slots__ = ("name", "_opcode", "subsystem", "ccsds", "title", "desc", "argdefns")
Expand All @@ -308,27 +328,31 @@ def __repr__(self):
return util.toRepr(self)

@property
def args(self):
"""The argument definitions to this command (excludes fixed
def args (self):
"""
The argument definitions to this command (excludes fixed
arguments).
"""
return filter(lambda a: not a.fixed, self.argdefns)

@property
def nargs(self):
"""The number of arguments to this command (excludes fixed
"""
The number of arguments to this command (excludes fixed
arguments).
"""
return len(list(self.args))

@property
def nbytes(self):
"""The number of bytes required to encode this command.
"""
The number of bytes required to encode this command.

Encoded commands are comprised of a two byte opcode, followed by a
one byte size, and then the command argument bytes. The size
indicates the number of bytes required to represent command
arguments.

"""
return len(self.opcode) + 1 + sum(arg.nbytes for arg in self.argdefns)

Expand Down Expand Up @@ -449,8 +473,10 @@ def create(self, name, *args, **kwargs):

return createCmd(defn, *args, **kwargs) # noqa


def decode(self, bytes):
"""Decodes the given bytes according to this AIT Command
"""
Decodes the given bytes according to this AIT Command
Definition.
"""
opcode = struct.unpack(">H", bytes[0:2])[0]
Expand Down Expand Up @@ -499,7 +525,7 @@ def toJSON(self): # noqa
return {name: defn.toJSON() for name, defn in self.items()}


def getDefaultCmdDict(reload=False): # noqa
def getDefaultCmdDict(reload=False):
return getDefaultDict(reload=reload)


Expand Down Expand Up @@ -554,6 +580,15 @@ def YAMLCtor_CmdDefn(loader, node): # noqa
return createCmdDefn(**fields) # noqa


def YAMLCtor_include(loader, node):
# Get the path out of the yaml file
name = os.path.join(os.path.dirname(loader.name), node.value)
data = None
with open(name,'r') as f:
data = yaml.load(f)
return data


def YAMLCtor_include(loader, node): # noqa
# Get the path out of the yaml file
name = os.path.join(os.path.dirname(loader.name), node.value)
Expand Down
1 change: 0 additions & 1 deletion ait/core/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@

from ait.core import cmd, dmc, log


# PrimitiveTypes
#
# Lists PrimitiveType names. Passing these names to get() will return
Expand Down
14 changes: 10 additions & 4 deletions ait/core/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# laws and regulations. User has the responsibility to obtain export licenses,
# or other export authority as may be required before exporting such
# information to foreign countries or providing access to foreign persons.

import datetime
import hashlib
import io
Expand Down Expand Up @@ -205,6 +206,7 @@ class FSWTabDefn(object):
single column. This includes the column name, its opcode,
subsystem, description and a list of argument definitions. Name and
opcode are required. All others are optional.

"""

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -375,6 +377,7 @@ def _decode_table_row(self, in_stream, raw=False):
Raises:
ValueError: When an EOFError is encountered while decoding any column
but the first or if any column decode returns None.

"""

row = []
Expand Down Expand Up @@ -406,6 +409,7 @@ class FSWTabDict(dict):

Table Dictionaries provide a Python dictionary (i.e. hashtable)
interface mapping Tables names to Column Definitions.

"""

def __init__(self, *args, **kwargs):
Expand All @@ -429,7 +433,7 @@ def create(self, name, *args):
tab = None
defn = self.get(name, None)
if defn:
tab = FSWTab(defn, *args)
tab = createFSWTab(defn, *args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createFSWTab method not defined

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return tab

def load(self, filename):
Expand Down Expand Up @@ -463,7 +467,7 @@ def dirty(self):
def load(self):
if self.fswtabdict is None:
if self.dirty():
self.fswtabdict = FSWTabDict(self.filename)
self.fswtabdict = createFSWTabDict(self.filename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createFSWTabDict not defined

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

self.update()
else:
with open(self.pcklname, "rb") as stream:
Expand Down Expand Up @@ -499,14 +503,14 @@ def getDefaultDict(): # noqa: N802

def YAMLCtor_FSWColDefn(loader, node): # noqa: N802
fields = loader.construct_mapping(node, deep=True)
return FSWColDefn(**fields)
return createFSWColDefn(**fields)


def YAMLCtor_FSWTabDefn(loader, node): # noqa: N802
fields = loader.construct_mapping(node, deep=True)
fields["fswheaderdefns"] = fields.pop("header", None)
fields["coldefns"] = fields.pop("columns", None)
return FSWTabDefn(**fields)
return createFSWTabDefn(**fields)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createFSWTabDict not defined

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



def encode_to_file(tbl_type, in_path, out_path):
Expand Down Expand Up @@ -544,3 +548,5 @@ def decode_to_file(tbl_type, in_path, out_path):

yaml.add_constructor("!FSWTable", YAMLCtor_FSWTabDefn)
yaml.add_constructor("!FSWColumn", YAMLCtor_FSWColDefn)

util.__init_extensions__(__name__, globals())
Loading