Total Complexity | 10 |
Total Lines | 36 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from django.core.management.base import BaseCommand, CommandError |
||
6 | class APICommand(BaseCommand): |
||
7 | help = 'Base API command' |
||
8 | |||
9 | def parse_options(self, options): |
||
10 | auth = options['auth'].split(':') |
||
11 | self.auth = (auth[0], auth[1]) |
||
12 | self.verbosity = options.get('verbosity', 1) |
||
13 | host = options['host'] |
||
14 | if not host.endswith('/'): |
||
15 | host = host + '/' |
||
16 | if not host.startswith('http://'): |
||
17 | host = 'http://' + host |
||
18 | self.host = host |
||
19 | self.filename = options['filename'] |
||
20 | |||
21 | def http_error(self, response): |
||
22 | with open('error.html', "w") as f: |
||
23 | f.write(response.text) |
||
24 | raise ValueError("Error type " + str(response.status_code) + " file written: error.html") |
||
25 | |||
26 | def cprint(self, msg): |
||
27 | if self.verbosity > 0: |
||
28 | print(msg) |
||
29 | |||
30 | def cout(self, msg): |
||
31 | if self.verbosity > 0: |
||
32 | sys.stdout.write(msg) |
||
33 | |||
34 | def add_arguments(self, parser): |
||
35 | parser.add_argument('auth', type=str, |
||
36 | help='authentication in the form user:password') |
||
37 | parser.add_argument('--host', type=str, |
||
38 | help='server to pull from', |
||
39 | default='http://127.0.0.1:8000/api/') |
||
40 | parser.add_argument('--filename', type=str, |
||
41 | default="data/download.json") |