Skip to main content

centopy

API for managing files in a specified folder.

Installation

$ pip install centopy

Usage

FilesManager

To use the FilesManager class in your Python script or project, import it as follows:

from centopy import FilesManager

Creating a FilesManager Instance

To create an instance of the FilesManager class, provide the folder path where you want to manage the files:

folder_path = "/path/to/folder"
file_manager = FilesManager(folder_path)

Saving and Loading Files

You can save and load file contents using the save_file and load_file methods, respectively:

file_name = "example.txt"
file_contents = "Hello, world!"

# Save file contents to the specified file
file_manager.save_file(file_name, file_contents)

# Load file contents from the specified file
loaded_contents = file_manager.load_file(file_name)

Reading and Writing Files

You can also directly read from and write content to files using the read_file and write_file methods:

file_name = "example.txt"
file_contents = "Hello, world!"

# Write contents to the specified file
file_manager.write_file(file_name, file_contents)

# Read contents from the specified file
read_contents = file_manager.read_file(file_name)

Handling File States

The FilesManager class keeps track of the state of each file that has been saved or loaded. You can check the state of a file using the get_file_state method:

file_name = "example.txt"
state = file_manager.get_file_state(file_name)
print(f"File state for '{file_name}': {state}")

Note: The state is represented as a list of events. For example, if a file is saved and loaded, the state attribute (file_manager.file_state[file_name]) will be ["saved", "loaded"].

Handling Exceptions

When loading a file, if the specified file is not found, the load_file method returns None and logs an error message using the logging module. To handle such cases, you can check if the loaded contents are None:

file_name = "nonexistent_file.txt"
loaded_contents = file_manager.load_file(file_name)

if loaded_contents is None:
    print(f"The file '{file_name}' was not found.")

Compressor:

The class centopy.Compressor is an api to the zipfile.ZipFile context manager. You can use a custom extension when instantiating.

Writing and appending files

archive = centopy.Compressor('file', extension='customzip', wdir='data')

An object centopy.Compressor has a attribute as an instance of centopy.FilesManager.

print(archive.manager.list_files())
['file.customzip', 'data.bin', 'hello.txt']
content = 'Hello, centopy!'
archive.write('hello.txt', content=content)

# list files inside archive
print(archive.namelist())
['hello.txt']

Binary Data Compression

# Create a Compressor object
compressor = Compressor("bin_archive")

# Write binary content to a new file and add it to the archive
binary_data = b'\x00\x01\x02\x03\x04'
compressor.writeb("binary_data.bin", binary_data)

# Append binary content to an existing file in the archive
new_binary_data = b'\x05\x06\x07'
compressor.appendb("binary_data.bin", new_binary_data)

# Read the binary content of a file from the archive
binary_content = compressor.read("binary_data.bin", as_text=False)

print("Binary content:", binary_content)

Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

centopy was created by Vagner Bessa. Vagner Bessa retains all rights to the source and it may not be reproduced, distributed, or used to create derivative works.

Credits

centopy was created with cookiecutter and the py-pkgs-cookiecutter template.

Metadata

Release files for centopy 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for centopy 0.3.0
File Size Uploaded Provenance Actions
centopy-0.3.0.tar.gz 9.9 kB Without provenance Details for centopy-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for centopy 0.3.0
File Interpreter ABI Platform Actions
centopy-0.3.0-py3-none-any.whl Python 3 none any Details for centopy-0.3.0-py3-none-any.whl

Total release size: 20.7 kB

Release files / centopy-0.3.0.tar.gz

Download URL centopy-0.3.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
89d2f150e1d0b66b9b10fd3d1970fa50301babb53cb56f164c1dabaebce8f5ce
BLAKE2b-256 checksum
How to use checksums
a43a2598908591566b1a3c6325a7bb46cba50a3324a9b197807c479835fa4794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-34-generic

Release files / centopy-0.3.0-py3-none-any.whl

Download URL centopy-0.3.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e82643b270e8b889c64b79b84f262bfaf2869c750ae1e6868489268f56c22dc
BLAKE2b-256 checksum
How to use checksums
01781999801cd48d7144df00164c889305632d4fa44d02ac01ff9e0945c23506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-34-generic

Security for centopy 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page