Commit 9201436b8d2b9616ce2aa080841f24a1e2765eee
1 parent
80092251
Fix several Python issues in comgendb
Showing
1 changed file
with
48 additions
and
9 deletions
modules/comscripts/comgendb.py
| ... | ... | @@ -105,7 +105,7 @@ class comgendb(ctools.cscript): |
| 105 | 105 | """ |
| 106 | 106 | # Set archive directory name |
| 107 | 107 | dirname = gammalib.expand_env(self['archive'].string()) |
| 108 | - | |
| 108 | + | |
| 109 | 109 | # Raise an exception if the archive does not exist |
| 110 | 110 | if not os.path.isdir(dirname): |
| 111 | 111 | msg = ('Archive directory "%s" does not exist.' % dirname) |
| ... | ... | @@ -121,6 +121,45 @@ class comgendb(ctools.cscript): |
| 121 | 121 | # Return phases |
| 122 | 122 | return phases |
| 123 | 123 | |
| 124 | + def _get_relpath(self, path, dest): | |
| 125 | + """ | |
| 126 | + Get relative path with respect to dest | |
| 127 | + | |
| 128 | + Parameters | |
| 129 | + ---------- | |
| 130 | + path : str | |
| 131 | + Full path name | |
| 132 | + dest : str | |
| 133 | + Destination path name | |
| 134 | + | |
| 135 | + Returns | |
| 136 | + ------- | |
| 137 | + relpath : str | |
| 138 | + Relative path | |
| 139 | + """ | |
| 140 | + # Split path and destination in segments | |
| 141 | + path_split = path.split('/') | |
| 142 | + dest_split = dest.split('/') | |
| 143 | + | |
| 144 | + # Get index of last agreement | |
| 145 | + index = -1 | |
| 146 | + for i, segment in enumerate(dest_split): | |
| 147 | + if segment == path_split[i]: | |
| 148 | + index = i | |
| 149 | + | |
| 150 | + # Set relative path | |
| 151 | + if index == -1: | |
| 152 | + relpath = path | |
| 153 | + else: | |
| 154 | + relpath = '' | |
| 155 | + for i in range(index+1,len(path_split)): | |
| 156 | + if relpath != '': | |
| 157 | + relpath += '/' | |
| 158 | + relpath += path_split[i] | |
| 159 | + | |
| 160 | + # Return relative path | |
| 161 | + return relpath | |
| 162 | + | |
| 124 | 163 | def _get_evp_list(self): |
| 125 | 164 | """ |
| 126 | 165 | Get EVP list |
| ... | ... | @@ -189,7 +228,7 @@ class comgendb(ctools.cscript): |
| 189 | 228 | for evp in evps: |
| 190 | 229 | |
| 191 | 230 | # Set EVP filename |
| 192 | - evpname = archive+'/'+os.path.relpath(evp, archive_path).strip('.gz') | |
| 231 | + evpname = archive+'/'+self._get_relpath(evp, archive_path).strip('.gz') | |
| 193 | 232 | |
| 194 | 233 | # Try opening file |
| 195 | 234 | try: |
| ... | ... | @@ -423,7 +462,7 @@ class comgendb(ctools.cscript): |
| 423 | 462 | for oad in oads: |
| 424 | 463 | |
| 425 | 464 | # Set OAD filename |
| 426 | - oadname = archive+'/'+os.path.relpath(oad, archive_path).strip('.gz') | |
| 465 | + oadname = archive+'/'+self._get_relpath(oad, archive_path).strip('.gz') | |
| 427 | 466 | |
| 428 | 467 | # Try opening file |
| 429 | 468 | try: |
| ... | ... | @@ -610,7 +649,7 @@ class comgendb(ctools.cscript): |
| 610 | 649 | for tim in tims: |
| 611 | 650 | |
| 612 | 651 | # Set TIM filename |
| 613 | - timname = archive+'/'+os.path.relpath(tim, archive_path).strip('.gz') | |
| 652 | + timname = archive+'/'+self._get_relpath(tim, archive_path).strip('.gz') | |
| 614 | 653 | |
| 615 | 654 | # Try opening file |
| 616 | 655 | try: |
| ... | ... | @@ -830,7 +869,7 @@ class comgendb(ctools.cscript): |
| 830 | 869 | vpname = os.path.basename(vp) |
| 831 | 870 | |
| 832 | 871 | # Get all TIMs for VP |
| 833 | - tims = filter(lambda k: k['vp'] == vpname, tim_list) | |
| 872 | + tims = list(filter(lambda k: k['vp'] == vpname, tim_list)) | |
| 834 | 873 | |
| 835 | 874 | # Continue if list is empty |
| 836 | 875 | if len(tims) < 1: |
| ... | ... | @@ -840,7 +879,7 @@ class comgendb(ctools.cscript): |
| 840 | 879 | quality = max([tim['quality'] for tim in tims]) |
| 841 | 880 | |
| 842 | 881 | # Get all TIMs for VP with maximum quality |
| 843 | - best_tims = filter(lambda k: k['quality'] == quality, tims) | |
| 882 | + best_tims = list(filter(lambda k: k['quality'] == quality, tims)) | |
| 844 | 883 | |
| 845 | 884 | # Append these TIMs to the filtered list |
| 846 | 885 | filtered_tim_list.extend(best_tims) |
| ... | ... | @@ -972,7 +1011,7 @@ class comgendb(ctools.cscript): |
| 972 | 1011 | vpname = os.path.basename(vp) |
| 973 | 1012 | |
| 974 | 1013 | # Get all EVPs for VP |
| 975 | - evps = filter(lambda k: k['vp'] == vpname, evp_list) | |
| 1014 | + evps = list(filter(lambda k: k['vp'] == vpname, evp_list)) | |
| 976 | 1015 | |
| 977 | 1016 | # Initialise best EVP |
| 978 | 1017 | best_verno_evp = None |
| ... | ... | @@ -1025,7 +1064,7 @@ class comgendb(ctools.cscript): |
| 1025 | 1064 | |
| 1026 | 1065 | # If there are several EVPs with highest version then select |
| 1027 | 1066 | # the one with the best quality |
| 1028 | - evps = filter(lambda k: k['vp'] == vpname and k['verno'] == best_verno, evp_list) | |
| 1067 | + evps = list(filter(lambda k: k['vp'] == vpname and k['verno'] == best_verno, evp_list)) | |
| 1029 | 1068 | if len(evps) > 1: |
| 1030 | 1069 | self._log_string(gammalib.NORMAL, '--- Several EVPs for %s ' |
| 1031 | 1070 | 'have highest version' % (vpname)) |
| ... | ... | @@ -1062,7 +1101,7 @@ class comgendb(ctools.cscript): |
| 1062 | 1101 | |
| 1063 | 1102 | # If there are several EVPs with the best quality then select the one |
| 1064 | 1103 | # with the highest version |
| 1065 | - evps = filter(lambda k: k['vp'] == vpname and k['quality'] == best_quality, evp_list) | |
| 1104 | + evps = list(filter(lambda k: k['vp'] == vpname and k['quality'] == best_quality, evp_list)) | |
| 1066 | 1105 | if len(evps) > 1: |
| 1067 | 1106 | self._log_string(gammalib.NORMAL, '--- Several EVPs for %s have ' |
| 1068 | 1107 | 'best quality' % (vpname)) | ... | ... |