| Conditions | 11 |
| Total Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 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 | """CLI for accessing the gtk/tickit UIs implemented by this package.""" |
||
| 97 | @click.command(context_settings=dict(allow_extra_args=True)) |
||
| 98 | @click.option('--prog') |
||
| 99 | @click.option('--notify', '-n', default=False, is_flag=True) |
||
| 100 | @click.option('--listen', '-l') |
||
| 101 | @click.option('--connect', '-c') |
||
| 102 | @click.option('--profile', |
||
| 103 | default='disable', |
||
| 104 | type=click.Choice(['ncalls', 'tottime', 'percall', 'cumtime', |
||
| 105 | 'name', 'disable'])) |
||
| 106 | @click.option('config_file', '--config', type=click.Path(exists=True)) |
||
| 107 | @click.option('--detach/--no-detach', default=True, is_flag=True) |
||
| 108 | @click.pass_context |
||
| 109 | def main(ctx, prog, notify, listen, connect, profile, config_file, detach): |
||
| 110 | """Entry point.""" |
||
| 111 | |||
| 112 | if detach: |
||
| 113 | exit_code = detach_proc() |
||
| 114 | |||
| 115 | address = connect or listen |
||
| 116 | |||
| 117 | if address: |
||
| 118 | import re |
||
| 119 | p = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:\:\d{1,5})?$') |
||
| 120 | |||
| 121 | if p.match(address): |
||
| 122 | args = ('tcp',) |
||
| 123 | kwargs = {'address': address} |
||
| 124 | else: |
||
| 125 | args = ('socket',) |
||
| 126 | kwargs = {'path': address} |
||
| 127 | |||
| 128 | if connect: |
||
| 129 | # connect to existing instance listening on address |
||
| 130 | nvim = attach(*args, **kwargs) |
||
| 131 | elif listen: |
||
| 132 | # spawn detached instance listening on address and connect to it |
||
| 133 | import os |
||
| 134 | import time |
||
| 135 | from subprocess import Popen |
||
| 136 | os.environ['NVIM_LISTEN_ADDRESS'] = address |
||
| 137 | nvim_argv = shlex.split(prog or 'nvim --headless') + ctx.args |
||
| 138 | # spawn the nvim with stdio redirected to /dev/null. |
||
| 139 | dnull = open(os.devnull) |
||
| 140 | p = Popen(nvim_argv, stdin=dnull, stdout=dnull, stderr=dnull) |
||
| 141 | dnull.close() |
||
| 142 | while p.poll() or p.returncode is None: |
||
| 143 | try: |
||
| 144 | nvim = attach(*args, **kwargs) |
||
| 145 | break |
||
| 146 | except IOError: |
||
| 147 | # socket not ready yet |
||
| 148 | time.sleep(0.050) |
||
| 149 | else: |
||
| 150 | # spawn embedded instance |
||
| 151 | nvim_argv = shlex.split(prog or 'nvim --embed') + ctx.args |
||
| 152 | nvim = attach('child', argv=nvim_argv) |
||
| 153 | |||
| 154 | from .gtk_ui import GtkUI |
||
| 155 | config = load_config(config_file) |
||
| 156 | ui = GtkUI(config) |
||
| 157 | bridge = UIBridge() |
||
| 158 | bridge.connect(nvim, ui, profile if profile != 'disable' else None, notify) |
||
| 159 | |||
| 160 | if detach: |
||
| 161 | sys.exit(exit_code) |
||
| 162 | |||
| 166 |