Permalink
Browse files

Restructure chrysoberyl tool to use sub-commands.

  • Loading branch information...
1 parent f1376cc commit 7be1bae9f83e1e4811756d8f3713a00856381098 @cpressey cpressey committed Sep 16, 2012
Showing with 188 additions and 138 deletions.
  1. +12 −0 bin/chrysoberyl
  2. +2 −6 data/documentation.yaml
  3. +0 −132 src/chrysoberyl.py
  4. +174 −0 src/chrysoberyl/commands.py
View
12 bin/chrysoberyl
@@ -0,0 +1,12 @@
+#!/usr/bin/env python
+
+from os.path import realpath, dirname, join
+import sys
+
+sys.path.insert(0, join(dirname(realpath(sys.argv[0])), '..', 'src'))
+
+from chrysoberyl import commands
+
+
+if __name__ == '__main__':
+ commands.perform(sys.argv[1:])
View
8 data/documentation.yaml
@@ -46,11 +46,8 @@ Documentation Index:
Cabra distribution:
- doc/cabra.html
Cfluviurrh distribution:
- - output.txt
- - one.txt
- LICENSE
- README.markdown
- - zero.txt
Circute distribution: []
Console::Virtual distribution:
- readme.txt
@@ -75,6 +72,7 @@ Documentation Index:
- README.markdown
- UNLICENSE
Dungeons of Ekileugor distribution:
+ - LICENSE
- README.markdown
Eightebed distribution:
- LICENSE
@@ -122,7 +120,6 @@ Documentation Index:
- 4. Open Sores Illgol##/doc/poundpoundreadme.txt
- 4. Open Sores Illgol##/doc/i2readme.txt
Iphigeneia distribution:
- - foo.txt
- README.markdown
Jaccia and Jacciata distribution:
- README.markdown
@@ -254,8 +251,7 @@ Documentation Index:
- doc/bootstrp.txt
Squishy2K distribution:
- README.txt
- Strelnokoff distribution:
- - index.html
+ Strelnokoff distribution: []
Super Wumpus Land distribution: []
TPiS distribution:
- LICENSE
View
132 src/chrysoberyl.py
@@ -1,132 +0,0 @@
-#!/usr/bin/env python
-# encoding: UTF-8
-
-"""
-A script to process the Chrysoberyl database.
-
-If no options are given, the database will simply be loaded and checked
-for consistency.\
-"""
-
-import codecs
-import json
-from optparse import OptionParser
-import os
-import shutil
-import sys
-
-import yaml
-try:
- from yaml import CLoader as Loader
-except ImportError:
- from yaml import Loader
-
-from chrysoberyl.checker import check_chrysoberyl_data, ApproximateDate
-from chrysoberyl.feed import make_news_feed
-from chrysoberyl.renderer import convert_chrysoberyl_data, Renderer
-from chrysoberyl.localrepos import troll_docs, survey_repos, test_repos
-
-
-def load_chrysoberyl_dir(dirname):
- data = {}
-
- count = 0
- for root, dirnames, filenames in os.walk(dirname):
- for filename in filenames:
- if filename.endswith('.yaml'):
- count += 1
- file = open(os.path.join(root, filename))
- data.update(yaml.load(file, Loader=Loader))
- file.close()
- del dirnames[:] # don't automatically descend
-
- print "%d files read." % count
- return data
-
-
-def transform_dates(thing):
- if isinstance(thing, ApproximateDate):
- return thing.stamp()
- elif isinstance(thing, list):
- return [transform_dates(x) for x in thing]
- elif isinstance(thing, dict):
- return dict([(k, transform_dates(v)) for k, v in thing.iteritems()])
- else:
- return thing
-
-
-def jsonify(data):
- return transform_dates(data)
-
-
-if __name__ == '__main__':
- optparser = OptionParser("[python] chrysoberyl.py {options}\n" + \
- __doc__)
- optparser.add_option("-c", "--clone-dir",
- dest="clone_dir", metavar='DIR', default='..',
- help="specify location of the hg clones "
- "of reference distributions (default: ..)")
- optparser.add_option("-d", "--data-dir",
- dest="data_dir", metavar='DIR', default='data',
- help="specify location of the Chrysoberyl "
- "Yaml data files (default: data)")
- optparser.add_option("-l", "--check-links",
- dest="check_links", action='store_true',
- default=False,
- help="check all web links for sanity "
- "(no 404's, etc.)")
- optparser.add_option("-o", "--output-to",
- dest="output_to", metavar='DIR',
- help="render all nodes (as HTML5 files) and "
- "write them into the given directory")
- optparser.add_option("--script-dir",
- dest="script_dir", metavar='DIR',
- help="write scripts into the given directory")
- optparser.add_option("--feed-dir",
- dest="feed_dir", metavar='DIR',
- help="write feeds into the given directory")
- optparser.add_option("--troll-docs",
- dest="troll_docs", action='store_true',
- default=False,
- help="assume that hg clones of reference "
- "distributions are available in DIR, and look "
- "through each reference distribution for "
- "anything that looks like documentation, and "
- "update documentation.yaml with those "
- "filenames.")
- optparser.add_option("--survey-repos",
- dest="survey_repos", action='store_true',
- default=False)
- (options, args) = optparser.parse_args(sys.argv[1:])
-
- print "Loading Chrysoberyl data..."
- data = load_chrysoberyl_dir(options.data_dir)
- check_chrysoberyl_data(data)
-
- if options.check_links:
- raise NotImplementedError
-
- if options.survey_repos:
- survey_repos(data, options.clone_dir)
- sys.exit(0)
-
- if options.troll_docs:
- troll_docs(data, options.clone_dir, options.data_dir)
- print "Re-run to incorporate docs in loaded data."
- sys.exit(0)
-
- if options.output_to:
- filename = os.path.join(options.output_to, 'chrysoberyl.json')
- with codecs.open(filename, 'w', 'utf-8') as file:
- json.dump(jsonify(data), file, encoding='utf-8', default=unicode)
- convert_chrysoberyl_data(data)
- r = Renderer(data, 'templates', options.output_to)
- r.render_chrysoberyl_data()
- for filename in ['chrysoberyl-query.js']:
- shutil.copy(os.path.join('static', filename),
- options.script_dir)
-
- if options.feed_dir:
- make_news_feed(data, options.feed_dir, 'atom_15_news.xml', limit=15)
- make_news_feed(data, options.feed_dir, 'atom_30_news.xml', limit=30)
- make_news_feed(data, options.feed_dir, 'atom_all_news.xml')
View
174 src/chrysoberyl/commands.py
@@ -0,0 +1,174 @@
+# encoding: UTF-8
+
+import codecs
+import json
+from optparse import OptionParser
+import os
+import shutil
+import sys
+
+import yaml
+try:
+ from yaml import CLoader as Loader
+except ImportError:
+ from yaml import Loader
+
+from chrysoberyl.checker import check_chrysoberyl_data, ApproximateDate
+from chrysoberyl.feed import make_news_feed
+from chrysoberyl.renderer import convert_chrysoberyl_data, Renderer
+from chrysoberyl.localrepos import troll_docs, survey_repos, test_repos
+
+
+def check(args, optparser):
+ """Check the Chrysoberyl database for consistency.
+
+ """
+ options, args = optparser.parse_args(args)
+ data = load_and_check(options.data_dir)
+ return 0
+
+
+def survey(args, optparser):
+ """Survey repos.
+
+ """
+ options, args = optparser.parse_args(args)
+ data = load_and_check(options.data_dir)
+ survey_repos(data, options.clone_dir)
+ return 0
+
+
+def troll(args, optparser):
+ """Troll local hg clones of distributions listed in Chrysoberyl,
+ looking through each distribution for anything that looks like
+ documentation, and update documentation.yaml with those
+ filenames.
+
+ """
+ options, args = optparser.parse_args(args)
+ data = load_and_check(options.data_dir)
+ troll_docs(data, options.clone_dir, options.data_dir)
+ print "Re-run to incorporate docs in loaded data."
+ return 0
+
+
+def render(args, optparser):
+ """Render all nodes to a set of HTML5 files.
+
+ """
+ optparser.add_option("--node-dir",
+ dest="node_dir", metavar='DIR',
+ help="write rendered nodes into this directory")
+ optparser.add_option("--script-dir",
+ dest="script_dir", metavar='DIR',
+ help="write scripts into this directory")
+ options, args = optparser.parse_args(args)
+ data = load_and_check(options.data_dir)
+ filename = os.path.join(options.node_dir, 'chrysoberyl.json')
+ with codecs.open(filename, 'w', 'utf-8') as file:
+ json.dump(jsonify(data), file, encoding='utf-8', default=unicode)
+ convert_chrysoberyl_data(data)
+ r = Renderer(data, 'templates', options.node_dir)
+ r.render_chrysoberyl_data()
+ for filename in ['chrysoberyl-query.js']:
+ shutil.copy(os.path.join('static', filename),
+ options.script_dir)
+
+
+def announce(args, optparser):
+ """Create news feeds from news item nodes in Chrysoberyl.
+
+ """
+ optparser.add_option("--feed-dir",
+ dest="feed_dir", metavar='DIR',
+ help="write feeds into the given directory")
+ options, args = optparser.parse_args(args)
+ data = load_and_check(options.data_dir)
+ convert_chrysoberyl_data(data)
+ make_news_feed(data, options.feed_dir, 'atom_15_news.xml', limit=15)
+ make_news_feed(data, options.feed_dir, 'atom_30_news.xml', limit=30)
+ make_news_feed(data, options.feed_dir, 'atom_all_news.xml')
+
+
+### helpers ###
+
+def load_chrysoberyl_dir(dirname):
+ data = {}
+
+ count = 0
+ for root, dirnames, filenames in os.walk(dirname):
+ for filename in filenames:
+ if filename.endswith('.yaml'):
+ count += 1
+ file = open(os.path.join(root, filename))
+ data.update(yaml.load(file, Loader=Loader))
+ file.close()
+ del dirnames[:] # don't automatically descend
+
+ print "%d files read." % count
+ return data
+
+
+def transform_dates(thing):
+ if isinstance(thing, ApproximateDate):
+ return thing.stamp()
+ elif isinstance(thing, list):
+ return [transform_dates(x) for x in thing]
+ elif isinstance(thing, dict):
+ return dict([(k, transform_dates(v)) for k, v in thing.iteritems()])
+ else:
+ return thing
+
+
+def jsonify(data):
+ return transform_dates(data)
+
+
+def load_and_check(dirname):
+ print "Loading Chrysoberyl data..."
+ data = load_chrysoberyl_dir(dirname)
+ check_chrysoberyl_data(data)
+ return data
+
+
+### driver ###
+
+COMMANDS = {
+ 'check': check,
+ 'render': render,
+ 'announce': announce,
+ 'troll': troll,
+ 'survey': survey,
+}
+
+
+def usage():
+ sys.stderr.write("Usage: chrysoberyl <command> [args...]\n")
+ sys.stderr.write(" where <command> is one of:\n")
+ for command in sorted(COMMANDS.keys()):
+ sys.stderr.write(" %s\n" % command)
+ sys.exit(1)
+
+
+def perform(args):
+ try:
+ command = args[0]
+ except IndexError:
+ usage()
+ args = args[1:]
+ func = COMMANDS.get(command, None)
+ if func is None:
+ usage()
+ message = "chrysoberyl %s [options]\n\n%s" % (command, func.__doc__)
+ message = message.rstrip()
+ optparser = OptionParser(message)
+ optparser.add_option("-c", "--clone-dir",
+ dest="clone_dir", metavar='DIR', default='..',
+ help="specify location of the hg clones "
+ "of reference distributions (default: ..)")
+ optparser.add_option("-d", "--data-dir",
+ dest="data_dir", metavar='DIR', default='data',
+ help="specify location of the Chrysoberyl "
+ "Yaml data files (default: data)")
+
+ sys.exit(func(args, optparser))

0 comments on commit 7be1bae

Please sign in to comment.