Skip to content

Commit

Permalink
resmgr: show shorthand location in list-installed
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Jan 21, 2021
1 parent fd8ca26 commit a3cff9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
15 changes: 10 additions & 5 deletions ocrd/ocrd/cli/resmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@

config = load_config_file()

def print_resources(executable, reslist):
def print_resources(executable, reslist, resmgr):
print('%s' % executable)
for resdict in reslist:
print('- %s (%s)\n %s' % (resdict['name'], resdict['url'], resdict['description']))
print('- %s %s (%s)\n %s' % (
resdict['name'],
'@ %s' % resmgr.resource_dir_to_location(resdict['path']) if 'path' in resdict else '',
resdict['url'],
resdict['description']
))
print()

@click.group("resmgr")
Expand All @@ -43,7 +48,7 @@ def list_available(executable=None):
"""
resmgr = OcrdResourceManager()
for executable, reslist in resmgr.list_available(executable):
print_resources(executable, reslist)
print_resources(executable, reslist, resmgr)

@resmgr_cli.command('list-installed')
@click.option('-e', '--executable', help='Show only resources for executable EXEC', metavar='EXEC')
Expand All @@ -54,7 +59,7 @@ def list_installed(executable=None):
resmgr = OcrdResourceManager()
ret = []
for executable, reslist in resmgr.list_installed(executable):
print_resources(executable, reslist)
print_resources(executable, reslist, resmgr)

@resmgr_cli.command('download')
@click.option('-n', '--any-url', help='Allow downloading/copying unregistered resources', is_flag=True)
Expand All @@ -74,7 +79,7 @@ def download(any_url, overwrite, location, executable, url_or_name):
"""
log = getLogger('ocrd.cli.resmgr')
resmgr = OcrdResourceManager()
basedir = resmgr.get_resource_dir(location)
basedir = resmgr.location_to_resource_dir(location)
is_url = url_or_name.startswith('https://') or url_or_name.startswith('http://')
is_filename = Path(url_or_name).exists()
find_kwargs = {'executable': executable}
Expand Down
15 changes: 12 additions & 3 deletions ocrd/ocrd/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def list_installed(self, executable=None):
if not resdict:
self.log.info("%s resource '%s' (%s) not a known resource, creating stub in %s'" % (this_executable, res_name, res_filename, self.user_list))
resdict = [self.add_to_user_database(this_executable, res_filename)]
resdict[0]['path'] = res_filename
reslist.append(resdict[0])
ret.append((this_executable, reslist))
return ret
Expand All @@ -103,7 +104,7 @@ def add_to_user_database(self, executable, res_filename, url=None):
resdict = {
'name': res_name,
'url': url if url else '???',
'description': 'Found at %s on %s' % (res_filename, datetime.now()),
'description': 'Found at %s on %s' % (self.resource_dir_to_location(res_filename), datetime.now()),
'version_range': '???',
'size': res_size
}
Expand Down Expand Up @@ -133,17 +134,25 @@ def find_resources(self, executable=None, name=None, url=None, database=None):
ret.append((executable, resdict))
return ret

def get_resource_dir(self, location):
def location_to_resource_dir(self, location):
return join(VIRTUAL_ENV, 'share', 'ocrd-resources') if location == 'virtualenv' and VIRTUAL_ENV else \
join(XDG_CACHE_HOME, 'ocrd-resources') if location == 'cache' else \
join(XDG_DATA_HOME, 'ocrd-resources') if location == 'data' else \
join(XDG_CONFIG_HOME, 'ocrd-resources') if location == 'config' else \
getcwd()

def resource_dir_to_location(self, resource_path):
resource_path = str(resource_path)
return 'virtualenv' if VIRTUAL_ENV and resource_path.startswith(join(VIRTUAL_ENV, 'share', 'ocrd-resources')) else \
'cache' if resource_path.startswith(join(XDG_CACHE_HOME, 'ocrd-resources')) else \
'data' if resource_path.startswith(join(XDG_DATA_HOME, 'ocrd-resources')) else \
'config' if resource_path.startswith(join(XDG_CONFIG_HOME, 'ocrd-resources')) else \
resource_path

@property
def default_resource_dir(self):
config = load_config_file()
return self.get_resource_dir(config.resource_location)
return self.location_to_resource_dir(config.resource_location)

def parameter_usage(self, name, usage='as-is'):
if usage == 'as-is':
Expand Down

0 comments on commit a3cff9e

Please sign in to comment.