pnbp

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 877b858dded5f6f0de9b535e67472286fc0839cd
parent fd3962aa7064361ad6011ed05feb27b73c859387
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Sun Aug 17 01:30:28 2014

refactored again

Diffstat:
 README.md        |   4 +-
 install.sh       |   2 +-
 src/build.py     | 205 +--------------------------------------------------------
 src/buildsite.py |   1 +-
 src/core.py      | 162 ++++++++++++++++++++++++++++++++++++++++++++-
 src/main.py      |  56 +++++++++++++++-
 6 files changed, 221 insertions(+), 209 deletions(-)

diff --git a/README.md b/README.md @@ -26,8 +26,6 @@ $ sudo sh install.sh #Basic usage -In the directory of a configured site, run the `build` command to generate the site into the `site/` directory. - -To specify the destination directory, pass the path in as an arguement of the `build [path]` command. +To set up a basic site, run `build -i` For information regarding configration documentation, please refer to the [documentation](http://pnbp.nanner.co). diff --git a/install.sh b/install.sh @@ -1,4 +1,4 @@ #!/bin/bash cp -r src /usr/local/bin/pnbp -ln -s /usr/local/bin/pnbp/build.py /usr/local/bin/build +ln -s /usr/local/bin/pnbp/main.py /usr/local/bin/build diff --git a/src/build.py b/src/build.py @@ -1,205 +0,0 @@ -#!/usr/bin/python -''' -' pnbp - pnbp is not a blogging platform -' build.py -' Paul Longtine - paullongtine@gmail.com -' -' For documentation, please visit http://static.nanner.co/pnbp -''' -#Core imports -import os, sys, shutil, json, yaml, time, traceback - -#Helper imports -import module -from buildsite import * -from functions import * -from initbasic import * - -# Adds in variables defined in pages.json -# -# t = raw template, var = "pagevar" variables in pages.json (<pagename> -> "pagevar") -def generateTemplate(t,var,page): - if page == "index": - page = "" - - t = t.replace("%page%",page) - - t = runInlineScript(t,page) - - for search,replace in var.items(): - if search[0] == ":": - try: - t.index("%"+search+"%") - exists = True - - except: - exists = False - - if exists: - inc = file(replace).read() - inc = generateTemplate(inc,var,page) - t = t.replace("%"+search+"%",inc) - - else: - t = t.replace("%"+search+"%",replace) - - return t - -#Takes all code blocks in templates ("{:print("Hi"):}") and executes it, and replaces the block with the "returns" variable -def runInlineScript(template,page): - exists = True - while exists: - try: - index = template.index("{:")+2 - findex = index - exists = True - - except: - exists = False - - if exists: - script = "" - while template[index:index+2] != ":}": - script = script + template[index] - index += 1 - - returns = "" - exec(script) - template = template.replace(template[findex-2:index+2],returns) - - return template - -# Runs modules defined in pages.json -# -# t = raw template, var = "pagemod" variables in pages.json (<pagename> -> "pagemod") -def runMod(t,var,page): - subpage = {} - for name, mdata in var['pagemod'].items(): - if mdata['mod'] != "page": - try: - #Runs module specified in settings - subpage.update(getattr(module,mdata['mod']).getPages(t,mdata['settings'],name,page)) - - except Exception,e: - print("Error occured at {} using module {}:".format(page,mdata['mod'])) - if type(e) == KeyError: - print("Missing attribute {}".format(e)) - sys.exit() - - else: - print(e) - - elif mdata['mod'] == "page": - #Built-in module page, takes configuration settings and builds a page at a location - if 'settings' in mdata: - try: - if 'template' in mdata['settings']: - template = file(mdata['settings']['template']).read() - - except: - print("Error occured at {} using module page".format(page)) - print("Cannot open file {}".format(mdata['settings']['template'])) - sys.exit() - else: - template = t - - if 'pagevar' in var: - pv = var['pagevar'] - if 'settings' in mdata: - if 'pagevar' in mdata['settings']: - pv.update(mdata['settings']['pagevar']) - - template = generateTemplate(template,pv,name) - - else: - template = runInlineScript(template,name) - - if not 'settings' == mdata: - t = {'default':template} - - else: - if 'location' in mdata: - t = {mdata['settings']['location']:{'default':template}} - - subpage.update(t) - - return subpage - -def build(bd): - site = {} - - #Loops through defined "sites" - for name,v in pagedata.items(): - try: - template = file(v['template']).read() - - except: - print("{}: Can't open file '{}'".format(name,v['template'])) - sys.exit() - - if 'pagevar' in v: - template = generateTemplate(template,v['pagevar'],name) - else: - template = runInlineScript(template,name) - - site[name] = runMod(template,v,name) - - buildSite(site,bd) - -if __name__ == "__main__": - buildDir = "site/" - init = False - - if len(sys.argv) > 1: - for i in sys.argv: - if i[0] != "-" and i != sys.argv[0]: - buildDir = i - - elif i == "--init" or i == "-i": - init = True - - elif i == "-d": - os.chdir(sys.argv.pop(sys.argv.index(i)+1)) - - elif i == "--help": - print("Usage: build [OPTION(s)]... [DIR]...\n" - "Build site in DIR using configuration in pwd\n" - "\n" - " -d DIR Use configuration in DIR, when not specified DIR is 'site/'\n" - " -i, --init Make a new site using the bare minimium config and build it in DIR\n" - " --help Display this help and exit\n" - "\n") - - sys.exit() - - elif i != sys.argv[0]: - print("Unknown option: {}".format(i)) - - if init: - init() - - print("Going through pages...") - start = time.time() - try: - pages = open("pages.yml") - - except: - print("Can't open file 'pages.yml'") - sys.exit() - - pagedata = yaml.load(pages) - pages.close() - - try: build(buildDir) - except Exception,e: - if type(e) == KeyError: - print("Missing or mistyped value: {}".format(e)) - - else: - print("Something went wrong...") - print(e) - - traceback.print_exc(file=sys.stdout) - sys.exit() - - print("Finished in {} ms.".format((time.time()-start)*1000)) diff --git a/src/buildsite.py b/src/buildsite.py @@ -8,6 +8,7 @@ import os, shutil # Builds the site off of a filestructure dictionary. +#site = dict of site directory tree/pages, loc = root of site def buildSite(site,loc): try: shutil.rmtree(loc) diff --git a/src/core.py b/src/core.py @@ -0,0 +1,162 @@ +''' +' pnbp - pnbp is not a blogging platform +' core.py +' Paul Longtine - paullongtine@gmail.com +' +' For documentation, please visit http://static.nanner.co/pnbp +''' +#Core imports +import os, sys, json, yaml + +#Helper imports +import module +from buildsite import * +from functions import * + +#Global variables + +pages = "" +pagedata = {} + +# Adds in variables defined in pages.json +# +# t = raw template, var = "pagevar" variables in pages.json (<pagename> -> "pagevar") +def generateTemplate(t,var,page): + if page == "index": + page = "" + + t = t.replace("%page%",page) + + t = runInlineScript(t,page) + + for search,replace in var.items(): + if search[0] == ":": + try: + t.index("%"+search+"%") + exists = True + + except: + exists = False + + if exists: + inc = file(replace).read() + inc = generateTemplate(inc,var,page) + t = t.replace("%"+search+"%",inc) + + else: + t = t.replace("%"+search+"%",replace) + + return t + +#Takes all code blocks in templates ("{:print("Hi"):}") and executes it, and replaces the block with the "returns" variable +def runInlineScript(template,page): + exists = True + while exists: + try: + index = template.index("{:")+2 + findex = index + exists = True + + except: + exists = False + + if exists: + script = "" + while template[index:index+2] != ":}": + script = script + template[index] + index += 1 + + returns = "" + exec(script) + template = template.replace(template[findex-2:index+2],returns) + + return template + +# Runs modules defined in pages.json +# +# t = raw template, var = "pagemod" variables in pages.json (<pagename> -> "pagemod") +def runMod(t,var,page): + subpage = {} + for name, meta in var['pagemod'].items(): + if meta['mod'] != "page": + if not "settings" in meta: meta['settings'] = {} + try: + #Runs module specified in settings + subpage.update( + getattr(module, meta['mod']).getPages( + t, meta['settings'], name, page + ) + ) + + except Exception,e: + print("Error occured at {} using module {}:".format(page,meta['mod'])) + if type(e) == KeyError: + print("Missing attribute {}".format(e)) + sys.exit() + + else: + print(e) + + elif meta['mod'] == "page": + #Built-in module page, takes configuration settings and builds a page at a location + if 'settings' in meta: + try: + if 'template' in meta['settings']: template = file(meta['settings']['template']).read() + + except: + print("Error occured at {} using module page".format(page)) + print("Cannot open file {}".format(meta['settings']['template'])) + sys.exit() + else: + template = t + + if 'pagevar' in var: + if 'settings' in meta: + if 'pagevar' in meta['settings']: + var['pagevar'].update(meta['settings']['pagevar']) + + template = generateTemplate(template,var['pagevar'],name) + + else: + template = runInlineScript(template,name) + + if not 'settings' == meta: + t = {'default':template} + + else: + if 'location' in meta: t = {meta['settings']['location']:{'default':template}} + + subpage.update(t) + + return subpage + +def build(bd): + global pages, pagedata + + #Try to get the config + try: pages = file("pages.yml") + except: + print("Can't open file 'pages.yml'") + sys.exit() + + pagedata = yaml.load(pages) + + site = {} + #Loops through defined "sites" + for name,v in pagedata.items(): + #Read the template + try: template = file(v['template']).read() + except: + print("{}: Can't open file '{}'".format(name,v['template'])) + sys.exit() + + #Check if pagevar is defined, skip the variable replacement step + if 'pagevar' in v: + template = generateTemplate(template,v['pagevar'],name) + + else: + template = runInlineScript(template,name) + + site[name] = runMod(template,v,name) + + buildSite(site,bd) diff --git a/src/main.py b/src/main.py @@ -0,0 +1,56 @@ +#!/usr/bin/python +''' +' pnbp - pnbp is not a blogging platform +' main.py +' Paul Longtine - paullongtine@gmail.com +' +' For documentation, please visit http://static.nanner.co/pnbp +''' +import os, sys, traceback, time, core +from initbasic import * + +#CLI Interface function +#args = list of command line arguementsn +def cli(args): + bd = "site/" + if len(args) > 1: + for i in args: + if i[0] != "-" and args.index(i) != 0: + bd = i + + elif i == "-d": + os.chdir(args.pop(args.index(i)+1)) + + elif i == "--help": + print("Usage: build [OPTION(s)]... [DIR]...\n" + "Build site in DIR using configuration in pwd\n" + "\n" + " -d DIR Use configuration in DIR, when not specified DIR is 'site/'\n" + " -i, --init Make a new site using the bare minimium config and build it in DIR\n" + " --help Display this help and exit\n") + + sys.exit() + + elif 0 != args.index(i): + print("Unknown option: {}".format(i)) + + if "--init" in args or "-i" in args: + init() + + return bd + +if __name__ == "__main__": + #Save the time for the caluation + start = time.time() + + #Try to build the site + try: core.build(cli(sys.argv)) + except: + print("Something went wrong...") + + traceback.print_exc(file=sys.stdout) + sys.exit() + #Print the time it took for the calculation + print("Finished in {} ms.".format((time.time()-start)*1000)) + +