Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 42 additions & 23 deletions spdx/cli_tools/convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,16 @@ def print_help_msg(command):
with click.Context(command) as ctx:
click.echo(command.get_help(ctx))

@click.command()
@click.argument("src", nargs=-1)
@click.option("--infile", "-i", help="The file to be converted ")
@click.option("--outfile", "-o", help="The file after converting")
@click.option(
"--to",
"-t",
type=click.Choice(["json", "rdf", "yaml", "xml", "tag"], case_sensitive=False)
)
@click.option(
"--from",
"-f",
"from_",
type=click.Choice(["tag", "rdf"], case_sensitive=False))
@click.option("--force", is_flag=True, help="convert even if there are some parsing errors or inconsistencies")
def main(infile, outfile, src, from_, to, force):
"""
CLI-TOOL for converting a RDF or TAG file to RDF, JSON, YAML, TAG or XML format.

To use : run 'convertor -f <from_TYPE> <input file> -t <to_TYPE> <output_file>' command on terminal or use ' convertor --infile <input file name> --outfile <output file name> '

"""
if infile is None and outfile is None and len(src) == 2:
def determine_infile_and_outfile(infile, outfile, src, from_, to):
if infile is not None and outfile is not None:
"""
when the CLI is of given format:
' convertor ---infile <input_file> ---outfile <output_file>.
"""
return infile, outfile

elif infile is None and outfile is None and len(src) == 2:
"""
' convertor -f/--from <type> <input_file> -t/--to <type> <output_file>.
"""
infile = src[0]
Expand All @@ -58,6 +43,7 @@ def main(infile, outfile, src, from_, to, force):
if to is not None:
outfile_path = os.path.splitext(outfile)[0]
outfile = outfile_path + "." + to
return infile, outfile

elif infile is None and outfile is not None:
"""
Expand All @@ -67,6 +53,7 @@ def main(infile, outfile, src, from_, to, force):
if from_ is not None:
infile_path = os.path.splitext(infile)[0]
infile = infile_path + "." + from_
return infile, outfile

elif infile is not None and outfile is None:
"""
Expand All @@ -76,9 +63,41 @@ def main(infile, outfile, src, from_, to, force):
if to is not None:
outfile_path = os.path.splitext(outfile)[0]
outfile = outfile_path + "." + to
return infile, outfile

else:
raise ValueError("Given arguments for convertor are invalid.")


@click.command()
@click.argument("src", nargs=-1)
@click.option("--infile", "-i", help="The file to be converted ")
@click.option("--outfile", "-o", help="The file after converting")
@click.option(
"--to",
"-t",
type=click.Choice(["json", "rdf", "yaml", "xml", "tag"], case_sensitive=False)
)
@click.option(
"--from",
"-f",
"from_",
type=click.Choice(["tag", "rdf"], case_sensitive=False))
@click.option("--force", is_flag=True, help="convert even if there are some parsing errors or inconsistencies")
def main(infile, outfile, src, from_, to, force):
"""
CLI-TOOL for converting a RDF or TAG file to RDF, JSON, YAML, TAG or XML format.

To use : run 'convertor -f <from_TYPE> <input file> -t <to_TYPE> <output_file>' command on terminal or use ' convertor --infile <input file name> --outfile <output file name> '

"""
try:
infile, outfile = determine_infile_and_outfile(infile, outfile, src, from_, to)
except ValueError as err:
print(err)
print_help_msg(main)
return

doc, errors = parse_file(infile)
if errors:
print("Errors while parsing: ", errors)
Expand Down
83 changes: 83 additions & 0 deletions tests/test_cli_convertor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (c) 2022 spdx tool contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from unittest import TestCase

from spdx.cli_tools.convertor import determine_infile_and_outfile

from tests.testing_utils import raises


class TestConvertor(TestCase):
maxDiff = None

def test_determine_input_with_known_i_o_format(self):
infile_given = 'infile.rdf'
outfile_given = 'outfile.json'
src = ()
from_ = None
to = None

infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to)

assert infile == infile_given
assert outfile == outfile_given

def test_determine_input_with_unknown_i_o_format(self):
infile_given = None
outfile_given = None
src = ('infile.in', 'outfile.out')
from_ = 'rdf'
to = 'json'
expected_infile = 'infile.rdf'
expected_outfile = 'outfile.json'

infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to)

assert infile == expected_infile
assert outfile == expected_outfile

def test_determine_input_with_known_i_format_unknown_o_format(self):
infile_given = 'infile.rdf'
outfile_given = None
src = ('outfile',)
from_ = None
to = 'json'
expected_outfile = 'outfile.json'

infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to)

assert infile == infile_given
assert outfile == expected_outfile

def test_determine_input_with_unknown_i_format_known_o_format(self):
infile_given = None
outfile_given = 'outfile.json'
src = ('infile',)
from_ = 'rdf'
to = None
expected_infile = 'infile.rdf'

infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to)

assert infile == expected_infile
assert outfile == outfile_given

@raises(ValueError)
def test_determine_input_with_invalid_arguments(self):
infile_given = None
outfile_given = None
src = ()
from_ = None
to = None

infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to)