Example #1
0
def scigraph_stress(rate,
                    timeout=5,
                    verbose=False,
                    debug=False,
                    scigraph=auth.get('scigraph-api')):
    # TODO use the api classes
    with open((auth.get_path('resources') / 'chebi-subset-ids.txt').as_posix(),
              'rt') as f:
        urls = [
            os.path.join(scigraph, f'vocabulary/id/{curie.strip()}')
            for curie in f.readlines()
        ]
    print(urls)
    url_blaster(urls, rate, timeout, verbose, debug)
Example #2
0
    def gencode(self):
        """ Run this to generate the code """
        resp = requests.get(self.api_url)
        if not resp.ok:
            if resp.status_code == 401 and 'scicrunch.org' in self.api_url:
                resp = requests.get(
                    self.api_url,
                    params={'key': auth.get('scigraph-api-key')})
            else:
                resp.raise_for_status()

        ledict = resp.json()
        for durl in self._dynamics:
            dj = requests.get(durl).json()
            for p in dj['paths']:
                if p.startswith('/dynamic') and p not in ledict['paths']:
                    ledict['paths'][p] = dj['paths'][p]

        ledict = self.dotopdict(ledict)
        out = self.dodict(ledict)
        self._code = out
Example #3
0
def main():
    from docopt import docopt, parse_defaults
    args = docopt(__doc__, version='ontutils 0.0.1')
    defaults = {
        o.name: o.value if o.argcount else None
        for o in parse_defaults(__doc__)
    }

    verbose = args['--verbose']
    debug = args['--debug']

    repo_name = args['<repo>']

    git_local = os.path.expanduser(args['--git-local'])

    epoch = args['--epoch']

    curies_location = args['--curies']
    curies = getCuries(curies_location)
    curie_prefixes = set(curies.values())

    filenames = args['<file>']
    filenames.sort(key=lambda f: os.path.getsize(f),
                   reverse=True)  # make sure the big boys go first
    refactor_skip = ('nif.ttl', 'resources.ttl', 'generated/chebislim.ttl',
                     'unused/ro_bfo_bridge.ttl', 'generated/ncbigeneslim.ttl',
                     'generated/NIF-NIFSTD-mapping.ttl')
    rfilenames = [f for f in filenames if f not in refactor_skip]

    if args['set']:
        from pyontutils.config import auth
        uc = auth.user_config

        def set_uc(var, value):
            with open(uc._path, 'rt') as f:
                text = f.read()

            if '#' in text:
                msg = f'Comments detected! Not writing config! {uc._path}'
                raise ValueError(msg)

            blob = uc.load()
            # XXX NEVER DUMP A CONFIG THIS YOU _WILL_ KLOBBER IT
            # BY ACCIDENT AT SOME POINT AND WILL ERASE ANY/ALL COMMENTS
            # THERE IS NO SAFETY WITH THIS IMPLEMENTATION
            # USERS SHOULD EDIT THEIR CONFIGS DIRECTLY
            # except that it makes giving instructions for
            # setting values a bit more complicated
            blob['auth-variables'][var] = value
            uc.dump(blob)

        if args['ontology-local-repo']:
            var = 'ontology-local-repo'
            olr = Path(args['<path>']).expanduser().resolve()
            olr_string = olr.as_posix()
            set_uc(var, olr_string)
            value2 = auth.get_path(var)
            if not value2.exists():
                msg = f'{var} path does not exist! {value2}'
                print(tc.red('WARNING'), msg)

            msg = f'{var} path {value2} written to {uc._path}'
            print(msg)
            assert olr == value2

        elif args['scigraph-api-key']:
            # FIXME this is a hack on top of orthauth, which will not
            #
            # check the secrets path first to make sure it is ok
            # be implementing programmtic modification of user config
            # files any time soon, though it might make sense to have a
            # "machine config path" in addition to auth and user config
            path = ['scigraph', 'api', 'key']
            spath = auth._pathit(uc.get_blob('auth-stores', 'secrets')['path'])
            if not spath.parent.exists():
                spath.parent.mkdir(parents=True)
                spath.parent.chmod(0o0700)
            if spath.suffix != '.yaml':
                msg = f"Can't write secrets file of type {spath.suffix}"
                args = None
                raise NotImplementedError(msg)

            v = None
            try:
                s = uc.secrets
                v = s(*path)
            except:
                pass

            if v is not None:
                v = None
                raise ValueError(f'Path already in secrets! {path} in {spath}')

            # safely append to the secrets file
            key = args['<key>']
            path_key = f'\nscigraph:\n  api:\n    key: {key}'
            if not spath.exists():
                spath.touch()
                spath.chmod(0o0600)

            with open(spath, 'a+') as f:
                f.write(path_key)

            # set the config var
            var = 'scigraph-api-key'
            value = {'path': ' '.join(path)}
            set_uc(var, value)  # set the path
            # XXX NOTE yes, it is correct to do this only after secrets succeeds
            # otherwise it is possible to get into a state where secrets does
            # not exist but there is a path pointing to it, so load this
            # ontutils file will fail during import time

            # test that we got the value we expected
            value2 = auth.get(var)
            msg = (f'Key written to secrets. {spath} and path to '
                   f'key was written to config {uc._path}')
            print(msg)
            assert key == value2, 'Key retrieved does not match key set!'

    elif args['devconfig']:
        if args['--write']:
            file = devconfig.write(args['--output-file'])
            print(f'config written to {file}')
        elif args['<field>']:
            for f in args['<field>']:
                print(getattr(devconfig, f, ''))
        else:
            print(devconfig)
    elif args['catalog-extras']:
        catalog_extras(args['--fetch'])
    elif args['version-iri']:
        version_iris(*filenames, epoch=epoch)
    elif args['scigraph-stress']:
        scigraph_stress(int(args['--rate']), int(args['--timeout']), verbose,
                        debug)
    elif args['deadlinks']:
        deadlinks(filenames, int(args['--rate']), int(args['--timeout']),
                  verbose, debug)
    elif args['spell']:
        spell(filenames, debug)
    elif args['iri-commit']:
        make_git_commit_command(git_local, repo_name)
    elif args['uri-switch']:
        uri_switch(rfilenames, uri_switch_values)
    elif args['backend-refactor']:
        backend_refactor(rfilenames, backend_refactor_values)
    elif args['todo']:
        graph = loadall(git_local, repo_name, local=True)
        graph_todo(graph, curie_prefixes, uri_switch_values)
        breakpoint()
    elif args['expand']:
        curies['NLXWIKI'] = 'http://legacy.neurolex.org/wiki/'
        for curie in args['<curie>']:
            prefix, suffix = curie.split(':')
            print(curies[prefix] + suffix)