File: status.py

package info (click to toggle)
montage-wrapper 0.9.9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 1,092 kB
  • sloc: python: 8,325; makefile: 116; ansic: 88
file content (78 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import print_function

import hashlib

from astropy import log


def simplify(value):
    try:
        return int(value)
    except:
        return float(value)


class MontageError(Exception):
    pass


def parse_struct(command, string):

    # Convert bytes to string
    string = string.decode('ascii')

    if "\n" in string:
        result = []
        for substring in string.splitlines():
            if 'struct' in substring:
                result.append(Struct(command, substring))
            else:
                print(substring)
    else:
        if 'struct' in string:
            result = Struct(command, string)
        else:
            print(string)
            result = None

    if result:
        return result
    else:
        return


class Struct(object):

    def __init__(self, command, string):

        string = string[8:-1]

        strings = {}
        while True:
            try:
                p1 = string.index('"')
                p2 = string.index('"', p1 + 1)
                substring = string[p1 + 1:p2]
                key = hashlib.md5(substring.encode('ascii')).hexdigest()
                strings[key] = substring
                string = string[:p1] + key + string[p2 + 1:]
            except:
                break

        for pair in string.split(', '):
            key, value = pair.split('=')
            if value in strings:
                self.__dict__[key] = strings[value]
            else:
                self.__dict__[key] = simplify(value)

        if self.stat == "ERROR":
            raise MontageError("%s: %s" % (command, self.msg))
        elif self.stat == "WARNING":
            log.warn(self.msg)

    def __repr__(self):
        string = ""
        for item in self.__dict__:
            string += item + " : " + str(self.__dict__[item]) + "\n"
        return string[:-1]