allista / WakatimeBlender

This is a plugin for the Blender that sends statistics to the Wakatime time tracking service.
MIT License
27 stars 16 forks source link

Added options to set / adjust the projects name in WakaTime #7

Closed hannes-pj closed 3 years ago

hannes-pj commented 4 years ago

Thank you for the WakaTime-AddOn. :smiley:

For me it unfortunately never transmitted any project-name to WakaTime, always just accumulating time under "Unknown Project".

So I added some kind of project-name "detection" and transmission. Description of the functionality is in the README.md.

Programmatically the "core" was not changed much:

  1. The --project command-line argument was added when calling the wakatime-client.
  2. handle_activity was extended to extract the project's name from filename or directory-name. After the extraction the name can be truncated (from the end of the string), post- and prefix can be added to the name too. All this can be configured in the "Add-Ons" -Menu.

Attention: This was tested under Linux only! :neutral_face:

jerry-sky commented 4 years ago

Really cool, but unfortunately it still doesn't work for me. WakaTime still shows nothing. No Unknown Project, no Blender in editors pie chart.

I'm also running Linux, Mint with Cinnamon DE to be exact.

hannes-pj commented 4 years ago

Hello jerry-sky!

Running Blender 2.83(.1) it works well here (Linux Mint), both counting towards Editor Blender and to individual projects (below projects even to files, e.g. when using the file-name for versioning or just having multiple blend-files in one project-folder).

Where is your wakatime-cli installed? The Add-On looks for it in this path: ~/.wakatime/wakatime-master/wakatime ... there must be the file cli.py. Running python cli.py should give you the available options for the command-line python-API.

Additionally, can you run blender from command-line? The Add-On prints out some information when loading or saving a file (one should see the "guessed" project-name).

Hope that helps in some way.

jerry-sky commented 4 years ago

When opening Blender with all logs showing there are actually some exceptions being raised:

[WakaTime] [INFO] Initializing WakaTime plugin v1.1.0
[WakaTime] [INFO] WakatimeBlender is registered
[WakaTime] [INFO] Downloading Wakatime...
Warning: class UI_OT_wakatime_api_key_dialog contains a property which should be an annotation!
/home/jerry-sky/.config/blender/2.83/scripts/addons/WakaTime.py:335
    assign as a type annotation: UI_OT_wakatime_api_key_dialog.api_key
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/snap/blender/40/2.83/python/lib/python3.7/http/client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/snap/blender/40/2.83/python/lib/python3.7/http/client.py", line 1290, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/snap/blender/40/2.83/python/lib/python3.7/http/client.py", line 1239, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/snap/blender/40/2.83/python/lib/python3.7/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/snap/blender/40/2.83/python/lib/python3.7/http/client.py", line 966, in send
    self.connect()
  File "/snap/blender/40/2.83/python/lib/python3.7/http/client.py", line 1414, in connect
    server_hostname=server_hostname)
  File "/snap/blender/40/2.83/python/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/snap/blender/40/2.83/python/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/snap/blender/40/2.83/python/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/snap/blender/40/2.83/python/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/home/jerry-sky/.config/blender/2.83/scripts/addons/WakaTime.py", line 239, in run
    request.urlretrieve(API_CLIENT_URL, zip_file)
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/snap/blender/40/2.83/python/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)>

As you can see, I installed Blender using snap. So as a simple sanity check, I uninstalled Blender from snap and installed it from blender.org (unpacked the archive to /opt and linked the binary file to /usr/bin/blender). But, unfortunately, it didn't make any difference – only the source of the exceptions changed from /snap/blender/40 to /opt/blender (obviously).

However, from the above logs, we can see that the add-on tries to download the CLI but fails to do so. This CERTIFICATE_VERIFY_FAILED error is actually a pretty common issue for Mac users of Python, but as I'm using Linux Mint the solution from this link doesn't apply to my case.

Then I found a script on this gist that presumably resolves this certificate issue. I ran it on my machine, but, as I expected it didn't have any effect on this Blender-related issue as Blender uses its own Python installation (that's my assumption based on paths in logs such as: /snap/blender/40/2.83/python/lib/python3.7/urllib/request.py).

So I kept searching and I found this issue related to some other Blender add-on.\ Based on the issue author's reply and on the add-on's code I found a solution that finally works in our WakaTime add-on:\ Having imported following libraries:

import ssl
import urllib

