| 
1 | 1 | import sys  | 
2 | 2 | import urlparse  | 
 | 3 | +import json  | 
3 | 4 | 
 
  | 
4 | 5 | def findId(doc, frg):  | 
5 | 6 |     if isinstance(doc, dict):  | 
@@ -27,44 +28,54 @@ def fixType(doc):  | 
27 | 28 |     return doc  | 
28 | 29 | 
 
  | 
29 | 30 | def _draft2toDraft3(doc, loader, baseuri):  | 
30 |  | -    if isinstance(doc, dict):  | 
31 |  | -        if "import" in doc:  | 
32 |  | -            imp = urlparse.urljoin(baseuri, doc["import"])  | 
33 |  | -            r = loader.fetch(imp)  | 
34 |  | -            if isinstance(r, list):  | 
35 |  | -                r = {"@graph": r}  | 
36 |  | -            r["id"] = imp  | 
37 |  | -            _, frag = urlparse.urldefrag(imp)  | 
38 |  | -            if frag:  | 
39 |  | -                frag = "#" + frag  | 
40 |  | -                r = findId(r, frag)  | 
41 |  | -            return _draft2toDraft3(r, loader, imp)  | 
42 |  | - | 
43 |  | -        if "include" in doc:  | 
44 |  | -            return loader.fetch_text(urlparse.urljoin(baseuri, doc["include"]))  | 
45 |  | - | 
46 |  | -        for t in ("type", "items"):  | 
47 |  | -            if t in doc:  | 
48 |  | -                doc[t] = fixType(doc[t])  | 
49 |  | - | 
50 |  | -        if "steps" in doc:  | 
51 |  | -            for i, s in enumerate(doc["steps"]):  | 
52 |  | -                if "id" not in s:  | 
53 |  | -                    s["id"] = "step%i" % i  | 
54 |  | -                for inp in s.get("inputs", []):  | 
55 |  | -                    if isinstance(inp.get("source"), list):  | 
56 |  | -                        if "requirements" not in doc:  | 
57 |  | -                            doc["requirements"] = []  | 
58 |  | -                        doc["requirements"].append({"class": "MultipleInputFeatureRequirement"})  | 
59 |  | - | 
60 |  | - | 
61 |  | -        for a in doc:  | 
62 |  | -            doc[a] = _draft2toDraft3(doc[a], loader, baseuri)  | 
63 |  | - | 
64 |  | -    if isinstance(doc, list):  | 
65 |  | -        return [_draft2toDraft3(a, loader, baseuri) for a in doc]  | 
66 |  | - | 
67 |  | -    return doc  | 
 | 31 | +    try:  | 
 | 32 | +        if isinstance(doc, dict):  | 
 | 33 | +            if "import" in doc:  | 
 | 34 | +                imp = urlparse.urljoin(baseuri, doc["import"])  | 
 | 35 | +                r = loader.fetch(imp)  | 
 | 36 | +                if isinstance(r, list):  | 
 | 37 | +                    r = {"@graph": r}  | 
 | 38 | +                r["id"] = imp  | 
 | 39 | +                _, frag = urlparse.urldefrag(imp)  | 
 | 40 | +                if frag:  | 
 | 41 | +                    frag = "#" + frag  | 
 | 42 | +                    r = findId(r, frag)  | 
 | 43 | +                return _draft2toDraft3(r, loader, imp)  | 
 | 44 | + | 
 | 45 | +            if "include" in doc:  | 
 | 46 | +                return loader.fetch_text(urlparse.urljoin(baseuri, doc["include"]))  | 
 | 47 | + | 
 | 48 | +            for t in ("type", "items"):  | 
 | 49 | +                if t in doc:  | 
 | 50 | +                    doc[t] = fixType(doc[t])  | 
 | 51 | + | 
 | 52 | +            if "steps" in doc:  | 
 | 53 | +                if not isinstance(doc["steps"], list):  | 
 | 54 | +                    raise Exception("Value of 'steps' must be a list")  | 
 | 55 | +                for i, s in enumerate(doc["steps"]):  | 
 | 56 | +                    if "id" not in s:  | 
 | 57 | +                        s["id"] = "step%i" % i  | 
 | 58 | +                    for inp in s.get("inputs", []):  | 
 | 59 | +                        if isinstance(inp.get("source"), list):  | 
 | 60 | +                            if "requirements" not in doc:  | 
 | 61 | +                                doc["requirements"] = []  | 
 | 62 | +                            doc["requirements"].append({"class": "MultipleInputFeatureRequirement"})  | 
 | 63 | + | 
 | 64 | + | 
 | 65 | +            for a in doc:  | 
 | 66 | +                doc[a] = _draft2toDraft3(doc[a], loader, baseuri)  | 
 | 67 | + | 
 | 68 | +        if isinstance(doc, list):  | 
 | 69 | +            return [_draft2toDraft3(a, loader, baseuri) for a in doc]  | 
 | 70 | + | 
 | 71 | +        return doc  | 
 | 72 | +    except Exception as e:  | 
 | 73 | +        err = json.dumps(doc, indent=4)  | 
 | 74 | +        if "id" in doc:  | 
 | 75 | +            err = doc["id"]  | 
 | 76 | +        elif "name" in doc:  | 
 | 77 | +            err = doc["name"]  | 
 | 78 | +        raise Exception("Error updating '%s'\n  %s" % (err, e))  | 
68 | 79 | 
 
  | 
69 | 80 | def draft2toDraft3(doc, loader, baseuri):  | 
70 | 81 |     return (_draft2toDraft3(doc, loader, baseuri), "https://w3id.org/cwl/cwl#draft-3.dev1")  | 
 | 
0 commit comments