Conditions | 17 |
Total Lines | 84 |
Code Lines | 56 |
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 glances.standalone.GlancesStandalone.__init__() 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 | # |
||
33 | def __init__(self, config=None, args=None): |
||
34 | self.config = config |
||
35 | self.args = args |
||
36 | |||
37 | # Quiet mode |
||
38 | self._quiet = args.quiet |
||
39 | self.refresh_time = args.time |
||
40 | |||
41 | # Init stats |
||
42 | start_duration = Counter() |
||
43 | start_duration.reset() |
||
44 | self.stats = GlancesStats(config=config, args=args) |
||
45 | logger.debug(f"Plugins initialisation duration: {start_duration.get()} seconds") |
||
46 | |||
47 | # Modules (plugins and exporters) are loaded at this point |
||
48 | # Glances can display the list if asked... |
||
49 | if args.modules_list: |
||
50 | self.display_modules_list() |
||
51 | sys.exit(0) |
||
52 | |||
53 | # Set the args for the glances_processes instance |
||
54 | glances_processes.set_args(args) |
||
55 | |||
56 | # If process extended stats is disabled by user |
||
57 | if not args.enable_process_extended: |
||
58 | logger.debug("Extended stats for top process are disabled") |
||
59 | glances_processes.disable_extended() |
||
60 | else: |
||
61 | logger.debug("Extended stats for top process are enabled") |
||
62 | glances_processes.enable_extended() |
||
63 | |||
64 | # Manage optional process filter |
||
65 | if args.process_filter is not None: |
||
66 | logger.info(f"Process filter is set to: {args.process_filter}") |
||
67 | glances_processes.process_filter = args.process_filter |
||
68 | |||
69 | if (args.export or args.stdout) and args.export_process_filter is not None: |
||
70 | logger.info(f"Export process filter is set to: {args.export_process_filter}") |
||
71 | glances_processes.export_process_filter = args.export_process_filter |
||
72 | |||
73 | if (not WINDOWS) and args.no_kernel_threads: |
||
74 | # Ignore kernel threads in process list |
||
75 | glances_processes.disable_kernel_threads() |
||
76 | |||
77 | # Initial system information update |
||
78 | start_duration.reset() |
||
79 | self.stats.update() |
||
80 | logger.debug(f"First stats update duration: {start_duration.get()} seconds") |
||
81 | |||
82 | if self.quiet: |
||
83 | logger.info("Quiet mode is ON, nothing will be displayed") |
||
84 | # In quiet mode, nothing is displayed |
||
85 | glances_processes.max_processes = 0 |
||
86 | elif args.stdout_issue: |
||
87 | logger.info("Issue mode is ON") |
||
88 | self.screen = GlancesStdoutIssue(config=config, args=args) |
||
89 | elif args.stdout_api_doc: |
||
90 | logger.info("Restful Python documentation mode is ON") |
||
91 | self.screen = GlancesStdoutApiDoc(config=config, args=args) |
||
92 | elif args.stdout_api_restful_doc: |
||
93 | logger.info("Restful API documentation mode is ON") |
||
94 | self.screen = GlancesStdoutApiRestfulDoc(config=config, args=args) |
||
95 | elif args.stdout: |
||
96 | logger.info(f"Stdout mode is ON, following stats will be displayed: {args.stdout}") |
||
97 | self.screen = GlancesStdout(config=config, args=args) |
||
98 | elif args.stdout_json: |
||
99 | logger.info(f"Stdout JSON mode is ON, following stats will be displayed: {args.stdout_json}") |
||
100 | self.screen = GlancesStdoutJson(config=config, args=args) |
||
101 | elif args.stdout_csv: |
||
102 | logger.info(f"Stdout CSV mode is ON, following stats will be displayed: {args.stdout_csv}") |
||
103 | self.screen = GlancesStdoutCsv(config=config, args=args) |
||
104 | elif args.stdout_fetch: |
||
105 | logger.info("Fetch mode is ON") |
||
106 | self.screen = GlancesStdoutFetch(config=config, args=args) |
||
107 | else: |
||
108 | # Init screen in default mode (curses) aka TUI mode |
||
109 | self.screen = GlancesCursesStandalone(config=config, args=args) |
||
110 | |||
111 | # If an error occur during the screen init, continue if export option is set |
||
112 | # It is done in the screen.init function |
||
113 | self._quiet = args.quiet |
||
114 | |||
115 | # Check the latest Glances version |
||
116 | self.outdated = Outdated(config=config, args=args) |
||
117 | |||
209 |