Package Gnumed :: Package business :: Module gmSoapDefs |
[frames] | no frames]

# Source Code for Module Gnumed.business.gmSoapDefs

      1  """GNUmed SOAP related defintions"""
      2
      3  __author__ = "Karsten Hilbert <Karsten.Hilbert@gmx.net>"
      4  __license__ = 'GPL v2 or later (for details see http://gnu.org)'
      5  #============================================================
      6  #============================================================
      7  if __name__ == '__main__':
      8          _ = lambda x:x
      9
     10
     11  _U_ELLIPSIS = '\u2026'
     12
     13  KNOWN_SOAP_CATS = list('soapu')
     14  KNOWN_SOAP_CATS.append(None)
     15
     16
     17  soap_cat2l10n = {
     18          's': _('SOAP_char_S=S').replace('SOAP_char_S=', ''),
     19          'o': _('SOAP_char_O=O').replace('SOAP_char_O=', ''),
     20          'a': _('SOAP_char_A=A').replace('SOAP_char_A=', ''),
     21          'p': _('SOAP_char_P=P').replace('SOAP_char_P=', ''),
     22          'u': _('SOAP_char_U=U').replace('SOAP_char_U=', ''),
     23          '': _U_ELLIPSIS,
     24          None: _U_ELLIPSIS
     25  }
     26
     27
     28  soap_cat2l10n_str = {
     29          's': _('SOAP_string_Subjective=Subjective').replace('SOAP_string_Subjective=', ''),
     30          'o': _('SOAP_string_Objective=Objective').replace('SOAP_string_Objective=', ''),
     31          'a': _('SOAP_string_Assessment=Assessment').replace('SOAP_string_Assessment=', ''),
     32          'p': _('SOAP_string_Plan=Plan').replace('SOAP_string_Plan=', ''),
     33          'u': _('SOAP_string_Unspecified=Unspecified').replace('SOAP_string_Unspecified=', ''),
     34          '':  _('SOAP_string_Administrative=Administrative').replace('SOAP_string_Administrative=', ''),
     35          None: _('SOAP_string_Administrative=Administrative').replace('SOAP_string_Administrative=', '')
     36  }
     37
     38
     39  l10n2soap_cat = {
     40          _('SOAP_char_S=S').replace('SOAP_char_S=', ''): 's',
     41          _('SOAP_char_O=O').replace('SOAP_char_O=', ''): 'o',
     42          _('SOAP_char_A=A').replace('SOAP_char_A=', ''): 'a',
     43          _('SOAP_char_P=P').replace('SOAP_char_P=', ''): 'p',
     44          _('SOAP_char_U=U').replace('SOAP_char_U=', ''): 'u',
     45          _U_ELLIPSIS: None,
     46          '.': None,
     47          ' ': None,
     48          '': None
     49  }
     50
     51  #============================================================
     52 -def soap_cats2list(soap_cats):
     53          """Normalizes a string or list of SOAP categories, preserving order.
     54
     55                  None -> gmSoapDefs.KNOWN_SOAP_CATS (all)
     56                  [] -> []
     57                  u'' -> []
     58                  u' ' -> [None]  (admin)
     59          """
     60          if soap_cats is None:
     61                  return KNOWN_SOAP_CATS
     62
     63          normalized_cats = []
     64          for cat in soap_cats:
     65                  if cat in [' ', None]:
     66                          if None in normalized_cats:
     67                                  continue
     68                          normalized_cats.append(None)
     69                          continue
     70                  cat = cat.lower()
     71                  if cat in KNOWN_SOAP_CATS:
     72                          if cat in normalized_cats:
     73                                  continue
     74                          normalized_cats.append(cat)
     75
     76          return normalized_cats
     77
     78  #============================================================
     79 -def are_valid_soap_cats(soap_cats, allow_upper=True):
     80          for cat2test in soap_cats:
     81                  if cat2test in KNOWN_SOAP_CATS:
     82                          continue
     83                  if not allow_upper:
     84                          return False
     85                  if cat2test.upper() in KNOWN_SOAP_CATS:
     86                          continue
     87                  return False
     88          return True
     89
     90  #============================================================
     91 -def normalize_soap_cat(soap_cat):
     92          if soap_cat in KNOWN_SOAP_CATS:
     93                  return soap_cat
     94          soap_cat = soap_cat.lower()
     95          if soap_cat in KNOWN_SOAP_CATS:
     96                  return soap_cat
     97          return False
     98
     99  #============================================================
    100  if __name__ == '__main__':
    101
    102          import sys
    103
    104          if len(sys.argv) < 2:
    105                  sys.exit()
    106
    107          if sys.argv[1] != 'test':
    108                  sys.exit()
    109
    110          sys.path.insert(0, '../../')
    111
    112          gmI18N.activate_locale()
    113          gmI18N.install_domain()
    114
    115          #--------------------------------------------------------
    116 -        def test_translation():
    117                  for c in KNOWN_SOAP_CATS:
    118                          print(c, soap_cat2l10n[c], soap_cat2l10n_str[c])
    119
    120          #--------------------------------------------------------
    121 -        def test_are_valid_cats():
    122                  cats = [
    123                          list('soap'),
    124                          list('soapSOAP'),
    125                          list('soapx'),
    126                          list('soapX'),
    127                          list('soapSOAPx'),
    128                          [None],
    129                          ['s', None],
    130                          ['s', None, 'O'],
    131                          ['s', None, 'x'],
    132                          ['s', None, 'X'],
    133                  ]
    134                  for cat_list in cats:
    135                          print(cat_list)
    136                          print('  valid (plain):', are_valid_soap_cats(cat_list, False))
    137                          print('  valid (w/ upper):', are_valid_soap_cats(cat_list, True))
    138
    139          #--------------------------------------------------------
    140          test_translation()
    141          test_are_valid_cats()
    142