Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf800bfc

Browse files
committed
move some code to dedicated modules
1 parent7153fd3 commitf800bfc

File tree

7 files changed

+144
-136
lines changed

7 files changed

+144
-136
lines changed

‎testgres/backup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
BACKUP_LOG_FILE, \
1717
DEFAULT_XLOG_METHOD
1818

19+
from .defaultsimportdefault_username
20+
1921
from .exceptionsimportBackupException
2022

2123
from .utilsimport \
2224
get_bin_path, \
23-
default_username, \
2425
execute_utility
2526

2627

‎testgres/cache.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
from .constsimportXLOG_CONTROL_FILE
1212

13+
from .defaultsimportgenerate_system_id
14+
1315
from .exceptionsimport \
1416
InitNodeException, \
1517
ExecUtilException
1618

1719
from .utilsimport \
1820
get_bin_path, \
19-
execute_utility, \
20-
generate_system_id
21+
execute_utility
2122

2223

2324
defcached_initdb(data_dir,logfile=None,params=None):

‎testgres/connection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
exceptImportError:
1010
raiseImportError("You must have psycopg2 or pg8000 modules installed")
1111

12+
from .defaultsimport \
13+
default_dbname, \
14+
default_username
15+
1216
fromenumimportEnum
1317

1418
from .exceptionsimportQueryException
1519

16-
from .utilsimport \
17-
default_dbname, \
18-
default_username
1920

2021
# export these exceptions
2122
InternalError=pglib.InternalError

‎testgres/decorators.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
importsix
2+
importfunctools
3+
4+
5+
defpositional_args_hack(*special_cases):
6+
"""
7+
Convert positional args described by
8+
'special_cases' into named args.
9+
10+
Example:
11+
@positional_args_hack(['abc'], ['def', 'abc'])
12+
def some_api_func(...)
13+
14+
This is useful for compatibility.
15+
"""
16+
17+
cases=dict()
18+
19+
forcaseinspecial_cases:
20+
k=len(case)
21+
assertknotinsix.iterkeys(cases),'len must be unique'
22+
cases[k]=case
23+
24+
defdecorator(function):
25+
@functools.wraps(function)
26+
defwrapper(*args,**kwargs):
27+
k=len(args)
28+
29+
ifkinsix.iterkeys(cases):
30+
case=cases[k]
31+
32+
foriinrange(0,k):
33+
arg_name=case[i]
34+
arg_val=args[i]
35+
36+
# transform into named
37+
kwargs[arg_name]=arg_val
38+
39+
# get rid of them
40+
args= []
41+
42+
returnfunction(*args,**kwargs)
43+
44+
returnwrapper
45+
46+
returndecorator
47+
48+
49+
defmethod_decorator(decorator):
50+
"""
51+
Convert a function decorator into a method decorator.
52+
"""
53+
54+
def_dec(func):
55+
def_wrapper(self,*args,**kwargs):
56+
@decorator
57+
defbound_func(*args2,**kwargs2):
58+
returnfunc.__get__(self,type(self))(*args2,**kwargs2)
59+
60+
# 'bound_func' is a closure and can see 'self'
61+
returnbound_func(*args,**kwargs)
62+
63+
# preserve docs
64+
functools.update_wrapper(_wrapper,func)
65+
66+
return_wrapper
67+
68+
# preserve docs
69+
functools.update_wrapper(_dec,decorator)
70+
71+
# change name for easier debugging
72+
_dec.__name__='method_decorator({})'.format(decorator.__name__)
73+
74+
return_dec

‎testgres/defaults.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
importdatetime
2+
importgetpass
3+
importos
4+
importstruct
5+
importuuid
6+
7+
8+
defdefault_dbname():
9+
"""
10+
Return default DB name.
11+
"""
12+
13+
return'postgres'
14+
15+
16+
defdefault_username():
17+
"""
18+
Return default username (current user).
19+
"""
20+
21+
returngetpass.getuser()
22+
23+
24+
defgenerate_app_name():
25+
"""
26+
Generate a new application name for node.
27+
"""
28+
29+
return'testgres-{}'.format(str(uuid.uuid4()))
30+
31+
32+
defgenerate_system_id():
33+
"""
34+
Generate a new 64-bit unique system identifier for node.
35+
"""
36+
37+
date1=datetime.datetime.utcfromtimestamp(0)
38+
date2=datetime.datetime.utcnow()
39+
40+
secs=int((date2-date1).total_seconds())
41+
usecs=date2.microsecond
42+
43+
# see pg_resetwal.c : GuessControlValues()
44+
system_id=0
45+
system_id|= (secs<<32)
46+
system_id|= (usecs<<12)
47+
system_id|= (os.getpid()&0xFFF)
48+
49+
# pack ULL in native byte order
50+
returnstruct.pack('=Q',system_id)

‎testgres/node.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
UTILS_LOG_FILE, \
3434
DEFAULT_XLOG_METHOD
3535

