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

Core should take file-like objects instead of file names #94

Closed
EliteMasterEric opened this issue Apr 5, 2017 · 4 comments
Closed

Core should take file-like objects instead of file names #94

EliteMasterEric opened this issue Apr 5, 2017 · 4 comments

Comments

@EliteMasterEric
Copy link
Contributor

In my experimentation with this library, I have found it frustrating that, if I have a file-like object, for example a StringIO, I am forced to write the data to a file before validating it.

This does not conform with Python's standard practices, and the fact that this lengthy process is only for validating the file type makes things even more difficult.

My proposal is:

  • The source_file argument now takes a file rather than a string filename.
  • Add a new optional argument validation_type, with possible values yaml or json
  • If validation_type is not None, use YAML or JSON resp6ctively to parse the file.
  • Else, try setting self.source to yaml.load(source_file), then to json.load(source_file); if neither work, you can just fall back to the source_data argument as you do now. If people have issues with this automatic parsing attempt, they can use the new argument to force it.

As a bonus, this fix also resolves #89.

@brownberry
Copy link

Together with the functionality described above, it would be nice if there was a clear way how to re-use the same scheme multiple times to validate data sets against it:

In the moment, it seems that both have to be parsed "together" into a "core object" core = Core( scheme_files, data_file ) which then features the core.validate() function.

However, it would be more practical the state something a la scheme = Scheme( scheme_files ) and then scheme.validate( data_file_A ) and scheme.validate( data_file_B ) and so forth ...

@Grokzen
Copy link
Owner

Grokzen commented Feb 6, 2018

@brownberry I have not yet analyzed how much this would change in the code but it feels like that would require some major rework of the code and i am not ready to commit to that one yet. If you want to probe the idea to see what would be required for this to work i would love to see some WIP MR that shows the scope/size of that kind of feature.

@Grokzen
Copy link
Owner

Grokzen commented Oct 19, 2019

@mastereric @brownberry a1919fa Feature implemented in next major release where you can now accept file like objects, specially designed for StringIO to be used.

The feature you suggested @brownberry you have to open up a new issue feature request for to reimplenet Schema as a object instead of how it is parsed right now.

@Grokzen Grokzen closed this as completed Oct 19, 2019
@Grokzen
Copy link
Owner

Grokzen commented Oct 19, 2019

The following example script shows how the new feature will work

schema = """
allowempty: True
mapping:
  intents:
    type: !!python/str "seq"
    sequence:
    - type: !!python/str "str"
"""

data = """
intents:
 - greet
 - default
 - goodbye
"""


import pykwalify
pykwalify.init_logging(4)

from pykwalify.core import Core

try:
    from StringIO import StringIO ## for Python 2
except ImportError:
    from io import StringIO ## for Python 3

s = StringIO()
s.write(schema)
s.seek(0)

d = StringIO()
d.write(data)
d.seek(0)

c = Core(schema_file_obj=s, data_file_obj=d)
c.validate()

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

No branches or pull requests

3 participants