On 17 aug. 2012, at 15:01, Eelco Deuling <[email protected]> wrote:
> Sorry: this part I had posted already!
>
> This is the second part:
(I'm not a python code checker ;-)
# make sure you import all at the top of your file
# see PEP8
import os
import re
##############################################
# remove OSX .DS_Store files
##############################################
for root, dirs, files in os.walk(os.path.curdir): # Use the standard library
if '.DS_Store' in files:
os.remove(os.path.join(root, '.DS_Store'))
##############################################
# create a temporary file
# for the directory list
##############################################
#1: Don't mix spaces and tabs, ever in python. It'll make you unhappy.
#2: Again: use standard lib
#3: This is only an intermediate file to list the 'other files' for later
processing.
# Better collect the info in a list, or generate the final format
#4: I wonder if there are too many assumptions here. os.walk returns the
complete tree
# starting from root. It appears you only want the files within the current
dir
filelist = ['<item id="<##-ID-##>" href="{0}"/>'.format(f)
for f in os.listdir(os.path.curdir)
if os.path.isfile(f)]
str2 = "\r".join(filelist)
##############################################
# Add proper mime-types
# The registry is at http://www.iana.org/assignments/media-types/
##############################################
# This is rather dirty hacking. Be sure to document where you found this, or
else how you came up with it.
# you are using regexes, a dot will match any char.
# Note that there is a function that just returns the extension of a file in
os.path.
# That is probably more robust that re.
def replace_words(text, My_Dictionary):
rc = re.compile('|'.join(map(re.escape, My_Dictionary)))
def translate(match):
return My_Dictionary[match.group(0)]
return rc.sub(translate, text)
My_Dictionary = {
# text_files
'.html"': '.html" media-type="application/xhtml+xml"',
'.xhtml"': '.xhtml" media-type="application/xhtml+xml"',
'.xml"': '.xml" media-type="text/xml"',
# css files
'.css"': '.css" media-type="text/css"',
# image files
'.gif"': '.gif" media-type="image/gif"',
'.jpg"': '.jpg" media-type="image/jpeg"',
'.jpeg"': '.jpeg" media-type="image/jpeg"',
'.png"': '.png" media-type="image/png"',
'.svg"': '.svg" media-type="image/svg+xml"',
# font files: ttf is not officially supported: woff is!
'.otf"': '.otf" media-type="application/x-font-otf"',
'.woff"': '.ttf" media-type="application/x-font-woff"',
'.ttf"': '.ttf" media-type="application/x-font-ttf"',
# Javascript
'.js"': '.js" media-type="application/javascript"',
# html5 audio and video
'.mp4"': '.mp4" media-type="audio/mp4"',
'.mp3"': '.mp3" media-type="audio/mpeg"',
'.m4v"': '.m4v" media-type="video/mpeg4"',
'.webm"': '.webm" media-type="video/webm"',
'.ogg"': '.ogg" media-type="audio/ogg"'
}
# call the function and get the changed text
str3 = replace_words(str2, My_Dictionary)
# after all this, str4 is exactly the same as str3, isn't it?
# write changed text back out
#fout = open('myfile_03.tmp', 'w')
#fout.write(str3)
#fout.close()
# append the text from the third temp_file to .xml
#fin2 = open('myfile_03.tmp', 'r')
#str4 = fin2.read()
#fin2.close()
fout2 = open('test.xml', 'a')
fout2.write(str3)
fout2.close()
##############################################
# remove the temporary Files
##############################################
[os.remove(f) for f in os.listdir(os.path.curdir) if f.endswith('.tmp') ]
--
--
You received this message because you are subscribed to the
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem,
please email "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>