Conditions | 13 |
Total Lines | 55 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like file_sync_tool.transfer.process.synchronize() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python3 |
||
59 | def synchronize(origin_path, target_path, exclude, client=mode.Client.LOCAL, pseudo_client=None, force_remote=False): |
||
60 | """ |
||
61 | Using rsync command to synchronize files between systems |
||
62 | :param origin_path: String |
||
63 | :param target_path: String |
||
64 | :param exclude: List |
||
65 | :param client: String |
||
66 | :param pseudo_client: String Client, which will be forced as remote client. Necessary for proxy transfer. |
||
67 | :param force_remote: Boolean |
||
68 | :return: |
||
69 | """ |
||
70 | _remote_client = None |
||
71 | if force_remote: |
||
72 | remote_client.load_ssh_client_origin() |
||
73 | _origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
||
74 | _target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
||
75 | elif mode.is_remote(mode.Client.ORIGIN) and pseudo_client != mode.Client.TARGET: |
||
76 | _remote_client = mode.Client.ORIGIN |
||
77 | _origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
||
78 | _target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
||
79 | elif mode.is_remote(mode.Client.TARGET) and pseudo_client != mode.Client.ORIGIN: |
||
80 | _remote_client = mode.Client.TARGET |
||
81 | _origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
||
82 | _target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
||
83 | elif not mode.is_remote(mode.Client.TARGET) and not mode.is_remote(mode.Client.ORIGIN): |
||
84 | _origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
||
85 | _target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
||
86 | |||
87 | _origin_name = helper.get_ssh_host_name(mode.Client.ORIGIN, True) if _remote_client == mode.Client.ORIGIN else '' |
||
88 | _target_name = helper.get_ssh_host_name(mode.Client.TARGET, True) if _remote_client == mode.Client.TARGET else '' |
||
89 | |||
90 | if not system.config['mute']: |
||
91 | print( |
||
92 | f'{_origin_subject}' |
||
93 | f'{_origin_name}' |
||
94 | f'{output.CliFormat.BLACK}{origin_path}{output.CliFormat.ENDC}' |
||
95 | ) |
||
96 | |||
97 | print( |
||
98 | f'{_target_subject}' |
||
99 | f'{_target_name}' |
||
100 | f'{output.CliFormat.BLACK}{target_path}{output.CliFormat.ENDC}' |
||
101 | ) |
||
102 | |||
103 | _origin_user_host = utility.get_host(mode.Client.ORIGIN) if _remote_client == mode.Client.ORIGIN else '' |
||
104 | _target_user_host = utility.get_host(mode.Client.TARGET) if _remote_client == mode.Client.TARGET else '' |
||
105 | |||
106 | _output = mode.run_command( |
||
107 | f'{utility.get_password_environment(_remote_client)}rsync {utility.get_options()} ' |
||
108 | f'{utility.get_authorization(_remote_client)} {utility.get_excludes(exclude)}' |
||
109 | f'{_origin_user_host}{origin_path} {_target_user_host}{target_path}', |
||
110 | client, |
||
111 | True |
||
112 | ) |
||
113 | utility.read_stats(_output) |
||
114 | |||
115 |