Conditions | 13 |
Total Lines | 62 |
Code Lines | 45 |
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 qtsass.cli.main() 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 |
||
70 | def main(): |
||
71 | """CLI entry point.""" |
||
72 | args = create_parser().parse_args() |
||
73 | |||
74 | # Setup CLI logging |
||
75 | debug = os.environ.get('QTSASS_DEBUG', args.debug) |
||
76 | if debug in ('1', 'true', 'True', 'TRUE', 'on', 'On', 'ON', True): |
||
77 | level = logging.DEBUG |
||
78 | else: |
||
79 | level = logging.INFO |
||
80 | enable_logging(level) |
||
81 | |||
82 | # Add a StreamHandler |
||
83 | handler = logging.StreamHandler() |
||
84 | if level == logging.DEBUG: |
||
85 | fmt = '%(levelname)-8s: %(name)s> %(message)s' |
||
86 | handler.setFormatter(logging.Formatter(fmt)) |
||
87 | logging.root.addHandler(handler) |
||
88 | logging.root.setLevel(level) |
||
89 | |||
90 | file_mode = os.path.isfile(args.input) |
||
91 | dir_mode = os.path.isdir(args.input) |
||
92 | |||
93 | if file_mode and not args.output: |
||
94 | with open(args.input, 'r') as f: |
||
95 | string = f.read() |
||
96 | |||
97 | css = compile( |
||
98 | string, |
||
99 | include_paths=os.path.abspath(os.path.dirname(args.input)), |
||
100 | ) |
||
101 | print(css) |
||
102 | sys.exit(0) |
||
103 | |||
104 | elif file_mode: |
||
105 | _log.debug('compile_filename({}, {})'.format(args.input, args.output)) |
||
106 | compile_filename(args.input, args.output) |
||
107 | |||
108 | elif dir_mode and not args.output: |
||
109 | print('Error: missing required option: -o/--output') |
||
110 | sys.exit(1) |
||
111 | |||
112 | elif dir_mode: |
||
113 | _log.debug('compile_dirname({}, {})'.format(args.input, args.output)) |
||
114 | compile_dirname(args.input, args.output) |
||
115 | |||
116 | else: |
||
117 | print('Error: input must be a file or a directory') |
||
118 | sys.exit(1) |
||
119 | |||
120 | if args.watch: |
||
121 | _log.info('qtsass is watching {}...'.format(args.input)) |
||
122 | |||
123 | watcher = watch(args.input, args.output) |
||
124 | watcher.start() |
||
125 | try: |
||
126 | while True: |
||
127 | time.sleep(0.5) |
||
128 | except KeyboardInterrupt: |
||
129 | watcher.stop() |
||
130 | watcher.join() |
||
131 | sys.exit(0) |
||
132 |