Conditions | 15 |
Total Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Complex classes like cp() 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 python |
||
10 | @public |
||
11 | def cp(source,target,force=True): |
||
12 | if isinstance(source,list): # list |
||
13 | # copy files to dir |
||
14 | targets = [] |
||
15 | for s in source: |
||
16 | t = cp(s,target,force) |
||
17 | targets.append(t) |
||
18 | return targets |
||
19 | assert_exists(source) # assert exists |
||
20 | if not force and exists(target): return |
||
21 | if source==target: |
||
22 | return target |
||
23 | if isfile(source) and isdir(target): |
||
24 | # target is DIR |
||
25 | target = join(target,basename(source)) |
||
26 | if isfile(source) or islink(source): |
||
27 | if (exists(target) or lexists(target)) and isfile(source)!=isfile(target): |
||
28 | os.unlink(target) |
||
29 | shutil.copy(source,target) |
||
30 | if isdir(source): |
||
31 | # first create dirs |
||
32 | if not exists(target): |
||
33 | os.makedirs(target) |
||
34 | dir_util.copy_tree(source,target) |
||
35 | return target |
||
36 | |||
50 |