python-script-to-build-an-oracle-or-mysql-flat-file-vault

JSON

rev 12  |  foxhop  |  1308770103000  |  JSON

rev 11
rev 12
6161
62 print v.getFilename() # generate a valid path and filename from vault62 print v.getFilename() # generate a valid path and filename from vault
n63 print v.getFilename() # generate a valid path and filename from vaultn63 print v.getFilename( '.jpg' ) # generate a valid path and jpg filename from vau
 >lt
6464
65 # create vault2 in /var/db 2 directories wide and 5 directories deep65 # create vault2 in /var/db 2 directories wide and 5 directories deep
66 v2 = Vault( path="/var/db", name="vault2", l=5, w=2 )66 v2 = Vault( path="/var/db", name="vault2", l=5, w=2 )
67 67 
t68 print v2.getFilename() # generate a valid path and filename from vaultt68 print v2.getFilename( '.mp3' ) # generate a valid path and mp3 filename from va
 >ult
69 print v2.getFilename() # generate a valid path and filename from vault69 print v2.getFilename() # generate a valid path and filename from vault
7070
rev 11  |  foxhop  |  1308769994000  |  JSON

rev 10
rev 11
50============================== 50============================== 
5151
n52*test_pyvault.py*n52*test_pyVault.py*
5353
54.. code-block:: python54.. code-block:: python
5555
nn56 # import pyVault module 
56 from pyvault import Vault57 from pyVault import Vault
5758
n58 v = Vault( )n59 # create a vault with default settings in the current directory.
60 v = Vault( ) # create a vault object
5961
n60 print v.getFilename()n62 print v.getFilename() # generate a valid path and filename from vault
61 print v.getFilename()63 print v.getFilename() # generate a valid path and filename from vault
6264
tt65 # create vault2 in /var/db 2 directories wide and 5 directories deep
63 v2 = Vault( root="myvault", l=5, w=2 )66 v2 = Vault( path="/var/db", name="vault2", l=5, w=2 )
64 67 
65 print v2.getFilename()68 print v2.getFilename() # generate a valid path and filename from vault
66 print v2.getFilename()69 print v2.getFilename() # generate a valid path and filename from vault
6770
6871
rev 10  |  foxhop  |  1308769431000  |  JSON

rev 9
rev 10
2================================================================2================================================================
33
n4This script will build a 64 by 64 flat filesystem.  The root directory will be nn4This module provides a vault class.  This class is useful for creating a flat fi
>amed thevault.  >lesystem that could be used for storing images, pdfs, mp3, autocad drawings etc.
55
n6This is useful for storing flatfiles like pictures, pdfs, mp3 or autocad drawingn6The meta data about this file system would have to be stored in a database.
>s and then use the database to store metadata about the files. 
77
tt8 
9 
8*pyvault.py*10*pyVault.py*
911
10.. code-block:: python12.. code-block:: python
rev 9  |  foxhop  |  1308769195000  |  JSON

rev 8
rev 9
10.. code-block:: python10.. code-block:: python
1111
n12 from os import makedirsn12 from os import makedirs, path
13 from random import randrange as r13 from random import randrange as r
14 from uuid import uuid414 from uuid import uuid4
1515
16 class Vault( object ):16 class Vault( object ):
n17    def __init__( self, root='thevault', l=65, w=65 ):n17     def __init__( self, path='', name='vault', l=65, w=65 ):
18        self.root = root18         self.path = path 
19         self.name = name
19        self.l = l20         self.l = l
20        self.w = w21         self.w = w
22         
23         self.initVault()
2124
n22        self.initVault()n25     def initVault( self ):
26         """Build the vault directories if they don't exist"""
27         p = self.path + '/' + self.name
28         if path.exists( p ):
29             pass
30         else:
31             for l in range( self.l ):
32                 for w in range( self.w ):
33                     if self.path == '':
34                         makedirs( self.name + '/' + str( l ) + '/' + str( w ) )
35                     else:
36                         makedirs(  self.path + '/' + self.name + '/' + str( l )
 > + '/' + str( w ) )
2337
n24    def initVault( self ):n38     def getFilename( self, ext='' ):
25        """Build the vault directories"""39         """Generate and return a valid unique file path based on vault"""
26        for l in range( self.l ):40         l, w = str( r(self.l) ), str( r(self.w) )
27            for w in range( self.w ):41         filename = str( uuid4() )
28                makedirs( self.root + '/' + str( l ) + '/' + str( w ) )42        
43         return self.name + '/' + l + '/' + w + '/' + filename + ext
2944
t30    def getFilename( self ):t
31        """Generate and return a valid unique file path based on vault"""
32        l, w = str( r(self.l) ), str( r(self.w) )
33        filename = str( uuid4() )
34        return self.root + '/' + l + '/' + w + '/' + filename
3545
3646
rev 8  |  foxhop  |  1297537913000  |  JSON

rev 7
rev 8
42.. code-block:: python42.. code-block:: python
4343
n44from pyvault import Vaultn44 from pyvault import Vault
4545
n46v = Vault( )n46 v = Vault( )
4747
n48print v.getFilename()n48 print v.getFilename()
49print v.getFilename()49 print v.getFilename()
5050
n51v2 = Vault( root="myvault", l=5, w=2 )n51 v2 = Vault( root="myvault", l=5, w=2 )
5252
n53print v2.getFilename()n53 print v2.getFilename()
54print v2.getFilename()54 print v2.getFilename()
5555
5656
7474
7575
n76Example algorithm to create file locationsn
77========================================================
78 
79.. code-block:: python
80 
81 from random import randrange as r
82 from uuid import uuid4
83 
84 def makeVaultName():
85     """Return a unique file path based on the vault"""
86     root = "thevault"
87     filename = uuid4()
88     return root + '/' + str(r(65)) + '/' + str(r(65)) + '/' + str(filename)
8976
9077
9178
t92 t
93 
94 
rev 7  |  foxhop  |  1297537849000  |  JSON

rev 6
rev 7
33        filename = str( uuid4() )33        filename = str( uuid4() )
34        return self.root + '/' + l + '/' + w + '/' + filename34        return self.root + '/' + l + '/' + w + '/' + filename
tt35 
36 
37Example pyvault test
38============================== 
39 
40*test_pyvault.py*
41 
42.. code-block:: python
43 
44from pyvault import Vault
45 
46v = Vault( )
47 
48print v.getFilename()
49print v.getFilename()
50 
51v2 = Vault( root="myvault", l=5, w=2 )
52 
53print v2.getFilename()
54print v2.getFilename()
3555
3656
rev 6  |  foxhop  |  1297537552000  |  JSON

rev 5
rev 6
1111
12 from os import makedirs12 from os import makedirs
nn13 from random import randrange as r
14 from uuid import uuid4
1315
n14 root = "thevault"n16 class Vault( object ):
17    def __init__( self, root='thevault', l=65, w=65 ):
18        self.root = root
19        self.l = l
20        self.w = w
1521
t16 for i in range( 1, 65 ):t22        self.initVault()
17     for j in range( 1, 65 ):23 
24    def initVault( self ):
25        """Build the vault directories"""
26        for l in range( self.l ):
27            for w in range( self.w ):
18         makedirs( root + '/' + str( i ) + '/' + str( j ) )28                makedirs( self.root + '/' + str( l ) + '/' + str( w ) )
29 
30    def getFilename( self ):
31        """Generate and return a valid unique file path based on vault"""
32        l, w = str( r(self.l) ), str( r(self.w) )
33        filename = str( uuid4() )
34        return self.root + '/' + l + '/' + w + '/' + filename
35 
1936
20Example database schema to use with vault37Example database schema to use with vault
rev 5  |  foxhop  |  1297536075000  |  JSON

rev 4
rev 5
45 from uuid import uuid445 from uuid import uuid4
4646
tt47 def makeVaultName():
48     """Return a unique file path based on the vault"""
47 root = "thevault"49     root = "thevault"
48 
49 filename = uuid4()50     filename = uuid4()
50 print root + '/' + str(r(65)) + '/' + str(r(65)) + '/' + str(filename)51     return root + '/' + str(r(65)) + '/' + str(r(65)) + '/' + str(filename)
5152
5253
rev 4  |  foxhop  |  1297535801000  |  JSON

rev 3
rev 4
4848
49 filename = uuid4()49 filename = uuid4()
t50 print root + '/' + str(r(65)) + '/' + str(r(65)) + '/' + filenamet50 print root + '/' + str(r(65)) + '/' + str(r(65)) + '/' + str(filename)
5151
5252
rev 3  |  foxhop  |  1297535759000  |  JSON

rev 2
rev 3
18         makedirs( root + '/' + str( i ) + '/' + str( j ) )18         makedirs( root + '/' + str( i ) + '/' + str( j ) )
1919
nn20Example database schema to use with vault
21====================================================
22 
20Then we could use uuids for the actual file names to prevent overwriting or the 23Then we could use uuids for the actual file names to prevent overwriting or the 
>need to lookup or keep track of names.>need to lookup or keep track of names.
2124
3437
3538
nn39Example algorithm to create file locations
40========================================================
41 
42.. code-block:: python
43 
44 from random import randrange as r
45 from uuid import uuid4
46 
47 root = "thevault"
48 
49 filename = uuid4()
50 print root + '/' + str(r(65)) + '/' + str(r(65)) + '/' + filename
3651
3752
tt53 
54 
55 
56 
rev 2  |  foxhop  |  1297535392000  |  JSON

rev 1
rev 2
18         makedirs( root + '/' + str( i ) + '/' + str( j ) )18         makedirs( root + '/' + str( i ) + '/' + str( j ) )
1919
n20I then normally use uuids for the acutal file names to prevent overwriting or thn20Then we could use uuids for the actual file names to prevent overwriting or the 
>e need to lookup or keep track of names.>need to lookup or keep track of names.
2121
t22I then have a database table similar to this:t22Below shows an example database schema that could use this vault filesystem:
2323
24.. code-block:: python24.. code-block:: python
rev 1  |  foxhop  |  1297535291000  |  JSON

empty
rev 1
tt1Python script to build an Oracle or mySQL flat file vault
2================================================================
3 
4This script will build a 64 by 64 flat filesystem.  The root directory will be n
 >amed thevault.  
5 
6This is useful for storing flatfiles like pictures, pdfs, mp3 or autocad drawing
 >s and then use the database to store metadata about the files.
7 
8*pyvault.py*
9 
10.. code-block:: python
11 
12 from os import makedirs
13 
14 root = "thevault"
15 
16 for i in range( 1, 65 ):
17     for j in range( 1, 65 ):
18         makedirs( root + '/' + str( i ) + '/' + str( j ) )
19 
20I then normally use uuids for the acutal file names to prevent overwriting or th
 >e need to lookup or keep track of names.
21 
22I then have a database table similar to this:
23 
24.. code-block:: python
25 
26 vault
27    id (int) # auto increment 
28    name (varchar 64) # human readable lookup name
29    location (varchar 128) # thevault/12/59/28c6104f-cb09-4910-8031-efad22e1ebf9
30    filetype (varchar 8) # or possibly another table to keep track of file types
 > or possibly add the extension to the uuid filename when saved.
31    description (text)
32 
33Then 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 an
 >d an autocad drawing when it was designed.
34 
35 
36 
37