Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F80270691
conffile.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Aug 30, 20:24
Size
2 KB
Mime Type
text/x-python
Expires
Sun, Sep 1, 20:24 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
20331855
Attached To
R3127 blackdynamite
conffile.py
View Options
#!/usr/bin/env python
from
__future__
import
print_function
__all__
=
[
"ConfFile"
,
"addFile"
]
import
sqlobject
import
re
import
os
class
ConfFile
(
sqlobject
.
SQLObject
):
"""
"""
def
addFile
(
self
,
filename
,
params
=
None
,
regex_params
=
None
,
content
=
None
):
print
(
"adding file "
+
filename
)
self
.
prepare
()
self
.
entries
[
"filename"
]
=
os
.
path
.
basename
(
filename
)
if
(
content
):
self
.
entries
[
"file"
]
=
content
else
:
self
.
entries
[
"file"
]
=
open
(
filename
,
'r'
)
.
read
()
if
(
regex_params
):
for
p
in
params
:
lowerp
=
p
.
lower
()
rp
=
"("
+
regex_params
rp
=
rp
.
replace
(
"%p"
,
")("
+
p
)
rp
=
rp
.
replace
(
"%v"
,
")(.*)"
)
rr
=
"
\\
1
\\
2__BLACKDYNAMITE__"
+
p
+
"__"
# print (rp)
# print (rr)
self
.
entries
[
"file"
]
=
re
.
sub
(
rp
,
rr
,
self
.
entries
[
"file"
],
flags
=
re
.
IGNORECASE
)
# print (self.entries["file"])
filelist
=
self
.
getMatchedObjectList
()
if
(
len
(
filelist
)
==
0
):
tmp_conffile
=
ConfFile
(
self
.
base
)
tmp_conffile
.
entries
=
dict
(
self
.
entries
)
del
tmp_conffile
.
entries
[
'filename'
]
md5filelist
=
tmp_conffile
.
getMatchedObjectList
()
if
(
len
(
md5filelist
)
!=
0
):
import
md5
for
f
in
md5filelist
:
raise
Exception
(
"""There is already another file with same content but different name: this is an impossible situation for BlackDynamite.
The file concerned is '{0}' md5:{1}
** If you want keep going, please rename the file before insertion **
"""
.
format
(
f
[
'filename'
],
md5
.
new
(
f
[
'file'
])
.
hexdigest
()))
self
.
base
.
insert
(
self
)
elif
(
len
(
filelist
)
==
1
):
self
.
entries
=
filelist
[
0
]
.
entries
self
.
id
=
filelist
[
0
]
.
id
def
__init__
(
self
,
connection
):
sqlobject
.
SQLObject
.
__init__
(
self
,
connection
)
self
.
table_name
=
"configfiles"
self
.
types
[
"filename"
]
=
str
self
.
types
[
"file"
]
=
str
def
addFile
(
filename
,
base
,
**
kwargs
):
cnffile
=
ConfFile
(
base
)
cnffile
.
addFile
(
filename
,
**
kwargs
)
return
cnffile
Event Timeline
Log In to Comment