# squidge.py # version 1.1.0 # dated 22 December 2000 # by Julius Welby # for information see http://www.outwardlynormal.com/python/squidge.htm # A Python module which supports two functions: # # A "squidge" takes a directory of HTML (or other) files and # copies them into a single file, delimited by user defined tags # (default is and ). # # An "unsquidge" takes the file created by a squidg and re-exports the # sub-files into a new directory, adjacent to the original squidged file. # # Why? To allow a single operation (eg. a spellcheck)on the squidged # file to be effectively sitewide once the site is unsquidged. # This module has been written so that the squidged output is in XML, # though by default the original file extension is preserved. # # New in v1.1.0 - the files appear in the squidged file in name order. # This is not important in terms of the squidge/unsquidge process, # but may be useful if you need to display a squidged file in a browser. import os, glob path = '' # option -you can name the root and first sub-elements whatever you want. root_element = "SQUIDGEDFILES" sub_element = "SQUIDGEDFILE" # the following two declarations can be edited if necessary. xml_declaration = '\n' doctype_declaration = "\n" # create top and tail strings xml_header = xml_declaration + doctype_declaration xml_footer = "" # Functions to set-up the job def welcome(): # intro text print '\nWelcome to squidge.py' print '\nDo you wish to squidge a folder of files (option 1)' print 'or unsquidge a file to a folder (option 2)?' getoption() def getoption(): #squidge or unsquidge? option = raw_input('Please enter 1 or 2 --> ') if option == '1' or option == '2': getdir(option) else: print '\nYou must enter the digit 1 or the digit 2.' getoption() def pathcheck(path, option): # is the working folder real? pathcheck = os.path.exists(path) if not pathcheck: print '\nThe folder path does not exist. Please re-enter.' getdir(option) if pathcheck: pass def getparam(variable, prompt): # general text input function variable = raw_input(prompt) return variable def getextn(): # what type of files should we work with? (Only one type (per squidge) currently supported) print '\nPlease enter the relevant file extension. (e.g. htm)' ex = getparam('extn', 'File extension --> ') return ex def getdir(option): # locate the relevant directory if option == '1': print '\nPlease enter the path of the folder containing the files.' path = getparam('path', 'Full path --> ') pathcheck(path,option) ex = getextn() squidge(path, ex) elif option == '2': print '\nPlease enter the path of the folder containing the squidged file.' path = getparam('path', 'Full path --> ') pathcheck(path,option) ex = getextn() unsquidge(path, ex) def squidge(path, ex): # squidge those files! store = '' # glob.glob1 looks into the folder (arg1) for matches to arg2, returns list of matches filenames = glob.glob1(path, '*'+ '.' + ex) if 'squidged.' + ex in filenames: # if there is already a squidged file, don't count it number = len(filenames) - 1 else: number = len(filenames) # number of files to squidge if number == 0: print '\nThere are no .' + ex + ' files in the specified directory.' print 'Please re-enter.' getdir('1') elif number == 1: print '\nThere is only ' + `number` + ' files in the specified directory.' print 'squidging one file will do nothing. Lets just forget it, shall we?' else: print '\nThere are ' + `number` + ' squidgable files in this directory with the extension ' + ex + '.' print 'The squidged file will appear in the same folder as the files.' print store = makestore(filenames, ex, path) # create the single string consisting of the wrapped individual files name = 'squidged.' + ex # name of squidged output file writefile(store, name, path) # write the content into a new file adjacent to the originals print 'Squidge completed successfully.' # welcome() # uncomment this line to restart on completion of squidge def makestore(filenames, extn, path): # create the single string consisting of the wrapped individual files filedict = {} store = '' for file in filenames: if not file == ('squidged.' + extn): inputfile = os.path.join(path, file) infile = open(inputfile, 'r') content = infile.read() end = '\n\n' filedict[file] = content else: pass thekeys = filedict.keys() thekeys.sort() # sort keys to order the files in the squidged file as you would expect. for file in thekeys: start = '\n\n\n' store = store + '\n' + start + filedict[file] + end store = xml_header + store + xml_footer # optional XML wrapping return store def writefile(content, filename, folder): # output content to a file called filename in named folder output = open(os.path.join(folder, filename), 'w') output.write(content) output.close() # the unsquidge function def unsquidge(path, ex): name = 'squidged.' + ex sourcefile = open(os.path.join(path, name), 'r') sourcelist = sourcefile.readlines() i = 0 startlist = [] endlist = [] filesdict = {} for item in sourcelist: if item == '\012': startlist.append(i) elif item == '\012': endlist.append(i) i = i + 1 startlen = len(startlist) endlen = len(endlist) if not startlen == endlen: print 'There is a missing file start or end tag.' print 'File cannot be processed. Sorry.' else: for i in range(startlen): filestart = startlist[i] + 1 fileend = endlist[i] filesdict[sourcelist[filestart]]=(sourcelist[filestart:fileend]) output(path, filesdict) def output(path, filesdict): try: outfolder = os.path.join(path, 'unsquidged') os.mkdir(outfolder) for nameline in filesdict.keys(): filename = nameline[19:-4] content = listtostring(filesdict[nameline][1:]) writefile(content, filename, outfolder) print '\nUnsquidge completed successfully.' print 'The unsquidged files will appear in a folder called "unsquidged" adjacent to the original files.' except OSError: print "It looks like there is already a folder of unsquidged files in that location. Please rename or delete it." def listtostring(contentlist): string = '' length = len(contentlist) for i in range(length): string = string + contentlist[i] return string welcome()