Conditions | 15 |
Total Lines | 86 |
Code Lines | 51 |
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 | # |
||
31 | def __init__(self, config=None, args=None): |
||
32 | self.config = config |
||
33 | self.args = args |
||
34 | |||
35 | # Quiet mode |
||
36 | self._quiet = args.quiet |
||
37 | self.refresh_time = args.time |
||
38 | |||
39 | # Init stats |
||
40 | start_duration = Counter() |
||
41 | start_duration.reset() |
||
42 | self.stats = GlancesStats(config=config, args=args) |
||
43 | logger.debug(f"Plugins initialisation duration: {start_duration.get()} seconds") |
||
44 | |||
45 | # Modules (plugins and exporters) are loaded at this point |
||
46 | # Glances can display the list if asked... |
||
47 | if args.modules_list: |
||
48 | self.display_modules_list() |
||
49 | sys.exit(0) |
||
50 | |||
51 | # Set the args for the glances_processes instance |
||
52 | glances_processes.set_args(args) |
||
53 | |||
54 | # If process extended stats is disabled by user |
||
55 | if not args.enable_process_extended: |
||
56 | logger.debug("Extended stats for top process are disabled") |
||
57 | glances_processes.disable_extended() |
||
58 | else: |
||
59 | logger.debug("Extended stats for top process are enabled") |
||
60 | glances_processes.enable_extended() |
||
61 | |||
62 | # Manage optional process filter |
||
63 | if args.process_filter is not None: |
||
64 | logger.info(f"Process filter is set to: {args.process_filter}") |
||
65 | glances_processes.process_filter = args.process_filter |
||
66 | |||
67 | if (args.export or args.stdout) and args.export_process_filter is not None: |
||
68 | logger.info(f"Export process filter is set to: {args.export_process_filter}") |
||
69 | glances_processes.export_process_filter = args.export_process_filter |
||
70 | |||
71 | if (not WINDOWS) and args.no_kernel_threads: |
||
72 | # Ignore kernel threads in process list |
||
73 | glances_processes.disable_kernel_threads() |
||
74 | |||
75 | # Initial system information update |
||
76 | start_duration.reset() |
||
77 | self.stats.update() |
||
78 | logger.debug(f"First stats update duration: {start_duration.get()} seconds") |
||
79 | |||
80 | if self.quiet: |
||
81 | logger.info("Quiet mode is ON, nothing will be displayed") |
||
82 | # In quiet mode, nothing is displayed |
||
83 | glances_processes.max_processes = 0 |
||
84 | elif args.stdout_issue: |
||
85 | logger.info("Issue mode is ON") |
||
86 | # Init screen |
||
87 | self.screen = GlancesStdoutIssue(config=config, args=args) |
||
88 | elif args.stdout_apidoc: |
||
89 | logger.info("Fields descriptions mode is ON") |
||
90 | # Init screen |
||
91 | self.screen = GlancesStdoutApiDoc(config=config, args=args) |
||
92 | elif args.stdout: |
||
93 | logger.info(f"Stdout mode is ON, following stats will be displayed: {args.stdout}") |
||
94 | # Init screen |
||
95 | self.screen = GlancesStdout(config=config, args=args) |
||
96 | elif args.stdout_json: |
||
97 | logger.info(f"Stdout JSON mode is ON, following stats will be displayed: {args.stdout_json}") |
||
98 | # Init screen |
||
99 | self.screen = GlancesStdoutJson(config=config, args=args) |
||
100 | elif args.stdout_csv: |
||
101 | logger.info(f"Stdout CSV mode is ON, following stats will be displayed: {args.stdout_csv}") |
||
102 | # Init screen |
||
103 | self.screen = GlancesStdoutCsv(config=config, args=args) |
||
104 | else: |
||
105 | # Default number of processes to displayed is set to 50 |
||
106 | glances_processes.max_processes = 50 |
||
107 | |||
108 | # Init screen |
||
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 |