36+
from .decoratorsimport \
37+
method_decorator, \
38+
positional_args_hack
39+
40+
from .defaultsimport \
41+
default_dbname, \
42+
default_username, \
43+
generate_app_name
44+
3645
from .exceptionsimport \
3746
CatchUpException, \
3847
ExecUtilException, \
@@ -49,12 +58,7 @@
4958
pg_version_ge, \
5059
reserve_port, \
5160
release_port, \
52-
default_dbname, \
53-
default_username, \
54-
generate_app_name, \
55-
execute_utility, \
56-
method_decorator, \
57-
positional_args_hack
61+
execute_utility
5862

5963

6064
classNodeStatus(Enum):

‎testgres/utils.py

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
from __future__importdivision
44
from __future__importprint_function
55

6-
importfunctools
76
importio
87
importos
98
importport_for
10-
importsix
119
importsubprocess
1210
importsys
1311

@@ -16,6 +14,7 @@
1614
from .configimporttestgres_config
1715
from .exceptionsimportExecUtilException
1816

17+
1918
# rows returned by PG_CONFIG
2019
_pg_config_data= {}
2120

@@ -42,56 +41,6 @@ def release_port(port):
4241
bound_ports.remove(port)
4342

4443

45-
defdefault_dbname():
46-
"""
47-
Return default DB name.
48-
"""
49-
50-
return'postgres'
51-
52-
53-
defdefault_username():
54-
"""
55-
Return default username (current user).
56-
"""
57-
58-
importgetpass
59-
returngetpass.getuser()
60-
61-
62-
defgenerate_app_name():
63-
"""
64-
Generate a new application name for node.
65-
"""
66-
67-
importuuid
68-
return'testgres-{}'.format(str(uuid.uuid4()))
69-
70-
71-
defgenerate_system_id():
72-
"""
73-
Generate a new 64-bit unique system identifier for node.
74-
"""
75-
76-
importdatetime
77-
importstruct
78-
79-
date1=datetime.datetime.utcfromtimestamp(0)
80-
date2=datetime.datetime.utcnow()
81-
82-
secs=int((date2-date1).total_seconds())
83-
usecs=date2.microsecond
84-
85-
# see pg_resetwal.c : GuessControlValues()
86-
system_id=0
87-
system_id|= (secs<<32)
88-
system_id|= (usecs<<12)
89-
system_id|= (os.getpid()&0xFFF)
90-
91-
# pack ULL in native byte order
92-
returnstruct.pack('=Q',system_id)
93-
94-
9544
defexecute_utility(args,logfile=None):
9645
"""
9746
Execute utility (pg_ctl, pg_dump etc).
@@ -268,77 +217,5 @@ def file_tail(f, num_lines):
268217
buffers=int(buffers*max(2,num_lines/max(cur_lines,1)))
269218

270219

271-
defpositional_args_hack(*special_cases):
272-
"""
273-
Convert positional args described by
274-
'special_cases' into named args.
275-
276-
Example:
277-
@positional_args_hack(['abc'], ['def', 'abc'])
278-
def some_api_func(...)
279-
280-
This is useful for compatibility.
281-
"""
282-
283-
cases=dict()
284-
285-
forcaseinspecial_cases:
286-
k=len(case)
287-
assertknotinsix.iterkeys(cases),'len must be unique'
288-
cases[k]=case
289-
290-
defdecorator(function):
291-
@functools.wraps(function)
292-
defwrapper(*args,**kwargs):
293-
k=len(args)
294-
295-
ifkinsix.iterkeys(cases):
296-
case=cases[k]
297-
298-
foriinrange(0,k):
299-
arg_name=case[i]
300-
arg_val=args[i]
301-
302-
# transform into named
303-
kwargs[arg_name]=arg_val
304-
305-
# get rid of them
306-
args= []
307-
308-
returnfunction(*args,**kwargs)
309-
310-
returnwrapper
311-
312-
returndecorator
313-
314-
315-
defmethod_decorator(decorator):
316-
"""
317-
Convert a function decorator into a method decorator.
318-
"""
319-
320-
def_dec(func):
321-
def_wrapper(self,*args,**kwargs):
322-
@decorator
323-
defbound_func(*args2,**kwargs2):
324-
returnfunc.__get__(self,type(self))(*args2,**kwargs2)
325-
326-
# 'bound_func' is a closure and can see 'self'
327-
returnbound_func(*args,**kwargs)
328-
329-
# preserve docs
330-
functools.update_wrapper(_wrapper,func)
331-
332-
return_wrapper
333-
334-
# preserve docs
335-
functools.update_wrapper(_dec,decorator)
336-
337-
# change name for easier debugging
338-
_dec.__name__='method_decorator({})'.format(decorator.__name__)
339-
340-
return_dec
341-
342-
343220
defeprint(*args,**kwargs):
344221
print(*args,file=sys.stderr,**kwargs)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp