dump-things-pyclient 0.2.5__py3-none-any.whl → 0.2.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,24 @@
1
1
  import json
2
2
  import logging
3
3
  import sys
4
+ from itertools import count
4
5
 
5
6
  import rich_click as click
7
+ from rich.console import Console
8
+ from rich.progress import track
6
9
 
7
10
  from ...communicate import (
8
11
  HTTPError,
9
12
  curated_write_record,
10
13
  collection_write_record,
14
+ get_session,
11
15
  )
12
16
 
13
17
 
14
18
  logger = logging.getLogger('post-records')
15
19
 
20
+ console = Console(file=sys.stderr)
21
+
16
22
 
17
23
  @click.command(short_help='Post records to an inbox or the curated area of a dump-things collection')
18
24
  @click.pass_obj
@@ -81,7 +87,7 @@ def post_records(
81
87
  ):
82
88
  token = obj
83
89
  if token is None:
84
- click.echo('ERROR: no token provided', err=True)
90
+ console.print('[red]Error[/red]: No token provided')
85
91
  return 1
86
92
 
87
93
  if curated:
@@ -89,9 +95,17 @@ def post_records(
89
95
  else:
90
96
  write_record = collection_write_record
91
97
 
92
- posted = False
93
- for line in sys.stdin:
94
- record = json.loads(line)
98
+ session = get_session()
99
+ for index, line in zip(count(), track(sys.stdin, console=console)):
100
+
101
+ try:
102
+ record = json.loads(line)
103
+ except Exception as e:
104
+ console.print(
105
+ f'[red]Error: reading JSON record #{index} failed[/red]: {e}',
106
+ )
107
+ continue
108
+
95
109
  try:
96
110
  write_record(
97
111
  service_url=service_url,
@@ -99,17 +113,16 @@ def post_records(
99
113
  class_name=cls,
100
114
  record=record,
101
115
  token=token,
116
+ session=session,
117
+ )
118
+ except HTTPError as e:
119
+ console.print(
120
+ f'[red]Error: writing record #{index} with pid {record["pid"]} failed[/red]: {e}: {e.response.text}',
102
121
  )
103
122
  except Exception as e:
104
- click.echo(f'ERROR: {e}', err=True)
105
- else:
106
- posted = True
107
- click.echo('.', nl=False)
108
-
109
- if posted:
110
- # echo a final newline
111
- click.echo('')
112
-
123
+ console.print(
124
+ f'[red]Error: writing record #{index} with pid {record["pid"]} failed[/red]: {e}',
125
+ )
113
126
  return 0
114
127
 
115
128
 
@@ -1,16 +1,21 @@
1
1
  import json
2
2
  import logging
3
+ import sys
3
4
 
4
5
  import rich_click as click
6
+ from rich.console import Console
5
7
 
6
8
  from ...communicate import (
7
9
  HTTPError,
8
10
  get_paginated,
11
+ get_session,
9
12
  )
10
13
 
11
14
 
12
15
  logger = logging.getLogger('read-pages')
13
16
 
17
+ console = Console(file=sys.stderr)
18
+
14
19
 
15
20
  @click.command(short_help='Read records from paginated dump-things endpoints')
16
21
  @click.pass_obj
@@ -106,8 +111,9 @@ def read_pages(
106
111
  token = obj
107
112
 
108
113
  if token is None:
109
- click.echo(f'WARNING: no token provided', err=True)
114
+ console.print(f'[yellow]Warning[/yellow]: no token provided')
110
115
 
116
+ session = get_session()
111
117
  result = get_paginated(
112
118
  url=url,
113
119
  token=token,
@@ -121,7 +127,8 @@ def read_pages(
121
127
  if matching is not None
122
128
  else {}
123
129
  ),
124
- }
130
+ },
131
+ session=session,
125
132
  )
126
133
 
127
134
  if stats: