-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Daethyra/working
Refactorization, added image conversion
- Loading branch information
Showing
18 changed files
with
884 additions
and
143 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,149 @@ | ||
import os | ||
import logging | ||
import click | ||
import mimetypes | ||
from PIL import Image | ||
import json | ||
import csv | ||
import odf.opendocument | ||
import xml.etree.ElementTree as ET | ||
|
||
|
||
logging.basicConfig(filename='file.log', level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s') | ||
|
||
|
||
class FileConverter: | ||
def __init__(self, input_path: str, output_path: str): | ||
""" | ||
Initializes a new instance of the FileConverter class. | ||
Args: | ||
input_path (str): Path to the input file. | ||
output_path (str): Path to save the converted file. | ||
""" | ||
self.input_path = input_path | ||
self.output_path = output_path | ||
|
||
def convert(self) -> None: | ||
""" | ||
Converts a file to the desired format. | ||
Raises: | ||
NotImplementedError: If the method is not implemented in the derived class. | ||
ValueError: If the input file format is not supported. | ||
""" | ||
raise NotImplementedError | ||
|
||
@staticmethod | ||
def supported_conversions() -> str: | ||
""" | ||
Returns a string with the supported file formats and conversions. | ||
Returns: | ||
str: Supported file formats and conversions. | ||
""" | ||
return "Supported file formats and conversions:\n" \ | ||
"JSON: can be converted to CSV or JSON\n" \ | ||
"CSV: can be converted to JSON or CSV (no conversion needed)\n" \ | ||
"ODT: can be converted to plain text\n" \ | ||
"XML: can be converted to JSON" | ||
|
||
|
||
class ImageConverter(FileConverter): | ||
def convert(self) -> None: | ||
""" | ||
Converts an image to the desired format. | ||
Raises: | ||
ValueError: If the input file format is not supported. | ||
""" | ||
try: | ||
img = Image.open(self.input_path) | ||
img.save(self.output_path) | ||
except Exception as e: | ||
logging.exception(f"Failed to convert image. Input: {self.input_path}, Output: {self.output_path}. Error: {str(e)}") | ||
raise ValueError("Unsupported file format.") | ||
|
||
|
||
class TextConverter(FileConverter): | ||
def convert(self) -> None: | ||
""" | ||
Converts a text document to the desired format. | ||
Raises: | ||
ValueError: If the input file format is not supported or the conversion is not possible. | ||
""" | ||
input_ext = os.path.splitext(self.input_path)[1].lower() | ||
output_ext = os.path.splitext(self.output_path)[1].lower() | ||
|
||
if input_ext == '.json' and output_ext == '.csv': | ||
with open(self.input_path, 'r') as f: | ||
data = json.load(f) | ||
with open(self.output_path, 'w', newline='') as f: | ||
writer = csv.writer(f) | ||
writer.writerow(data[0].keys()) | ||
for row in data: | ||
writer.writerow(row.values()) | ||
elif input_ext == '.csv' and output_ext == '.json': | ||
with open(self.input_path, 'r') as f: | ||
reader = csv.DictReader(f) | ||
data = [row for row in reader] | ||
with open(self.output_path, 'w') as f: | ||
json.dump(data, f, indent=4) | ||
elif input_ext == '.odt' and output_ext == '.txt': | ||
doc = odf.opendocument.load(self.input_path) | ||
with open(self.output_path, 'w') as f: | ||
f.write(doc.text().replace('\n', ' ')) | ||
elif input_ext == '.xml' and output_ext == '.json': | ||
tree = ET.parse(self.input_path) | ||
root = tree.getroot() | ||
data = [] | ||
for child in root: | ||
data.append(child.attrib) | ||
with open(self.output_path, 'w') as f: | ||
json.dump(data, f, indent=4) | ||
else: | ||
raise ValueError(f"Unsupported file format or conversion. Input: {input_ext}, Output: {output_ext}. " | ||
f"{FileConverter.supported_conversions()}") | ||
|
||
if not os.path.exists(self.output_path): | ||
raise ValueError(f"Conversion failed. Input: {self.input_path}, Output: {self.output_path}. " | ||
f"{FileConverter.supported_conversions()}") | ||
|
||
|
||
@click.command() | ||
@click.argument('input_path', type=click.Path(exists=True)) | ||
@click.argument('output_path', type=click.Path()) | ||
def convert_file(input_path: str, output_path: str) -> None: | ||
""" | ||
Converts a file to the desired format. | ||
Args: | ||
input_path (str): Path to the input file. | ||
output_path (str): Path to save the converted file. | ||
Raises: | ||
ValueError: If the input and output file formats are the same or the input file format is not supported. | ||
""" | ||
input_ext = os.path.splitext(input_path)[1].lower() | ||
output_ext = os.path.splitext(output_path)[1].lower() | ||
|
||
if input_ext == output_ext: | ||
raise ValueError("Input and output file formats cannot be the same.") | ||
|
||
file_type, _ = mimetypes.guess_type(input_path) | ||
if file_type is None: | ||
raise ValueError("Unsupported file format.") | ||
|
||
if file_type.startswith('image'): | ||
converter = ImageConverter(input_path, output_path) | ||
elif file_type.startswith('text'): | ||
converter = TextConverter(input_path, output_path) | ||
else: | ||
raise ValueError("Unsupported file format.") | ||
|
||
converter.convert() | ||
|
||
|
||
if __name__ == "__main__": | ||
convert_file() |
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,29 @@ | ||
index,Date,Week Day,Total Screen Time ,Social Networking,Reading and Reference,Other,Productivity,Health and Fitness,Entertainment,Creativity,Yoga | ||
0,04/17/19,Wednesday,187,89,17,41,22,0,0,0,0 | ||
1,04/18/19,Thursday,123,78,17,8,9,0,0,0,0 | ||
2,04/19/19,Friday,112,52,40,8,4,0,3,0,0 | ||
3,04/20/19,Saturday,101,69,9,38,2,0,3,0,0 | ||
4,04/21/19,Sunday,56,35,2,43,3,0,1,1,0 | ||
5,04/22/19,Monday,189,68,0,9,3,4,0,0,0 | ||
6,04/23/19,Tuesday,158,56,18,41,12,15,0,0,0 | ||
7,04/24/19,Wednesday,135,98,3,33,16,0,0,0,0 | ||
8,04/25/19,Thursday,52,25,7,3,16,0,0,0,0 | ||
9,04/26/19,Friday,198,76,8,29,15,0,32,0,0 | ||
10,04/27/19,Saturday,116,75,10,20,5,0,0,0,0 | ||
11,04/28/19,Sunday,85,42,22,4,2,0,0,0,0 | ||
12,04/29/19,Monday,109,46,8,13,9,15,1,0,1 | ||
13,04/30/19,Tuesday,79,40,2,9,12,0,0,0,1 | ||
14,05/01/19,Wednesday,127,90,0,10,7,0,0,0,1 | ||
15,05/02/19,Thursday,170,60,3,2,11,0,0,0,1 | ||
16,05/03/19,Friday,91,64,2,18,5,1,1,2,1 | ||
17,05/04/19,Saturday,58,34,4,5,3,0,1,0,1 | ||
18,05/05/19,Sunday,133,109,5,1,3,0,0,0,1 | ||
19,05/06/19,Monday,144,81,4,5,3,0,0,0,1 | ||
20,05/07/19,Tuesday,110,70,5,6,15,0,9,0,1 | ||
21,05/08/19,Wednesday,122,53,25,26,15,0,0,0,1 | ||
22,05/09/19,Thursday,96,42,15,16,19,0,0,0,1 | ||
23,05/10/19,Friday,161,93,13,17,16,1,0,0,1 | ||
24,05/11/19,Saturday,58,49,1,2,2,0,0,2,1 | ||
25,05/12/19,Sunday,52,28,1,1,6,0,0,1,1 | ||
26,05/13/19,Monday,61,37,1,0,4,0,0,0,1 | ||
27,05/14/19,Tuesday,88,41,2,7,15,0,0,0,1 |
Oops, something went wrong.