OSDN Git Service

rename commonutils.py to utils/common.py
[ti2/ti2.git] / linkpair / utils / common.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 verbose_out(self, msg):
23         if self.opts.verbose is True:
24             print msg
25
26     def debug_out(self, msg):
27         if self.opts.debug is True:
28             print "debug: " + msg
29
30     def message_out(self, msg):
31         print msg
32
33     def parse_remote_desc(self, remote_desc):
34         ssh_username = ""
35         ssh_hostname = ""
36         ssh_hostport = 22
37
38         if remote_desc is not None:
39             if self.d_push(re.match(r'(.+?)@(.+?):(.+)', remote_desc)):
40                 user_info = self.d_pop()
41                 ssh_username = user_info.group(1)
42                 ssh_hostname = user_info.group(2)
43                 ssh_hostport = int(user_info.group(3))
44             elif self.d_push(re.match(r'(.+?)@(.+)', remote_desc)):
45                 user_info = self.d_pop()
46                 ssh_username = user_info.group(1)
47                 ssh_hostname = user_info.group(2)
48                 ssh_hostport = 22
49         return [ssh_username, ssh_hostname, ssh_hostport]
50
51     def str_join(self, base, added, sep):
52         if base is None or base == "":
53             return added
54         else:
55             return base + sep + added