Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds recursive site init feature loading.

Adds the current blogofile version to the blog controller in this fashion.
  • Loading branch information...
commit e52d9c68ca6bcac5447e06e508e375958e9851d6 1 parent 2cd6eef
@EnigmaCurry authored
View
45 blogofile/site_init/__init__.py
@@ -5,9 +5,11 @@
import logging
import shutil
import imp
+import traceback
+from .. import __version__ as bf_version
logger = logging.getLogger("blogofile.site_init")
-
+
from .. import util
available_sites = [
@@ -25,7 +27,11 @@
extra_features = {
"simple_blog": ["blog_features"],
"simple_html5_blog": ["blog_features", "html5_blog_features"],
- "blog_unit_test":["blog_features"]
+ "blog_unit_test":["blog_features"],
+ "blog_features":[(util.rewrite_strings_in_files,
+ {"existing_string":"@BLOGOFILE_VERSION_REPLACED_HERE@",
+ "replacement_string":bf_version,
+ "paths":["_controllers/blog/__init__.py"]})]
}
all_sites = list(available_sites)
@@ -33,7 +39,6 @@
site_modules = dict((x[0], x[2]) for x in all_sites)
-
def zip_site_init(): #pragma: no cover .. only used by setuptools
"""Zip up all of the subdirectories of site_init
@@ -59,7 +64,6 @@ def zip_site_init(): #pragma: no cover .. only used by setuptools
finally:
os.chdir(curdir)
-
def do_help(): #pragma: no cover
print("Available site templates:\n")
for meta in available_sites:
@@ -69,20 +73,24 @@ def do_help(): #pragma: no cover
print("For example, create a simple site, with a blog, and no theme:\n")
print(" blogofile init simple_blog\n")
-
-def import_site_init(name):
+def import_site_init(feature):
"""Copy a site_init template to the build dir.
- site_init templates can be of three forms:
+ site_init templates can be of four forms:
1) A directory
2) A zip file
3) A .py file
+ 4) A function and args tuple
Directories are usually used in development,
whereas zip files are used in production.
- .py files are special in that they control how to initialize a site,
+ .py files and functions are special in that they control how to initialize a site,
for example, pulling from a git repository."""
#If the directory exists, just use that.
- path = os.path.join(os.path.split(__file__)[0], name)
+ if type(feature) == tuple:
+ feature[0](**feature[1])
+ return
+
+ path = os.path.join(os.path.split(__file__)[0], feature)
if os.path.isdir(path):
logger.info(u"Initializing site from directory: " + path)
for root, dirs, files in os.walk(path):
@@ -98,8 +106,8 @@ def import_site_init(name):
mod.do_init()
#Otherwise, load it from the zip file
else:
- logger.info("Initializing site from zip file")
- zip_data = pkgutil.get_data("blogofile.site_init", name + ".zip")
+ logger.info("Initializing feature from zip file: {0}".format(feature))
+ zip_data = pkgutil.get_data("blogofile.site_init", feature + ".zip")
zip_file = zipfile.ZipFile(StringIO.StringIO(zip_data))
for name in zip_file.namelist():
if name.endswith('/'):
@@ -109,7 +117,14 @@ def import_site_init(name):
f = open(name, 'wb')
f.write(zip_file.read(name))
f.close()
-
+ #Recursively import child features of this feature
+ try:
+ child_features = extra_features[feature]
+ except KeyError:
+ pass
+ else:
+ for child_feature in child_features:
+ import_site_init(child_feature)
def do_init(args):
if not args.SITE_TEMPLATE: #pragma: no cover
@@ -127,9 +142,3 @@ def do_init(args):
args.SITE_TEMPLATE))
template = site_modules[args.SITE_TEMPLATE]
import_site_init(template)
- try:
- for feature in extra_features[template]:
- import_site_init(feature)
- except KeyError: #pragma: no cover
- pass
-
View
1  blogofile/site_init/blog_features/_controllers/blog/__init__.py
@@ -12,6 +12,7 @@
config = {
"name": "Blog",
"description": "Creates a Blog",
+ "version": "@BLOGOFILE_VERSION_REPLACED_HERE@",
"priority": 90.0,
#Posts
View
8 blogofile/util.py
@@ -3,6 +3,7 @@
import sys
from urlparse import urlparse
import logging
+import fileinput
from cache import bf
bf.util = sys.modules['blogofile.util']
@@ -162,6 +163,13 @@ def recursive_file_list(directory, regex=None):
else:
yield os.path.join(root, f)
+def rewrite_strings_in_files(existing_string, replacement_string, paths):
+ """Replace existing_string with replacement_string
+ in all the files listed in paths"""
+ for line in fileinput.input(paths,inplace=True):
+ line=line.replace(existing_string,replacement_string)
+ #inplace=True redirects sys.stdout back to the file
+ sys.stdout.write(line)
def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): #pragma: no cover
"""
Please sign in to comment.
Something went wrong with that request. Please try again.