V4maker

From CAC Documentation wiki
Revision as of 20:06, 18 September 2015 by Ad876 (talk | contribs) (Created page with "I got tired of cutting and pasting to create all of the blank pages for VW's, so I made a python script to do it for me. Basically, you just make a blank aspx (I have a base....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

I got tired of cutting and pasting to create all of the blank pages for VW's, so I made a python script to do it for me. Basically, you just make a blank aspx (I have a base.aspx if you want it) file (C# directives and the place holder asp:Content tags) and drop it in the directory. Then set up your TOC.txt with the files you'll need.

Then:

python filemaker.py z:\vwmodule\base.aspx
 Skipping Computational Steering from TOC.txt.
 default.aspx created OK.
 goals.aspx created OK.
 prerequisites.aspx created OK.
 Whatis.aspx created OK.
 types.aspx created OK.
 basicexamples.aspx created OK.
 interactive_example.aspx created OK.
 approaches.aspx created OK.
 architecture.aspx created OK.
 howto_plan.aspx created OK.
 howto_steerfile.aspx created OK.
 howto_app.aspx created OK.
 ...

It's non-destructive, so if a file already exists, it skips it and doesn't overwrite it.

File links: internal: Media:filemaker.py external: at cacvis

source:

import sys
import os
import shutil

def Usage():
    print "python2.5 aspxmaker.py path/to/base.aspx"
    print "   Looks in path/to for default.aspx and TOC.txt"
    print "   and will create all of the aspx files (w/o overwrite)"
    print "   specified in the TOC.txt file"

def createFileFromBase(basedir,newfilename, filetocopy):
    #Check to see if already exists
    fullpath = os.path.join(basedir,newfilename)
    if os.path.exists(fullpath):
        if os.path.isfile(fullpath):
            print "%s already exists, not overwriting" % fullpath
        else:
            print "%s appears to be a directory?!?" % fullpath
        return None
    #should be safe to copy
    try:
        shutil.copy(filetocopy,fullpath)
        if str(os.path.getsize(newfilename)) != str(os.path.getsize(filetocopy)):
            raise Error, "%s isn't the same size as the copied file!" % newfilename
        print "%s created OK." % newfilename
    except IOError:
        print "IOError reported for %s, check permissions, continuing" % newfilename
    return 1

def run(base):
    basedir = os.path.dirname(base)
    fid = open(os.path.join(basedir,'TOC.txt'))
    try:
        for line in fid:
            if line.strip().endswith('.aspx'):
                createFileFromBase(basedir, line.strip(),base)
            else:
                print "Skipping %s from TOC.txt." % line.strip()
    finally:
        fid.close()


if __name__=='__main__':
    if (len(sys.argv) != 2):
        Usage()
        sys.exit(1)
    base = sys.argv[1]
    run(base)