Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add folders module #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
[download all modules](https://download-directory.github.io/?url=https://github.com/tasmota/Berry_playground/tree/main/modules)

### [googleapis - Google API auth with Google Drive example](./googleapis/)

### [folders - berry with folders - provides zap(folder) to delete folde and conten](./folders/)
52 changes: 52 additions & 0 deletions modules/folders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Google API usage from Tasmote via Berry

[return to modules](../README.md)
[Home](../../README.md)

[Download this 'folders' folder complete](https://download-directory.github.io/?url=https://github.com/tasmota/Berry_playground/tree/main/modules/folders)

Originator: btsimonh

For [Questions and Discussion about this module](https://github.com/tasmota/Berry_playground/discussions/22) see this topic

Tas/Berry/Other features used:
* Tas filesystem

Any and all contributions welcome without specific consultation. I don't consider this module to be production quality.

## Files in this folder relate to the use of folders from Berry in Tasmota.


### Requirements

Latest development version

### Files:

#### [zap.be](./zap.be)
This provides a zap(folder) function which will delete a folder and all content

* Stability: Fairly stable, pending feedback.

Usage:

zap(folder)

or

zap(folder, true) - to get more feedback in logs


#### [testzap.be](./testzap.be)

Creates some folders and files and then zap them.

Example of usage of path.mkdir() and path.rmdir(), plus other file features.


### Acknowledgements


### Alternative approaches

None. There was no folder manipulation in TAS.
19 changes: 19 additions & 0 deletions modules/folders/testzap.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path
#load("zap.be")

path.mkdir('/testdir')
path.mkdir('/testdir/test')
var f = open('/testdir/test/test.txt', 'w')
print(f)
f.write('test test')
f.close()

#print(path.remove('/testdir/test/test.txt'))
#print(path.rmdir('/testdir/test'))
#print(path.rmdir('/testdir'))

print(path.listdir('/testdir/test'))

zap('/testdir/', 1)

print(path.listdir('/testdir/test'))
41 changes: 41 additions & 0 deletions modules/folders/zap.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path

def zap(folder, diag)
if !folder || folder == '/'
print('refusing to zap root')
return false
end
if folder == '/sd' || folder == '/sd/'
print('refusing to zap sd root')
return false
end
if diag print('zap '+folder) end
if folder[-1] == '/'
if diag print('remove trailing slash '+folder) end
folder = folder[0..-2]
end
if path.exists(folder)

var files = path.listdir(folder)
if !size(files)
if diag print(folder..' is empty') end
end

for f:files
if !path.remove(folder .. '/' .. f)
zap(folder .. '/' .. f)
else
if diag print('deleted file '..folder .. '/' .. f) end
end
end
if !path.rmdir(folder)
if diag print('failed to remove '..folder) end
else
if diag print('removed '..folder) end
end
else
if diag print('folder '..folder ..' does not exist') end
end
end