APICommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
dl 0
loc 36
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add_arguments() 0 8 1
A http_error() 0 4 2
A cprint() 0 3 2
A parse_options() 0 11 3
A cout() 0 3 2
1
from django.core.management.base import BaseCommand, CommandError
2
import requests
3
import os
4
import sys
5
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")