import textwrap
import copy
import json
+import datetime
# Third party modules
import six
# Own modules
from . import __version__
-from .common import pp, to_str
+from .common import pp, to_str, is_sequence
from .base_app import BaseHookError, BaseHookApp
self.cache_file = None
self._output_type = 'json'
+ self.filters = None
super(ShowModulesApp, self).__init__(
appname=appname, verbose=verbose, version=version)
self.read_stdin = False
self.cache_file = os.path.join(self.data_dir, 'modules-info.yaml')
+ self.init_filters()
self.initialized = True
+ # -------------------------------------------------------------------------
+ def init_filters(self):
+
+ if 'vendor' in self.query:
+ q_vendors = []
+ if is_sequence(self.query['vendor']):
+ for vendor in self.query['vendor']:
+ v = vendor.strip().lower()
+ if v:
+ q_vendors.append(v)
+ else:
+ v = self.query['vendor'].strip().lower()
+ if v:
+ q_vendors.append(v)
+ if q_vendors:
+ if self.filters is None:
+ self.filters = {}
+ self.filters['vendor'] = q_vendors
+
+
# -------------------------------------------------------------------------
def run(self):
"""Main routine."""
self.print_out("#" * len_title)
self.print_out('')
+ module_infos = []
try:
- self.read_cache_file()
+ module_infos = self.read_cache_file()
except ShowModulesUncriticalError as e:
LOG.error(str(e))
else:
- if self.output_type == 'json':
- data = {'msg': self.html_title}
- self.print_out(json.dumps(data))
+ self.output_modules(module_infos)
+# if self.output_type == 'json':
+# data = {'msg': self.html_title}
+# self.print_out(json.dumps(data))
+
+ # -------------------------------------------------------------------------
+ def output_modules(self, module_infos):
+
+ if self.output_type == 'json':
+ self.output_modules_json(module_infos)
+
+ # -------------------------------------------------------------------------
+ def output_modules_json(self, module_infos):
+
+ output_list = []
+ for module_info in module_infos:
+ if module_info.ts_checked:
+ date_checked = datetime.datetime.fromtimestamp(
+ module_info.ts_checked, self.tz).strftime('%Y-%m-%d %H:%M:%S %Z')
+ else:
+ date_checked = None
+ data = {
+ 'name': module_info.name,
+ 'vendor': module_info.vendor,
+ 'full_name': module_info.full_name,
+ 'forge_version': module_info.forge_version,
+ 'forge_avail': module_info.forge_avail,
+ 'forge_homepage_url': module_info.forge_homepage_url,
+ 'repo': module_info.repo,
+ 'ts_checked': module_info.ts_checked,
+ 'date_checked': date_checked,
+ 'version_development': module_info.local_version_output('development'),
+ 'version_test': module_info.local_version_output('test'),
+ 'version_production': module_info.local_version_output('production'),
+ }
+ output_list.append(data)
+
+ self.print_out(json.dumps(output_list))
# -------------------------------------------------------------------------
def read_cache_file(self):
struct, appname=self.appname, verbose=self.verbose,
base_dir=self.base_dir)
if module_info:
- data.append(module_info)
+ if self.should_display(module_info):
+ data.append(module_info)
except yaml.YAMLError as e:
raise ShowModulesUncriticalError(
"Could not evaluate content of {f!r}: {e}".format(f=self.cache_file, e=e))
return data
+ # -------------------------------------------------------------------------
+ def should_display(self, module_info):
+
+ if not self.filters:
+ return True
+
+ do_display = False
+ if 'vendor' in self.filters:
+ for vendor in self.filters['vendor']:
+ if vendor == module_info.vendor:
+ do_display = True
+ break
+ if not do_display:
+ return False
+
+ return True
+
# =============================================================================
if __name__ == "__main__":