Commits

Nick Stenning committed cef6d3b

Various fixes to command interface and logging throughout the app.

Comments (0)

Files changed (16)

wdmmg/config/middleware.py

     """
     # Configure the Pylons environment
     load_environment(global_conf, app_conf)
-    
-    
-    #assert model.Dataset.find_one({'name': config['pylons.app_globals'].default_dataset}), \
-    #    "Default dataset %s does not exist!" % config['pylons.app_globals'].default_dataset
 
     # The Pylons WSGI app
     app = PylonsApp()
         static_app = StaticURLParser(config['pylons.paths']['static_files'],
                         cache_max_age=max_age)
         static_parsers = [static_app, app]
-        
+
         # Configurable extra static file paths
         extra_public_paths = config.get('extra_public_paths')
         if extra_public_paths:
                                               cache_max_age=max_age) \
                               for public_path in \
                               extra_public_paths.split(' ')] + static_parsers
-        
+
         app = Cascade(static_parsers)
 
     return app

wdmmg/lib/ckan.py

-from pylons import config
 from ckanclient import CkanClient, CkanApiError
 
-openspending_group = config.get('wdmmg.ckan_group', 'openspending')
-base_location = config.get('wdmmg.ckan_location', 'http://ckan.net/api')
-api_key = config.get('wdmmg.ckan_api_key')
+openspending_group = None
+base_location = None
+api_key = None
 
 _client = None
 
 def make_client():
+    from pylons import config
+
+    global openspending_group
+    global base_location
+    global api_key
+
+    openspending_group = config.get('wdmmg.ckan_group', 'openspending')
+    base_location = config.get('wdmmg.ckan_location', 'http://ckan.net/api')
+    api_key = config.get('wdmmg.ckan_api_key')
+
     return CkanClient(base_location=base_location, api_key=api_key)
 
 def get_client():

wdmmg/lib/command/base.py

 import os
+import logging
 
+from paste.deploy import appconfig
 from paste.script import command
 
+from wdmmg.config.environment import load_environment
+
 # Monkeypatch Paste's version of OptionParser to prevent it munging newlines
 # in command descriptions
 command.optparse.OptionParser.format_description = lambda self, d: self.description
 
+log = logging.getLogger('wdmmg')
+
 class WdmmgCommand(command.Command):
     group_name = 'wdmmg'
 
                           help='Config file to use [$REPO_ROOT/development.ini].')
         return parser
 
+    def command(self):
+        self._load_config()
+
+        # With default log level set to WARN, this gives -v == INFO, -vv == DEBUG
+        log.setLevel(max(10, log.getEffectiveLevel() - 10 * self.options.verbose))
+
     def _load_config(self):
-        from paste.script.util.logging_config import fileConfig
-        from paste.deploy import appconfig
-        from wdmmg.config.environment import load_environment
+        config_file = self.options.config
+        self.logging_file_config(config_file)
 
-        if not self.options.config:
-            msg = 'No config file supplied'
-            raise self.BadCommand(msg)
-        self.filename = os.path.abspath(self.options.config)
-        try:
-            fileConfig(self.filename)
-
-        except Exception: pass
-        conf = appconfig('config:' + self.filename)
+        here_dir = os.getcwd()
+        conf = appconfig('config:' + config_file, relative_to=here_dir)
         load_environment(conf.global_conf, conf.local_conf)
 
-    def _setup_app(self):
-        from paste.script.appinstall import SetupCommand
-
-        cmd = SetupCommand('setup-app')
-        cmd.run([self.filename])
+    def _check_args_length(self, n):
+        if len(self.args) != n:
+            self.__class__.parser.print_help()
+            raise self.BadCommand('\nWrong number of arguments: see usage message above!')

wdmmg/lib/command/ckan.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
+        super(CkanCommand, self).command()
+
         if len(self.args) < 2:
             CkanCommand.parser.print_help()
             return 1
 
-        self._load_config()
-
         from wdmmg.lib import ckan
-        from pylons import config
-
-        # Why do I have to do this?
-        ckan.api_key = config.get('wdmmg.ckan_api_key')
 
         self.c = ckan.get_client()
 
 
         if cmd == 'show':
             self._cmd_show()
-        if cmd == 'check':
+        elif cmd == 'check':
             self._cmd_check()
         elif cmd == 'hintadd':
             self._cmd_hintadd()

wdmmg/lib/command/db.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
-        if len(self.args) < 1:
-            DbCommand.parser.print_help()
-            return 1
+        super(DbCommand, self).command()
 
-        self._load_config()
-
-        cmd = self.args.pop(0)
+        cmd = self.args[0] if len(self.args) > 0 else None
 
         if cmd == 'drop':
             self._cmd_drop()
         elif cmd == 'loadexample':
             self._cmd_loadexample()
         else:
-            raise self.BadCommand("Subcommand '%s' not recognized " \
-                                  "by 'db' command!" % cmd)
-
+            DbCommand.parser.print_help()
+            return 1
 
     def _cmd_drop(self):
+        self._check_args_length(1)
         from wdmmg.model.mongo import drop_db
         drop_db()
 
     def _cmd_dropcollections(self):
+        self._check_args_length(1)
         from wdmmg.model.mongo import drop_collections
         drop_collections()
 
     def _cmd_loadexample(self):
+        self._check_args_length(2)
         # TODO: separate the concepts of example/development data and test
         #       fixtures.
         from wdmmg.tests.helpers import load_fixture
-        try:
-            fixture_name = self.args.pop(0)
-            load_fixture(fixture_name)
-        except IndexError:
-            raise self.BadCommand("Subcommand 'loadexample' requires " \
-                                  "fixture name as first argument!")
+        fixture_name = self.args[1]
+        load_fixture(fixture_name)

wdmmg/lib/command/graph.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
+        super(GraphCommand, self).command()
+
         if not nx:
             print "Could not load 'networkx' module, which is needed for graph"\
                   " command.\nHave you tried `pip install networkx`?"
             GraphCommand.parser.print_help()
             return 1
 
-        self._load_config()
         dataset_name, file_name = self.args
 
         g = nx.DiGraph()

wdmmg/lib/command/importer.py

                       help="URL of JSON format metadata.")
 
     def command(self):
-        if len(self.args) != 1:
-            CSVImportCommand.parser.print_help()
-            return 1
+        super(CSVImportCommand, self).command()
+        self._check_args_length(1)
 
         from wdmmg.lib.csvimport import load_dataset
         from wdmmg.lib import json
                       help="Use CKAN to find resource UUID and mapping URL.")
 
     def command(self):
-        if len(self.args) != 1:
-            CKANImportCommand.parser.print_help()
-            return 1
+        super(CKANImportCommand, self).command()
+        self._check_args_length(1)
 
         from wdmmg.lib import ckan, json
 
-        self._load_config()
-
-        package = ckan.Package(self.args.pop(0))
+        package = ckan.Package(self.args[0])
 
         if not self.options.use_ckan_tags:
             if not self.options.mapping:
     parser = WdmmgCommand.standard_parser()
 
     def command(self):
-        if len(self.args) != 0:
-            ImportReportCommand.parser.print_help()
-            return 1
+        super(ImportReportCommand, self).command()
+        self._check_args_length(0)
 
         from wdmmg.lib import ckan, json
 

wdmmg/lib/command/load.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
-        if len(self.args) != 1:
-            LoadCommand.parser.print_help()
-            return 1
+        super(LoadCommand, self).command()
+        self._check_args_length(1)
 
-        self._load_config()
         cmd = self.args[0]
         import pkg_resources
         loader_method = None

wdmmg/lib/command/mapping_convert.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
-        if len(self.args) != 1:
-            MappingConvertCommand.parser.print_help()
-            return 1
+        super(MappingConvertCommand, self).command()
+        self._check_args_length(1)
 
-        self._load_config()
         from wdmmg.lib.mappingimporter import MappingImporter
         from wdmmg.lib import json
 

wdmmg/lib/command/mapping_url.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
-        if len(self.args) != 1:
-            MappingUrlCommand.parser.print_help()
-            return 1
+        super(MappingUrlCommand, self).command()
+        self._check_args_length(1)
 
-        self._load_config()
         from wdmmg.lib.mappingimporter import MappingImporter
         from wdmmg.lib.helpers import json
 

wdmmg/lib/command/remove_entries.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
-        if len(self.args) != 1:
-            RemoveEntriesCommand.parser.print_help()
-            return 1
-
-        self._load_config()
+        super(RemoveEntriesCommand, self).command()
+        self._check_args_length(1)
 
         from wdmmg.lib.tasks import remove_entries
         remove_entries(self.args[0])

wdmmg/lib/command/solr.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
+        super(SolrCommand, self).command()
+
         if len(self.args) < 1:
             SolrCommand.parser.print_help()
             return 1
 
-        self._load_config()
         cmd = self.args[0]
 
         from wdmmg.lib import solr

wdmmg/lib/command/user.py

     parser = WdmmgCommand.standard_parser()
 
     def command(self):
-        if len(self.args) != 1:
-            GrantAdminCommand.parser.print_help()
-            return 1
-
-        self._load_config()
+        super(GrantAdminCommand, self).command()
+        self._check_args_length(1)
 
         from wdmmg.model.account import Account
 

wdmmg/lib/csvimport.py

 
 class DatasetImportError(Exception):
     def __init__(self, exception, line_number=None):
-        print "DatasetImportError init with: %s, %s" % (exception, line_number)
+        log.warn("DatasetImportError init with: %s, %s" % (exception, line_number))
         self.exception = exception
         self.line_number = line_number
         if isinstance(exception, colander.Invalid):
             self.message = str(exception)
         else:
             self.message = repr(exception)
-        print 'message: %s' % self.message
 
     def __repr__(self):
         return "Line %s: %s" % (self.line_number, self.message)

wdmmg/lib/loader.py

         Returns: A :class:`wdmmg.lib.views.View` object.
         '''
         log.info("pre-aggregating view %s on %r where %r",
-                (name, cls, view_filters))
+                 name, cls, view_filters)
         view = View(self.dataset, name, label, dimension,
                     breakdown, cuts=view_filters)
         view.apply_to(cls, add_filters)

wdmmg/lib/views.py

         return cls(dataset, name, data.get('label'), data.get('dimension'),
                    drilldown=data.get('drilldown'),
                    cuts=data.get('cuts', {}))
-    
+
     @property
     def base_dimensions(self):
         dimensions = [self.dimension, 'year']
         _filters = self.view.cuts.items()
         _filters.append((self.view.dimension + "._id", self.obj.get('_id')))
         return _filters
-    
+
     @property
     def totals(self):
         if self._totals is None:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.