annotate hglib/util.py @ 19:19d2c55c3928

util: introduce skiplines
author Idan Kamara <idankk86@gmail.com>
date Thu, 11 Aug 2011 15:42:58 +0300
parents 5882a698ad5c
children 3d7e0325ba1c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
1 import itertools, cStringIO
0
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
2
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
3 def grouper(n, iterable):
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
4 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] '''
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
5 args = [iter(iterable)] * n
79f88b4db15f Initial commit
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
6 return itertools.izip(*args)
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
7
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
8 def eatlines(s, n):
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
9 """
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
10 >>> eatlines("1\\n2", 1)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
11 '2'
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
12 >>> eatlines("1\\n2", 2)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
13 ''
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
14 >>> eatlines("1\\n2", 3)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
15 ''
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
16 >>> eatlines("1\\n2\\n3", 1)
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
17 '2\\n3'
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
18 """
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
19 cs = cStringIO.StringIO(s)
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
20
9
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
21 for line in cs:
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
22 n -= 1
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
23 if n == 0:
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
24 return cs.read()
5882a698ad5c util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents: 8
diff changeset
25 return ''
8
3ac38d500d68 move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents: 3
diff changeset
26
19
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
27 def skiplines(s, prefix):
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
28 """
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
29 Skip lines starting with prefix in s
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
30
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
31 >>> skiplines('a\\nb\\na\\n', 'a')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
32 'b\\na\\n'
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
33 >>> skiplines('a\\na\\n', 'a')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
34 ''
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
35 >>> skiplines('', 'a')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
36 ''
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
37 >>> skiplines('a\\nb', 'b')
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
38 'a\\nb'
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
39 """
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
40 cs = cStringIO.StringIO(s)
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
41
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
42 for line in cs:
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
43 if not line.startswith(prefix):
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
44 return line + cs.read()
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
45
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
46 return ''
19d2c55c3928 util: introduce skiplines
Idan Kamara <idankk86@gmail.com>
parents: 9
diff changeset
47
3
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
48 def cmdbuilder(name, *args, **kwargs):
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
49 """
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
50 A helper for building the command arguments
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
51
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
52 args are the positional arguments
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
53
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
54 kwargs are the options
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
55 keys that are single lettered are prepended with '-', others with '--',
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
56 underscores are replaced with dashes
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
57
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
58 keys with False boolean values are ignored, lists add the key multiple times
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
59
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
60 None arguments are skipped
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
61
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
62 >>> cmdbuilder('cmd', a=True, b=False, c=None)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
63 ['cmd', '-a']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
64 >>> cmdbuilder('cmd', long=True)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
65 ['cmd', '--long']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
66 >>> cmdbuilder('cmd', str='s')
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
67 ['cmd', '--str', 's']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
68 >>> cmdbuilder('cmd', d_ash=True)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
69 ['cmd', '--d-ash']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
70 >>> cmdbuilder('cmd', _=True)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
71 ['cmd', '-']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
72 >>> cmdbuilder('cmd', list=[1, 2])
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
73 ['cmd', '--list', '1', '--list', '2']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
74 >>> cmdbuilder('cmd', None)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
75 ['cmd']
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
76 """
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
77 cmd = [name]
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
78 for arg, val in kwargs.items():
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
79 if val is None:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
80 continue
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
81
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
82 arg = arg.replace('_', '-')
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
83 if arg != '-':
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
84 arg = '-' + arg if len(arg) == 1 else '--' + arg
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
85 if isinstance(val, bool):
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
86 if val:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
87 cmd.append(arg)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
88 elif isinstance(val, list):
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
89 for v in val:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
90 cmd.append(arg)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
91 cmd.append(str(v))
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
92 else:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
93 cmd.append(arg)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
94 cmd.append(str(val))
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
95
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
96 for a in args:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
97 if a is not None:
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
98 cmd.append(a)
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
99
d7903b923217 util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents: 0
diff changeset
100 return cmd