Exemple #1
0
def quote_newick_name(s, needs_quotes_pattern=_NEWICK_NEEDING_QUOTING):
    s = UNICODE(s)
    if "'" in s:
        return u"'{}'".format("''".join(s.split("'")))
    if needs_quotes_pattern.search(s):
        return u"'{}'".format(s)
    return s
Exemple #2
0
 def _process_query_dict(self, query_dict, valid_keys, kwargs):
     if query_dict is None:
         query_dict = {}
     for k, v in kwargs.items():
         if k in valid_keys:
             query_dict[k] = v
         else:
             query_dict['ot:' + k] = v
     nq = len(query_dict)
     if nq == 0:
         if self.use_v1:
             raise ValueError(
                 'The property/value pairs for the query should be passed in as keyword arguments'
             )
         return None
     if nq > 1:
         raise NotImplementedError(
             'Currently only searches for one property/value pair are supported'
         )
     k = list(query_dict.keys())[0]
     if k not in valid_keys:
         m = '"{k}" is not a valid search term. Expecting it to be one of the following: {kl}'
         m = m.format(k=k, kl=repr(valid_keys))
         raise ValueError(m)
     v = query_dict[k]
     if not is_str_type(v):
         v = UNICODE(v)
     if k == 'ot:studyPublication':
         v = doi2url(v)
     return (k, v)
Exemple #3
0
def _dict_summary(d, name):
    dk = list(d.keys())
    dk.sort()
    sd = UNICODE(d)
    if len(sd) < _CUTOFF_LEN_DETAILED_VIEW:
        a = []
        for k in dk:
            a.extend([repr(k), ': ', repr(d[k]), ', '])
        return u'%s={%s}' % (name, ''.join(a))
    return u'%s-keys=%s' % (name, repr(dk))
 def testValidFilesPass(self):
     # just one test file for now
     msg = ''
     frag = 'amendment-good.json'
     amendment = pathmap.amendment_obj(frag)
     aa = validate_amendment(amendment)
     errors = aa[0]
     for e in errors:
         _LOG.debug('unexpected error from {f}: {m}'.format(f=frag, m=UNICODE(e)))
     if len(errors) > 0:
         ofn = pathmap.amendment_source_path(frag + '.output')
         testing_write_json(errors, ofn)
         msg = "File failed to validate cleanly. See {o}".format(o=ofn)
     self.assertEqual(len(errors), 0, msg)
Exemple #5
0
 def testValidFilesPass(self):
     format_list = ['1.2']
     msg = ''
     for d in VALID_NEXSON_DIRS:
         for nf in format_list:
             frag = os.path.join(d, 'v{f}.json'.format(f=nf))
             nexson = pathmap.nexson_obj(frag)
             aa = validate_nexson(nexson)
             annot = aa[0]
             for e in annot.errors:
                 _LOG.debug('unexpected error from {f}: {m}'.format(
                     f=frag, m=UNICODE(e)))
             if len(annot.errors) > 0:
                 ofn = pathmap.nexson_source_path(frag + '.output')
                 ew_dict = annot.get_err_warn_summary_dict()
                 testing_write_json(ew_dict, ofn)
                 msg = "File failed to validate cleanly. See {o}".format(
                     o=ofn)
             self.assertEqual(len(annot.errors), 0, msg)
Exemple #6
0
 def _partition_keys_for_xml(self, o):
     '''Breaks o into four content type by key syntax:
         attrib keys (start with '@'),
         text (value associated with the '$' or None),
         child element keys (all others)
         meta element
     '''
     ak = {}
     tk = None
     ck = {}
     mc = {}
     #_LOG.debug('o = {o}'.format(o=o))
     for k, v in o.items():
         if k.startswith('@'):
             if k == '@xmlns':
                 if '$' in v:
                     ak['xmlns'] = v['$']
                 for nsk, nsv in v.items():
                     if nsk != '$':
                         ak['xmlns:' + nsk] = nsv
             else:
                 s = k[1:]
                 if type(v) == bool:
                     if v == True:
                         v = u'true'
                     else:
                         v = u'false'
                 ak[s] = UNICODE(v)
         elif k == '$':
             tk = v
         elif k.startswith('^') and (not self._migrating_from_bf):
             s = k[1:]
             val = _convert_hbf_meta_val_for_xml(s, v)
             _add_value_to_dict_bf(mc, s, val)
         elif (k == u'meta') and self._migrating_from_bf:
             s, val = _convert_bf_meta_val_for_xml(v)
             _add_value_to_dict_bf(mc, s, val)
         else:
             ck[k] = v
     return ak, tk, ck, mc
Exemple #7
0
def _create_sub_el(doc, parent, tag, attrib, data=None):
    '''Creates and xml element for the `doc` with the given `parent`
    and `tag` as the tagName.
    `attrib` should be a dictionary of string keys to primitives or dicts
        if the value is a dict, then the keys of the dict are joined with
        the `attrib` key using a colon. This deals with the badgerfish
        convention of nesting xmlns: attributes in a @xmnls object
    If `data` is not None, then it will be written as data. If it is a boolean,
        the xml true false will be writtten. Otherwise it will be
        converted to python unicode string, stripped and written.
    Returns the element created
    '''
    el = doc.createElement(tag)
    if attrib:
        if ('id' in attrib) and ('about' not in attrib):
            about_val = '#' + attrib['id']
            el.setAttribute('about', about_val)
        for att_key, att_value in attrib.items():
            if isinstance(att_value, dict):
                for inner_key, inner_val in att_value.items():
                    rk = ':'.join([att_key, inner_key])
                    el.setAttribute(rk, inner_val)
            else:
                el.setAttribute(att_key, att_value)
    if parent:
        parent.appendChild(el)
    if data:
        if data is True:
            el.appendChild(doc.createTextNode('true'))
        elif data is False:
            el.appendChild(doc.createTextNode('false'))
        else:
            u = UNICODE(data).strip()
            if u:
                el.appendChild(doc.createTextNode(u))
    return el