-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.pyc | ||
__pycache__ | ||
*.egg-info | ||
.eggs | ||
.vscode | ||
.mypy_cache | ||
.pytest_cache | ||
*cache | ||
dist | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2018 zWaR | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[mypy] | ||
python_version=3.6 | ||
ignore_missing_imports=True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
setuptools==38.5.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[aliases] | ||
test=pytest | ||
|
||
[tool:pytest] | ||
addopts=--mypy | ||
python_files=tests/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
from setuptools import setup | ||
|
||
setup( | ||
name="csv2json", | ||
version="1.0.0", | ||
description="A tool for csv to json file conversion", | ||
author="zWaR", | ||
author_email="[email protected]", | ||
classifiers=[ | ||
"Indetend Audience :: Developers", | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3.6" | ||
], | ||
keywords="csv json conversion tool", | ||
packages=["src"], | ||
setup_requires=["pytest-runner"], | ||
tests_require=["pytest", "pytest-cov", "pyfakefs", "pytest-mypy"], | ||
url="https://github.com/zWaR/csv2json", | ||
project_urls={ | ||
"Source Code": "https://github.com/zWaR/csv2json.git", | ||
"Bug Tracker": "https://github.com/zWaR/csv2json/issues", | ||
"Documentation": "https://github.com/zWaR/csv2json" | ||
}, | ||
entry_points={ | ||
"console_scripts": [ | ||
"csv2json = src.main:main" | ||
] | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
import csv | ||
import json | ||
|
||
from typing import List | ||
from typing import Dict | ||
from typing import Iterable | ||
|
||
class Csv2Json(object): | ||
|
||
def run(self, | ||
csv_path: str, | ||
json_path: str, | ||
has_header: bool = False, | ||
pretty_print: bool = False | ||
) -> None: | ||
"""Execute csv 2 json conversion. | ||
Args: | ||
csv_path (str): Path to the CSV input file. | ||
json_path (str): Path to the JSON output file. | ||
has_header (bool): Does the CSV file have a header. | ||
""" | ||
|
||
csv_array: List = [] | ||
with open( | ||
csv_path, newline="", encoding="utf-8", errors="ignore" | ||
) as csv_file: | ||
csv_content: Iterable = csv.reader(csv_file, delimiter=",") | ||
for row in csv_content: | ||
csv_array.append(row) | ||
|
||
header: List[str] = [] | ||
json_struct: List = [] | ||
if has_header: | ||
header = csv_array.pop(0) | ||
for row in csv_array: | ||
temp_struct: Dict = {} | ||
for index, element in enumerate(header): | ||
temp_struct[element] = row[index] | ||
json_struct.append(temp_struct) | ||
|
||
if not has_header: | ||
for row in csv_array: | ||
json_struct.append(row) | ||
|
||
with open(json_path, "w") as json_file: | ||
if pretty_print: | ||
json.dump(json_struct, json_file, indent=2) | ||
else: | ||
json.dump(json_struct, json_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
import argparse | ||
|
||
from .csv2json import Csv2Json | ||
|
||
def main(): | ||
parser: argparse.ArgumentParser = argparse.ArgumentParser() | ||
parser.add_argument("csv", help="CSV input file path", type=str) | ||
parser.add_argument("json", help="JSON otput path", type=str) | ||
parser.add_argument( | ||
"--has_header", | ||
help="Does the CSV file have a header", | ||
action="store_true" | ||
) | ||
parser.add_argument( | ||
"--pretty_print", | ||
help="JSON should be pretty formated", | ||
action="store_true" | ||
) | ||
args: argparse.Namespace = parser.parse_args() | ||
|
||
Csv2Json().run(args.csv, args.json, args.has_header, args.pretty_print) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import pytest | ||
|
||
from src.csv2json import Csv2Json | ||
|
||
@pytest.fixture | ||
def csv2json(): | ||
csv2json: Csv2Json = Csv2Json() | ||
|
||
yield csv2json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import json | ||
|
||
from typing import Dict | ||
from typing import List | ||
|
||
from src.csv2json import Csv2Json | ||
from pyfakefs.fake_filesystem import FakeFilesystem | ||
|
||
|
||
class TestCsv2Json(object): | ||
|
||
def test_run_header(self, csv2json: Csv2Json, fs: FakeFilesystem) -> None: | ||
mock_csv: str = "a,b,c,d\n1,2,3,4\n5,6,7,8" | ||
mock_csv_file_path: str = "/tmp/file.csv" | ||
mock_json_file_path: str = "/tmp/result.json" | ||
has_header: bool = True | ||
|
||
with open(mock_csv_file_path, "w") as file: | ||
file.write(mock_csv) | ||
|
||
csv2json.run(mock_csv_file_path, mock_json_file_path, has_header) | ||
|
||
expected_result: List[Dict[str, str]] = [ | ||
{ | ||
"a": "1", | ||
"b": "2", | ||
"c": "3", | ||
"d": "4" | ||
}, | ||
{ | ||
"a": "5", | ||
"b": "6", | ||
"c": "7", | ||
"d": "8" | ||
} | ||
|
||
] | ||
|
||
with open(mock_json_file_path, "r") as file: | ||
result_content = json.load(file) | ||
|
||
assert expected_result == result_content | ||
|
||
def test_run_no_header(self, csv2json: Csv2Json, fs: FakeFilesystem) -> None: | ||
mock_csv: str = "1,2,3,4\n5,6,7,8" | ||
mock_csv_file_path: str = "/tmp/file.csv" | ||
mock_json_file_path: str = "/tmp/result.json" | ||
has_header: bool = False | ||
|
||
with open(mock_csv_file_path, "w") as file: | ||
file.write(mock_csv) | ||
|
||
csv2json.run(mock_csv_file_path, mock_json_file_path, has_header) | ||
|
||
expected_result: List[List[str]] = [ | ||
[ "1", "2", "3", "4" ], | ||
[ "5", "6", "7", "8" ] | ||
] | ||
|
||
with open(mock_json_file_path, "r") as file: | ||
result_content = json.load(file) | ||
|
||
assert expected_result == result_content |