newportxps 0.3.0__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.
newportxps/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .newportxps import NewportXPS
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env python
2
+ from __future__ import print_function
3
+
4
+ import time
5
+
6
+ class debugtime(object):
7
+ def __init__(self, verbose=False):
8
+ self.clear()
9
+ self.verbose = verbose
10
+ self.add('init')
11
+
12
+ def clear(self):
13
+ self.times = []
14
+
15
+ def add(self,msg=''):
16
+ if self.verbose:
17
+ print(msg, time.ctime())
18
+ self.times.append((msg,time.time()))
19
+
20
+ def get_report(self):
21
+ m0, t0 = self.times[0]
22
+ tlast= t0
23
+ out = []
24
+ add = out.append
25
+ add("# %s %s " % (m0,time.ctime(t0)))
26
+ add("#----------------")
27
+ add("# Message Total Delta")
28
+ for m,t in self.times[1:]:
29
+ tt = t-t0
30
+ dt = t-tlast
31
+ if len(m)<32:
32
+ m = m + ' '*(32-len(m))
33
+ add(" %32s %.3f %.3f" % (m,tt, dt))
34
+ tlast = t
35
+ return "\n".join(out)
36
+
37
+ def show(self, clear=True):
38
+ print(self.get_report())
39
+ if clear:
40
+ self.clear()
41
+
42
+ def save(self, fname='debugtimer.dat'):
43
+ dat = self.get_report()
44
+ with open(fname, 'w') as fh:
45
+ fh.write('%s\n' % dat)
46
+
47
+
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/env python
2
+
3
+ import ftplib
4
+ from io import BytesIO
5
+ from .utils import str2bytes, bytes2str, ENCODING
6
+
7
+ import logging
8
+ logger = logging.getLogger('paramiko')
9
+ logger.setLevel(logging.ERROR)
10
+
11
+
12
+ HAS_PYSFTP = False
13
+ try:
14
+ import pysftp
15
+ HAS_PYSFTP = True
16
+ except ImportError:
17
+ pass
18
+
19
+ class FTPBaseWrapper(object):
20
+ """base clase for ftp interactions for Newport XPS
21
+ needs to be overwritten -- use SFTPWrapper or FTPWrapper"""
22
+ def __init__(self, host=None, username='Administrator',
23
+ password='Administrator'):
24
+ self.host = host
25
+ self.username = username
26
+ self.password = password
27
+ self._conn = None
28
+
29
+ def close(self):
30
+ if self._conn is not None:
31
+ self._conn.close()
32
+ self._conn = None
33
+
34
+ def cwd(self, remotedir):
35
+ self._conn.cwd(remotedir)
36
+
37
+ def connect(self, host=None, username=None, password=None):
38
+ raise NotImplementedError
39
+
40
+ def save(self, remotefile, localfile):
41
+ "save remote file to local file"
42
+ raise NotImplementedError
43
+
44
+ def getlines(self, remotefile):
45
+ "read text of remote file"
46
+ raise NotImplementedError
47
+
48
+ def put(self, text, remotefile):
49
+ "put text to remote file"
50
+ raise NotImplementedError
51
+
52
+
53
+ class SFTPWrapper(FTPBaseWrapper):
54
+ """wrap ftp interactions for Newport XPS models D"""
55
+ def __init__(self, host=None, username='Administrator',
56
+ password='Administrator'):
57
+ FTPBaseWrapper.__init__(self, host=host,
58
+ username=username, password=password)
59
+
60
+ def connect(self, host=None, username=None, password=None):
61
+ if host is not None:
62
+ self.host = host
63
+ if username is not None:
64
+ self.username = username
65
+ if password is not None:
66
+ self.password = password
67
+
68
+ if not HAS_PYSFTP:
69
+ raise ValueError("pysftp not installed.")
70
+ try:
71
+ self._conn = pysftp.Connection(self.host,
72
+ username=self.username,
73
+ password=self.password)
74
+ except:
75
+ print("ERROR: sftp connection to %s failed" % self.host)
76
+ print("You may need to add the host keys for your XPS to your")
77
+ print("ssh known_hosts file, using a command like this:")
78
+ print(" ssh-keyscan %s >> ~/.ssh/known_hosts" % self.host)
79
+
80
+
81
+ def save(self, remotefile, localfile):
82
+ "save remote file to local file"
83
+ self._conn.get(remotefile, localfile)
84
+
85
+ def getlines(self, remotefile):
86
+ "read text of remote file"
87
+ tmp = BytesIO()
88
+ self._conn.getfo(remotefile, tmp)
89
+ tmp.seek(0)
90
+ text = bytes2str(tmp.read())
91
+ return text.split('\n')
92
+
93
+ def put(self, text, remotefile):
94
+ txtfile = BytesIO(str2bytes(text))
95
+ self._conn.putfo(txtfile, remotefile)
96
+
97
+
98
+ class FTPWrapper(FTPBaseWrapper):
99
+ """wrap ftp interactions for Newport XPS models C and Q"""
100
+ def __init__(self, host=None, username='Administrator',
101
+ password='Administrator'):
102
+ FTPBaseWrapper.__init__(self, host=host,
103
+ username=username, password=password)
104
+
105
+ def connect(self, host=None, username=None, password=None):
106
+ if host is not None:
107
+ self.host = host
108
+ if username is not None:
109
+ self.username = username
110
+ if password is not None:
111
+ self.password = password
112
+
113
+ self._conn = ftplib.FTP()
114
+ self._conn.connect(self.host)
115
+ self._conn.login(self.username, self.password)
116
+
117
+ def list(self):
118
+ "list files in a given directory (default the current)"
119
+ return self._conn.nlst()
120
+
121
+ def save(self, remotefile, localfile):
122
+ "save remote file to local file"
123
+ output = []
124
+ self._conn.retrbinary(f'RETR {remotefile}', output.append)
125
+ with open(localfile, 'w', encoding=ENCODING) as fout:
126
+ fout.write(''.join([bytes2str(s) for s in output]))
127
+
128
+ def getlines(self, remotefile):
129
+ "read text of remote file"
130
+ output = []
131
+ self._conn.retrbinary('RETR %s' % remotefile, output.append)
132
+ text = ''.join([bytes2str(line) for line in output])
133
+ return text.split('\n')
134
+
135
+ def put(self, text, remotefile):
136
+ txtfile = BytesIO(str2bytes(text))
137
+ self._conn.storbinary('STOR %s' % remotefile, txtfile)
138
+
139
+ def delete(self, remotefile):
140
+ "delete remote file"
141
+ self._conn.delete(remotefile)