mirror of
https://github.com/wakatime/sublime-wakatime.git
synced 2023-08-10 21:13:02 +03:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
5e2e1be779 | |||
b1d344cb46 | |||
7245cbeb58 | |||
21395579ea | |||
08b64b4ff6 | |||
20571ec085 | |||
e43dcc1c83 | |||
4610ff3e0c | |||
c86d6254e0 | |||
df331db5cc | |||
baff0f415d | |||
499dc167a5 | |||
83f4a29a15 | |||
8f02adacf9 | |||
e631d33944 | |||
cbd92a69b3 | |||
b7c047102d | |||
d0bfd04602 | |||
101ab38c70 | |||
8632c4ff08 | |||
80556d0cbf | |||
253728545c | |||
49d9b1d7dc |
55
HISTORY.rst
55
HISTORY.rst
@ -3,6 +3,61 @@ History
|
||||
-------
|
||||
|
||||
|
||||
2.0.17 (2014-11-18)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.1.6
|
||||
- fix list index error when detecting subversion project
|
||||
|
||||
|
||||
2.0.16 (2014-11-12)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.1.4
|
||||
- when Python was not compiled with https support, log an error to the log file
|
||||
|
||||
|
||||
2.0.15 (2014-11-10)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.1.3
|
||||
- correctly detect branch for subversion projects
|
||||
|
||||
|
||||
2.0.14 (2014-10-14)
|
||||
++++++++++++++++++
|
||||
|
||||
- popup error message if Python binary not found
|
||||
|
||||
|
||||
2.0.13 (2014-10-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.1.2
|
||||
- still log heartbeat when something goes wrong while reading num lines in file
|
||||
|
||||
|
||||
2.0.12 (2014-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.1.1
|
||||
- fix bug where binary file opened as utf-8
|
||||
|
||||
|
||||
2.0.11 (2014-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.1.0
|
||||
- python3 compatibility changes
|
||||
|
||||
|
||||
2.0.10 (2014-08-29)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.8
|
||||
- supress output from svn command
|
||||
|
||||
|
||||
2.0.9 (2014-08-27)
|
||||
++++++++++++++++++
|
||||
|
||||
|
16
README.md
16
README.md
@ -7,27 +7,25 @@ Installation
|
||||
------------
|
||||
|
||||
Heads Up! For Sublime Text 2 on Windows & Linux, WakaTime depends on [Python](http://www.python.org/getit/) being installed to work correctly.
|
||||
|
||||
1. Get an api key from: https://wakatime.com/#apikey
|
||||
|
||||
1. Install [Sublime Package Control](https://sublime.wbond.net/installation).
|
||||
|
||||
2. Using [Sublime Package Control](http://wbond.net/sublime_packages/package_control):
|
||||
|
||||
a) Press `ctrl+shift+p`(Windows, Linux) or `cmd+shift+p`(OS X).
|
||||
a) Inside Sublime, press `ctrl+shift+p`(Windows, Linux) or `cmd+shift+p`(OS X).
|
||||
|
||||
b) Type `install`, then press `enter` with `Package Control: Install Package` selected.
|
||||
|
||||
c) Type `wakatime`, then press `enter` with the `WakaTime` plugin selected.
|
||||
|
||||
3. You will see a prompt at the bottom asking for your [api key](https://wakatime.com/#apikey). Enter your api key, then press `enter`.
|
||||
3. Enter your [api key](https://wakatime.com/settings#apikey) from https://wakatime.com/settings#apikey, then press `enter`.
|
||||
|
||||
4. Use Sublime and your time will automatically be tracked for you.
|
||||
4. Use Sublime and your time will be tracked for you automatically.
|
||||
|
||||
5. Visit https://wakatime.com to see your logged time.
|
||||
|
||||
6. Consider installing [BIND9](https://help.ubuntu.com/community/BIND9ServerHowto#Caching_Server_configuration) to cache your repeated DNS requests: `sudo apt-get install bind9`
|
||||
5. Visit https://wakatime.com/dashboard to see your logged time.
|
||||
|
||||
Screen Shots
|
||||
------------
|
||||
|
||||

|
||||

|
||||
|
||||
|
@ -6,7 +6,7 @@ License: BSD, see LICENSE for more details.
|
||||
Website: https://wakatime.com/
|
||||
==========================================================="""
|
||||
|
||||
__version__ = '2.0.9'
|
||||
__version__ = '2.0.17'
|
||||
|
||||
import sublime
|
||||
import sublime_plugin
|
||||
@ -181,6 +181,13 @@ class SendActionThread(threading.Thread):
|
||||
def plugin_loaded():
|
||||
global SETTINGS
|
||||
print('[WakaTime] Initializing WakaTime plugin v%s' % __version__)
|
||||
|
||||
if not HAS_SSL:
|
||||
python = python_binary()
|
||||
if not python:
|
||||
sublime.error_message("Unable to find Python binary!\nWakaTime needs Python to work correctly.\n\nGo to https://www.python.org/downloads")
|
||||
return
|
||||
|
||||
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
||||
after_loaded()
|
||||
|
||||
|
@ -3,6 +3,54 @@ History
|
||||
-------
|
||||
|
||||
|
||||
2.1.6 (2014-11-18)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix list index error when detecting subversion project
|
||||
|
||||
|
||||
2.1.5 (2014-11-17)
|
||||
++++++++++++++++++
|
||||
|
||||
- catch exceptions when getting current machine time zone
|
||||
|
||||
|
||||
2.1.4 (2014-11-12)
|
||||
++++++++++++++++++
|
||||
|
||||
- when Python was not compiled with https support, log an error to the log file
|
||||
|
||||
|
||||
2.1.3 (2014-11-10)
|
||||
++++++++++++++++++
|
||||
|
||||
- correctly detect branch name for subversion projects
|
||||
|
||||
|
||||
2.1.2 (2014-10-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- still log heartbeat when something goes wrong while reading num lines in file
|
||||
|
||||
|
||||
2.1.1 (2014-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix bug where binary file opened as utf-8
|
||||
|
||||
|
||||
2.1.0 (2014-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- python3 compatibility changes
|
||||
|
||||
|
||||
2.0.8 (2014-08-29)
|
||||
++++++++++++++++++
|
||||
|
||||
- supress output from svn command
|
||||
|
||||
|
||||
2.0.7 (2014-08-27)
|
||||
++++++++++++++++++
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
from __future__ import print_function
|
||||
|
||||
__title__ = 'wakatime'
|
||||
__version__ = '2.0.7'
|
||||
__version__ = '2.1.6'
|
||||
__author__ = 'Alan Hamlett'
|
||||
__license__ = 'BSD'
|
||||
__copyright__ = 'Copyright 2014 Alan Hamlett'
|
||||
@ -40,6 +40,7 @@ except ImportError:
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages'))
|
||||
|
||||
from .compat import u, open, is_py2, is_py3
|
||||
from .queue import Queue
|
||||
from .log import setup_logging
|
||||
from .project import find_project
|
||||
@ -54,11 +55,6 @@ except:
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class FileAction(argparse.Action):
|
||||
|
||||
@ -82,7 +78,7 @@ def upgradeConfigFile(configFile):
|
||||
'ignore': [],
|
||||
}
|
||||
|
||||
with open(oldConfig) as fh:
|
||||
with open(oldConfig, 'r', encoding='utf-8') as fh:
|
||||
for line in fh.readlines():
|
||||
line = line.split('=', 1)
|
||||
if len(line) == 2 and line[0].strip() and line[1].strip():
|
||||
@ -91,7 +87,7 @@ def upgradeConfigFile(configFile):
|
||||
else:
|
||||
configs[line[0].strip()] = line[1].strip()
|
||||
|
||||
with open(configFile, 'w') as fh:
|
||||
with open(configFile, 'w', encoding='utf-8') as fh:
|
||||
fh.write("[settings]\n")
|
||||
for name, value in configs.items():
|
||||
if isinstance(value, list):
|
||||
@ -119,15 +115,14 @@ def parseConfigFile(configFile):
|
||||
|
||||
configs = configparser.SafeConfigParser()
|
||||
try:
|
||||
with open(configFile) as fh:
|
||||
with open(configFile, 'r', encoding='utf-8') as fh:
|
||||
try:
|
||||
configs.readfp(fh)
|
||||
except configparser.Error:
|
||||
print(traceback.format_exc())
|
||||
return None
|
||||
except IOError:
|
||||
if not os.path.isfile(configFile):
|
||||
print('Error: Could not read from config file ~/.wakatime.cfg')
|
||||
print(u('Error: Could not read from config file {0}').format(configFile))
|
||||
return configs
|
||||
|
||||
|
||||
@ -231,7 +226,7 @@ def should_ignore(fileName, patterns):
|
||||
if compiled.search(fileName):
|
||||
return pattern
|
||||
except re.error as ex:
|
||||
log.warning(unicode('Regex error ({msg}) for ignore pattern: {pattern}').format(
|
||||
log.warning(u('Regex error ({msg}) for ignore pattern: {pattern}').format(
|
||||
msg=str(ex),
|
||||
pattern=pattern,
|
||||
))
|
||||
@ -243,13 +238,13 @@ def should_ignore(fileName, patterns):
|
||||
def get_user_agent(plugin):
|
||||
ver = sys.version_info
|
||||
python_version = '%d.%d.%d.%s.%d' % (ver[0], ver[1], ver[2], ver[3], ver[4])
|
||||
user_agent = unicode('wakatime/{ver} ({platform}) Python{py_ver}').format(
|
||||
user_agent = u('wakatime/{ver} ({platform}) Python{py_ver}').format(
|
||||
ver=__version__,
|
||||
platform=platform.platform(),
|
||||
py_ver=python_version,
|
||||
)
|
||||
if plugin:
|
||||
user_agent = unicode('{user_agent} {plugin}').format(
|
||||
user_agent = u('{user_agent} {plugin}').format(
|
||||
user_agent=user_agent,
|
||||
plugin=plugin,
|
||||
)
|
||||
@ -268,9 +263,9 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
|
||||
if hidefilenames and targetFile is not None:
|
||||
data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1]
|
||||
if len(data['file'].strip('.').split('.', 1)) > 1:
|
||||
data['file'] = unicode('HIDDEN.{ext}').format(ext=data['file'].strip('.').rsplit('.', 1)[-1])
|
||||
data['file'] = u('HIDDEN.{ext}').format(ext=data['file'].strip('.').rsplit('.', 1)[-1])
|
||||
else:
|
||||
data['file'] = unicode('HIDDEN')
|
||||
data['file'] = u('HIDDEN')
|
||||
if stats.get('lines'):
|
||||
data['lines'] = stats['lines']
|
||||
if stats.get('language'):
|
||||
@ -284,16 +279,20 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
|
||||
log.debug(data)
|
||||
|
||||
# setup api request
|
||||
request = Request(url=url, data=str.encode(json.dumps(data)))
|
||||
request_body = json.dumps(data)
|
||||
request = Request(url=url, data=str.encode(request_body) if is_py3 else request_body)
|
||||
request.add_header('User-Agent', get_user_agent(plugin))
|
||||
request.add_header('Content-Type', 'application/json')
|
||||
auth = unicode('Basic {key}').format(key=bytes.decode(base64.b64encode(str.encode(key))))
|
||||
auth = u('Basic {key}').format(key=u(base64.b64encode(str.encode(key) if is_py3 else key)))
|
||||
request.add_header('Authorization', auth)
|
||||
|
||||
# add Olson timezone to request
|
||||
tz = tzlocal.get_localzone()
|
||||
try:
|
||||
tz = tzlocal.get_localzone()
|
||||
except:
|
||||
tz = None
|
||||
if tz:
|
||||
request.add_header('TimeZone', unicode(tz.zone))
|
||||
request.add_header('TimeZone', u(tz.zone))
|
||||
|
||||
# log time to api
|
||||
response = None
|
||||
@ -302,7 +301,7 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
|
||||
except HTTPError as exc:
|
||||
exception_data = {
|
||||
'response_code': exc.getcode(),
|
||||
sys.exc_info()[0].__name__: unicode(sys.exc_info()[1]),
|
||||
sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
|
||||
}
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
exception_data['traceback'] = traceback.format_exc()
|
||||
@ -315,14 +314,16 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
|
||||
log.error(exception_data)
|
||||
except:
|
||||
exception_data = {
|
||||
sys.exc_info()[0].__name__: unicode(sys.exc_info()[1]),
|
||||
sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
|
||||
}
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
exception_data['traceback'] = traceback.format_exc()
|
||||
if offline:
|
||||
queue = Queue()
|
||||
queue.push(data, plugin)
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
if 'unknown url type: https' in u(sys.exc_info()[1]):
|
||||
log.error(exception_data)
|
||||
elif log.isEnabledFor(logging.DEBUG):
|
||||
log.warn(exception_data)
|
||||
else:
|
||||
log.error(exception_data)
|
||||
@ -365,7 +366,7 @@ def main(argv=None):
|
||||
|
||||
ignore = should_ignore(args.targetFile, args.ignore)
|
||||
if ignore is not False:
|
||||
log.debug(unicode('File ignored because matches pattern: {pattern}').format(
|
||||
log.debug(u('File ignored because matches pattern: {pattern}').format(
|
||||
pattern=ignore,
|
||||
))
|
||||
return 0
|
||||
|
38
packages/wakatime/wakatime/compat.py
Normal file
38
packages/wakatime/wakatime/compat.py
Normal file
@ -0,0 +1,38 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.compat
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
For working with Python2 and Python3.
|
||||
|
||||
:copyright: (c) 2014 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import codecs
|
||||
import io
|
||||
import sys
|
||||
|
||||
|
||||
is_py2 = (sys.version_info[0] == 2)
|
||||
is_py3 = (sys.version_info[0] == 3)
|
||||
|
||||
|
||||
if is_py2:
|
||||
|
||||
def u(text):
|
||||
if isinstance(text, str):
|
||||
return text.decode('utf-8')
|
||||
return unicode(text)
|
||||
open = codecs.open
|
||||
basestring = basestring
|
||||
|
||||
|
||||
elif is_py3:
|
||||
|
||||
def u(text):
|
||||
if isinstance(text, bytes):
|
||||
return text.decode('utf-8')
|
||||
return str(text)
|
||||
open = open
|
||||
basestring = (str, bytes)
|
@ -14,6 +14,7 @@ import os
|
||||
import sys
|
||||
|
||||
from .packages import simplejson as json
|
||||
from .compat import u
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError:
|
||||
@ -30,7 +31,7 @@ class CustomEncoder(json.JSONEncoder):
|
||||
encoded = super(CustomEncoder, self).default(obj)
|
||||
except UnicodeDecodeError:
|
||||
encoding = sys.getfilesystemencoding()
|
||||
obj = obj.decode(encoding, 'ignore').encode('utf-8')
|
||||
obj = u(obj)
|
||||
encoded = super(CustomEncoder, self).default(obj)
|
||||
return encoded
|
||||
|
||||
|
@ -13,18 +13,12 @@ import logging
|
||||
import os
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class Git(BaseProject):
|
||||
|
||||
def process(self):
|
||||
@ -34,7 +28,7 @@ class Git(BaseProject):
|
||||
def name(self):
|
||||
base = self._project_base()
|
||||
if base:
|
||||
return unicode(os.path.basename(base))
|
||||
return u(os.path.basename(base))
|
||||
return None
|
||||
|
||||
def branch(self):
|
||||
@ -42,8 +36,8 @@ class Git(BaseProject):
|
||||
if base:
|
||||
head = os.path.join(self._project_base(), '.git', 'HEAD')
|
||||
try:
|
||||
with open(head) as fh:
|
||||
return unicode(fh.readline().strip().rsplit('/', 1)[-1])
|
||||
with open(head, 'r', encoding='utf-8') as fh:
|
||||
return u(fh.readline().strip().rsplit('/', 1)[-1])
|
||||
except IOError:
|
||||
pass
|
||||
return None
|
||||
|
@ -13,18 +13,12 @@ import logging
|
||||
import os
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class Mercurial(BaseProject):
|
||||
|
||||
def process(self):
|
||||
@ -33,18 +27,18 @@ class Mercurial(BaseProject):
|
||||
|
||||
def name(self):
|
||||
if self.configDir:
|
||||
return unicode(os.path.basename(os.path.dirname(self.configDir)))
|
||||
return u(os.path.basename(os.path.dirname(self.configDir)))
|
||||
return None
|
||||
|
||||
def branch(self):
|
||||
if self.configDir:
|
||||
branch_file = os.path.join(self.configDir, 'branch')
|
||||
try:
|
||||
with open(branch_file) as fh:
|
||||
return unicode(fh.readline().strip().rsplit('/', 1)[-1])
|
||||
with open(branch_file, 'r', encoding='utf-8') as fh:
|
||||
return u(fh.readline().strip().rsplit('/', 1)[-1])
|
||||
except IOError:
|
||||
pass
|
||||
return unicode('default')
|
||||
return u('default')
|
||||
|
||||
def _find_hg_config_dir(self, path):
|
||||
path = os.path.realpath(path)
|
||||
|
@ -24,18 +24,12 @@ import logging
|
||||
import os
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class ProjectMap(BaseProject):
|
||||
|
||||
def process(self):
|
||||
@ -68,5 +62,5 @@ class ProjectMap(BaseProject):
|
||||
|
||||
def name(self):
|
||||
if self.project:
|
||||
return unicode(self.project)
|
||||
return u(self.project)
|
||||
return None
|
||||
|
@ -15,6 +15,7 @@ import platform
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError:
|
||||
@ -24,13 +25,6 @@ except ImportError:
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class Subversion(BaseProject):
|
||||
binary_location = None
|
||||
|
||||
@ -38,12 +32,10 @@ class Subversion(BaseProject):
|
||||
return self._find_project_base(self.path)
|
||||
|
||||
def name(self):
|
||||
return unicode(self.info['Repository Root'].split('/')[-1])
|
||||
return u(self.info['Repository Root'].split('/')[-1])
|
||||
|
||||
def branch(self):
|
||||
if self.base:
|
||||
unicode(os.path.basename(self.base))
|
||||
return None
|
||||
return u(self.info['URL'].split('/')[-1])
|
||||
|
||||
def _find_binary(self):
|
||||
if self.binary_location:
|
||||
@ -54,12 +46,13 @@ class Subversion(BaseProject):
|
||||
'/usr/local/bin/svn',
|
||||
]
|
||||
for location in locations:
|
||||
try:
|
||||
Popen([location, '--version'])
|
||||
self.binary_location = location
|
||||
return location
|
||||
except:
|
||||
pass
|
||||
with open(os.devnull, 'wb') as DEVNULL:
|
||||
try:
|
||||
Popen([location, '--version'], stdout=DEVNULL, stderr=DEVNULL)
|
||||
self.binary_location = location
|
||||
return location
|
||||
except:
|
||||
pass
|
||||
self.binary_location = 'svn'
|
||||
return 'svn'
|
||||
|
||||
@ -75,16 +68,11 @@ class Subversion(BaseProject):
|
||||
pass
|
||||
else:
|
||||
if stdout:
|
||||
interesting = [
|
||||
'Repository Root',
|
||||
'Repository UUID',
|
||||
'URL',
|
||||
]
|
||||
for line in stdout.splitlines():
|
||||
if isinstance(line, bytes):
|
||||
line = bytes.decode(line)
|
||||
line = line.split(': ', 1)
|
||||
if line[0] in interesting:
|
||||
if len(line) == 2:
|
||||
info[line[0]] = line[1]
|
||||
return info
|
||||
|
||||
|
@ -15,18 +15,12 @@ import logging
|
||||
import os
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class WakaTime(BaseProject):
|
||||
|
||||
def process(self):
|
||||
@ -37,9 +31,9 @@ class WakaTime(BaseProject):
|
||||
if self.config:
|
||||
|
||||
try:
|
||||
with open(self.config) as fh:
|
||||
self._project_name = unicode(fh.readline().strip())
|
||||
self._project_branch = unicode(fh.readline().strip())
|
||||
with open(self.config, 'r', encoding='utf-8') as fh:
|
||||
self._project_name = u(fh.readline().strip())
|
||||
self._project_branch = u(fh.readline().strip())
|
||||
except IOError as e:
|
||||
log.exception("Exception:")
|
||||
|
||||
|
@ -13,6 +13,8 @@ import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from .compat import u, open
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments2'))
|
||||
else:
|
||||
@ -22,17 +24,13 @@ from pygments.lexers import guess_lexer_for_filename
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
# force file name extensions to be recognized as a certain language
|
||||
EXTENSIONS = {
|
||||
'j2': 'HTML',
|
||||
'markdown': 'Markdown',
|
||||
'md': 'Markdown',
|
||||
'mdown': 'Markdown',
|
||||
'twig': 'Twig',
|
||||
}
|
||||
TRANSLATIONS = {
|
||||
@ -54,12 +52,12 @@ def guess_language(file_name):
|
||||
return language
|
||||
lexer = None
|
||||
try:
|
||||
with open(file_name) as f:
|
||||
lexer = guess_lexer_for_filename(file_name, f.read(512000))
|
||||
with open(file_name, 'r', encoding='utf-8') as fh:
|
||||
lexer = guess_lexer_for_filename(file_name, fh.read(512000))
|
||||
except:
|
||||
pass
|
||||
if lexer:
|
||||
return translate_language(unicode(lexer.name))
|
||||
return translate_language(u(lexer.name))
|
||||
else:
|
||||
return None
|
||||
|
||||
@ -82,10 +80,10 @@ def translate_language(language):
|
||||
def number_lines_in_file(file_name):
|
||||
lines = 0
|
||||
try:
|
||||
with open(file_name) as f:
|
||||
for line in f:
|
||||
with open(file_name, 'r', encoding='utf-8') as fh:
|
||||
for line in fh:
|
||||
lines += 1
|
||||
except IOError:
|
||||
except:
|
||||
return None
|
||||
return lines
|
||||
|
||||
|
Reference in New Issue
Block a user