Source code for ecpi.common

"""generic classes, functions. Could be used by other projects.

"""

import os.path as osp
import os 


[docs]class CreatePathAssociatedTest(object): '''create output directory with same path than test module ''' def __init__(self, complet_path_test): ''' :param complet_path_test: ''' path_asso = complet_path_test.replace('/tests/', '/') self.path_asso = path_asso.replace('.py', '') pattern_ecpi = '/src/ecpi' idx_ecpi = self.path_asso.find(pattern_ecpi) + len(pattern_ecpi) # idx_first_path = self.path_asso[idx_ecpi:].find('/') self.path_asso = self.path_asso[idx_first_path + idx_ecpi + 1:] self.path_asso = add_path_tests_eclairs(self.path_asso) if not osp.isdir(self.path_asso): os.makedirs(self.path_asso)
[docs] def __call__(self, p_file): ''' function to define a path :param p_module: path of module :param p_file: path of file :return: a path at the same level that p_module :rtype: string ''' return osp.join(self.path_asso, p_file)
[docs]def add_path_current_module(p_module, p_file): ''' function to define a path :param p_module: path of module :param p_file: path of file :return: a path at the same level that p_module :rtype: string ''' return osp.join(osp.dirname(p_module), p_file)
[docs]def get_root_eclairs(): """get the path of the ECLAIRs directory. :return: path of the root of the ECLAIRs directory :rtype: string """ a = __file__.split('/') b = "/".join(a[:-4]) return b
[docs]def add_path_eclairs(p_file): """add to p_file the path of the ECLAIRs directory. :return: absolute path of p_file :rtype: string """ return osp.join(get_root_eclairs(), p_file)
[docs]def add_path_tests_eclairs(p_file): """add to p_file the path of the ECLAIRs test directory. The ECLAIRs test directory is ECLAIRs/DATA_TEST. It is a non committed directory. :return: absolute path of p_file :rtype: string """ return osp.join(get_root_eclairs(), 'DATA_TEST', p_file)
[docs]def add_path_process_eclairs(p_file): """add to p_file the path of the ECLAIRs processing directory. The ECLAIRs test directory is ECLAIRs/DATA_PROCESS. It is a non committed directory. :return: absolute path of p_file :rtype: string """ return osp.join(get_root_eclairs(), 'DATA_PROCESS', p_file)
[docs]def add_path_data_ref_eclairs(p_file): """add to p_file the path of the ECLAIRs data simulation or analysis directory. :return: absolute path of p_file :rtype: string """ return osp.join(get_root_eclairs(), 'src/ecpi/common/data', p_file)
[docs]def add_path_output_eclairs(p_file): """add to p_file the path of the ECLAIRs output directory. The ECLAIRs output directory is ECLAIRs/OUTPUT. It is a non commited directory. :return: absolute path of p_file :rtype: string """ return osp.join(get_root_eclairs(), 'OUTPUT', p_file)
[docs]def get_path_associated_test(): """add to p_file the path of the ECLAIRs output directory. The ECLAIRs output directory is ECLAIRs/OUTPUT. It is a non commited directory. :return: absolute path of p_file :rtype: string """