Skip to content

Commit

Permalink
support toml and yaml configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric05 authored Jul 15, 2023
1 parent 0466cc7 commit ed35d4c
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 40 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ requests_ntlm = "==1.1.0"
RestrictedPython = "==6.1"
Faker = "==13.11.1"
requests-hawk = "==1.1.1"
toml = "0.10.2"
pyyaml = "6.0"

[dev-packages]
pyinstaller = "*"
Expand Down
124 changes: 93 additions & 31 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 27 additions & 7 deletions dothttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
except ImportError:
import json
validate = None

import yaml
import toml
from textx import TextXSyntaxError, metamodel_from_file

from .dsl_jsonparser import json_or_array_to_json
Expand Down Expand Up @@ -314,7 +315,6 @@ class Property:
key: Union[str, None] = None
value: Union[str, None] = None


class BaseModelProcessor:
var_regex = re.compile(r'{{(?P<var>.*?)}}')

Expand Down Expand Up @@ -350,11 +350,26 @@ def load_properties_n_headers(self):
"""
if not self.property_file and self.file:
base_logger.debug('property file not specified')
default = os.path.join(os.path.dirname(self.file), ".dothttp.json")
if os.path.exists(default):
default_json = os.path.join(os.path.dirname(self.file), ".dothttp.json")
default_yaml = os.path.join(os.path.dirname(self.file), ".dothttp.yaml")
default_yml = os.path.join(os.path.dirname(self.file), ".dothttp.yml")
default_toml = os.path.join(os.path.dirname(self.file), ".dothttp.toml")
if os.path.exists(default_json):
base_logger.debug(
f'file: {default_json} exists. it will be used for property reference')
self.property_file = default_json
elif os.path.exists(default_yaml):
base_logger.debug(
f'file: {default_yaml} exists. it will be used for property reference')
self.property_file = default_yaml
elif os.path.exists(default_yml):
base_logger.debug(
f'file: {default_yaml} exists. it will be used for property reference')
self.property_file = default_yml
elif os.path.exists(default_toml):
base_logger.debug(
f'file: {default} exists. it will be used for property reference')
self.property_file = default
f'file: {default_yaml} exists. it will be used for property reference')
self.property_file = default_toml
if self.property_file and not os.path.exists(self.property_file):
base_logger.debug(
f'file: {self.property_file} not found')
Expand All @@ -363,7 +378,12 @@ def load_properties_n_headers(self):
if self.property_file:
with open(self.property_file, 'r') as f:
try:
props = json.load(f)
if self.property_file.endswith('.json'):
props = json.load(f)
elif self.property_file.endswith('.yaml') or self.property_file.endswith('.yml') :
props = yaml.load(f, yaml.SafeLoader)
elif self.property_file.endswith('.toml'):
props = toml.load(f)
base_logger.debug(
f'file: {self.property_file} loaded successfully')
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion dothttp/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.42.a9'
__version__ = '0.0.42.a10'
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ requests-aws4auth==1.1.1
requests_ntlm==1.1.0
RestrictedPython==6.0
Faker==13.11.1
requests-hawk==1.1.1
requests-hawk==1.1.1
PyYaml==6.0
toml==0.10.2

0 comments on commit ed35d4c

Please sign in to comment.