Skip to content

Commit

Permalink
Add support for --encoding cli argument when opening files to read.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grokzen committed Oct 4, 2018
1 parent bffe78c commit 7a1e826
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pykwalify/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def parse_cli():

__docopt__ = """
usage: pykwalify -d FILE -s FILE ... [-e FILE ...]
[--strict-rule-validation] [--fix-ruby-style-regex] [--allow-assertions] [-v ...] [-q]
[--strict-rule-validation] [--fix-ruby-style-regex] [--allow-assertions] [--encoding ENCODING]
[-v ...] [-q]
optional arguments:
-d FILE, --data-file FILE the file to be tested
Expand All @@ -39,6 +40,7 @@ def parse_cli():
--allow-assertions By default assertions is disabled due to security risk.
Error will be raised if assertion is used in schema
but this flag is not used. This option enables assert keyword.
--encoding ENCODING Specify encoding to open data and schema files with.
-h, --help show this help message and exit
-q, --quiet suppress terminal output
-v, --verbose verbose terminal output (multiple -v increases verbosity)
Expand Down Expand Up @@ -78,6 +80,7 @@ def run(cli_args):
strict_rule_validation=cli_args['--strict-rule-validation'],
fix_ruby_style_regex=cli_args['--fix-ruby-style-regex'],
allow_assertions=cli_args['--allow-assertions'],
file_encoding=cli_args['--encoding'],
)
c.validate()
return c
Expand Down
7 changes: 4 additions & 3 deletions pykwalify/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import traceback
import time
from io import open

# pyKwalify imports
import pykwalify
Expand All @@ -31,7 +32,7 @@ class Core(object):
""" Core class of pyKwalify """

def __init__(self, source_file=None, schema_files=None, source_data=None, schema_data=None, extensions=None, strict_rule_validation=False,
fix_ruby_style_regex=False, allow_assertions=False,):
fix_ruby_style_regex=False, allow_assertions=False, file_encoding=None):
"""
:param extensions:
List of paths to python files that should be imported and available via 'func' keywork.
Expand Down Expand Up @@ -65,7 +66,7 @@ def __init__(self, source_file=None, schema_files=None, source_data=None, schema
if not os.path.exists(source_file):
raise CoreError(u"Provided source_file do not exists on disk: {0}".format(source_file))

with open(source_file, "r") as stream:
with open(source_file, "r", encoding=file_encoding) as stream:
if source_file.endswith(".json"):
self.source = json.load(stream)
elif source_file.endswith(".yaml") or source_file.endswith('.yml'):
Expand All @@ -83,7 +84,7 @@ def __init__(self, source_file=None, schema_files=None, source_data=None, schema
if not os.path.exists(f):
raise CoreError(u"Provided source_file do not exists on disk : {0}".format(f))

with open(f, "r") as stream:
with open(f, "r", encoding=file_encoding) as stream:
if f.endswith(".json"):
data = json.load(stream)
elif f.endswith(".yaml") or f.endswith(".yml"):
Expand Down

0 comments on commit 7a1e826

Please sign in to comment.