| Conditions | 9 |
| Total Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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:
| 1 | # coding: utf-8 |
||
| 128 | def save_data(self, trigger_id, **data): |
||
| 129 | """ |
||
| 130 | let's save the data |
||
| 131 | don't want to handle empty title nor content |
||
| 132 | otherwise this will produce an Exception by |
||
| 133 | the Evernote's API |
||
| 134 | |||
| 135 | :param trigger_id: trigger ID from which to save data |
||
| 136 | :param data: the data to check to be used and save |
||
| 137 | :type trigger_id: int |
||
| 138 | :type data: dict |
||
| 139 | :return: the status of the save statement |
||
| 140 | :rtype: boolean |
||
| 141 | """ |
||
| 142 | kwargs = {} |
||
| 143 | # set the title and content of the data |
||
| 144 | title, content = super(ServiceEvernote, self).save_data(trigger_id, |
||
| 145 | data, |
||
| 146 | **kwargs) |
||
| 147 | |||
| 148 | if len(title): |
||
| 149 | # get the evernote data of this trigger |
||
| 150 | trigger = Evernote.objects.get(trigger_id=trigger_id) |
||
| 151 | |||
| 152 | try: |
||
| 153 | note_store = self.client.get_note_store() |
||
| 154 | except EDAMSystemException as e: |
||
| 155 | # rate limite reach have to wait 1 hour ! |
||
| 156 | if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED: |
||
| 157 | logger.warn("Rate limit reached {code}\n" |
||
| 158 | "Retry your request in {msg} seconds\n" |
||
| 159 | "Data set to cache again until" |
||
| 160 | " limit reached".format(code=e.errorCode, |
||
| 161 | msg=e.rateLimitDuration) |
||
| 162 | ) |
||
| 163 | cache.set('th_evernote_' + str(trigger_id), |
||
| 164 | data, |
||
| 165 | version=2) |
||
| 166 | return True |
||
| 167 | else: |
||
| 168 | logger.critical(e) |
||
| 169 | return False |
||
| 170 | except Exception as e: |
||
| 171 | logger.critical(e) |
||
| 172 | return False |
||
| 173 | |||
| 174 | # note object |
||
| 175 | note = Types.Note() |
||
| 176 | if trigger.notebook: |
||
| 177 | # get the notebookGUID ... |
||
| 178 | notebook_id = get_notebook(note_store, trigger.notebook) |
||
| 179 | # create notebookGUID if it does not exist then return its id |
||
| 180 | note.notebookGuid = set_notebook(note_store, |
||
| 181 | trigger.notebook, |
||
| 182 | notebook_id) |
||
| 183 | |||
| 184 | if trigger.tag: |
||
| 185 | # ... and get the tagGUID if a tag has been provided |
||
| 186 | tag_id = get_tag(note_store, trigger.tag) |
||
| 187 | if tag_id is False: |
||
| 188 | tag_id = set_tag(note_store, trigger.tag, tag_id) |
||
| 189 | # set the tag to the note if a tag has been provided |
||
| 190 | note.tagGuids = tag_id |
||
| 191 | |||
| 192 | logger.debug("notebook that will be used %s", trigger.notebook) |
||
| 193 | |||
| 194 | # attribute of the note: the link to the website |
||
| 195 | note_attribute = set_note_attribute(data) |
||
| 196 | if note_attribute: |
||
| 197 | note.attributes = note_attribute |
||
| 198 | |||
| 199 | # footer of the note |
||
| 200 | footer = set_note_footer(data, trigger) |
||
| 201 | content += footer |
||
| 202 | |||
| 203 | note.title = title |
||
| 204 | note.content = set_header() |
||
| 205 | note.content += sanitize(content) |
||
| 206 | # create a note |
||
| 207 | return create_note(note_store, note, trigger_id, data) |
||
| 208 | |||
| 209 | else: |
||
| 210 | logger.critical("no title provided " |
||
| 211 | "for trigger ID {}".format(trigger_id)) |
||
| 212 | return False |
||
| 213 | |||
| 280 |