Conditions | 32 |
Total Lines | 136 |
Lines | 0 |
Ratio | 0 % |
Tests | 79 |
CRAP Score | 32.0154 |
Changes | 4 | ||
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 _import_osm_dom() 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 | # ~*~ coding: utf-8 ~*~ |
||
36 | 1 | def _import_osm_dom(osma, session, dom): |
|
37 | """ Import a DOM tree from OSM XML into an OSMAlchemy model. |
||
38 | |||
39 | Not called directly; used by _import_osm_xml and _import_osm_file. |
||
40 | """ |
||
41 | |||
42 | 1 | def _dom_attrs_to_any(e, element): |
|
43 | 1 | if "version" in e.attributes.keys(): |
|
44 | 1 | element.version = int(e.attributes["version"].value) |
|
45 | 1 | if "changeset" in e.attributes.keys(): |
|
46 | 1 | element.changeset = int(e.attributes["changeset"].value) |
|
47 | 1 | if "user" in e.attributes.keys(): |
|
48 | 1 | element.user = e.attributes["user"].value |
|
49 | 1 | if "uid" in e.attributes.keys(): |
|
50 | 1 | element.uid = int(e.attributes["uid"].value) |
|
51 | 1 | if "visible" in e.attributes.keys(): |
|
52 | 1 | element.visible = True if e.attributes["visible"].value == "true" else False |
|
53 | 1 | if "timestamp" in e.attributes.keys(): |
|
54 | 1 | element.timestamp = dateutil.parser.parse(e.attributes["timestamp"].value) |
|
55 | |||
56 | 1 | def _dom_tags_to_any(e, element): |
|
57 | # Remove all tags previously associated with element |
||
58 | 1 | for k in list(element.tags.keys()): |
|
59 | del element.tags[k] |
||
60 | |||
61 | # Iterate over all <tag /> nodes in the DOM element |
||
62 | 1 | for t in e.getElementsByTagName("tag"): |
|
63 | # Append data to tags |
||
64 | 1 | element.tags[t.attributes["k"].value] = t.attributes["v"].value |
|
65 | |||
66 | 1 | def _dom_to_node(e): |
|
67 | 1 | with session.no_autoflush: |
|
68 | # Get mandatory node id |
||
69 | 1 | id = int(e.attributes["id"].value) |
|
70 | |||
71 | # Find object in database and create if non-existent |
||
72 | 1 | node = session.query(osma.node).filter_by(id=id).scalar() |
|
73 | 1 | if node is None: |
|
74 | 1 | node = osma.node(id=id) |
|
75 | |||
76 | # Store mandatory latitude and longitude |
||
77 | 1 | node.latitude = e.attributes["lat"].value |
|
78 | 1 | node.longitude = e.attributes["lon"].value |
|
79 | |||
80 | # Store other attributes and tags |
||
81 | 1 | _dom_attrs_to_any(e, node) |
|
82 | 1 | _dom_tags_to_any(e, node) |
|
83 | |||
84 | # Add to session |
||
85 | 1 | session.add(node) |
|
86 | 1 | session.commit() |
|
87 | |||
88 | 1 | def _dom_to_way(e): |
|
89 | 1 | with session.no_autoflush: |
|
90 | # Get mandatory way id |
||
91 | 1 | id = int(e.attributes["id"].value) |
|
92 | |||
93 | # Find object in database and create if non-existent |
||
94 | 1 | way = session.query(osma.way).filter_by(id=id).scalar() |
|
95 | 1 | if way is None: |
|
96 | 1 | way = osma.way(id=id) |
|
97 | |||
98 | # Find all related nodes |
||
99 | 1 | for n in e.getElementsByTagName("nd"): |
|
100 | # Get node id and find object |
||
101 | 1 | ref = int(n.attributes["ref"].value) |
|
102 | 1 | node = session.query(osma.node).filter_by(id=ref).one() |
|
103 | # Append to nodes in way |
||
104 | 1 | way.nodes.append(node) |
|
105 | |||
106 | # Store other attributes and tags |
||
107 | 1 | _dom_attrs_to_any(e, way) |
|
108 | 1 | _dom_tags_to_any(e, way) |
|
109 | |||
110 | # Add to session |
||
111 | 1 | session.add(way) |
|
112 | 1 | session.commit() |
|
113 | |||
114 | 1 | def _dom_to_relation(e): |
|
115 | 1 | with session.no_autoflush: |
|
116 | # Get mandatory way id |
||
117 | 1 | id = int(e.attributes["id"].value) |
|
118 | |||
119 | # Find object in database and create if non-existent |
||
120 | 1 | relation = session.query(osma.relation).filter_by(id=id).scalar() |
|
121 | 1 | if relation is None: |
|
122 | 1 | relation = osma.relation(id=id) |
|
123 | |||
124 | # Find all members |
||
125 | 1 | for m in e.getElementsByTagName("member"): |
|
126 | # Get member attributes |
||
127 | 1 | ref = int(m.attributes["ref"].value) |
|
128 | 1 | type = m.attributes["type"].value |
|
129 | |||
130 | 1 | if "role" in m.attributes.keys(): |
|
131 | 1 | role = m.attributes["role"].value |
|
132 | else: |
||
133 | role = "" |
||
134 | 1 | element = session.query(osma.element).filter_by(id=ref, type=type).scalar() |
|
135 | 1 | if element is None: |
|
136 | # We do not know the member yet, create a stub |
||
137 | 1 | if type == "node": |
|
138 | 1 | element = osma.node(id=ref) |
|
139 | 1 | elif type == "way": |
|
140 | 1 | element = osma.way(id=ref) |
|
141 | 1 | elif type == "relation": |
|
142 | 1 | element = osma.relation(id=ref) |
|
143 | # We need to commit here because element could be repeated |
||
144 | 1 | session.add(element) |
|
145 | 1 | session.commit() |
|
146 | # Append to members |
||
147 | 1 | relation.members.append((element, role)) |
|
148 | |||
149 | # Store other attributes and tags |
||
150 | 1 | _dom_attrs_to_any(e, relation) |
|
151 | 1 | _dom_tags_to_any(e, relation) |
|
152 | |||
153 | # Add to session |
||
154 | 1 | session.add(relation) |
|
155 | 1 | session.commit() |
|
156 | |||
157 | # Get root element |
||
158 | 1 | osm = dom.documentElement |
|
159 | |||
160 | # Iterate over children to find nodes, ways and relations |
||
161 | 1 | for e in osm.childNodes: |
|
162 | # Determine element type |
||
163 | 1 | if e.nodeName == "node": |
|
164 | 1 | _dom_to_node(e) |
|
165 | 1 | elif e.nodeName == "way": |
|
166 | 1 | _dom_to_way(e) |
|
167 | 1 | elif e.nodeName == "relation": |
|
168 | 1 | _dom_to_relation(e) |
|
169 | |||
170 | # Rmove children |
||
171 | 1 | e.unlink() |
|
172 | |||
198 |