-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
9e4c917
commit c454895
Showing
19 changed files
with
222 additions
and
193 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,32 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2022 Arcangelo Massari <[email protected]> | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any purpose | ||
# with or without fee is hereby granted, provided that the above copyright notice | ||
# and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, | ||
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | ||
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
# SOFTWARE. | ||
|
||
|
||
from contextlib import contextmanager | ||
from oc_meta.lib.cleaner import Cleaner | ||
from pathlib import Path | ||
from typing import List, Dict, Set | ||
from zipfile import ZipFile, ZIP_DEFLATED | ||
import csv | ||
import json | ||
import os | ||
import sys | ||
import zipfile | ||
|
||
|
||
def get_data(filepath:str) -> List[Dict[str, str]]: | ||
def get_csv_data(filepath:str) -> List[Dict[str, str]]: | ||
field_size_changed = False | ||
cur_field_size = 128 | ||
data = list() | ||
|
@@ -76,7 +95,79 @@ def zipdir(path, ziph): | |
os.path.join(path, '..'))) | ||
|
||
def zipit(dir_list:list, zip_name:str) -> None: | ||
zipf = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) | ||
zipf = ZipFile(file=zip_name, mode='w', compression=ZIP_DEFLATED, allowZip64=True) | ||
for dir in dir_list: | ||
zipdir(dir, zipf) | ||
zipf.close() | ||
zipf.close() | ||
|
||
def zip_json_files_in_dir(src_dir:str, dst_dir:str, replace_files:bool=False) -> None: | ||
''' | ||
This method zips json files individually in all directories starting from a specified root directory. | ||
In other words, this function does not zip the entire folder but individual files | ||
while maintaining the folder hierarchy in the specified output directory. | ||
:params src_dir: the source directory | ||
:type src_dir: str | ||
:params dst_dir: the destination directory | ||
:type dst_dir: str | ||
:params replace_files: True if you want to replace the original unzipped files with their zipped versions. The dafult value is False | ||
:type replace_files: bool | ||
:returns: None | ||
''' | ||
for dirpath, _, filenames in os.walk(src_dir): | ||
for filename in filenames: | ||
src_path = os.path.join(dirpath, filename) | ||
dst_path = os.path.join( | ||
dst_dir, | ||
str(Path(src_path).parent) | ||
.replace(f'{src_dir}{os.sep}', '')) | ||
if not os.path.exists(dst_path): | ||
os.makedirs(dst_path) | ||
with ZipFile(file=os.path.join(dst_path, filename) + '.zip', mode='w', compression=ZIP_DEFLATED, allowZip64=True) as zipf: | ||
zipf.write(src_path, arcname=filename) | ||
if replace_files: | ||
os.remove(src_path) | ||
|
||
def unzip_files_in_dir(src_dir:str, dst_dir:str, replace_files:bool=False) -> None: | ||
''' | ||
This method unzips zipped files individually in all directories starting from a specified root directory. | ||
In other words, this function does not unzip the entire folder but individual files | ||
while maintaining the folder hierarchy in the specified output directory. | ||
:params src_dir: the source directory | ||
:type src_dir: str | ||
:params dst_dir: the destination directory | ||
:type dst_dir: str | ||
:params replace_files: True if you want to replace the original zipped files with their unzipped versions, defaults to [False] | ||
:type replace_files: bool | ||
:returns: None | ||
''' | ||
for dirpath, _, filenames in os.walk(src_dir): | ||
for filename in filenames: | ||
if os.path.splitext(filename)[1] == '.zip': | ||
src_path = os.path.join(dirpath, filename) | ||
dst_path = os.path.join( | ||
dst_dir, | ||
str(Path(src_path).parent) | ||
.replace(f'{src_dir}{os.sep}', '')) | ||
if not os.path.exists(dst_path): | ||
os.makedirs(dst_path) | ||
with ZipFile(file=os.path.join(dst_path, filename), mode='r') as zipf: | ||
zipf.extractall(dst_path) | ||
if replace_files: | ||
os.remove(src_path) | ||
|
||
def read_zipped_json(filepath:str) -> dict: | ||
''' | ||
This method reads a zipped json file. | ||
:params filepath: the zipped json file path | ||
:type src_dir: str | ||
:returns: dict -- It returns the json file as a dictionary | ||
''' | ||
with ZipFile(filepath, 'r') as zipf: | ||
for filename in zipf.namelist(): | ||
with zipf.open(filename) as f: | ||
json_data = f.read() | ||
json_dict = json.loads(json_data.decode("utf-8")) | ||
return json_dict |
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.