Save (Version 2)
A system to easly write, read, delete data. with support of multiple file systems and encryption levels.
from src.PythonFunctions import save
sv = save()
Note
Multiple types of file systems are supported! More information about them later
Functions
Note
It does not matter what os you are on, if you use / or \ it will still be translated and works for any os.
DriveExists
- DriveExists()
Checks if gCred.json is found within the local storage
- Returns
If it is found
- Return type
bool
print(sv.DriveExists())
DriveCredentials
- DriveCredentials(path, mode)
Copies and deletes drive credentials file.
- Parameters
path (str) – The path where the file is
mode (DriveCredentialsMode) – What to do
- Returns
Information about what happened
- Return type
str
- Raises
AttributeError – Invalid mode
AttributeError – Can’t find gCred.json under path
sv.DriveCredentials(".", sv.DriveCredentialsMode.ADD)
Write
- Write(data, path, *, encoding)
Save the specified data to a file (path) with the specified encoding
- Parameters
data (any) – The data to save
path (str) – The location to save to
encoding (List[encoding] | encoding) – (Optional) The levels of encoding to apply to the data
- Returns
Information to do with the saving
- Return type
bool | str | dict
sv.Write("Hello World", "Example.txt")
Read
- Read(path, *, encoding)
Returns what the file contains after going through the specified encoding
- Parameters
path (str) – The path to read from
encoding (List[encoding] | encoding) – The encoding to apply to the file
- Returns
The data stored in the file
- Return type
any
data = sv.Read("Example.txt")
MakeFolders
- MakeFolders(path)
Make a folder, or folders following the path given.
- Parameters
path (str) – The folders to make
- Returns
The path of the folders
- Return type
str | dict | None
path = sv.MakeFolders("Test/a/b/c/d")
RemoveFile
- RemoveFile(path)
Remove path
- Parameters
path (str | List[str]) – File to remove
- Returns
Information about deletion
- Return type
str | bool | None
sv.RemoveFile("Test/a/b/test.txt")
RemoveFolder
- RemoveFolder(path)
Remove the folder and all subfolders / paths
- Parameters
path (str | List[str]) – Parent folder to remove
- Returns
Information about deletion
- Return type
str | bool | None
sv.RemoveFolder("Test")
ChangePasscode
Note
Changing your Passcode will lock you out of your encrypted data, please make sure to decrypt and save it before changing your passcode.
The passcode is also randomlly generated by the Encryption module
- ChangePasscode()
Changes the stored passcode to a new random value
- Returns
The new passcode
- Return type
bytes
passcode = sv.ChangePasscode()
File System support
This module doesn’t just support the default file system, instead it supports way more (and custom ones)
Note
Other than the ones provided, new ones are probably never going to be added or maintaied by me.
Note
If you want one to be in this module, submit a pull request. However you are going to have to maintain it.
Prefixs
To call upon a different file system, we use a different prefix before the path. This tells the class which file system we want to use.
The prefixes are as followed:
gdr:// (google drive)
ftp:// (File transfer protocol)
oth:// (Other, custom)
Note
If the required modules aren’t installed then some of these file systems might not work
Depending on the file system, you might have to provide more information, which will be listed below.
ftp (File Transfer Protocol)
The ftp system requires a bit more information before you can provide the path. After which, it will ask for some details before actually getting to the path.
Note
I belive this is using the insecure version of ftp because i don’t know if there is a secure module out there. Please only use this on your local network and not some kind of big private company network.
Example of ftp file: ftp://192.168.120.1:21/Hi.txt
ftp:// Tells the script the file system
192.168.120.1 Tells the script the IP to connect to
:21 Tells the script the port to connect to (Default: 21, so not needed for default)
Hi.txt and after, tells the script the file to interact with.
Note
On program start, the first time you call the server to do anything you will be asked to check your username and enter your password. You can’t switch servers unless the other server has the same username and surname as the first server or you change your information.
gdr (Google Drive API)
Note
Google has it’s own api which needs to be installed. If you want to install it run pip install PythonFunctions[google] -U
Google drive api takes a bit more setup other than installing the module. Some of which i can not automate. I could set up a default one but i don’t want to because then i have to handle it and everyone will share the same data.
To set up google drive api follow these steps:
Make a new project
go to https://console.cloud.google.com/apis/enableflow?apiid=drive.googleapis.com and follow the steps
- Create Credentials -> OAuth client ID.
Application type -> Desktop app
Name (anything you want, something useful though)
Create
Download json file provided, rename to gCred.json
Place gCred.json in the folder with the program.
- Run the program (It should open your browser and ask for you to sign in one first time use.)
Make sure to run sv.Drivecredentials() beforehand, If you don’t want to always run it, check using sv.DriveExists()
Note
GOOGLE NOTES:
Even now and again you will need to reverify, this is a google thing not me. It is super easy to do though.
If google is saying you don’t have access, add yourself as a tester. Go to: https://console.cloud.google.com/apis/credentials/consent. Scroll down until you see Test users. Add User, enter you email and save.
oth (Other)
This prefix is mainly for your own custom file system. You can make one and use it by this command.
Note
Please follow this template otherwise you will run into some errors whilst using it.
The first time you call that file in that class, you will be asked to provide the path.
encoding
As well as multiple file system supports, there is also supports for the following encoding
None
Json
Binary
Cryptography
CSV
When passing in an encoding argument, it can either be a list or one value and will be treated left to right. (Except for decoding which will treat right to left)
Note
If you pass in the arguments in the wrong order, you are going to run into issues. The order to pass them in is the same order as you did when you saved it.
To get an encoding value, use the enum provided.
from PythonFunctions import Save
binary = Save.Encoding.BINARY
CSV encoding
CSV encoding is handled differently so requires it’s own little section here. To use this format, you need to pass in a dictionary as the data with no other encoding before hand. This has to be the first thing encoded. The dictionary must contain these values: header and rows
header must be of type list rows must also be of type list with sub dictionaries with all of the headers and fields in them.
Example:
csvItems = {
"header": ["name", "test"],
"rows": [
{
"name": "Save",
"test": "Making sure stuff saves correctly"
},
{
"name": "Clean",
"test": "Cleaning out a folder to only have the stuff you want"
}
]
}
If not provided in this format, then it will not work.