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
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 15 additions & 16 deletions ait/core/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@

PATH_KEYS = "directory", "file", "filename", "path", "pathname"

def expandConfigPaths (config, prefix=None, datetime=None, pathvars=None, parameter_key='', *keys):

def expand_config_paths(
config, prefix=None, datetime=None, pathvars=None, parameter_key="", *keys
):
"""
Updates all relative configuration paths in dictionary config,
which contain a key in keys, by prepending prefix.
Expand All @@ -48,7 +51,6 @@ def expandConfigPaths (config, prefix=None, datetime=None, pathvars=None, parame
'filename', 'path', 'pathname'.

See util.expandPath().

"""
if len(keys) == 0:
keys = PATH_KEYS
Expand Down Expand Up @@ -123,7 +125,7 @@ def replace_variables(path, datetime=None, pathvars=None):
raise TypeError(msg)

# get the list of possible variable values
value_list = v if type(v) is list else [ v ]
value_list = v if type(v) is list else [v]

# create temp_list for now
temp_list = []
Expand Down Expand Up @@ -202,6 +204,7 @@ class AitConfigError(Exception):

pass


class AitConfigMissing(Exception):
"""Raised when a AIT configuration parameter is missing."""

Expand All @@ -212,9 +215,8 @@ def __init__(self, param):
self.param = param


class AitConfig (object):
"""
AitConfig
class AitConfig(object):
"""AitConfig

A AitConfig object holds configuration parameters read from a
YAML configuration file. The YAML data structure has three levels
Expand All @@ -223,7 +225,6 @@ class AitConfig (object):

NOTE: The platform string is Python's sys.platform, i.e. 'linux2',
'darwin', 'win32'.

"""

_ROOT_DIR = os.path.abspath(os.environ.get("AIT_ROOT", os.getcwd()))
Expand Down Expand Up @@ -347,14 +348,13 @@ def _datapaths(self):

return paths

def reload (self, filename=None, data=None):
"""
Reloads the a AIT configuration.
def reload(self, filename=None, data=None):
"""Reloads the a AIT configuration.

The AIT configuration is automatically loaded when the AIT
package is first imported. To replace the configuration, call
reload() (defaults to the current config.filename) or
reload(new_filename).

"""
if data is None and filename is None:
filename = self._filename
Expand All @@ -378,11 +378,10 @@ def reload (self, filename=None, data=None):
self._datetime,
merge(self._config, self._pathvars),
)

else:
self._config = {}

def get (self, name, default=None):
def get(self, name, default=None):
"""Returns the attribute value *AitConfig.name* or *default*
if name does not exist.

Expand Down Expand Up @@ -410,9 +409,9 @@ def get (self, name, default=None):

return config[tail] if tail in config else default

def getDefaultFilename(self):
if 'AIT_CONFIG' in os.environ:
filename = os.path.abspath(os.environ.get('AIT_CONFIG'))
def get_default_filename(self):
if "AIT_CONFIG" in os.environ:
filename = os.path.abspath(os.environ.get("AIT_CONFIG"))
else:
msg = "AIT_CONFIG not set. Falling back to AIT_ROOT or CWD"
log.warn(msg)
Expand Down
33 changes: 25 additions & 8 deletions ait/core/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ def args (self):
"""
The argument definitions to this command (excludes fixed
arguments).

"""
return filter(lambda a: not a.fixed, self.argdefns)

Expand All @@ -337,7 +336,6 @@ def nargs(self):
"""
The number of arguments to this command (excludes fixed
arguments).

"""
return len(list(self.args))

Expand Down Expand Up @@ -469,8 +467,12 @@ def create(self, name, *args, **kwargs):
if defn is None:
raise TypeError("Unrecognized command: %s" % name)

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 @@ -566,18 +568,33 @@ def YAMLCtor_ArgDefn(loader, node): # noqa
return createArgDefn(**fields) # noqa


def YAMLCtor_CmdDefn(loader, node): # noqa
fields = loader.construct_mapping(node, deep=True)
fields["argdefns"] = fields.pop("arguments", None)
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, Loader=yaml.Loader)
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)
data = None
with open(name, "r") as f:
data = yaml.load(f)
return data
Copy link
Contributor

Choose a reason for hiding this comment

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

This overwrites previous definition of YAMLCtor_include. Is this done on purpose?



yaml.add_constructor('!include' , YAMLCtor_include)
yaml.add_constructor('!Command' , YAMLCtor_CmdDefn)
yaml.add_constructor('!Argument', YAMLCtor_ArgDefn)
yaml.add_constructor('!Fixed' , YAMLCtor_ArgDefn)
yaml.add_constructor("!include", YAMLCtor_include)
yaml.add_constructor("!Command", YAMLCtor_CmdDefn)
yaml.add_constructor("!Argument", YAMLCtor_ArgDefn)
yaml.add_constructor("!Fixed", YAMLCtor_ArgDefn)

util.__init_extensions__(__name__, globals())
3 changes: 2 additions & 1 deletion ait/core/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@
import sys
import re

from ait.core import cmd, dmc, log, util, table
from typing import Dict, Any

from ait.core import cmd, dmc, log

# PrimitiveTypes
#
Expand Down
13 changes: 6 additions & 7 deletions ait/core/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class FSWColDefn(object):

Argument Definitions encapsulate all information required to define
a single column.

"""

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -434,7 +433,7 @@ def create(self, name, *args):
tab = None
defn = self.get(name, None)
if defn:
tab = createFSWTab(defn, *args)
tab = FSWTab(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.

Sorry, my comment from last review was incorrect. The createFSWxxx method gets created in run-time during the extension process (i.e. util.__init_extensions__). So it is okay to call createFSWxxx method here. Please revert back to before this commit.

return tab

def load(self, filename):
Expand Down Expand Up @@ -468,7 +467,7 @@ def dirty(self):
def load(self):
if self.fswtabdict is None:
if self.dirty():
self.fswtabdict = createFSWTabDict(self.filename)
self.fswtabdict = FSWTabDict(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.

Sorry, my comment from last review was incorrect. The createFSWxxx method gets created in run-time during the extension process (i.e. util.__init_extensions__). So it is okay to call createFSWxxx method here. Please revert back to before this commit.

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

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

Choose a reason for hiding this comment

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

Sorry, my comment from last review was incorrect. The createFSWxxx method gets created in run-time during the extension process (i.e. util.__init_extensions__). So it is okay to call createFSWxxx method here. Please revert back to before this commit.



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 createFSWTabDefn(**fields)
fields["fswheaderdefns"] = fields.pop("header", None)
fields["coldefns"] = fields.pop("columns", None)
return FSWTabDefn(**fields)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, my comment from last review was incorrect. The createFSWxxx method gets created in run-time during the extension process (i.e. util.__init_extensions__). So it is okay to call createFSWxxx method here. Please revert back to before this commit.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I made the changes back and update utll.py



def encode_to_file(tbl_type, in_path, out_path):
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.