def abs_NXDL_filename(file_name): '''return absolute path to file_name, within NXDL directory''' import punx.cache_manager cm = punx.cache_manager.CacheManager() punx.LOG_MESSAGE('file_name: ' + str(file_name), punx.DEBUG) punx.LOG_MESSAGE('cm.default_file_set: ' + str(cm.default_file_set), punx.DEBUG) if cm.default_file_set is None: raise ValueError('no default file set') absolute_name = os.path.abspath(os.path.join(cm.default_file_set.path, file_name)) if not os.path.exists(absolute_name): msg = 'file does not exist: ' + absolute_name raise punx.FileNotFound(msg) punx.LOG_MESSAGE('user cache: ' + absolute_name, punx.DEBUG) return absolute_name
def interceptor_logfile(args): ''' special handling for subcommands with a *logfile* option ''' if 'logfile' in args: if args.logfile == CONSOLE_LOGGING_DEFAULT_CHOICE: punx.DEFAULT_LOG_LEVEL = args.interest punx.LOG_MESSAGE = logs.to_console punx.LOG_MESSAGE('logging output to console only', punx.DEBUG) else: lo = punx.NOISY hi = punx.CRITICAL args.interest = max(lo, min(hi, args.interest)) _log = logs.Logger(log_file=args.logfile, level=args.interest) punx.LOG_MESSAGE('sys.argv: ' + ' '.join(sys.argv), punx.DEBUG) punx.LOG_MESSAGE('args: ' + str(args), punx.DEBUG)
def __init__(self): self.default_file_set = None self.source = SourceCache() self.user = UserCache() self.NXDL_file_sets = self.file_sets() msg = 'NXDL_file_sets names = ' msg += str(sorted(list(self.NXDL_file_sets.keys()))) punx.LOG_MESSAGE(msg, punx.DEBUG) try: self.select_NXDL_file_set() except KeyError: pass if self.default_file_set is None: msg = 'CacheManager: no default_file_set selected yet' punx.LOG_MESSAGE(msg, punx.DEBUG)
def select_NXDL_file_set(self, ref=None): ''' return the named self.default_file_set instance or raise KeyError exception if unknown ''' import punx.github_handler msg = 'DEBUG - given ref: ' + str(ref) punx.LOG_MESSAGE(msg, punx.DEBUG) ref = ref or punx.github_handler.DEFAULT_NXDL_SET msg = 'DEBUG - final ref: ' + str(ref) punx.LOG_MESSAGE(msg, punx.DEBUG) if ref not in self.NXDL_file_sets: #msg = 'unknown NXDL file set: ' + str(ref) msg = 'expected one of ' + ' '.join(sorted(self.NXDL_file_sets.keys())) msg += ', received: ' + str(ref) raise KeyError(msg) self.default_file_set = self.NXDL_file_sets[ref] return self.default_file_set
def file_sets(self): ''' index all NXDL file sets in both source and user caches, return a dictionary ''' fs = {k: v for k, v in self.source.file_sets().items()} msg = 'DEBUG - source file set names: ' msg += str(sorted(list(fs.keys()))) punx.LOG_MESSAGE(msg, punx.DEBUG) for k, v in self.user.file_sets().items(): if k in fs: raise ValueError('user cache file set already known: ' + k) else: fs[k] = v self.NXDL_file_sets = fs # remember msg = 'DEBUG - all file set names: ' msg += str(sorted(list(fs.keys()))) punx.LOG_MESSAGE(msg, punx.DEBUG) return fs
def update_NXDL_Cache(force_update=False): ''' update the cache of NeXus NXDL files :param bool force_update: (optional, default: False) update if GitHub is available ''' import punx.cache_manager, punx.github_handler punx.LOG_MESSAGE('update_NXDL_Cache(force_update=%s)' % str(force_update)) cm = punx.cache_manager.CacheManager() grr = punx.github_handler.GitHub_Repository_Reference() grr.connect_repo() cm.install_NXDL_file_set(grr, user_cache=True, ref='master') qset = qsettings() info = dict(git_sha=grr.sha, git_time=grr.last_modified, zip_url=grr.zip_url) info['file'] = str(qset.fileName()) punx.LOG_MESSAGE('update .ini file: ' + str(qset.fileName()), punx.INFO) qset.updateGroupKeys(info)
def exit_message(msg, status=None, exit_code=1): ''' exit this code with a message and a status :param str msg: text to be reported :param int status: 0 - 50 (default: punx.ERROR = 40) :param int exit_code: 0: no error, 1: error (default) ''' if status is None: status = punx.ERROR punx.LOG_MESSAGE(msg, status) if punx.LOG_MESSAGE != logs.to_console: print(msg) exit(exit_code)
def file_sets(self): ''' index all NXDL file sets in this cache ''' fs = {} if self.qsettings is None: raise RuntimeError('cache qsettings not defined!') cache_path = self.path() msg = 'DEBUG - cache path: ' + str(cache_path) punx.LOG_MESSAGE(msg, punx.DEBUG) for item in os.listdir(cache_path): if os.path.isdir(os.path.join(cache_path, item)): info_file = os.path.join(cache_path, item, INFO_FILE_NAME) if os.path.exists(info_file): fs[item] = NXDL_File_Set() fs[item].read_info_file(info_file) return fs