class Folder
Represents a folder item in a file system hierarchy, extending the Item base class with the ability to contain children and be uploaded as a ZIP archive.
/tf/active/vicechatdev/rmcl/items.py
284 - 297
moderate
Purpose
The Folder class models a directory/folder in what appears to be a reMarkable tablet file system. It maintains a collection of child items and provides functionality to upload the folder as a ZIP file containing metadata. The upload method creates an empty content file within the ZIP archive, suggesting this is part of a document management system where folders need to be synchronized or uploaded to a remote service.
Source Code
class Folder(Item):
def __init__(self, metadata):
super().__init__(metadata)
self.children = []
@add_sync
async def upload(self):
f = io.BytesIO()
with zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as zf:
zf.writestr(f'{self.id}.content', '')
f.seek(0)
return await self.upload_raw(f)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Item | - |
Parameter Details
metadata: A dictionary or object containing metadata information about the folder. This is passed to the parent Item class constructor and likely includes properties such as folder ID, name, parent folder reference, creation/modification timestamps, and other folder-specific attributes required by the Item base class.
Return Value
The Folder constructor returns a new Folder instance. The upload() method returns the result of upload_raw(f), which is likely a response object or status from uploading the ZIP file to a remote service. Since upload() is an async method decorated with @add_sync, it can be called both synchronously and asynchronously.
Class Interface
Methods
__init__(self, metadata)
Purpose: Initializes a new Folder instance with the provided metadata and an empty children list
Parameters:
metadata: Dictionary or object containing folder metadata to be passed to the parent Item class
Returns: None (constructor)
async upload(self) -> Any
Purpose: Creates a ZIP archive containing an empty content file for the folder and uploads it using the parent class's upload_raw method
Returns: The result from the upload_raw() method call, likely a response object or status indicator from the upload operation
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
children |
list | A list that stores child items (files or subfolders) contained within this folder. Initialized as an empty list and should be populated with Item instances or subclasses. | instance |
metadata |
dict or object | Inherited from Item base class. Stores metadata information about the folder such as ID, name, parent reference, timestamps, etc. | instance |
id |
str | Inherited from Item base class. Unique identifier for the folder, used in the upload method to name the content file in the ZIP archive. | instance |
Dependencies
iozipfiletriofunctools
Required Imports
import io
import zipfile
from sync import add_sync
Usage Example
# Assuming Item base class and necessary imports are available
metadata = {
'id': 'folder-123',
'name': 'My Documents',
'parent': 'root',
'type': 'CollectionType'
}
# Create a folder instance
folder = Folder(metadata)
# Add children to the folder
folder.children.append(child_item1)
folder.children.append(child_item2)
# Upload the folder (async)
import trio
async def main():
result = await folder.upload()
print(f'Upload result: {result}')
trio.run(main)
# Or use synchronously (if @add_sync decorator provides sync version)
result = folder.upload()
print(f'Upload result: {result}')
Best Practices
- Always initialize the Folder with proper metadata that includes at minimum an 'id' field, as this is used in the upload() method
- The children list should be populated with Item or Item-subclass instances to maintain the folder hierarchy
- The upload() method is async and should be awaited when called in async contexts, or called directly if the @add_sync decorator provides a synchronous wrapper
- The upload() method creates an in-memory ZIP file, so be mindful of memory usage for folders with many children or large metadata
- Ensure the parent Item class's upload_raw() method is properly implemented before calling upload()
- The ZIP file created contains an empty content file named '{id}.content', suggesting this is a placeholder for folder representation in the storage system
- Do not modify the children list during iteration without proper safeguards to avoid concurrent modification issues
- The Folder instance maintains state through the children list, so be careful with shared references
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Item 65.2% similar
-
class FolderDebugger 65.1% similar
-
class VirtualFolder 63.1% similar
-
class RemarkableNode 62.1% similar
-
class Document_v1 60.3% similar