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

Conversation

JimHofman
Copy link

No description provided.

@JimHofman JimHofman requested review from a team as code owners October 20, 2021 16:21
@nttoole
Copy link
Contributor

nttoole commented Oct 20, 2021

@JimHofman Looks like you still need to declare your newCmd as an extension in the config.yaml

See: https://ait-core.readthedocs.io/en/master/extensions.html

Config yaml would contain an entry that looks like:

....
extensions:
    ait.core.cmd.Cmd: ait.core.newCmd.NewCmd

Would presumably need something similar for the table extension.

@JimHofman
Copy link
Author

I'll check that out Nick. I was sure I had added those. Hmmm. Thanks Nick.

@MJJoyce
Copy link
Member

MJJoyce commented Oct 20, 2021

Hey @JimHofman, replying here with regards to the issue(s) you're running into with the table extension support. Thanks for throwing this branch up so we have something to dig through.

It looks like you got the "general extension" stuff added and working correctly. That's why you can see log message indicating that the extension class has been added. However, it's not getting fully pulled in for a few reasons (I think).

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


def YAMLCtor_FSWTabDefn(loader, node):
    fields = loader.construct_mapping(node, deep=True)
    fields['fswheaderdefns'] = fields.pop('header', None)
    fields['coldefns'] = fields.pop('columns', None)
    return FSWTabDefn(**fields)

These functions are used by the custom YAML class handling stuff when processing the dictionary

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

So, if you import the table dictionary you'll see a message indicating that the extension was loaded but it's never going to actually load your class because the above functions (and others in the code) are calling explicit class names instead of the "extensions classes". These createXXX functions that should be used are (poorly) documented in the extension code:
https://github.com/NASA-AMMOS/AIT-Core/blob/master/ait/core/util.py#L112

Looking through the table code really quickly (so you should sanity check that I'm not missing something) I think you need to make the following modifications:

@JimHofman
Copy link
Author

Table extensions now work. Created a newTable.py to override FSWTabDict.
newTable.py
from ait.core import table, log

class NewFSWTabDict(table.FSWTabDict):
def init(self, *args, **kwargs):
super().init(*args, **kwargs)

def load(self, fname):
    log.info('Starting the table load() from the extended 
    	     FSWTabDict class, using file: {}'.format(self.filename))
    return super(NewFSWTabDict, self).load(fname)

def custom():
    log.info('Test of a unique method defined in an extension.')

Used the following to instantiate and verify:

import ait.core.table as table

table_dict = table.getDefaultFSWTabDict()

Copy link
Contributor

@jasonmlkang jasonmlkang left a comment

Choose a reason for hiding this comment

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

Please see comments.

I think createFSWxxx methods should be auto generated during the extension process. Otherwise it would throw error when no extension is defined in the config.

Also the branch has conflicts with current master.

ait/core/cmd.py Outdated
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)
data = yaml.load(f, Loader=yaml.Loader)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this necessary?

Copy link
Author

@JimHofman JimHofman Mar 14, 2022

Choose a reason for hiding this comment

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

I took out loader=yaml.loader.
We might want to get rid of data=None and use a try/except around the open block.

@@ -71,7 +71,7 @@
import sys
import re

from ait.core import cmd, dmc, log, util
from ait.core import cmd, dmc, log, util, table
Copy link
Contributor

Choose a reason for hiding this comment

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

table is not reference in this module

@@ -427,7 +432,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

@@ -461,7 +466,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



def YAMLCtor_FSWTabDefn(loader, node):
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

Comment on lines 172 to 173
ait.core.table.FSWTabDict: ait.core.newTable.NewFSWTabDict
Copy link
Contributor

Choose a reason for hiding this comment

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

These are testing codes?

Copy link
Author

Choose a reason for hiding this comment

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

Yes that is for testing. I took it out.

@nttoole
Copy link
Contributor

nttoole commented Mar 14, 2022

Correct, the createXxx() methods are implicitly added here:

def __init_extensions__(modname, modsyms): # noqa

Copy link
Contributor

@jasonmlkang jasonmlkang left a comment

Choose a reason for hiding this comment

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

Please see comment. Thanks!

ait/core/cmd.py Outdated
Comment on lines 586 to 592
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?

@@ -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.

@@ -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.

@@ -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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants