File: totalopenstation-cli-connector.py

package info (click to toggle)
totalopenstation 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,784 kB
  • sloc: python: 4,123; makefile: 84; xml: 50
file content (104 lines) | stat: -rwxr-xr-x 3,180 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# filename: totalopenstation-cli-connector.py
# Copyright 2021 Stefano Costa <steko@iosa.it>

# This file is part of Total Open Station.

# Total Open Station is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# Total Open Station is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Total Open Station.  If not, see
# <http://www.gnu.org/licenses/>.


import gettext
import sys
import os

from optparse import OptionParser

import serial

from totalopenstation.models import BUILTIN_MODELS


t = gettext.translation('totalopenstation', './locale', fallback=True)
_ = t.gettext

usage = _("Usage: %prog [option] arg1 [option] arg2 ...")

parser = OptionParser(usage=usage)
parser.add_option("-m",
                "--model",
                action="store",
                type="string",
                dest="model",
                help=_("Select input MODEL"),
                metavar="MODEL")
parser.add_option("-p",
                "--port",
                action="store",
                type="string",
                dest="port",
                help=_("Select input SERIAL PORT"),
                metavar="PORT")
parser.add_option("-o",
                "--outfile",
                action="store",
                type="string",
                dest="outfile",
                help=_("Select output FILE (do not specify for stdout)"),
                metavar="FILE")

(options, args) = parser.parse_args()

if not (options.model and options.port):
    sys.exit(_("Please specify your model and the port to download from"))

modelclass = BUILTIN_MODELS[options.model]

# import input format parser
if isinstance(modelclass, tuple):
    try:
        # builtin format parser
        mod, cls, name = modelclass
        modelclass = getattr(
            __import__('totalopenstation.models.%s' % mod, None, None, [cls]), cls)
    except ImportError as msg:
        sys.exit(_('Error loading the required model module: %s') % msg)

station = modelclass(options.port)
try:
    station.close()  # sometimes the port will be already open for no reason
    station.open()
except serial.SerialException as detail:
    sys.exit(detail)

print(_("Now you can start download from %s device") % options.model)

station.start()
station.dl_started.wait()
print(_("Download started..."))
station.dl_finished.wait()
print(_("Download finished..."))
result = station.result

if options.outfile:
    if not os.path.exists(options.outfile):
        e = open(options.outfile, "wb")
        e.write(result)
        e.close()
        print(_("Downloaded data saved to out file %s") % options.outfile)
    else:
        sys.exit(_("Specified output file already exists\n"))
else:
    sys.stdout.write(result)