Conditions | 11 |
Total Lines | 72 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Tests | 36 |
CRAP Score | 11 |
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 inji.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 python3 |
||
101 | 1 | def main(): |
|
102 | """ Our main method """ |
||
103 | |||
104 | # cleanly handle ctrl-c's |
||
105 | 1 | signal.signal(signal.SIGINT, sigint_handler) |
|
106 | |||
107 | 1 | assert sys.version_info >= (3,5), 'Python version ({}.{} !>= 3.5)'.format( |
|
108 | sys.version_info.major, |
||
109 | sys.version_info.minor |
||
110 | ) |
||
111 | |||
112 | 1 | args = cli_args() |
|
113 | |||
114 | # this holds all the possible vars files we are told about or imply |
||
115 | 1 | vars_files = [] |
|
116 | |||
117 | # context in the local configuration files - p5 |
||
118 | 1 | vars_files += fnmatch.filter(os.listdir('.'), "*inji.y*ml") |
|
119 | |||
120 | # context in the overlay directories - p4 |
||
121 | 1 | for d in args.overlay_dir: |
|
122 | 1 | files = list(utils.recursive_iglob(d, '*.y*ml')) |
|
123 | 1 | if len(files): |
|
124 | 1 | loc = locale.getlocale() |
|
125 | 1 | locale.setlocale(locale.LC_ALL, 'C') # Using LC_ALL=C POSIX locale |
|
126 | 1 | files.sort() # We force the sort collation of files |
|
127 | 1 | locale.setlocale(locale.LC_ALL, loc) # And then reset it back |
|
128 | 1 | vars_files += files |
|
129 | |||
130 | # context from named vars files - p3 |
||
131 | 1 | vars_files += args.vars_file |
|
132 | |||
133 | # This will hold the final vars dict merged from various available sources |
||
134 | 1 | context = {} |
|
135 | 1 | for file in vars_files: |
|
136 | 1 | context.update(utils.read_context(file)) |
|
137 | |||
138 | # context from environment variables - p2 |
||
139 | 1 | context.update(os.environ) |
|
140 | |||
141 | # context at the command line (either JSON or KV type) - p1 |
||
142 | 1 | if args.json_string: |
|
143 | 1 | context.update(args.json_string) |
|
144 | |||
145 | 1 | if args.kv_pair: |
|
146 | # we've appended dicts into args.kv_pair .. unpack them in order |
||
147 | 1 | for d in args.kv_pair: |
|
148 | 1 | context.update(d) |
|
149 | |||
150 | 1 | if '-' in args.template: |
|
151 | # Template passed in via stdin. Create template as a tempfile and use it |
||
152 | # instead but since includes are possible (though not likely), we have to do |
||
153 | # this in an isolated tmpdir container to prevent inadvertent reading of |
||
154 | # includes not meant to be read. |
||
155 | 1 | tmpdir = tempfile.mkdtemp(prefix=__name__) |
|
156 | 1 | atexit.register(shutil.rmtree, tmpdir) |
|
157 | |||
158 | 1 | _, tmpfile = tempfile.mkstemp(prefix='stdin-', dir=tmpdir, text=True) |
|
159 | 1 | atexit.register(os.remove, tmpfile) |
|
160 | |||
161 | 1 | with open(tmpfile, "a+") as f: |
|
162 | 1 | f.write(sys.stdin.read()) |
|
163 | |||
164 | # Yes, even if user specifies multiple other templates, the fact he |
||
165 | # specified '-' just once means we only deal with one template i.e. '-' |
||
166 | 1 | args.template = [tmpfile] |
|
167 | |||
168 | 1 | engine = TemplateEngine( undefined_variables_mode_behaviour=args.undefined_variables_mode ) |
|
169 | |||
170 | 1 | for template in args.template: |
|
171 | 1 | for block in engine.render( template=template, context=context ): |
|
172 | print(block) |
||
173 |