Permalink
Browse files

Refactored tools

  • Loading branch information...
1 parent add672e commit f6388f81db807a9b56afa0263f20216d4d254f91 @bjornua bjornua committed Aug 23, 2011
@@ -0,0 +1 @@
+# -*- coding: utf-8 -*-
View
@@ -1,11 +1,4 @@
-#!/usr/bin/python2
# -*- coding: utf-8 -*-
-import sys
-import os
-sys.path[0] = os.path.join(os.path.dirname(__file__), "..")
-sys.path.insert(0, os.path.join(sys.path[0], "3rd-party"))
-os.chdir(sys.path[1])
-
import app.model.user as user
from app.config.generated import config
View
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+_toolname = "add_users"
+_tooldesc = "Adds a hard-coded list of users defined in app/tool/add_users.py"
+
+import app.model.user as user
+
+from app.config.generated import config
+from random import choice
+
+def generate_password(chars="ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789", length=10):
+ return "".join(choice(chars) for x in range(length))
+
+def main():
+ for rustur, vejledere in (
+ ("lerustour", (
+ "alexandra",
+ "bjorn",
+ "efs",
+ "hk",
+ "jenny",
+ "lund",
+ "mark",
+ "sdahlgaard"
+ )),
+ ("diwinger", (
+ "ahmed",
+ "davy",
+ "henrik",
+ "kirstejn",
+ "jens",
+ "caro",
+ "martin",
+ "soren",
+ "steph"
+ )),
+ ("prinsesseturen", (
+ "christina",
+ "daniel",
+ "ejnar",
+ "kristoffer",
+ "lasse",
+ "peter",
+ "martin",
+ "markus",
+ )),
+ ):
+ for vejleder in vejledere:
+ password = generate_password()
+ user.add(vejleder, rustur, password)
+ print "Brugernavn: %s" % (vejleder,)
+ print "Password: %s" % (password,)
+ print "-"*40
@@ -1,11 +1,11 @@
-#!/usr/bin/python2
# -*- coding: utf-8 -*-
+_toolname = "config"
+_tooldesc = "Interactively generates the project config files."
+
import os.path
import sys
import grp
-sys.path[0] = os.path.join(os.path.dirname(__file__), "..")
-
def user_query(itemname, converter, defaultrep, default=None):
while True:
if default == None:
@@ -36,7 +36,7 @@ def prompt_update_config():
return config
def write_config(config):
- filename = os.path.join(os.path.dirname(__file__), "..", "app", "config", "generated.py")
+ filename = os.path.join("app", "config", "generated.py")
fhandle = open(filename, "w")
fhandle.write(
"# -*- coding: utf-8 -*-\n"
@@ -45,6 +45,6 @@ def write_config(config):
+ "config.update(" + repr(config) + ")"
)
-if __name__ == "__main__":
+def main():
config = prompt_update_config()
write_config(config)
View
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+import sys
+
+_toolname = u"help"
+_tooldesc = u"This command :P"
+
+def main():
+ import app.tool.main as main_
+ if len(sys.argv) < 3:
+ name = ""
+ else:
+ name = sys.argv[2]
+
+ toolsfound = 0
+ for t in main_.tools:
+ if not t._toolname.startswith(name):
+ continue
+ toolsfound += 1
+
+ print "* %s - %s" % (t._toolname, t._tooldesc)
+
+ if toolsfound == 0:
+ sys.stderr.write("Command(s) not found\n")
+ sys.stderr.flush()
+
+
View
@@ -0,0 +1,44 @@
+import sys
+
+import app.tool.add_users as add_users
+import app.tool.help as help_
+import app.tool.config_setup as config_setup
+import app.tool.update_database as update_database
+
+tools = [
+ add_users,
+ config_setup,
+ help_,
+ update_database
+]
+
+
+def main():
+ if len(sys.argv) < 2:
+ sys.stderr.write(u"You didn't specify a command\n")
+ sys.stderr.flush()
+ return
+
+ name = sys.argv[1]
+
+ possibletools = [t for t in tools if t._toolname.startswith(name)]
+
+ if len(possibletools) > 1:
+ sys.stderr.write(u"Ambiguous command %s could be:\n" %(name, ))
+ for tool in possibletools:
+ sys.stderr.write(u"* %s\n" % (tool._toolname,))
+ sys.stderr.flush()
+ return
+
+ if len(possibletools) < 1:
+ sys.stderr.write("Could't find command %s\n" % (name,))
+ sys.stderr.flush()
+ return
+
+ tool, = possibletools
+
+ tool.main()
+
+
+if __name__ == "__main__":
+ main()
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+_toolname = "push_views"
+_tooldesc = "Pushes whatever CouchDB javascript views it can find in ./views"
+
+import couchdb
+from itertools import groupby
+
+from app.config.generated import config
+from app.utils.folder import get_files
+
+import os
+import os.path as path
+
+def main():
+ try:
+ server = couchdb.Server(config["couchdb_server_url"])
+ db = server[config["couchdb_db"]]
+ except couchdb.ResourceNotFound:
+ db = server.create(config["couchdb_db"])
+ except:
+ print "Couldn't connect to couchdb."
+ raise SystemExit
+
+ views = []
+ for file in get_files(directory="views"):
+ content = open(file).read()
+ designdoc = os.path.split(os.path.split(os.path.split(file)[0])[0])[1]
+ viewname = os.path.split(os.path.split(file)[0])[1]
+ type = os.path.split(file)[1][:-3]
+ views += [(designdoc, type, viewname, content)]
+ views.sort()
+
+ docs = []
+ for designdoc, views in groupby(views, lambda val: val[0]):
+ try:
+ doc = db["_design/" + designdoc]
+ except couchdb.ResourceNotFound:
+ doc = {}
+ exists = False
+ else:
+ exists = True
+ doc["_id"] = "_design/" + designdoc
+ doc["language"] = "javascript"
+ doc["views"] = {}
+ for designdoc, type, viewname, content in views:
+ if not viewname in doc["views"]:
+ doc["views"][viewname] = {}
+ doc["views"][viewname].update({type: content})
+
+ if not exists or db[doc["_id"]] != doc:
+ print "Updating", designdoc + ",", viewname + ",", type
+ db[doc["_id"]] = doc
View
@@ -14,17 +14,20 @@
from app.config.generated import config
path = {}
-path["root"] = os.path.join(os.path.dirname(__file__), "..")
-path["static"] = os.path.join(path["root"], "../static")
-path["templates"] = os.path.join(path["root"], "../templates")
+path["static"] = "static"
+path["templates"] = "templates"
local = werkzeug.Local()
local_manager = werkzeug.LocalManager([local])
application = local("application")
-db_ = couchdb.Server(config["couchdb_server_url"])[config["couchdb_db"]]
+_db = None
def db():
- return db_
+ global _db
+ if _db is None:
+ _db = couchdb.Server(config["couchdb_server_url"])[config["couchdb_db"]]
+ return _db
+
template_lookup = mako.lookup.TemplateLookup(
directories=[path["templates"]],
View
@@ -1,4 +1,3 @@
-#!/usr/bin/python2
# -*- coding: utf-8 -*-
import sys
from os.path import dirname, join, realpath
View
@@ -0,0 +1,9 @@
+#!/usr/bin/python2
+# -*- coding: utf-8 -*-
+import env
+
+from app.tool.main import main
+
+if __name__:
+ main()
+
View
@@ -1,55 +0,0 @@
-#!/usr/bin/python2
-# -*- coding: utf-8 -*-
-import sys
-import os
-sys.path[0] = os.path.join(os.path.dirname(__file__), "..")
-sys.path.insert(0, os.path.join(sys.path[0], "3rd-party"))
-os.chdir(sys.path[1])
-
-import app.model.user as user
-
-from app.config.generated import config
-from random import choice
-
-def generate_password(chars="ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789", length=10):
- return "".join(choice(chars) for x in range(length))
-
-for rustur, vejledere in (
- ("lerustour", (
- "alexandra",
- "bjorn",
- "efs",
- "hk",
- "jenny",
- "lund",
- "mark",
- "sdahlgaard"
- )),
- ("diwinger", (
- "ahmed",
- "davy",
- "henrik",
- "kirstejn",
- "jens",
- "caro",
- "martin",
- "soren",
- "steph"
- )),
- ("prinsesseturen", (
- "christina",
- "daniel",
- "ejnar",
- "kristoffer",
- "lasse",
- "peter",
- "martin",
- "markus",
- )),
-):
- for vejleder in vejledere:
- password = generate_password()
- user.add(vejleder, rustur, password)
- print "Brugernavn: %s" % (vejleder,)
- print "Password: %s" % (password,)
- print "-"*40
@@ -1,62 +0,0 @@
-#!/usr/bin/python2
-# -*- coding: utf-8 -*-
-import sys
-import os
-sys.path[0] = os.path.join(os.path.dirname(__file__), "..")
-sys.path.insert(0, os.path.join(sys.path[0], "3rd-party"))
-os.chdir(sys.path[1])
-
-
-import couchdb
-from itertools import groupby
-
-from app.config.generated import config
-
-
-try:
- server = couchdb.Server(config["couchdb_server_url"])
- db = server[config["couchdb_db"]]
-except couchdb.ResourceNotFound:
- db = server.create(config["couchdb_db"])
-except:
- print "Couldn't connect to couchdb."
- raise SystemExit
-
-def get_files(directory="."):
- dir = os.listdir(directory)
- dir = [os.path.join(directory, entry) for entry in dir]
- files = filter(os.path.isfile, dir)
- directories = filter(os.path.isdir, dir)
- for dir in directories:
- files += get_files(dir)
- return files
-
-views = []
-for file in get_files(directory="views"):
- content = open(file).read()
- designdoc = os.path.split(os.path.split(os.path.split(file)[0])[0])[1]
- viewname = os.path.split(os.path.split(file)[0])[1]
- type = os.path.split(file)[1][:-3]
- views += [(designdoc, type, viewname, content)]
-views.sort()
-
-docs = []
-for designdoc, views in groupby(views, lambda val: val[0]):
- try:
- doc = db["_design/" + designdoc]
- except couchdb.ResourceNotFound:
- doc = {}
- exists = False
- else:
- exists = True
- doc["_id"] = "_design/" + designdoc
- doc["language"] = "javascript"
- doc["views"] = {}
- for designdoc, type, viewname, content in views:
- if not viewname in doc["views"]:
- doc["views"][viewname] = {}
- doc["views"][viewname].update({type: content})
-
- if not exists or db[doc["_id"]] != doc:
- print "Updating", designdoc + ",", viewname + ",", type
- db[doc["_id"]] = doc

0 comments on commit f6388f8

Please sign in to comment.