ransware2 /
csv-to-json-converter
This project provides a lightweight Python script that converts structured CSV files into JSON format, useful for data processing and application development.
37/100 healthLoading repository data…
ryanlevee / repository
A lightweight Python script designed to merge files from a project directory and subdirectories into a single output file, while skipping specified folders and files. Works with any programming language.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
This is a simple, lightweight Python script designed to merge files from a directory and subdirectories into a single output file, while skipping specified folders and files. This can be useful for creating a consolidated text version of a project for documentation or review purposes.
unpack_to_dataclassdataclasses module to define a Config class for managing configuration settings in a clean and efficient manner.walk_directory, handle_file, read_file, process_file) to efficiently process files and directories, which is particularly useful for handling large datasets.Selected from shared topics, language and repository description—not editorial ratings.
ransware2 /
This project provides a lightweight Python script that converts structured CSV files into JSON format, useful for data processing and application development.
37/100 healthunpack_to_dataclassThe unpack_to_dataclass function is a key component of the script, designed to dynamically convert a dictionary into an instance of the Config data class. This function ensures that each key in the dictionary matches the corresponding field name in the Config data class and that the value is of the expected type. The expected type is determined using the default_factory of each field, providing a robust and flexible way to handle configuration data.
Key Features:
Config data class and that the value is of the expected type.KeyError if a required key is missing and a TypeError if a value is not of the expected type, ensuring that the configuration data is accurate and reliable.default_factory of each field to determine the expected type, allowing for a wide range of configuration data to be handled dynamically.Function Signature:
def unpack_to_dataclass(data: Dict[str, ConfigDataType]) -> Config:
"""
Converts a dictionary to a Config data class instance.
Args:
data (Dict[str, ConfigDataType]): The dictionary containing configuration data.
Raises:
KeyError: If a key in the dictionary does not match the corresponding field name in the Config data class.
TypeError: If a value in the dictionary is not of the expected type.
Returns:
Config: An instance of the Config data class with fields populated from the dictionary.
"""
This function enhances the script's robustness and flexibility, making it easier to manage and validate configuration data dynamically.
The script imports necessary modules and sets up logging:
import json
import logging
import os
from dataclasses import dataclass, field, fields
from typing import Dict, Generator, List
logging.basicConfig(level=logging.INFO)
A Config data class is defined to hold configuration settings. The LanguageSyntax and WalkingFilters data classes are used to manage comment syntax and file filtering settings, respectively. The Config class inherits from both to consolidate all configuration settings.
@dataclass
class LanguageSyntax:
block_comment: BlockCommentType = field(default_factory=dict)
inline_comment: InlineCommentType = field(default_factory=dict)
@dataclass
class WalkingFilters:
skip_folders: FiltersType = field(default_factory=list)
skip_files: FiltersType = field(default_factory=list)
allowed_extensions: FiltersType = field(default_factory=list)
@dataclass
class Config(LanguageSyntax, WalkingFilters):
root_path: str = field(default_factory=str)
project_dir: str = field(default_factory=str)
output_dir: str = field(default_factory=str)
output_filename: str = field(default_factory=str)
output_extension: str = field(default_factory=str)
project_language: str = field(default_factory=str)
The FileMerger class handles the merging of files:
Config data class instance.The run() function loads configuration data from JSON files and starts the file processing:
def run() -> None:
config_files: Dict[str, str] = {
"skip_folders": "skip_folders.json",
"skip_files": "skip_files.json",
"allowed_extensions": "allowed_extensions.json",
"project_config": "project_config.json",
}
config_data: Dict[str, ConfigDataType] = {
key: load_json(path) for key, path in config_files.items()
}
project_data: ConfigDataType = config_data.pop("project_config")
if not isinstance(project_data, dict):
raise TypeError("Expected 'project_config' to be a dictionary")
config_data.update(project_data)
typed_config: Config = unpack_to_dataclass(config_data)
file_merger = FileMerger(typed_config)
file_merger.start()
if __name__ == "__main__":
run()
Clone the repository:
git clone https://github.com/ryanlevee/project-to-single-file-converter.git
cd project-to-single-file-converter
Ensure you have Python 3.7 or later installed, as the script relies on the dataclasses module introduced in Python 3.7.
The script uses the following JSON files for configuration:
allowed_extensions.json: Specifies the file extensions to include.skip_folders.json: Lists the folders to skip.skip_files.json: Lists the files to skip.project_config.json: Contains the main configuration settings.Example of project_config.json:
{
"root_path": "C:\\Users\\[your-username]\\Documents",
"project_dir": "project-to-single-file-converter",
"output_dir": "output",
"output_filename": "single-file-project",
"output_extension": "txt",
"project_language": "python"
}
Ensure the configuration files are correctly set up.
Run the script:
python main.py
This project is licensed under the MIT License - see the LICENSE file for details.