-
-
Save ciiqr/11195812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def makeResourceFromFiles(fpaths): | |
""" | |
Create a Resource object from the passed file(s) and return it | |
""" | |
for fpath in fpaths: | |
if not os.path.exists(fpath): | |
print "Path doesn't exist: %s" % fpath | |
continue | |
# try to determine the MIME type of the file | |
mt = mimetypes.guess_type(fpath) | |
typestr = mt[0] | |
if typestr.split('/')[0] == "text": | |
rmode = 'r' | |
else: | |
rmode = 'rb' | |
fdata = open(fpath, rmode).read() | |
md5 = hashlib.md5() | |
md5.update(fdata) | |
hashs = md5.digest() | |
data = Types.Data() | |
data.size = len(fdata) | |
data.bodyHash = hashs | |
data.body = fdata | |
resource = Types.Resource() | |
resource.mime = typestr | |
resource.data = data | |
attrs = Types.ResourceAttributes() | |
attrs.fileName = os.path.basename(fpath) | |
resource.attributes = attrs | |
yield resource |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment