This file is indexed.

/usr/lib/python3/dist-packages/drslib/translate.py is in python3-drslib 0.3.0a3-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# BSD Licence
# Copyright (c) 2010, Science & Technology Facilities Council (STFC)
# All rights reserved.
#
# See the LICENSE file in the source distribution of this software for
# the full license text.

"""
A framework for translating DRS objects to and from filenames and paths.

This module is very object-heavy and over engineered.  However, it does the job.

"""

import re, os

import logging
log = logging.getLogger(__name__)

from drslib.drs import DRS
from drslib.config import CMIP5_DRS, CMIP5_CMOR_DRS

class TranslationError(Exception):
    pass


class TranslatorContext(object):
    """
    Represents a DRS URL or path being translated.

    :ivar path_parts: A list of directories following the DRS prefix
    :ivar file_parts: A list of '_'-separated parts of the filename
    :ivar drs: The DRS of interest
    :ivar table_store: The mip tables being used.

    """
    def __init__(self, filename=None, path=None, drs=None, table_store=None):
        if path is None:
            self.path_parts = [None] * 12
        else:
            self.path_parts = path.split('/')

        if filename is None:
            self.file_parts = [None] * 7
        else:
            self.file_parts = os.path.splitext(filename)[0].split('_')
            
        # Work-arround because CMOR encodes 'clim' as flag as _clim
        if self.file_parts[-1] == 'clim':
            self.file_parts[-2:] = [self.file_parts[-2] + '-clim']

        if drs is None:
            self.drs = DRS()
        else:
            self.drs = drs

        self.table_store = table_store

    def set_drs_component(self, drs_component, value):
        """
        Set a DRS component checking that the value doesn't conflict
        with any current value.

        """
        v = self.drs[drs_component]
        if v is None:
            setattr(self.drs, drs_component, value)
        else:
            if v != value:
                raise TranslationError('Conflicting value of DRS component %s' % drs_component)

    def path_to_string(self):
        self._trim_parts()

        parts = self.path_parts
        
        return os.path.join(*parts)
    
    def file_to_string(self):
        # To allow optional portions any None's are removed
        fp = [x for x in self.file_parts if x is not None]

        # Check there is at last 1 item in the list
        assert len(fp) > 0

        return '_'.join(fp)+'.nc'
    
    def to_string(self):
        """Returns the full DRS path and filename.
        """
        self._trim_parts()

        return os.path.join(self.path_to_string(), self.file_to_string())

    def _trim_parts(self):
        """Remove extranious Nones from path_parts and file_parts.
        """

        while self.path_parts[-1] is None:
            self.path_parts.pop()

        while self.file_parts[-1] is None:
            self.file_parts.pop()


class BaseComponentTranslator(object):
    """
    Each component is translated by a separate IComponentTranslator object.

    """

    def __init__(self, table_store=None):
        self.table_store = table_store

    def filename_to_drs(self, context):
        """
        Translate the relevant component of the filename to a drs component.

        :raises TranslationError: if the filename is invalid
        :returns: context

        """
        raise NotImplementedError

    def path_to_drs(self, context):
        """
        Translate the relevant component of the DRS path to a drs component.

        :raises TranslationError: if the path is invalid
        :returns: context

        """
        raise NotImplementedError


    def drs_to_filepath(self, context):
        """
        Sets context.path_parts and context.file_parts for this component.

        :returns: context

        """
        raise NotImplementedError

class GenericComponentTranslator(BaseComponentTranslator):
    """
    The simplest type of translator just validating strings

    """

    # Class attributes set in subclasses
    path_i = NotImplemented
    file_i = NotImplemented
    component = NotImplemented
    vocab = NotImplemented

    def filename_to_drs(self, context):
        if self.file_i is not None:
            try:
                s = self._validate(context.file_parts[self.file_i])
            except IndexError:
                raise TranslationError("Filename contains too few components")
            context.set_drs_component(self.component, s)


    def path_to_drs(self, context):
        if self.path_i is not None:
            s = self._validate(context.path_parts[self.path_i])
            context.set_drs_component(self.component, s)

    def drs_to_filepath(self, context):
        s = self._validate(context.drs[self.component])
        
        if self.path_i is not None:
            context.path_parts[self.path_i] = s
        if self.file_i is not None:
            context.file_parts[self.file_i] = s

    #----

    def _validate(self, s):
        if self.vocab is None:
            return s

        if s not in self.vocab:
            raise TranslationError('Component value %s not in vocabulary of component %s' % (s, self.component))

        return s





class CMORVarTranslator(BaseComponentTranslator):
    """
    CMIP Variable translator

    For CMIP5, each variable is uniquely identified by a combination
    of two strings: 1) a name associated generically with the variable
    (typically, as recorded in the netCDF file ? e.g., tas, pr, ua),
    and 2) the name of the CMOR variable table (e.g., Amon, da, aero)
    in which the variable appears. Together these two strings ensure
    that the variable is uniquely 4 defined. There are some
    applications in which the second string might be unnecessary
    (e.g., for CMIP5, this will be omitted from the directory
    structure). Note that for CMIP5 a hyphen ('-') is forbidden to be
    included anywhere in the first string.

    """

    file_var_i = CMIP5_CMOR_DRS.FILE_VARIABLE
    file_table_i = CMIP5_CMOR_DRS.FILE_TABLE
    path_var_i = CMIP5_CMOR_DRS.PATH_VARIABLE

    def filename_to_drs(self, context):
        varname = context.file_parts[self.file_var_i]
        table = context.file_parts[self.file_table_i]

        #!TODO: table checking

        context.set_drs_component('variable', varname)
        context.set_drs_component('table', table)


    def path_to_drs(self, context):
        varname = context.file_parts[self.file_var_i]
        
        #!TODO: table checking

        context.set_drs_component('variable', varname)

    def drs_to_filepath(self, context):
        context.file_parts[self.file_var_i] = context.drs.variable
        context.file_parts[self.file_table_i] = context.drs.table
        context.path_parts[self.path_var_i] = context.drs.variable


    #----

class VersionedVarTranslator(CMORVarTranslator):
    file_var_i = CMIP5_DRS.FILE_VARIABLE
    file_table_i = CMIP5_DRS.FILE_TABLE
    path_var_i = CMIP5_DRS.PATH_VARIABLE
    path_table_i = CMIP5_DRS.PATH_TABLE

    def path_to_drs(self, context):
        super(VersionedVarTranslator, self).path_to_drs(context)

        tablename = context.path_parts[self.path_table_i]

        context.set_drs_component('table', tablename)

    def drs_to_filepath(self, context):
        super(VersionedVarTranslator, self).drs_to_filepath(context)

        context.path_parts[self.path_table_i] = context.drs.table

class EnsembleTranslator(BaseComponentTranslator):
    file_i = CMIP5_CMOR_DRS.FILE_ENSEMBLE
    path_i = CMIP5_CMOR_DRS.PATH_ENSEMBLE

    def filename_to_drs(self, context):
        context.drs.ensemble = self._convert(context.file_parts[self.file_i])


    def path_to_drs(self, context):
        context.drs.ensemble = self._convert(context.path_parts[self.path_i])


    def drs_to_filepath(self, context):
        (r, i, p) = context.drs.ensemble
        a = ['r%d' % r]
        if i is not None:
            a.append('i%d' % i)
        if p is not None:
            a.append('p%d' % p)
        v = ''.join(a)
        
        context.file_parts[self.file_i] = v
        context.path_parts[self.path_i] = v


    #----

    def _convert(self, component):
        mo = re.match(r'(?:r(\d+))?(?:i(\d+))?(?:p(\d+))?', component)
        if not mo:
            raise TranslationError('Unrecognised ensemble syntax %s' % component)

        (r, i, p) = mo.groups()
        return (_int_or_none(r), _int_or_none(i), _int_or_none(p))

class VersionedEnsembleTranslator(EnsembleTranslator):
    file_i = CMIP5_DRS.FILE_ENSEMBLE
    path_i = CMIP5_DRS.PATH_ENSEMBLE


class VersionTranslator(BaseComponentTranslator):

    def filename_to_drs(self, context):
        pass

    def path_to_drs(self, context):
        context.drs.version = self._convert(context.path_parts[CMIP5_DRS.PATH_VERSION])


    def drs_to_filepath(self, context):
        if context.drs.version is not None:
            context.path_parts[CMIP5_DRS.PATH_VERSION] = 'v%d' % context.drs.version
        
    #----

    def _convert(self, component):
        mo = re.match(r'v(\d+)', component)
        if not mo:
            raise TranslationError('Unrecognised version syntax %s' % component)

        try:
            v = int(mo.group(1))
        except TypeError:
            raise TranslationError('Unrecognised version syntax %s' % component)

        return v

    

class SubsetTranslator(BaseComponentTranslator):
    """
    Currently just temporal subsets

    :cvar allow_missing_subset: A boolean value configuring whether to 
        complain if the subset is missing.

    """

    allow_missing_subset = True

    def filename_to_drs(self, context):
        try:
            v = context.file_parts[CMIP5_DRS.FILE_SUBSET]
        except IndexError:
            if self.allow_missing_subset:
                return
            else:
                raise TranslationError('Missing temporal subset')
        
        mo = re.match(r'(\d+)(?:-(\d+))?(-clim)?', v)
        if not mo:
            raise TranslationError('Unrecognised temporal subset %s' % v)

        n1, n2, clim = mo.groups()
        if clim: 
            clim=True

        context.drs.subset = (_to_date(n1), _to_date(n2), clim)


    def path_to_drs(self, component):
        pass

    def drs_to_filepath(self, context):

        if self.allow_missing_subset and context.drs.subset is None:
            return
                
        (n1, n2, clim) = context.drs.subset
        
        parts = []
        parts.append(_from_date(n1))
        if n2:
            parts.append(_from_date(n2))
        if clim:
            parts.append('clim')

        context.file_parts[CMIP5_DRS.FILE_SUBSET] = '-'.join(parts)


class Translator(object):
    """
    An abstract class for translating between filepaths and
    :class:`drslib.drs.DRS` objects.

    Concrete subclasses are returned by factory functions such as
    :mod:`drslib.cmip5.make_translator`.

    :property prefix: The prefix for all DRS paths including the
        activity.  All paths are interpreted as relative to this
        prefix.  Generated paths have this prefix added.

    :property table_store: A :class:`drslib.mip_table.MIPTableStore`
        object containing all MIP tables being used.
    
    """

    ContextClass = TranslatorContext

    def __init__(self, prefix='', table_store=None):
        self.prefix = prefix
        self.table_store = table_store

    def filename_to_drs(self, filename, context=None):
        """
        Translate a filename into a :class:`drslib.drs.DRS` object.

        Only those DRS components known from the filename will be set.

        """

        if context is None:
            context = self.ContextClass(filename=filename, drs=self.init_drs(),
                                        table_store = self.table_store)
        for t in self.translators:
            t.filename_to_drs(context)

        return context.drs

    def path_to_drs(self, path, context=None):
        """
        Translate a directory path into a :class:`drslib.drs.DRS`
        object.
        
        Only those DRS components known from the path will be set.

        """
        if context is None:
            context = self.ContextClass(path=self._split_prefix(path), drs=self.init_drs(),
                                        table_store = self.table_store)
        for t in self.translators:
            t.path_to_drs(context)

        return context.drs

    def filepath_to_drs(self, filepath, context=None):
        """
        Translate a full filepath to a :class:`drslib.drs.DRS` object.

        """
        filepath = self._split_prefix(filepath)
        path, filename = os.path.split(filepath)
        if context is None:
            context = self.ContextClass(filename=filename, path=path, drs=self.init_drs(),
                                        table_store = self.table_store)
        for t in self.translators:
            t.path_to_drs(context)
            t.filename_to_drs(context)
            
        return context.drs
            
    def drs_to_context(self, drs):
        context = self.ContextClass(drs=self.init_drs(drs), table_store = self.table_store)
        for t in self.translators:
            t.drs_to_filepath(context)

        return context

    def drs_to_filepath(self, drs):
        """
        Translate a :class:`drslib.drs.DRS` object into a full filepath.

        """
        context = self.drs_to_context(drs)

        return os.path.join(self.prefix, context.to_string())

    def drs_to_path(self, drs):
        """
        Translate a :class:`drslib.drs.DRS` object into a directory path.

        """
        context = self.drs_to_context(drs)
        
        return os.path.join(self.prefix, context.path_to_string())

    def drs_to_file(self, drs):
        """
        Translate a :class:`drslib.drs.DRS` object into a filename.

        """
        context = self.drs_to_context(drs)
        
        return context.file_to_string()

    
    def _split_prefix(self, path):
        n = len(self.prefix)
        if path[:n] == self.prefix:
            return path[n:].lstrip('/')
        else:
            log.warn('Path %s does not have prefix %s' % (path, self.prefix))
            return path

    def init_drs(self):
        """
        Returns an empty DRS instance initialised for this translator.

        This class must be overloaded in subclasses.

        """
        raise NotImplementedError


def _to_date(date_str):
    mo = re.match(r'(\d{4})(\d{2})?(\d{2})?(\d{2})?(\d{2})?', date_str)
    if not mo:
        raise ValueError()

    (y, m, d, h, mn) = mo.groups()
    return (int(y), _int_or_none(m), _int_or_none(d), _int_or_none(h), _int_or_none(mn))

def _from_date(date):
    (y, m, d, h, mn) = date

    ret = ['%04d' % y]
    if m is not None:
        ret.append('%02d' % m)
    if d is not None:
        ret.append('%02d' % d)
    if h is not None:
        ret.append('%02d' % h)
    if mn is not None:
        ret.append('%02d' % mn)
    return ''.join(ret)
    
def _int_or_none(x):
    if x is None:
        return None
    else:
        return int(x)