OSDN Git Service

1b28b7fb6bae640c46e11d241d5b5402150f1a7d
[ti2/ti2.git] / linkpair / commonutils.py
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
3 #
4
5 __version__ = '1.1'
6
7 import re
8
9
10 class CommonUtils(object):
11
12     def __init__(self):
13         self._tmp_stack = []
14
15     def d_push(self, x):
16         self._tmp_stack.append(x)
17         return x
18
19     def d_pop(self):
20         return self._tmp_stack.pop()
21
22     def parse_remote_desc(self, remote_desc):
23         ssh_username = ""
24         ssh_hostname = ""
25         ssh_hostport = 22
26
27         if remote_desc is not None:
28             if self.d_push(re.match(r'(.+?)@(.+?):(.+)', remote_desc)):
29                 user_info = self.d_pop()
30                 ssh_username = user_info.group(1)
31                 ssh_hostname = user_info.group(2)
32                 ssh_hostport = int(user_info.group(3))
33             elif self.d_push(re.match(r'(.+?)@(.+)', remote_desc)):
34                 user_info = self.d_pop()
35                 ssh_username = user_info.group(1)
36                 ssh_hostname = user_info.group(2)
37                 ssh_hostport = 22
38         return [ssh_username, ssh_hostname, ssh_hostport]
39
40
41     def str_join(self, base, added, sep):
42         if base is None or base == "":
43             return added
44         else:
45             return base + sep + added
46