we can now alter the way we download the CLI:

#request.urlretrieve(API_CLIENT_URL, zip_file) # the old way
# the new way:
req = urllib.request.Request(API_CLIENT_URL)
context = ssl._create_unverified_context()
with urllib.request.urlopen(req, context=context) as r, open(zip_file, 'wb+') as fo:
    fo.write(r.read())

I tested this solution and it finally works on my machine.

Should I issue another pull request with said changes applied? The error handling could be improved though (based on that referenced add-on's code).

On another note, the add-on accurately reports Blender activity in terms of the editor but the language is reported as Other. I know, that Blender does not involve programming, but it would be nice if it reported something more specific IMO.

hannes-pj commented 4 years ago

Wow! So downloading the client failed.

I renamed my ~/.wakatime/ client-directory and was able to reproduce. (It looks like I was just lucky before, as the client was already installed by/for other editors/tools.)

Using your patch, blender downloaded and unzipped the wakatime-client correctly! :+1:

I would suggest a new pull-request, as your patch is a FIX and fairly small. (not sure about potential side-effects under Windows or Mac). Maybe additionally raise an issue here on GitHub?

Languages: also tried to send the language set to "blender", but it looks like WakaTime ignores this. Adding "blender" to the languages/defaults.json within the clients directory-structure did not help either. Looks like this is a limitation on WakaTime's backend?!

jerry-sky commented 4 years ago

Yeah, so I will apply this fix and I will at least try to enhance error-handling a bit and open another PR.

As you said, it should also be tested for Windows or Mac OSes. As I don't own a Mac machine, I can only test this add-on on Windows – and I will do so. As for Macs, you know, they are Unix based so maybe it will be fine? But certainly, somebody could test the add-on on a Mac machine as well.

allista commented 4 years ago

In my experience wakatime CLI usually figures out the project by git repository in which the file resides. But I understand that not everyone use git in their work, so this feature should be really useful. Thank you for your contribution!

hannes-pj commented 4 years ago

So maybe I should add a checkbox to enable the project-naming from within Blender (and thereby overwrite the name derived from the git-repo)?

allista commented 4 years ago

That was my thought -- to allow wakatime work as it is supposed to, but also allow the user to define their overrides via UI. There's also this option in the CLI

--alternate-project ALTERNATE_PROJECT
                        optional alternate project name; auto-discovered
                        project takes priority

But I'm not sure if it's better than explicit "override project name" checkbox.

And yea, the docs confirm my memory:

wakatime-cli is a Python module containing common code that every plugin uses. It does things like detect the current project, current branch, syntax language, and send heartbeats to the WakaTime api.

hannes-pj commented 4 years ago

Good point, missed that alternate-project. Will test this!

hannes-pj commented 4 years ago

Yes, so --project enforces the project-name (overwrites WakaTimes auto-discover (e.g. git-repo)), while --alternate-project is only used, when WakaTimes auto-discover can not come up with a name ("Unknown Project").

Maybe a checkbox to decide whether to always overwrite WakaTime's auto-discover? If unchecked, only the alternate-project will be set?

allista commented 4 years ago

Yes, "use folder as default project name" and "override project auto-discovery", something like that.

I like it.

hannes-pj commented 4 years ago

That should be it, README.md was updated too. :-)

hannes-pj commented 4 years ago

Sorry, I am new to python, so thanks for your time clearing things up.

I pushed some new commits and resolved the requests I could fix myself.

There are four open comments ... I answered all of them. Maybe some can get closed (e.g. if you think the current solution is acceptable), or maybe you have better ideas?

allista commented 4 years ago

Perfect! If you have tested it locally, I'm ready to merge :fireworks:

hannes-pj commented 4 years ago

Sounds lovely! :wink: That was quite a bit of work, thanks for iterating over all those code-changes. :+1:

There is just one feedback/FYI open: @lru_cache - maxsize must be defined. I could not solve it otherwise :disappointed:

But anyways: start the fireworks :fireworks: :champagne: :stuck_out_tongue_winking_eye: Cheers!

PS: I just hope that there are at least some people using the Add-On :laughing:

allista commented 3 years ago

There is just one feedback/FYI open: @lru_cache - maxsize must be defined.

Not an issue; this probably depends on python's version bundled with blender.

Thanks for the great work! And judging by the number of people in this discussion, I'd say some do indeed use it. To my surprise :open_mouth: and delight :grinning: