python-script-to-build-an-oracle-or-mysql-flat-file-vault
Python script to build an Oracle or mySQL flat file vault
This module provides a vault class. This class is useful for creating a flat filesystem that could be used for storing images, pdfs, mp3, autocad drawings etc.
The meta data about this file system would have to be stored in a database.
from os import makedirs, path
from random import randrange as r
from uuid import uuid4
class Vault( object ):
def __init__( self, path='', name='vault', l=65, w=65 ):
self.path = path
self.name = name
self.l = l
self.w = w
self.initVault()
def initVault( self ):
"""Build the vault directories if they don't exist"""
p = self.path + '/' + self.name
if path.exists( p ):
pass
else:
for l in range( self.l ):
for w in range( self.w ):
if self.path == '':
makedirs( self.name + '/' + str( l ) + '/' + str( w ) )
else:
makedirs( self.path + '/' + self.name + '/' + str( l ) + '/' + str( w ) )
def getFilename( self, ext='' ):
"""Generate and return a valid unique file path based on vault"""
l, w = str( r(self.l) ), str( r(self.w) )
filename = str( uuid4() )
return self.name + '/' + l + '/' + w + '/' + filename + extExample pyvault test
# import pyVault module
from pyVault import Vault
# create a vault with default settings in the current directory.
v = Vault( ) # create a vault object
print v.getFilename() # generate a valid path and filename from vault
print v.getFilename( '.jpg' ) # generate a valid path and jpg filename from vault
# create vault2 in /var/db 2 directories wide and 5 directories deep
v2 = Vault( path="/var/db", name="vault2", l=5, w=2 )
print v2.getFilename( '.mp3' ) # generate a valid path and mp3 filename from vault
print v2.getFilename() # generate a valid path and filename from vaultExample database schema to use with pyVault
UUIDs are used for the actual file names to prevent overwriting if we need version control.
UUIDs are also a great 'key' that we could search on in a database.
Below shows an example database schema that could use this vault filesystem:
vault
id (int) # auto increment
uuid (varchar 32) # indexed field: 28c6104f-cb09-4910-8031-efad22e1ebf9
name (varchar 64) # human readable filename: foxhop-october.jpg
location (varchar 128) # vault/12/59/28c6104f-cb09-4910-8031-efad22e1ebf9.jpg
filetype (varchar 8) # extension (move to own table?): jpg
description (text) # comments, description, info: foxhop in october
Then other tables can refer and relate to this data, like a job table could have relations mapped to jpg files and pdf files, and a video of the finished job and an autocad drawing when it was designed.
Remarkbox
Comments