Conditions | 9 |
Total Lines | 65 |
Code Lines | 35 |
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:
1 | #!/usr/bin/env python3 |
||
78 | def main(): |
||
79 | """ Our main method """ |
||
80 | |||
81 | # cleanly handle ctrl-c's |
||
82 | signal.signal(signal.SIGINT, sigint_handler) |
||
83 | |||
84 | # set process name |
||
85 | setproctitle('inji') |
||
86 | |||
87 | args = cli_args() |
||
88 | |||
89 | # this holds all the possible vars files we are told about or imply |
||
90 | vars_files = [] |
||
91 | |||
92 | # context in the local configuration files - p5 |
||
93 | vars_files += fnmatch.filter(os.listdir('.'), "*inji.y*ml") |
||
94 | |||
95 | # context in the overlay directories - p4 |
||
96 | for d in args.overlay_dir: |
||
97 | files = list(utils.recursive_iglob(d, '*.y*ml')) |
||
98 | if len(files): |
||
99 | loc = locale.getlocale() |
||
100 | locale.setlocale(locale.LC_ALL, 'C') # Using LC_ALL=C POSIX locale |
||
101 | files.sort() # We force the sort collation of files |
||
102 | locale.setlocale(locale.LC_ALL, loc) # And then reset it back |
||
103 | vars_files += files |
||
104 | |||
105 | # context from named vars files - p3 |
||
106 | vars_files += args.vars_file |
||
107 | |||
108 | # This will hold the final vars dict merged from various available sources |
||
109 | context = {} |
||
110 | for file in vars_files: |
||
111 | context.update(utils.read_context(file)) |
||
112 | |||
113 | # context from environment variables - p2 |
||
114 | context.update(os.environ) |
||
115 | |||
116 | # context at the command line (either JSON or KV type) - p1 |
||
117 | if args.json_string: |
||
118 | context.update(args.json_string) |
||
119 | |||
120 | if args.kv_pair: |
||
121 | context.update(args.kv_pair) |
||
122 | |||
123 | if args.template == '-': |
||
124 | # Template passed in via stdin. Create template as a tempfile and use it |
||
125 | # instead but since includes are possible (though not likely), we have to do |
||
126 | # this in an isolated tmpdir container to prevent inadvertent reading of |
||
127 | # includes not meant to be read. |
||
128 | tmpdir = tempfile.mkdtemp() |
||
129 | atexit.register(shutil.rmtree, tmpdir) |
||
130 | |||
131 | _, tmpfile = tempfile.mkstemp(prefix='stdin-', dir=tmpdir, text=True) |
||
132 | atexit.register(os.remove, tmpfile) |
||
133 | |||
134 | with open(tmpfile, "a+") as f: |
||
135 | f.write(sys.stdin.read()) |
||
136 | args.template = tmpfile |
||
137 | |||
138 | engine = TemplateEngine( undefined_variables_mode_behaviour=args.undefined_variables_mode ) |
||
139 | for block in engine.render( template=args.template, |
||
140 | context=context, |
||
141 | ): |
||
142 | print(block) |
||
143 |