Skip to content

Commit

Permalink
Bug fixes and default filename extension change from feedback from PBlom
Browse files Browse the repository at this point in the history
  • Loading branch information
chetzer-ncpa committed Aug 19, 2024
1 parent 6a2dd87 commit c32cbd2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ncpa/g2s/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def timestr(self,time):

class NCPAPropG2SFormatter(G2SFormatter):
def __init__(self,*args,**kwargs):
self.ext = 'ncpaprop'
self.ext = 'met'
super().__init__(*args,**kwargs)

def filename(self,profile):
Expand Down
5 changes: 5 additions & 0 deletions ncpa/g2scli/management/commands/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
from io import StringIO
from urllib.error import HTTPError
import sys

from ncpa.g2s.formatters import G2SProfileDecoder, G2SFormatterFactory

Expand Down Expand Up @@ -65,13 +66,15 @@ def handle(self,*args,**options):

url = format_url('grid', time=t, **params)
holder = StringIO(initial_value='')
warnings = []
try:
request_and_write(
url,
out=holder,
timeout=timeout,
chunksize=chunksize,
encoding=encoding)

except HTTPError as err:
raise CommandError(f'Server returned error {err.code}: {err.reason}')
jsontext = holder.getvalue()
Expand All @@ -83,3 +86,5 @@ def handle(self,*args,**options):
if options['output']:
formatargs['output'] = options['output']
G2SFormatterFactory.factory.create(finalformat).format(grid,**formatargs)


19 changes: 12 additions & 7 deletions ncpa/g2scli/management/commands/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import copy
from io import StringIO
from urllib.error import HTTPError
import sys

from ncpa.g2s.formatters import G2SProfileDecoder, G2SFormatterFactory

from ncpa.g2scli.commands import BaseCommand, CommandError
from ncpa.g2scli.commands import BaseCommand
from ncpa.g2scli.settings import config
from ncpa.g2scli.urls import format_url
from ncpa.g2scli.options import add_date_arguments, parse_datetimes, add_line_location_arguments
Expand Down Expand Up @@ -61,6 +62,7 @@ def handle(self,*args,**options):
params['outputformat'] = tmpformat

linelist = []
warnings = []
for t in times:
url = format_url('line', time=t, **params)
holder = StringIO(initial_value='')
Expand All @@ -70,18 +72,21 @@ def handle(self,*args,**options):
timeout=timeout,
chunksize=chunksize,
encoding=encoding)

jsontext = holder.getvalue()
line = json.loads(jsontext,cls=G2SProfileDecoder)
linelist.append(line)
except HTTPError as err:
raise CommandError(f'Server returned error {err.code}: {err.reason}')
jsontext = holder.getvalue()
line = json.loads(jsontext,cls=G2SProfileDecoder)
linelist.append(line)
warnings.append(f'Server returned error {err.code}: {err.reason}')
holder.close()

formatargs={}
if options['output']:
formatargs['output'] = options['output']
G2SFormatterFactory.factory.create(finalformat).format(
linelist[0] if len(linelist) == 1 else linelist,
**formatargs)


if warnings:
sys.stderr.write('\n\nNOTE: Server returned the following errors:')
sys.stderr.write('\n'.join(warnings))
sys.stderr.write('\n')
18 changes: 14 additions & 4 deletions ncpa/g2scli/management/commands/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import json
import copy
from io import StringIO
import sys

from ncpa.g2scli.options import add_date_arguments, parse_datetimes, add_point_location_arguments
from ncpa.g2scli.commands import CommandError
from ncpa.g2scli.requests import request_and_write

ZIP_RE = re.compile('.*filename="?(.+\.zip)')
Expand Down Expand Up @@ -59,6 +59,7 @@ def handle(self,*args,**options):
params['outputformat'] = tmpformat

profiles = G2SProfileSet()
warnings = []
for t in times:
url = format_url('point', time=t, **params)
holder = StringIO()
Expand All @@ -68,13 +69,22 @@ def handle(self,*args,**options):
timeout=timeout,
chunksize=chunksize,
encoding=encoding)
profiles.append(json.loads(holder.getvalue(),cls=G2SProfileDecoder))
except HTTPError as err:
raise CommandError(f'Server returned error {err.code}: {err.reason}')
profiles.append(json.loads(holder.getvalue(),cls=G2SProfileDecoder))
# raise CommandError(f'Server returned error {err.code}: {err.reason}')
warnings.append(f'Error {err.code}: {err.reason}')
holder.close()

formatargs={}
if options['output']:
formatargs['output'] = options['output']
if len(profiles) == 1:
profiles = profiles[0]
G2SFormatterFactory.factory.create(finalformat).format(profiles,**formatargs)
G2SFormatterFactory.factory.create(finalformat).format(profiles,**formatargs)

if warnings:
sys.stderr.write('\n\nNOTE: Server returned the following errors:')
sys.stderr.write('\n'.join(warnings))
sys.stderr.write('\n')


2 changes: 1 addition & 1 deletion ncpa/object_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def create(self, key, **kwargs):
builder = self._builders.get(key)
if not builder:
raise ValueError(key)
return builder(**kwargs)
return builder(**kwargs)

0 comments on commit c32cbd2

Please sign in to comment.