centopy


Namecentopy JSON
Version 0.3.0 PyPI version JSON
download
home_page
SummaryPython package for managing file creation and loading
upload_time2023-10-19 22:29:58
maintainer
docs_urlNone
authorVagner Bessa
requires_python>=3.10,<4.0
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # centopy

API for managing files in a specified folder.

## Installation

```bash
$ pip install centopy
```

## Usage

### FilesManager

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

```python
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:

```python
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:

```python
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:

```python
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:

```python
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:

```python
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

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

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

```python
print(archive.manager.list_files())
```
```bash
['file.customzip', 'data.bin', 'hello.txt']
```

```Python
content = 'Hello, centopy!'
archive.write('hello.txt', content=content)

# list files inside archive
print(archive.namelist())
```

```bash
['hello.txt']
```

#### Binary Data Compression

```python
# 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`](https://cookiecutter.readthedocs.io/en/latest/) and the `py-pkgs-cookiecutter` [template](https://github.com/py-pkgs/py-pkgs-cookiecutter).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "centopy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Vagner Bessa",
    "author_email": "bessavagner@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a4/3a/2598908591566b1a3c6325a7bb46cba50a3324a9b197807c479835fa4794/centopy-0.3.0.tar.gz",
    "platform": null,
    "description": "# centopy\n\nAPI for managing files in a specified folder.\n\n## Installation\n\n```bash\n$ pip install centopy\n```\n\n## Usage\n\n### FilesManager\n\nTo use the FilesManager class in your Python script or project, import it as follows:\n\n```python\nfrom centopy import FilesManager\n```\n#### Creating a FilesManager Instance\n\nTo create an instance of the FilesManager class, provide the folder path where you want to manage the files:\n\n```python\nfolder_path = \"/path/to/folder\"\nfile_manager = FilesManager(folder_path)\n```\n\n#### Saving and Loading Files\n\nYou can save and load file contents using the save_file and load_file methods, respectively:\n\n```python\nfile_name = \"example.txt\"\nfile_contents = \"Hello, world!\"\n\n# Save file contents to the specified file\nfile_manager.save_file(file_name, file_contents)\n\n# Load file contents from the specified file\nloaded_contents = file_manager.load_file(file_name)\n```\n\n#### Reading and Writing Files\n\nYou can also directly read from and write content to files using the read_file and write_file methods:\n\n```python\nfile_name = \"example.txt\"\nfile_contents = \"Hello, world!\"\n\n# Write contents to the specified file\nfile_manager.write_file(file_name, file_contents)\n\n# Read contents from the specified file\nread_contents = file_manager.read_file(file_name)\n```\n\n#### Handling File States\n\nThe 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:\n\n```python\nfile_name = \"example.txt\"\nstate = file_manager.get_file_state(file_name)\nprint(f\"File state for '{file_name}': {state}\")\n```\n\n**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\"]`.\n\n#### Handling Exceptions\n\nWhen 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:\n\n```python\nfile_name = \"nonexistent_file.txt\"\nloaded_contents = file_manager.load_file(file_name)\n\nif loaded_contents is None:\n    print(f\"The file '{file_name}' was not found.\")\n```\n\n### Compressor:\n\nThe class `centopy.Compressor` is an api to the `zipfile.ZipFile` context manager. You can use a custom extension when instantiating.\n\n#### Writing and appending files\n\n```python\narchive = centopy.Compressor('file', extension='customzip', wdir='data')\n```\n\nAn object `centopy.Compressor` has a attribute as an instance of `centopy.FilesManager`.\n\n```python\nprint(archive.manager.list_files())\n```\n```bash\n['file.customzip', 'data.bin', 'hello.txt']\n```\n\n```Python\ncontent = 'Hello, centopy!'\narchive.write('hello.txt', content=content)\n\n# list files inside archive\nprint(archive.namelist())\n```\n\n```bash\n['hello.txt']\n```\n\n#### Binary Data Compression\n\n```python\n# Create a Compressor object\ncompressor = Compressor(\"bin_archive\")\n\n# Write binary content to a new file and add it to the archive\nbinary_data = b'\\x00\\x01\\x02\\x03\\x04'\ncompressor.writeb(\"binary_data.bin\", binary_data)\n\n# Append binary content to an existing file in the archive\nnew_binary_data = b'\\x05\\x06\\x07'\ncompressor.appendb(\"binary_data.bin\", new_binary_data)\n\n# Read the binary content of a file from the archive\nbinary_content = compressor.read(\"binary_data.bin\", as_text=False)\n\nprint(\"Binary content:\", binary_content)\n\n```\n\n## Contributing\n\nInterested 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.\n\n## License\n\n`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.\n\n## Credits\n\n`centopy` was created with [`cookiecutter`](https://cookiecutter.readthedocs.io/en/latest/) and the `py-pkgs-cookiecutter` [template](https://github.com/py-pkgs/py-pkgs-cookiecutter).\n",
    "bugtrack_url": null,
    "license": "None",
    "summary": "Python package for managing file creation and loading",
    "version": "0.3.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "01781999801cd48d7144df00164c889305632d4fa44d02ac01ff9e0945c23506",
                "md5": "bf748c456a962d3fa2c39a6dcf6023a0",
                "sha256": "6e82643b270e8b889c64b79b84f262bfaf2869c750ae1e6868489268f56c22dc"
            },
            "downloads": -1,
            "filename": "centopy-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bf748c456a962d3fa2c39a6dcf6023a0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 10774,
            "upload_time": "2023-10-19T22:29:55",
            "upload_time_iso_8601": "2023-10-19T22:29:55.973831Z",
            "url": "https://files.pythonhosted.org/packages/01/78/1999801cd48d7144df00164c889305632d4fa44d02ac01ff9e0945c23506/centopy-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a43a2598908591566b1a3c6325a7bb46cba50a3324a9b197807c479835fa4794",
                "md5": "7ec5e4bf6dfd0c75a98f4924cd63cb81",
                "sha256": "89d2f150e1d0b66b9b10fd3d1970fa50301babb53cb56f164c1dabaebce8f5ce"
            },
            "downloads": -1,
            "filename": "centopy-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "7ec5e4bf6dfd0c75a98f4924cd63cb81",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 9884,
            "upload_time": "2023-10-19T22:29:58",
            "upload_time_iso_8601": "2023-10-19T22:29:58.452378Z",
            "url": "https://files.pythonhosted.org/packages/a4/3a/2598908591566b1a3c6325a7bb46cba50a3324a9b197807c479835fa4794/centopy-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-19 22:29:58",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "centopy"
}
        
Elapsed time: 3.18107s