mirror of
https://github.com/wakatime/sublime-wakatime.git
synced 2023-08-10 21:13:02 +03:00
Compare commits
70 Commits
Author | SHA1 | Date | |
---|---|---|---|
8574abe012 | |||
6b6f60d8e8 | |||
986e592d1e | |||
6ec3b171e1 | |||
bcfb9862af | |||
85cf9f4eb5 | |||
d2a996e845 | |||
c863bde54a | |||
e19f85f081 | |||
7b854d4041 | |||
e122f73e6b | |||
474942eb6a | |||
a5f031b046 | |||
66fddc07b9 | |||
e56a07e909 | |||
64ea40b3f5 | |||
17fd6ef8e1 | |||
e5e399dfbe | |||
bcf037e8a4 | |||
7e678a38bd | |||
533aaac313 | |||
7f4f70cc85 | |||
4adb8a8796 | |||
48e1993b24 | |||
8a3375bb23 | |||
8bd54a7427 | |||
fcbbf05933 | |||
9733087094 | |||
da4e02199a | |||
09a16dea1e | |||
4c7adf0943 | |||
216a8eaa0a | |||
81f838489d | |||
d6228b8dce | |||
7a2c2b9750 | |||
d9cc911595 | |||
805e2fe222 | |||
bbcb39b2cf | |||
9f9b97c69f | |||
908ff98613 | |||
37f74b4b56 | |||
a1c0d7e489 | |||
3127f264b4 | |||
049bc57019 | |||
03ec38bb67 | |||
4fc1a55ff7 | |||
f60815b813 | |||
ca47c2308d | |||
146a959747 | |||
906184cd88 | |||
a13e11d24d | |||
d34432217f | |||
f2e8f85198 | |||
05b08b6ab2 | |||
685d242c60 | |||
023c1dfbe3 | |||
9255fd2c34 | |||
784ad38c38 | |||
36def5c8b8 | |||
2c8dd6c9e7 | |||
e8151535c1 | |||
744116079a | |||
791a969a10 | |||
46c5171d6a | |||
fe641d01d4 | |||
4f03423333 | |||
e812e9fe15 | |||
a92ebad2f2 | |||
78a7e5cbcb | |||
5616206b48 |
350
HISTORY.rst
Normal file
350
HISTORY.rst
Normal file
@ -0,0 +1,350 @@
|
||||
|
||||
History
|
||||
-------
|
||||
|
||||
|
||||
2.0.9 (2014-08-27)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.7
|
||||
- fix support for subversion projects on Mac OS X
|
||||
|
||||
|
||||
2.0.8 (2014-08-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.6
|
||||
- fix unicode bug by encoding json POST data
|
||||
|
||||
|
||||
2.0.7 (2014-07-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.5
|
||||
- option in .wakatime.cfg to obfuscate file names
|
||||
|
||||
|
||||
2.0.6 (2014-07-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.4
|
||||
- use unique logger namespace to prevent collisions in shared plugin environments
|
||||
|
||||
|
||||
2.0.5 (2014-06-18)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.3
|
||||
- use project name from sublime-project file when no revision control project found
|
||||
|
||||
|
||||
2.0.4 (2014-06-09)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.2
|
||||
- disable offline logging when Python not compiled with sqlite3 module
|
||||
|
||||
|
||||
2.0.3 (2014-05-26)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.1
|
||||
- fix bug in queue preventing completed tasks from being purged
|
||||
|
||||
|
||||
2.0.2 (2014-05-26)
|
||||
++++++++++++++++++
|
||||
|
||||
- disable syncing offline time until bug fixed
|
||||
|
||||
|
||||
2.0.1 (2014-05-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v2.0.0
|
||||
- offline time logging using sqlite3 to queue editor events
|
||||
|
||||
|
||||
1.6.5 (2014-03-05)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v1.0.1
|
||||
- use new domain wakatime.com
|
||||
|
||||
|
||||
1.6.4 (2014-02-05)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade external wakatime package to v1.0.0
|
||||
- support for mercurial revision control
|
||||
|
||||
|
||||
1.6.3 (2014-01-15)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade common wakatime package to v0.5.3
|
||||
|
||||
|
||||
1.6.2 (2014-01-14)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade common wakatime package to v0.5.2
|
||||
|
||||
|
||||
1.6.1 (2013-12-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade common wakatime package to v0.5.1
|
||||
- second line in .wakatime-project now sets branch name
|
||||
|
||||
|
||||
1.6.0 (2013-12-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgrade common wakatime package to v0.5.0
|
||||
|
||||
|
||||
1.5.2 (2013-12-03)
|
||||
++++++++++++++++++
|
||||
|
||||
- use non-localized datetime in log
|
||||
|
||||
|
||||
1.5.1 (2013-12-02)
|
||||
++++++++++++++++++
|
||||
|
||||
- decode file names with filesystem encoding, then encode as utf-8 for logging
|
||||
|
||||
|
||||
1.5.0 (2013-11-28)
|
||||
++++++++++++++++++
|
||||
|
||||
- increase "ping" frequency from every 5 minutes to every 2 minutes
|
||||
- prevent sending multiple api requests when saving the same file
|
||||
|
||||
|
||||
1.4.12 (2013-11-21)
|
||||
+++++++++++++++++++
|
||||
|
||||
- handle UnicodeDecodeError exceptions when json encoding log messages
|
||||
|
||||
|
||||
1.4.11 (2013-11-13)
|
||||
+++++++++++++++++++
|
||||
|
||||
- placing .wakatime-project file in a folder will read the project's name from that file
|
||||
|
||||
|
||||
1.4.10 (2013-10-31)
|
||||
++++++++++++++++++
|
||||
|
||||
- recognize jinja2 file extensions as HTML
|
||||
|
||||
|
||||
1.4.9 (2013-10-28)
|
||||
++++++++++++++++++
|
||||
|
||||
- handle case where ignore patterns not defined
|
||||
|
||||
|
||||
1.4.8 (2013-10-27)
|
||||
++++++++++++++++++
|
||||
|
||||
- new setting to ignore files that match a regular expression pattern
|
||||
|
||||
|
||||
1.4.7 (2013-10-26)
|
||||
++++++++++++++++++
|
||||
|
||||
- simplify some language lexer names into more common versions
|
||||
|
||||
|
||||
1.4.6 (2013-10-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- force some file extensions to be recognized as certain language
|
||||
|
||||
|
||||
1.4.5 (2013-10-14)
|
||||
++++++++++++++++++
|
||||
|
||||
- remove support for subversion projects on Windows to prevent cmd window popups
|
||||
- ignore all errors from pygments library
|
||||
|
||||
|
||||
1.4.4 (2013-10-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- read git branch from .git/HEAD without running command line git client
|
||||
|
||||
|
||||
1.4.3 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- send olson timezone string to api for displaying logged time in user's zone
|
||||
|
||||
|
||||
1.4.2 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- print error code in Sublime's console if api request fails
|
||||
|
||||
|
||||
1.4.1 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix SSL support problem for Linux users
|
||||
|
||||
|
||||
1.4.0 (2013-09-22)
|
||||
++++++++++++++++++
|
||||
|
||||
- log source code language type of files
|
||||
- log total number of lines in files
|
||||
- better python3 support
|
||||
|
||||
|
||||
1.3.7 (2013-09-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix relative import bug
|
||||
|
||||
|
||||
1.3.6 (2013-09-06)
|
||||
++++++++++++++++++
|
||||
|
||||
- switch back to urllib2 instead of requests library in wakatime package
|
||||
|
||||
|
||||
1.3.5 (2013-09-05)
|
||||
++++++++++++++++++
|
||||
|
||||
- send Sublime version with api requests for easier debugging
|
||||
|
||||
|
||||
1.3.4 (2013-09-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgraded wakatime package
|
||||
|
||||
|
||||
1.3.3 (2013-09-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- using requests package in wakatime package
|
||||
|
||||
|
||||
1.3.2 (2013-08-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix bug causing wrong file name detected
|
||||
- misc bug fixes
|
||||
|
||||
|
||||
1.3.0 (2013-08-15)
|
||||
++++++++++++++++++
|
||||
|
||||
- detect git branches
|
||||
|
||||
|
||||
1.2.0 (2013-08-12)
|
||||
++++++++++++++++++
|
||||
|
||||
- run wakatime package in new process when no SSL support in Sublime
|
||||
|
||||
|
||||
1.1.0 (2013-08-12)
|
||||
++++++++++++++++++
|
||||
|
||||
- run wakatime package in main Sublime process
|
||||
|
||||
|
||||
1.0.1 (2013-08-09)
|
||||
++++++++++++++++++
|
||||
|
||||
- no longer beta for Package Control versioning requirement
|
||||
|
||||
|
||||
0.4.2 (2013-08-08)
|
||||
++++++++++++++++++
|
||||
|
||||
- remove away prompt popup
|
||||
|
||||
|
||||
0.4.0 (2013-08-08)
|
||||
++++++++++++++++++
|
||||
|
||||
- run wakatime package in background
|
||||
|
||||
|
||||
0.3.3 (2013-08-06)
|
||||
++++++++++++++++++
|
||||
|
||||
- support installing via Sublime Package Control
|
||||
|
||||
|
||||
0.3.2 (2013-08-06)
|
||||
++++++++++++++++++
|
||||
|
||||
- fixes for user sublime-settings file
|
||||
|
||||
|
||||
0.3.1 (2013-08-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- renamed plugin folder
|
||||
|
||||
|
||||
0.3.0 (2013-08-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- use WakaTime.sublime-settings file for configuration settings
|
||||
|
||||
|
||||
0.2.10 (2013-07-29)
|
||||
+++++++++++++++++++
|
||||
|
||||
- Python3 support
|
||||
- better Windows support by detecting pythonw.exe location
|
||||
|
||||
|
||||
0.2.9 (2013-07-22)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgraded wakatime package
|
||||
- bug fix when detecting git repos
|
||||
|
||||
|
||||
0.2.8 (2013-07-21)
|
||||
++++++++++++++++++
|
||||
|
||||
- Windows bug fixes
|
||||
|
||||
|
||||
0.2.7 (2013-07-20)
|
||||
++++++++++++++++++
|
||||
|
||||
- prevent cmd window opening in background (Windows users only)
|
||||
|
||||
|
||||
0.2.6 (2013-07-17)
|
||||
++++++++++++++++++
|
||||
|
||||
- log errors from wakatime package to ~/.wakatime.log
|
||||
|
||||
|
||||
0.2.5 (2013-07-17)
|
||||
++++++++++++++++++
|
||||
|
||||
- distinguish between write events and normal events
|
||||
- prompt user for api key if one does not already exist
|
||||
- rename ~/.wakatime to ~/.wakatime.conf
|
||||
- set away prompt to 5 minutes
|
||||
- fix bug in custom logger
|
||||
|
||||
|
||||
0.2.1 (2013-07-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- Birth
|
||||
|
29
LICENSE
Normal file
29
LICENSE
Normal file
@ -0,0 +1,29 @@
|
||||
Copyright (c) 2014 Alan Hamlett https://wakatime.com
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* Neither the names of Wakatime or WakaTime, nor the names of its
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
|
||||
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
|
||||
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
README.md
14
README.md
@ -1,14 +1,14 @@
|
||||
sublime-wakatime
|
||||
================
|
||||
|
||||
Automatic time tracking for Sublime Text 2 & 3.
|
||||
Fully automatic time tracking for Sublime Text 2 & 3.
|
||||
|
||||
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://wakati.me
|
||||
1. Get an api key from: https://wakatime.com/#apikey
|
||||
|
||||
2. Using [Sublime Package Control](http://wbond.net/sublime_packages/package_control):
|
||||
|
||||
@ -18,20 +18,16 @@ Heads Up! For Sublime Text 2 on Windows & Linux, WakaTime depends on [Python](ht
|
||||
|
||||
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://www.wakati.me/#apikey). Enter your api key, then press `enter`.
|
||||
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`.
|
||||
|
||||
4. Use Sublime and your time will automatically be tracked for you.
|
||||
|
||||
5. Visit https://wakati.me to see your logged time.
|
||||
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`
|
||||
|
||||
Screen Shots
|
||||
------------
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||

|
||||
|
||||
|
162
WakaTime.py
162
WakaTime.py
@ -1,11 +1,12 @@
|
||||
""" ==========================================================
|
||||
File: WakaTime.py
|
||||
Description: Automatic time tracking for Sublime Text 2 and 3.
|
||||
Maintainer: WakaTi.me <support@wakatime.com>
|
||||
Website: https://www.wakati.me/
|
||||
Maintainer: WakaTime <support@wakatime.com>
|
||||
License: BSD, see LICENSE for more details.
|
||||
Website: https://wakatime.com/
|
||||
==========================================================="""
|
||||
|
||||
__version__ = '1.4.3'
|
||||
__version__ = '2.0.9'
|
||||
|
||||
import sublime
|
||||
import sublime_plugin
|
||||
@ -17,18 +18,21 @@ import sys
|
||||
import time
|
||||
import threading
|
||||
import uuid
|
||||
from os.path import expanduser, dirname, realpath, isfile, join, exists
|
||||
from os.path import expanduser, dirname, basename, realpath, isfile, join, exists
|
||||
|
||||
|
||||
# globals
|
||||
ACTION_FREQUENCY = 5
|
||||
ACTION_FREQUENCY = 2
|
||||
ST_VERSION = int(sublime.version())
|
||||
PLUGIN_DIR = dirname(realpath(__file__))
|
||||
API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR
|
||||
SETTINGS_FILE = 'WakaTime.sublime-settings'
|
||||
SETTINGS = {}
|
||||
LAST_ACTION = 0
|
||||
LAST_FILE = None
|
||||
LAST_ACTION = {
|
||||
'time': 0,
|
||||
'file': None,
|
||||
'is_write': False,
|
||||
}
|
||||
HAS_SSL = False
|
||||
LOCK = threading.RLock()
|
||||
|
||||
@ -47,139 +51,137 @@ if HAS_SSL:
|
||||
import wakatime
|
||||
|
||||
|
||||
def setup_settings_file():
|
||||
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings
|
||||
"""
|
||||
global SETTINGS
|
||||
# To be backwards compatible, rename config file
|
||||
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
||||
api_key = SETTINGS.get('api_key', '')
|
||||
if not api_key:
|
||||
api_key = ''
|
||||
try:
|
||||
with open(join(expanduser('~'), '.wakatime.conf')) as old_file:
|
||||
for line in old_file:
|
||||
line = line.split('=', 1)
|
||||
if line[0] == 'api_key':
|
||||
api_key = str(line[1].strip())
|
||||
try:
|
||||
os.remove(join(expanduser('~'), '.wakatime.conf'))
|
||||
except:
|
||||
pass
|
||||
except IOError:
|
||||
pass
|
||||
SETTINGS.set('api_key', api_key)
|
||||
sublime.save_settings(SETTINGS_FILE)
|
||||
|
||||
|
||||
def prompt_api_key():
|
||||
global SETTINGS
|
||||
if not SETTINGS.get('api_key'):
|
||||
if SETTINGS.get('api_key'):
|
||||
return True
|
||||
else:
|
||||
def got_key(text):
|
||||
if text:
|
||||
SETTINGS.set('api_key', str(text))
|
||||
sublime.save_settings(SETTINGS_FILE)
|
||||
window = sublime.active_window()
|
||||
if window:
|
||||
window.show_input_panel('Enter your WakaTime api key:', '', got_key, None, None)
|
||||
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', '', got_key, None, None)
|
||||
return True
|
||||
else:
|
||||
print('Error: Could not prompt for api key because no window found.')
|
||||
print('[WakaTime] Error: Could not prompt for api key because no window found.')
|
||||
return False
|
||||
|
||||
|
||||
def python_binary():
|
||||
python = 'python'
|
||||
if platform.system() == 'Windows':
|
||||
python = 'pythonw'
|
||||
try:
|
||||
Popen([python, '--version'])
|
||||
Popen(['pythonw', '--version'])
|
||||
return 'pythonw'
|
||||
except:
|
||||
for path in glob.iglob('/python*'):
|
||||
if exists(realpath(join(path, 'pythonw.exe'))):
|
||||
python = realpath(join(path, 'pythonw'))
|
||||
break
|
||||
return python
|
||||
return realpath(join(path, 'pythonw'))
|
||||
return None
|
||||
return 'python'
|
||||
|
||||
|
||||
def enough_time_passed(now):
|
||||
if now - LAST_ACTION > ACTION_FREQUENCY * 60:
|
||||
def enough_time_passed(now, last_time):
|
||||
if now - last_time > ACTION_FREQUENCY * 60:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def handle_write_action(view):
|
||||
global LOCK, LAST_FILE, LAST_ACTION
|
||||
with LOCK:
|
||||
targetFile = view.file_name()
|
||||
thread = SendActionThread(targetFile, isWrite=True)
|
||||
thread.start()
|
||||
LAST_FILE = targetFile
|
||||
LAST_ACTION = time.time()
|
||||
def find_project_name_from_folders(folders):
|
||||
for folder in folders:
|
||||
for file_name in os.listdir(folder):
|
||||
if file_name.endswith('.sublime-project'):
|
||||
return file_name.replace('.sublime-project', '', 1)
|
||||
return None
|
||||
|
||||
|
||||
def handle_normal_action(view):
|
||||
global LOCK, LAST_FILE, LAST_ACTION
|
||||
def handle_action(view, is_write=False):
|
||||
global LOCK, LAST_ACTION
|
||||
with LOCK:
|
||||
targetFile = view.file_name()
|
||||
thread = SendActionThread(targetFile)
|
||||
thread.start()
|
||||
LAST_FILE = targetFile
|
||||
LAST_ACTION = time.time()
|
||||
target_file = view.file_name()
|
||||
if target_file:
|
||||
project = view.window().project_file_name() if hasattr(view.window(), 'project_file_name') else None
|
||||
if project:
|
||||
project = basename(project).replace('.sublime-project', '', 1)
|
||||
thread = SendActionThread(target_file, is_write=is_write, project=project, folders=view.window().folders())
|
||||
thread.start()
|
||||
LAST_ACTION = {
|
||||
'file': target_file,
|
||||
'time': time.time(),
|
||||
'is_write': is_write,
|
||||
}
|
||||
|
||||
|
||||
class SendActionThread(threading.Thread):
|
||||
|
||||
def __init__(self, targetFile, isWrite=False, force=False):
|
||||
def __init__(self, target_file, is_write=False, project=None, folders=None, force=False):
|
||||
threading.Thread.__init__(self)
|
||||
self.targetFile = targetFile
|
||||
self.isWrite = isWrite
|
||||
self.target_file = target_file
|
||||
self.is_write = is_write
|
||||
self.project = project
|
||||
self.folders = folders
|
||||
self.force = force
|
||||
self.debug = SETTINGS.get('debug')
|
||||
self.api_key = SETTINGS.get('api_key', '')
|
||||
self.last_file = LAST_FILE
|
||||
self.ignore = SETTINGS.get('ignore', [])
|
||||
self.last_action = LAST_ACTION
|
||||
|
||||
def run(self):
|
||||
if self.targetFile:
|
||||
if self.target_file:
|
||||
self.timestamp = time.time()
|
||||
if self.force or self.isWrite or self.targetFile != self.last_file or enough_time_passed(self.timestamp):
|
||||
if self.force or (self.is_write and not self.last_action['is_write']) or self.target_file != self.last_action['file'] or enough_time_passed(self.timestamp, self.last_action['time']):
|
||||
self.send()
|
||||
|
||||
def send(self):
|
||||
if not self.api_key:
|
||||
print('missing api key')
|
||||
print('[WakaTime] Error: missing api key.')
|
||||
return
|
||||
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
||||
cmd = [
|
||||
API_CLIENT,
|
||||
'--file', self.targetFile,
|
||||
'--file', self.target_file,
|
||||
'--time', str('%f' % self.timestamp),
|
||||
'--plugin', ua,
|
||||
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
|
||||
]
|
||||
if self.isWrite:
|
||||
if self.is_write:
|
||||
cmd.append('--write')
|
||||
if self.project:
|
||||
cmd.extend(['--project', self.project])
|
||||
elif self.folders:
|
||||
project_name = find_project_name_from_folders(self.folders)
|
||||
if project_name:
|
||||
cmd.extend(['--project', project_name])
|
||||
for pattern in self.ignore:
|
||||
cmd.extend(['--ignore', pattern])
|
||||
if self.debug:
|
||||
cmd.append('--verbose')
|
||||
if HAS_SSL:
|
||||
if self.debug:
|
||||
print(cmd)
|
||||
print('[WakaTime] %s' % ' '.join(cmd))
|
||||
code = wakatime.main(cmd)
|
||||
if code != 0:
|
||||
print('Error: Response code %d from wakatime package' % code)
|
||||
print('[WakaTime] Error: Response code %d from wakatime package.' % code)
|
||||
else:
|
||||
cmd.insert(0, python_binary())
|
||||
if self.debug:
|
||||
print(cmd)
|
||||
if platform.system() == 'Windows':
|
||||
Popen(cmd, shell=False)
|
||||
python = python_binary()
|
||||
if python:
|
||||
cmd.insert(0, python)
|
||||
if self.debug:
|
||||
print('[WakaTime] %s' % ' '.join(cmd))
|
||||
if platform.system() == 'Windows':
|
||||
Popen(cmd, shell=False)
|
||||
else:
|
||||
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
||||
Popen(cmd, stderr=stderr)
|
||||
else:
|
||||
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
||||
Popen(cmd, stderr=stderr)
|
||||
print('[WakaTime] Error: Unable to find python binary.')
|
||||
|
||||
|
||||
def plugin_loaded():
|
||||
setup_settings_file()
|
||||
global SETTINGS
|
||||
print('[WakaTime] Initializing WakaTime plugin v%s' % __version__)
|
||||
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
||||
after_loaded()
|
||||
|
||||
|
||||
@ -196,10 +198,10 @@ if ST_VERSION < 3000:
|
||||
class WakatimeListener(sublime_plugin.EventListener):
|
||||
|
||||
def on_post_save(self, view):
|
||||
handle_write_action(view)
|
||||
handle_action(view, is_write=True)
|
||||
|
||||
def on_activated(self, view):
|
||||
handle_normal_action(view)
|
||||
handle_action(view)
|
||||
|
||||
def on_modified(self, view):
|
||||
handle_normal_action(view)
|
||||
handle_action(view)
|
||||
|
@ -3,10 +3,14 @@
|
||||
// This settings file will be overwritten when upgrading.
|
||||
|
||||
{
|
||||
// Your api key from https://www.wakati.me/#apikey
|
||||
// Your api key from https://wakatime.com/#apikey
|
||||
// Set this in your User specific WakaTime.sublime-settings file.
|
||||
"api_key": "",
|
||||
|
||||
// Ignore files; Files (including absolute paths) that match one of these
|
||||
// POSIX regular expressions will not be logged.
|
||||
"ignore": ["^/tmp/", "^/etc/", "^/var/"],
|
||||
|
||||
// Debug mode. Set to true for verbose logging. Defaults to false.
|
||||
"debug": false
|
||||
}
|
||||
|
4
packages/wakatime/.gitignore
vendored
4
packages/wakatime/.gitignore
vendored
@ -33,3 +33,7 @@ nosetests.xml
|
||||
.mr.developer.cfg
|
||||
.project
|
||||
.pydevproject
|
||||
|
||||
virtualenv
|
||||
venv
|
||||
.DS_Store
|
||||
|
15
packages/wakatime/AUTHORS
Normal file
15
packages/wakatime/AUTHORS
Normal file
@ -0,0 +1,15 @@
|
||||
WakaTime is written and maintained by Alan Hamlett and
|
||||
various contributors:
|
||||
|
||||
|
||||
Development Lead
|
||||
----------------
|
||||
|
||||
- Alan Hamlett <alan.hamlett@gmail.com>
|
||||
|
||||
|
||||
Patches and Suggestions
|
||||
-----------------------
|
||||
|
||||
- 3onyc <3onyc@x3tech.com>
|
||||
- userid <xixico@ymail.com>
|
@ -3,6 +3,123 @@ History
|
||||
-------
|
||||
|
||||
|
||||
2.0.7 (2014-08-27)
|
||||
++++++++++++++++++
|
||||
|
||||
- find svn binary location from common install directories
|
||||
|
||||
|
||||
2.0.6 (2014-08-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- encode json data as str when passing to urllib
|
||||
|
||||
|
||||
2.0.5 (2014-07-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- option in .wakatime.cfg to obfuscate file names
|
||||
|
||||
|
||||
2.0.4 (2014-07-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- use unique logger namespace to prevent collisions in shared plugin environments
|
||||
|
||||
|
||||
2.0.3 (2014-06-18)
|
||||
++++++++++++++++++
|
||||
|
||||
- use project from command line arg when no revision control project is found
|
||||
|
||||
|
||||
2.0.2 (2014-06-09)
|
||||
++++++++++++++++++
|
||||
|
||||
- include python3.2 compatible versions of simplejson, pytz, and tzlocal
|
||||
- disable offline logging when Python was not compiled with sqlite3 module
|
||||
|
||||
|
||||
2.0.1 (2014-05-26)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix bug in queue preventing actions with NULL values from being purged
|
||||
|
||||
|
||||
2.0.0 (2014-05-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- offline time logging using sqlite3 to queue editor events
|
||||
|
||||
|
||||
1.0.2 (2014-05-06)
|
||||
++++++++++++++++++
|
||||
|
||||
- ability to set project from command line argument
|
||||
|
||||
|
||||
1.0.1 (2014-03-05)
|
||||
++++++++++++++++++
|
||||
|
||||
- use new domain name wakatime.com
|
||||
|
||||
|
||||
1.0.0 (2014-02-05)
|
||||
++++++++++++++++++
|
||||
|
||||
- detect project name and branch name from mercurial revision control
|
||||
|
||||
|
||||
0.5.3 (2014-01-15)
|
||||
++++++++++++++++++
|
||||
|
||||
- bug fix for unicode in Python3
|
||||
|
||||
|
||||
0.5.2 (2014-01-14)
|
||||
++++++++++++++++++
|
||||
|
||||
- minor bug fix for Subversion on non-English systems
|
||||
|
||||
|
||||
0.5.1 (2013-12-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- second line in .wakatime-project file now sets branch name
|
||||
|
||||
|
||||
0.5.0 (2013-12-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- Convert ~/.wakatime.conf to ~/.wakatime.cfg and use configparser format
|
||||
- new [projectmap] section in cfg file for naming projects based on folders
|
||||
|
||||
|
||||
0.4.10 (2013-11-13)
|
||||
+++++++++++++++++++
|
||||
|
||||
- Placing .wakatime-project file in a folder will read the project's name from that file
|
||||
|
||||
|
||||
0.4.9 (2013-10-27)
|
||||
++++++++++++++++++
|
||||
|
||||
- New config for ignoring files from regular expressions
|
||||
- Parse more options from config file (verbose, logfile, ignore)
|
||||
|
||||
|
||||
0.4.8 (2013-10-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- Read git HEAD file to find current branch instead of running git command line
|
||||
|
||||
|
||||
0.4.7 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- Sending local olson timezone string in api request
|
||||
|
||||
|
||||
0.4.6 (2013-09-22)
|
||||
++++++++++++++++++
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
Copyright (c) 2013 Alan Hamlett https://wakati.me
|
||||
Copyright (c) 2013 Alan Hamlett https://wakatime.com
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@ -12,7 +12,7 @@ modification, are permitted provided that the following conditions are met:
|
||||
in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* Neither the names of Wakatime or Wakati.Me, nor the names of its
|
||||
* Neither the names of Wakatime or WakaTime, nor the names of its
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
|
@ -1,12 +1,20 @@
|
||||
WakaTime
|
||||
========
|
||||
|
||||
Automatic time tracking for your text editor. This is the command line
|
||||
event appender for the WakaTime api. You shouldn't need to directly
|
||||
use this outside of a text editor plugin.
|
||||
Fully automatic time tracking for programmers.
|
||||
|
||||
This is the common interface for the WakaTime api. You shouldn't need to directly use this package unless you are creating a new plugin or your text editor's plugin asks you to install the wakatime-cli interface.
|
||||
|
||||
Go to http://wakatime.com to install the plugin for your text editor.
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
https://www.wakati.me/help/plugins/installing-plugins
|
||||
pip install wakatime
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
https://wakatime.com/
|
||||
|
@ -11,13 +11,10 @@ setup(
|
||||
name='wakatime',
|
||||
version=VERSION,
|
||||
license='BSD 3 Clause',
|
||||
description=' '.join([
|
||||
'Action event appender for Wakati.Me, a time',
|
||||
'tracking api for text editors.',
|
||||
]),
|
||||
description='Interface to the WakaTime api.',
|
||||
long_description=open('README.rst').read(),
|
||||
author='Alan Hamlett',
|
||||
author_email='alan.hamlett@gmail.com',
|
||||
author_email='alan@wakatime.com',
|
||||
url='https://github.com/wakatime/wakatime',
|
||||
packages=packages,
|
||||
package_dir={'wakatime': 'wakatime'},
|
||||
@ -28,7 +25,7 @@ setup(
|
||||
'console_scripts': ['wakatime = wakatime.__init__:main'],
|
||||
},
|
||||
classifiers=(
|
||||
'Development Status :: 3 - Alpha',
|
||||
'Development Status :: 5 - Production/Stable',
|
||||
'Environment :: Console',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
|
@ -1,10 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime-cli
|
||||
~~~~~~~~~~~~
|
||||
|
||||
Action event appender for Wakati.Me, auto time tracking for text editors.
|
||||
Command-line entry point.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
|
@ -3,7 +3,8 @@
|
||||
wakatime
|
||||
~~~~~~~~
|
||||
|
||||
Action event appender for Wakati.Me, auto time tracking for text editors.
|
||||
Common interface to the WakaTime api.
|
||||
http://wakatime.com
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
@ -12,10 +13,10 @@
|
||||
from __future__ import print_function
|
||||
|
||||
__title__ = 'wakatime'
|
||||
__version__ = '0.4.7'
|
||||
__version__ = '2.0.7'
|
||||
__author__ = 'Alan Hamlett'
|
||||
__license__ = 'BSD'
|
||||
__copyright__ = 'Copyright 2013 Alan Hamlett'
|
||||
__copyright__ = 'Copyright 2014 Alan Hamlett'
|
||||
|
||||
|
||||
import base64
|
||||
@ -26,23 +27,37 @@ import re
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
|
||||
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 .log import setup_logging
|
||||
from .project import find_project
|
||||
from .stats import get_file_stats
|
||||
from .packages import argparse
|
||||
from .packages import simplejson as json
|
||||
from .packages import tzlocal
|
||||
try:
|
||||
import ConfigParser as configparser
|
||||
except ImportError:
|
||||
import configparser
|
||||
try:
|
||||
from urllib2 import HTTPError, Request, urlopen
|
||||
except ImportError:
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
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'))
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
from .queue import Queue
|
||||
from .log import setup_logging
|
||||
from .project import find_project
|
||||
from .stats import get_file_stats
|
||||
from .packages import argparse
|
||||
from .packages import simplejson as json
|
||||
try:
|
||||
from .packages import tzlocal
|
||||
except:
|
||||
from .packages import tzlocal3
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class FileAction(argparse.Action):
|
||||
@ -52,13 +67,84 @@ class FileAction(argparse.Action):
|
||||
setattr(namespace, self.dest, values)
|
||||
|
||||
|
||||
def upgradeConfigFile(configFile):
|
||||
"""For backwards-compatibility, upgrade the existing config file
|
||||
to work with configparser and rename from .wakatime.conf to .wakatime.cfg.
|
||||
"""
|
||||
|
||||
if os.path.isfile(configFile):
|
||||
# if upgraded cfg file already exists, don't overwrite it
|
||||
return
|
||||
|
||||
oldConfig = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
||||
try:
|
||||
configs = {
|
||||
'ignore': [],
|
||||
}
|
||||
|
||||
with open(oldConfig) as fh:
|
||||
for line in fh.readlines():
|
||||
line = line.split('=', 1)
|
||||
if len(line) == 2 and line[0].strip() and line[1].strip():
|
||||
if line[0].strip() == 'ignore':
|
||||
configs['ignore'].append(line[1].strip())
|
||||
else:
|
||||
configs[line[0].strip()] = line[1].strip()
|
||||
|
||||
with open(configFile, 'w') as fh:
|
||||
fh.write("[settings]\n")
|
||||
for name, value in configs.items():
|
||||
if isinstance(value, list):
|
||||
fh.write("%s=\n" % name)
|
||||
for item in value:
|
||||
fh.write(" %s\n" % item)
|
||||
else:
|
||||
fh.write("%s = %s\n" % (name, value))
|
||||
|
||||
os.remove(oldConfig)
|
||||
except IOError:
|
||||
pass
|
||||
|
||||
|
||||
def parseConfigFile(configFile):
|
||||
"""Returns a configparser.SafeConfigParser instance with configs
|
||||
read from the config file. Default location of the config file is
|
||||
at ~/.wakatime.cfg.
|
||||
"""
|
||||
|
||||
if not configFile:
|
||||
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
|
||||
|
||||
upgradeConfigFile(configFile)
|
||||
|
||||
configs = configparser.SafeConfigParser()
|
||||
try:
|
||||
with open(configFile) 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')
|
||||
return configs
|
||||
|
||||
|
||||
def parseArguments(argv):
|
||||
"""Parse command line arguments and configs from ~/.wakatime.cfg.
|
||||
Command line arguments take precedence over config file settings.
|
||||
Returns instances of ArgumentParser and SafeConfigParser.
|
||||
"""
|
||||
|
||||
try:
|
||||
sys.argv
|
||||
except AttributeError:
|
||||
sys.argv = argv
|
||||
|
||||
# define supported command line arguments
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Wakati.Me event api appender')
|
||||
description='Common interface for the WakaTime api.')
|
||||
parser.add_argument('--file', dest='targetFile', metavar='file',
|
||||
action=FileAction, required=True,
|
||||
help='absolute path to file for current action')
|
||||
@ -72,9 +158,19 @@ def parseArguments(argv):
|
||||
parser.add_argument('--plugin', dest='plugin',
|
||||
help='optional text editor plugin name and version '+
|
||||
'for User-Agent header')
|
||||
parser.add_argument('--project', dest='project_name',
|
||||
help='optional project name; will auto-discover by default')
|
||||
parser.add_argument('--key', dest='key',
|
||||
help='your wakati.me api key; uses api_key from '+
|
||||
help='your wakatime api key; uses api_key from '+
|
||||
'~/.wakatime.conf by default')
|
||||
parser.add_argument('--disableoffline', dest='offline',
|
||||
action='store_false',
|
||||
help='disables offline time logging instead of queuing logged time')
|
||||
parser.add_argument('--hidefilenames', dest='hidefilenames',
|
||||
action='store_true',
|
||||
help='obfuscate file names; will not send file names to api')
|
||||
parser.add_argument('--ignore', dest='ignore', action='append',
|
||||
help='filename patterns to ignore; POSIX regex syntax; can be used more than once')
|
||||
parser.add_argument('--logfile', dest='logfile',
|
||||
help='defaults to ~/.wakatime.log')
|
||||
parser.add_argument('--config', dest='config',
|
||||
@ -82,52 +178,99 @@ def parseArguments(argv):
|
||||
parser.add_argument('--verbose', dest='verbose', action='store_true',
|
||||
help='turns on debug messages in log file')
|
||||
parser.add_argument('--version', action='version', version=__version__)
|
||||
|
||||
# parse command line arguments
|
||||
args = parser.parse_args(args=argv[1:])
|
||||
|
||||
# use current unix epoch timestamp by default
|
||||
if not args.timestamp:
|
||||
args.timestamp = time.time()
|
||||
|
||||
# parse ~/.wakatime.cfg file
|
||||
configs = parseConfigFile(args.config)
|
||||
if configs is None:
|
||||
return args, configs
|
||||
|
||||
# update args from configs
|
||||
if not args.key:
|
||||
default_key = get_api_key(args.config)
|
||||
default_key = None
|
||||
if configs.has_option('settings', 'api_key'):
|
||||
default_key = configs.get('settings', 'api_key')
|
||||
if default_key:
|
||||
args.key = default_key
|
||||
else:
|
||||
parser.error('Missing api key')
|
||||
return args
|
||||
if not args.ignore:
|
||||
args.ignore = []
|
||||
if configs.has_option('settings', 'ignore'):
|
||||
try:
|
||||
for pattern in configs.get('settings', 'ignore').split("\n"):
|
||||
if pattern.strip() != '':
|
||||
args.ignore.append(pattern)
|
||||
except TypeError:
|
||||
pass
|
||||
if args.offline and configs.has_option('settings', 'offline'):
|
||||
args.offline = configs.getboolean('settings', 'offline')
|
||||
if not args.hidefilenames and configs.has_option('settings', 'hidefilenames'):
|
||||
args.hidefilenames = configs.getboolean('settings', 'hidefilenames')
|
||||
if not args.verbose and configs.has_option('settings', 'verbose'):
|
||||
args.verbose = configs.getboolean('settings', 'verbose')
|
||||
if not args.verbose and configs.has_option('settings', 'debug'):
|
||||
args.verbose = configs.getboolean('settings', 'debug')
|
||||
if not args.logfile and configs.has_option('settings', 'logfile'):
|
||||
args.logfile = configs.get('settings', 'logfile')
|
||||
|
||||
return args, configs
|
||||
|
||||
|
||||
def get_api_key(configFile):
|
||||
if not configFile:
|
||||
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
||||
api_key = None
|
||||
def should_ignore(fileName, patterns):
|
||||
try:
|
||||
cf = open(configFile)
|
||||
for line in cf:
|
||||
line = line.split('=', 1)
|
||||
if line[0] == 'api_key':
|
||||
api_key = line[1].strip()
|
||||
cf.close()
|
||||
except IOError:
|
||||
print('Error: Could not read from config file.')
|
||||
return api_key
|
||||
for pattern in patterns:
|
||||
try:
|
||||
compiled = re.compile(pattern, re.IGNORECASE)
|
||||
if compiled.search(fileName):
|
||||
return pattern
|
||||
except re.error as ex:
|
||||
log.warning(unicode('Regex error ({msg}) for ignore pattern: {pattern}').format(
|
||||
msg=str(ex),
|
||||
pattern=pattern,
|
||||
))
|
||||
except TypeError:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
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 = 'wakatime/%s (%s) Python%s' % (__version__,
|
||||
platform.platform(), python_version)
|
||||
user_agent = unicode('wakatime/{ver} ({platform}) Python{py_ver}').format(
|
||||
ver=__version__,
|
||||
platform=platform.platform(),
|
||||
py_ver=python_version,
|
||||
)
|
||||
if plugin:
|
||||
user_agent = user_agent+' '+plugin
|
||||
user_agent = unicode('{user_agent} {plugin}').format(
|
||||
user_agent=user_agent,
|
||||
plugin=plugin,
|
||||
)
|
||||
return user_agent
|
||||
|
||||
|
||||
def send_action(project=None, branch=None, stats={}, key=None, targetFile=None,
|
||||
timestamp=None, isWrite=None, plugin=None, **kwargs):
|
||||
url = 'https://www.wakati.me/api/v1/actions'
|
||||
log.debug('Sending action to api at %s' % url)
|
||||
def send_action(project=None, branch=None, stats=None, key=None, targetFile=None,
|
||||
timestamp=None, isWrite=None, plugin=None, offline=None,
|
||||
hidefilenames=None, **kwargs):
|
||||
url = 'https://wakatime.com/api/v1/actions'
|
||||
log.debug('Sending heartbeat to api at %s' % url)
|
||||
data = {
|
||||
'time': timestamp,
|
||||
'file': targetFile,
|
||||
}
|
||||
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])
|
||||
else:
|
||||
data['file'] = unicode('HIDDEN')
|
||||
if stats.get('lines'):
|
||||
data['lines'] = stats['lines']
|
||||
if stats.get('language'):
|
||||
@ -144,13 +287,13 @@ def send_action(project=None, branch=None, stats={}, key=None, targetFile=None,
|
||||
request = Request(url=url, data=str.encode(json.dumps(data)))
|
||||
request.add_header('User-Agent', get_user_agent(plugin))
|
||||
request.add_header('Content-Type', 'application/json')
|
||||
auth = 'Basic %s' % bytes.decode(base64.b64encode(str.encode(key)))
|
||||
auth = unicode('Basic {key}').format(key=bytes.decode(base64.b64encode(str.encode(key))))
|
||||
request.add_header('Authorization', auth)
|
||||
|
||||
# add Olson timezone to request
|
||||
tz = tzlocal.get_localzone()
|
||||
if tz:
|
||||
request.add_header('TimeZone', str(tz.zone))
|
||||
request.add_header('TimeZone', unicode(tz.zone))
|
||||
|
||||
# log time to api
|
||||
response = None
|
||||
@ -159,53 +302,111 @@ def send_action(project=None, branch=None, stats={}, key=None, targetFile=None,
|
||||
except HTTPError as exc:
|
||||
exception_data = {
|
||||
'response_code': exc.getcode(),
|
||||
sys.exc_info()[0].__name__: str(sys.exc_info()[1]),
|
||||
sys.exc_info()[0].__name__: unicode(sys.exc_info()[1]),
|
||||
}
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
exception_data['traceback'] = traceback.format_exc()
|
||||
log.error(exception_data)
|
||||
if offline:
|
||||
queue = Queue()
|
||||
queue.push(data, plugin)
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
log.warn(exception_data)
|
||||
else:
|
||||
log.error(exception_data)
|
||||
except:
|
||||
exception_data = {
|
||||
sys.exc_info()[0].__name__: str(sys.exc_info()[1]),
|
||||
sys.exc_info()[0].__name__: unicode(sys.exc_info()[1]),
|
||||
}
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
exception_data['traceback'] = traceback.format_exc()
|
||||
log.error(exception_data)
|
||||
if offline:
|
||||
queue = Queue()
|
||||
queue.push(data, plugin)
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
log.warn(exception_data)
|
||||
else:
|
||||
log.error(exception_data)
|
||||
else:
|
||||
if response.getcode() == 201:
|
||||
log.debug({
|
||||
'response_code': response.getcode(),
|
||||
})
|
||||
return True
|
||||
log.error({
|
||||
'response_code': response.getcode(),
|
||||
'response_content': response.read(),
|
||||
})
|
||||
if offline:
|
||||
queue = Queue()
|
||||
queue.push(data, plugin)
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
log.warn({
|
||||
'response_code': response.getcode(),
|
||||
'response_content': response.read(),
|
||||
})
|
||||
else:
|
||||
log.error({
|
||||
'response_code': response.getcode(),
|
||||
'response_content': response.read(),
|
||||
})
|
||||
else:
|
||||
log.error({
|
||||
'response_code': response.getcode(),
|
||||
'response_content': response.read(),
|
||||
})
|
||||
return False
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
if not argv:
|
||||
argv = sys.argv
|
||||
args = parseArguments(argv)
|
||||
|
||||
args, configs = parseArguments(argv)
|
||||
if configs is None:
|
||||
return 103 # config file parsing error
|
||||
|
||||
setup_logging(args, __version__)
|
||||
|
||||
ignore = should_ignore(args.targetFile, args.ignore)
|
||||
if ignore is not False:
|
||||
log.debug(unicode('File ignored because matches pattern: {pattern}').format(
|
||||
pattern=ignore,
|
||||
))
|
||||
return 0
|
||||
|
||||
if os.path.isfile(args.targetFile):
|
||||
branch = None
|
||||
name = None
|
||||
|
||||
stats = get_file_stats(args.targetFile)
|
||||
project = find_project(args.targetFile)
|
||||
|
||||
project = find_project(args.targetFile, configs=configs)
|
||||
branch = None
|
||||
project_name = args.project_name
|
||||
if project:
|
||||
branch = project.branch()
|
||||
name = project.name()
|
||||
project_name = project.name()
|
||||
|
||||
if send_action(
|
||||
project=name,
|
||||
project=project_name,
|
||||
branch=branch,
|
||||
stats=stats,
|
||||
**vars(args)
|
||||
):
|
||||
return 0
|
||||
return 102
|
||||
queue = Queue()
|
||||
while True:
|
||||
action = queue.pop()
|
||||
if action is None:
|
||||
break
|
||||
sent = send_action(project=action['project'],
|
||||
targetFile=action['file'],
|
||||
timestamp=action['time'],
|
||||
branch=action['branch'],
|
||||
stats={'language': action['language'], 'lines': action['lines']},
|
||||
key=args.key, isWrite=action['is_write'],
|
||||
plugin=action['plugin'],
|
||||
offline=args.offline,
|
||||
hidefilenames=args.hidefilenames)
|
||||
if not sent:
|
||||
break
|
||||
return 0 # success
|
||||
|
||||
return 102 # api error
|
||||
|
||||
else:
|
||||
log.debug('File does not exist; ignoring this action.')
|
||||
return 101
|
||||
|
||||
return 0
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from .packages import simplejson as json
|
||||
try:
|
||||
@ -25,7 +26,13 @@ class CustomEncoder(json.JSONEncoder):
|
||||
if isinstance(obj, bytes):
|
||||
obj = bytes.decode(obj)
|
||||
return json.dumps(obj)
|
||||
return super(CustomEncoder, self).default(obj)
|
||||
try:
|
||||
encoded = super(CustomEncoder, self).default(obj)
|
||||
except UnicodeDecodeError:
|
||||
encoding = sys.getfilesystemencoding()
|
||||
obj = obj.decode(encoding, 'ignore').encode('utf-8')
|
||||
encoded = super(CustomEncoder, self).default(obj)
|
||||
return encoded
|
||||
|
||||
|
||||
class JsonFormatter(logging.Formatter):
|
||||
@ -66,10 +73,10 @@ def set_log_level(logger, args):
|
||||
|
||||
|
||||
def setup_logging(args, version):
|
||||
logger = logging.getLogger()
|
||||
logger = logging.getLogger('WakaTime')
|
||||
set_log_level(logger, args)
|
||||
if len(logger.handlers) > 0:
|
||||
formatter = JsonFormatter(datefmt='%a %b %d %H:%M:%S %Z %Y')
|
||||
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
||||
formatter.setup(
|
||||
timestamp=args.timestamp,
|
||||
isWrite=args.isWrite,
|
||||
@ -83,7 +90,7 @@ def setup_logging(args, version):
|
||||
if not logfile:
|
||||
logfile = '~/.wakatime.log'
|
||||
handler = logging.FileHandler(os.path.expanduser(logfile))
|
||||
formatter = JsonFormatter(datefmt='%a %b %d %H:%M:%S %Z %Y')
|
||||
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
||||
formatter.setup(
|
||||
timestamp=args.timestamp,
|
||||
isWrite=args.isWrite,
|
||||
|
1512
packages/wakatime/wakatime/packages/pytz3/__init__.py
Normal file
1512
packages/wakatime/wakatime/packages/pytz3/__init__.py
Normal file
File diff suppressed because it is too large
Load Diff
48
packages/wakatime/wakatime/packages/pytz3/exceptions.py
Normal file
48
packages/wakatime/wakatime/packages/pytz3/exceptions.py
Normal file
@ -0,0 +1,48 @@
|
||||
'''
|
||||
Custom exceptions raised by pytz.
|
||||
'''
|
||||
|
||||
__all__ = [
|
||||
'UnknownTimeZoneError', 'InvalidTimeError', 'AmbiguousTimeError',
|
||||
'NonExistentTimeError',
|
||||
]
|
||||
|
||||
|
||||
class UnknownTimeZoneError(KeyError):
|
||||
'''Exception raised when pytz is passed an unknown timezone.
|
||||
|
||||
>>> isinstance(UnknownTimeZoneError(), LookupError)
|
||||
True
|
||||
|
||||
This class is actually a subclass of KeyError to provide backwards
|
||||
compatibility with code relying on the undocumented behavior of earlier
|
||||
pytz releases.
|
||||
|
||||
>>> isinstance(UnknownTimeZoneError(), KeyError)
|
||||
True
|
||||
'''
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTimeError(Exception):
|
||||
'''Base class for invalid time exceptions.'''
|
||||
|
||||
|
||||
class AmbiguousTimeError(InvalidTimeError):
|
||||
'''Exception raised when attempting to create an ambiguous wallclock time.
|
||||
|
||||
At the end of a DST transition period, a particular wallclock time will
|
||||
occur twice (once before the clocks are set back, once after). Both
|
||||
possibilities may be correct, unless further information is supplied.
|
||||
|
||||
See DstTzInfo.normalize() for more info
|
||||
'''
|
||||
|
||||
|
||||
class NonExistentTimeError(InvalidTimeError):
|
||||
'''Exception raised when attempting to create a wallclock time that
|
||||
cannot exist.
|
||||
|
||||
At the start of a DST transition period, the wallclock time jumps forward.
|
||||
The instants jumped over never occur.
|
||||
'''
|
148
packages/wakatime/wakatime/packages/pytz3/lazy.py
Normal file
148
packages/wakatime/wakatime/packages/pytz3/lazy.py
Normal file
@ -0,0 +1,148 @@
|
||||
from threading import RLock
|
||||
try:
|
||||
from UserDict import DictMixin
|
||||
except ImportError:
|
||||
from collections import Mapping as DictMixin
|
||||
|
||||
|
||||
_fill_lock = RLock()
|
||||
|
||||
|
||||
class LazyDict(DictMixin):
|
||||
"""Dictionary populated on first use."""
|
||||
data = None
|
||||
def __getitem__(self, key):
|
||||
if self.data is None:
|
||||
_fill_lock.acquire()
|
||||
try:
|
||||
if self.data is None:
|
||||
self._fill()
|
||||
finally:
|
||||
_fill_lock.release()
|
||||
return self.data[key.upper()]
|
||||
|
||||
def __contains__(self, key):
|
||||
if self.data is None:
|
||||
_fill_lock.acquire()
|
||||
try:
|
||||
if self.data is None:
|
||||
self._fill()
|
||||
finally:
|
||||
_fill_lock_release()
|
||||
return key in self.data
|
||||
|
||||
def __iter__(self):
|
||||
if self.data is None:
|
||||
_fill_lock.acquire()
|
||||
try:
|
||||
if self.data is None:
|
||||
self._fill()
|
||||
finally:
|
||||
_fill_lock.release()
|
||||
return iter(self.data)
|
||||
|
||||
def __len__(self):
|
||||
if self.data is None:
|
||||
_fill_lock.acquire()
|
||||
try:
|
||||
if self.data is None:
|
||||
self._fill()
|
||||
finally:
|
||||
_fill_lock.release()
|
||||
return len(self.data)
|
||||
|
||||
def keys(self):
|
||||
if self.data is None:
|
||||
_fill_lock.acquire()
|
||||
try:
|
||||
if self.data is None:
|
||||
self._fill()
|
||||
finally:
|
||||
_fill_lock.release()
|
||||
return list(self.data.keys())
|
||||
|
||||
|
||||
class LazyList(list):
|
||||
"""List populated on first use."""
|
||||
def __new__(cls, fill_iter):
|
||||
|
||||
class LazyList(list):
|
||||
_fill_iter = None
|
||||
|
||||
_props = (
|
||||
'__str__', '__repr__', '__unicode__',
|
||||
'__hash__', '__sizeof__', '__cmp__', '__nonzero__',
|
||||
'__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__',
|
||||
'append', 'count', 'index', 'extend', 'insert', 'pop', 'remove',
|
||||
'reverse', 'sort', '__add__', '__radd__', '__iadd__', '__mul__',
|
||||
'__rmul__', '__imul__', '__contains__', '__len__', '__nonzero__',
|
||||
'__getitem__', '__setitem__', '__delitem__', '__iter__',
|
||||
'__reversed__', '__getslice__', '__setslice__', '__delslice__')
|
||||
|
||||
def lazy(name):
|
||||
def _lazy(self, *args, **kw):
|
||||
if self._fill_iter is not None:
|
||||
_fill_lock.acquire()
|
||||
try:
|
||||
if self._fill_iter is not None:
|
||||
list.extend(self, self._fill_iter)
|
||||
self._fill_iter = None
|
||||
finally:
|
||||
_fill_lock.release()
|
||||
real = getattr(list, name)
|
||||
setattr(self.__class__, name, real)
|
||||
return real(self, *args, **kw)
|
||||
return _lazy
|
||||
|
||||
for name in _props:
|
||||
setattr(LazyList, name, lazy(name))
|
||||
|
||||
new_list = LazyList()
|
||||
new_list._fill_iter = fill_iter
|
||||
return new_list
|
||||
|
||||
|
||||
class LazySet(set):
|
||||
"""Set populated on first use."""
|
||||
def __new__(cls, fill_iter):
|
||||
|
||||
class LazySet(set):
|
||||
_fill_iter = None
|
||||
|
||||
_props = (
|
||||
'__str__', '__repr__', '__unicode__',
|
||||
'__hash__', '__sizeof__', '__cmp__', '__nonzero__',
|
||||
'__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__',
|
||||
'__contains__', '__len__', '__nonzero__',
|
||||
'__getitem__', '__setitem__', '__delitem__', '__iter__',
|
||||
'__sub__', '__and__', '__xor__', '__or__',
|
||||
'__rsub__', '__rand__', '__rxor__', '__ror__',
|
||||
'__isub__', '__iand__', '__ixor__', '__ior__',
|
||||
'add', 'clear', 'copy', 'difference', 'difference_update',
|
||||
'discard', 'intersection', 'intersection_update', 'isdisjoint',
|
||||
'issubset', 'issuperset', 'pop', 'remove',
|
||||
'symmetric_difference', 'symmetric_difference_update',
|
||||
'union', 'update')
|
||||
|
||||
def lazy(name):
|
||||
def _lazy(self, *args, **kw):
|
||||
if self._fill_iter is not None:
|
||||
_fill_lock.acquire()
|
||||
try:
|
||||
if self._fill_iter is not None:
|
||||
for i in self._fill_iter:
|
||||
set.add(self, i)
|
||||
self._fill_iter = None
|
||||
finally:
|
||||
_fill_lock.release()
|
||||
real = getattr(set, name)
|
||||
setattr(self.__class__, name, real)
|
||||
return real(self, *args, **kw)
|
||||
return _lazy
|
||||
|
||||
for name in _props:
|
||||
setattr(LazySet, name, lazy(name))
|
||||
|
||||
new_set = LazySet()
|
||||
new_set._fill_iter = fill_iter
|
||||
return new_set
|
127
packages/wakatime/wakatime/packages/pytz3/reference.py
Normal file
127
packages/wakatime/wakatime/packages/pytz3/reference.py
Normal file
@ -0,0 +1,127 @@
|
||||
'''
|
||||
Reference tzinfo implementations from the Python docs.
|
||||
Used for testing against as they are only correct for the years
|
||||
1987 to 2006. Do not use these for real code.
|
||||
'''
|
||||
|
||||
from datetime import tzinfo, timedelta, datetime
|
||||
from pytz import utc, UTC, HOUR, ZERO
|
||||
|
||||
# A class building tzinfo objects for fixed-offset time zones.
|
||||
# Note that FixedOffset(0, "UTC") is a different way to build a
|
||||
# UTC tzinfo object.
|
||||
|
||||
class FixedOffset(tzinfo):
|
||||
"""Fixed offset in minutes east from UTC."""
|
||||
|
||||
def __init__(self, offset, name):
|
||||
self.__offset = timedelta(minutes = offset)
|
||||
self.__name = name
|
||||
|
||||
def utcoffset(self, dt):
|
||||
return self.__offset
|
||||
|
||||
def tzname(self, dt):
|
||||
return self.__name
|
||||
|
||||
def dst(self, dt):
|
||||
return ZERO
|
||||
|
||||
# A class capturing the platform's idea of local time.
|
||||
|
||||
import time as _time
|
||||
|
||||
STDOFFSET = timedelta(seconds = -_time.timezone)
|
||||
if _time.daylight:
|
||||
DSTOFFSET = timedelta(seconds = -_time.altzone)
|
||||
else:
|
||||
DSTOFFSET = STDOFFSET
|
||||
|
||||
DSTDIFF = DSTOFFSET - STDOFFSET
|
||||
|
||||
class LocalTimezone(tzinfo):
|
||||
|
||||
def utcoffset(self, dt):
|
||||
if self._isdst(dt):
|
||||
return DSTOFFSET
|
||||
else:
|
||||
return STDOFFSET
|
||||
|
||||
def dst(self, dt):
|
||||
if self._isdst(dt):
|
||||
return DSTDIFF
|
||||
else:
|
||||
return ZERO
|
||||
|
||||
def tzname(self, dt):
|
||||
return _time.tzname[self._isdst(dt)]
|
||||
|
||||
def _isdst(self, dt):
|
||||
tt = (dt.year, dt.month, dt.day,
|
||||
dt.hour, dt.minute, dt.second,
|
||||
dt.weekday(), 0, -1)
|
||||
stamp = _time.mktime(tt)
|
||||
tt = _time.localtime(stamp)
|
||||
return tt.tm_isdst > 0
|
||||
|
||||
Local = LocalTimezone()
|
||||
|
||||
# A complete implementation of current DST rules for major US time zones.
|
||||
|
||||
def first_sunday_on_or_after(dt):
|
||||
days_to_go = 6 - dt.weekday()
|
||||
if days_to_go:
|
||||
dt += timedelta(days_to_go)
|
||||
return dt
|
||||
|
||||
# In the US, DST starts at 2am (standard time) on the first Sunday in April.
|
||||
DSTSTART = datetime(1, 4, 1, 2)
|
||||
# and ends at 2am (DST time; 1am standard time) on the last Sunday of Oct.
|
||||
# which is the first Sunday on or after Oct 25.
|
||||
DSTEND = datetime(1, 10, 25, 1)
|
||||
|
||||
class USTimeZone(tzinfo):
|
||||
|
||||
def __init__(self, hours, reprname, stdname, dstname):
|
||||
self.stdoffset = timedelta(hours=hours)
|
||||
self.reprname = reprname
|
||||
self.stdname = stdname
|
||||
self.dstname = dstname
|
||||
|
||||
def __repr__(self):
|
||||
return self.reprname
|
||||
|
||||
def tzname(self, dt):
|
||||
if self.dst(dt):
|
||||
return self.dstname
|
||||
else:
|
||||
return self.stdname
|
||||
|
||||
def utcoffset(self, dt):
|
||||
return self.stdoffset + self.dst(dt)
|
||||
|
||||
def dst(self, dt):
|
||||
if dt is None or dt.tzinfo is None:
|
||||
# An exception may be sensible here, in one or both cases.
|
||||
# It depends on how you want to treat them. The default
|
||||
# fromutc() implementation (called by the default astimezone()
|
||||
# implementation) passes a datetime with dt.tzinfo is self.
|
||||
return ZERO
|
||||
assert dt.tzinfo is self
|
||||
|
||||
# Find first Sunday in April & the last in October.
|
||||
start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year))
|
||||
end = first_sunday_on_or_after(DSTEND.replace(year=dt.year))
|
||||
|
||||
# Can't compare naive to aware objects, so strip the timezone from
|
||||
# dt first.
|
||||
if start <= dt.replace(tzinfo=None) < end:
|
||||
return HOUR
|
||||
else:
|
||||
return ZERO
|
||||
|
||||
Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
|
||||
Central = USTimeZone(-6, "Central", "CST", "CDT")
|
||||
Mountain = USTimeZone(-7, "Mountain", "MST", "MDT")
|
||||
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")
|
||||
|
36
packages/wakatime/wakatime/packages/pytz3/tests/test_docs.py
Normal file
36
packages/wakatime/wakatime/packages/pytz3/tests/test_docs.py
Normal file
@ -0,0 +1,36 @@
|
||||
# -*- coding: ascii -*-
|
||||
|
||||
from doctest import DocTestSuite
|
||||
import unittest, os, os.path, sys
|
||||
import warnings
|
||||
|
||||
# We test the documentation this way instead of using DocFileSuite so
|
||||
# we can run the tests under Python 2.3
|
||||
def test_README():
|
||||
pass
|
||||
|
||||
this_dir = os.path.dirname(__file__)
|
||||
locs = [
|
||||
os.path.join(this_dir, os.pardir, 'README.txt'),
|
||||
os.path.join(this_dir, os.pardir, os.pardir, 'README.txt'),
|
||||
]
|
||||
for loc in locs:
|
||||
if os.path.exists(loc):
|
||||
test_README.__doc__ = open(loc).read()
|
||||
break
|
||||
if test_README.__doc__ is None:
|
||||
raise RuntimeError('README.txt not found')
|
||||
|
||||
|
||||
def test_suite():
|
||||
"For the Z3 test runner"
|
||||
return DocTestSuite()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(
|
||||
this_dir, os.pardir, os.pardir
|
||||
)))
|
||||
unittest.main(defaultTest='test_suite')
|
||||
|
||||
|
813
packages/wakatime/wakatime/packages/pytz3/tests/test_tzinfo.py
Normal file
813
packages/wakatime/wakatime/packages/pytz3/tests/test_tzinfo.py
Normal file
@ -0,0 +1,813 @@
|
||||
# -*- coding: ascii -*-
|
||||
|
||||
import sys, os, os.path
|
||||
import unittest, doctest
|
||||
try:
|
||||
import pickle as pickle
|
||||
except ImportError:
|
||||
import pickle
|
||||
from datetime import datetime, time, timedelta, tzinfo
|
||||
import warnings
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Only munge path if invoked as a script. Testrunners should have setup
|
||||
# the paths already
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.pardir, os.pardir)))
|
||||
|
||||
import pytz
|
||||
from pytz import reference
|
||||
from pytz.tzfile import _byte_string
|
||||
from pytz.tzinfo import DstTzInfo, StaticTzInfo
|
||||
|
||||
# I test for expected version to ensure the correct version of pytz is
|
||||
# actually being tested.
|
||||
EXPECTED_VERSION='2013d'
|
||||
|
||||
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
|
||||
|
||||
NOTIME = timedelta(0)
|
||||
|
||||
# GMT is a tzinfo.StaticTzInfo--the class we primarily want to test--while
|
||||
# UTC is reference implementation. They both have the same timezone meaning.
|
||||
UTC = pytz.timezone('UTC')
|
||||
GMT = pytz.timezone('GMT')
|
||||
assert isinstance(GMT, StaticTzInfo), 'GMT is no longer a StaticTzInfo'
|
||||
|
||||
def prettydt(dt):
|
||||
"""datetime as a string using a known format.
|
||||
|
||||
We don't use strftime as it doesn't handle years earlier than 1900
|
||||
per http://bugs.python.org/issue1777412
|
||||
"""
|
||||
if dt.utcoffset() >= timedelta(0):
|
||||
offset = '+%s' % (dt.utcoffset(),)
|
||||
else:
|
||||
offset = '-%s' % (-1 * dt.utcoffset(),)
|
||||
return '%04d-%02d-%02d %02d:%02d:%02d %s %s' % (
|
||||
dt.year, dt.month, dt.day,
|
||||
dt.hour, dt.minute, dt.second,
|
||||
dt.tzname(), offset)
|
||||
|
||||
|
||||
try:
|
||||
str
|
||||
except NameError:
|
||||
# Python 3.x doesn't have unicode(), making writing code
|
||||
# for Python 2.3 and Python 3.x a pain.
|
||||
str = str
|
||||
|
||||
|
||||
class BasicTest(unittest.TestCase):
|
||||
|
||||
def testVersion(self):
|
||||
# Ensuring the correct version of pytz has been loaded
|
||||
self.assertEqual(EXPECTED_VERSION, pytz.__version__,
|
||||
'Incorrect pytz version loaded. Import path is stuffed '
|
||||
'or this test needs updating. (Wanted %s, got %s)'
|
||||
% (EXPECTED_VERSION, pytz.__version__)
|
||||
)
|
||||
|
||||
def testGMT(self):
|
||||
now = datetime.now(tz=GMT)
|
||||
self.assertTrue(now.utcoffset() == NOTIME)
|
||||
self.assertTrue(now.dst() == NOTIME)
|
||||
self.assertTrue(now.timetuple() == now.utctimetuple())
|
||||
self.assertTrue(now==now.replace(tzinfo=UTC))
|
||||
|
||||
def testReferenceUTC(self):
|
||||
now = datetime.now(tz=UTC)
|
||||
self.assertTrue(now.utcoffset() == NOTIME)
|
||||
self.assertTrue(now.dst() == NOTIME)
|
||||
self.assertTrue(now.timetuple() == now.utctimetuple())
|
||||
|
||||
def testUnknownOffsets(self):
|
||||
# This tzinfo behavior is required to make
|
||||
# datetime.time.{utcoffset, dst, tzname} work as documented.
|
||||
|
||||
dst_tz = pytz.timezone('US/Eastern')
|
||||
|
||||
# This information is not known when we don't have a date,
|
||||
# so return None per API.
|
||||
self.assertTrue(dst_tz.utcoffset(None) is None)
|
||||
self.assertTrue(dst_tz.dst(None) is None)
|
||||
# We don't know the abbreviation, but this is still a valid
|
||||
# tzname per the Python documentation.
|
||||
self.assertEqual(dst_tz.tzname(None), 'US/Eastern')
|
||||
|
||||
def clearCache(self):
|
||||
pytz._tzinfo_cache.clear()
|
||||
|
||||
def testUnicodeTimezone(self):
|
||||
# We need to ensure that cold lookups work for both Unicode
|
||||
# and traditional strings, and that the desired singleton is
|
||||
# returned.
|
||||
self.clearCache()
|
||||
eastern = pytz.timezone(str('US/Eastern'))
|
||||
self.assertTrue(eastern is pytz.timezone('US/Eastern'))
|
||||
|
||||
self.clearCache()
|
||||
eastern = pytz.timezone('US/Eastern')
|
||||
self.assertTrue(eastern is pytz.timezone(str('US/Eastern')))
|
||||
|
||||
|
||||
class PicklingTest(unittest.TestCase):
|
||||
|
||||
def _roundtrip_tzinfo(self, tz):
|
||||
p = pickle.dumps(tz)
|
||||
unpickled_tz = pickle.loads(p)
|
||||
self.assertTrue(tz is unpickled_tz, '%s did not roundtrip' % tz.zone)
|
||||
|
||||
def _roundtrip_datetime(self, dt):
|
||||
# Ensure that the tzinfo attached to a datetime instance
|
||||
# is identical to the one returned. This is important for
|
||||
# DST timezones, as some state is stored in the tzinfo.
|
||||
tz = dt.tzinfo
|
||||
p = pickle.dumps(dt)
|
||||
unpickled_dt = pickle.loads(p)
|
||||
unpickled_tz = unpickled_dt.tzinfo
|
||||
self.assertTrue(tz is unpickled_tz, '%s did not roundtrip' % tz.zone)
|
||||
|
||||
def testDst(self):
|
||||
tz = pytz.timezone('Europe/Amsterdam')
|
||||
dt = datetime(2004, 2, 1, 0, 0, 0)
|
||||
|
||||
for localized_tz in list(tz._tzinfos.values()):
|
||||
self._roundtrip_tzinfo(localized_tz)
|
||||
self._roundtrip_datetime(dt.replace(tzinfo=localized_tz))
|
||||
|
||||
def testRoundtrip(self):
|
||||
dt = datetime(2004, 2, 1, 0, 0, 0)
|
||||
for zone in pytz.all_timezones:
|
||||
tz = pytz.timezone(zone)
|
||||
self._roundtrip_tzinfo(tz)
|
||||
|
||||
def testDatabaseFixes(self):
|
||||
# Hack the pickle to make it refer to a timezone abbreviation
|
||||
# that does not match anything. The unpickler should be able
|
||||
# to repair this case
|
||||
tz = pytz.timezone('Australia/Melbourne')
|
||||
p = pickle.dumps(tz)
|
||||
tzname = tz._tzname
|
||||
hacked_p = p.replace(_byte_string(tzname), _byte_string('???'))
|
||||
self.assertNotEqual(p, hacked_p)
|
||||
unpickled_tz = pickle.loads(hacked_p)
|
||||
self.assertTrue(tz is unpickled_tz)
|
||||
|
||||
# Simulate a database correction. In this case, the incorrect
|
||||
# data will continue to be used.
|
||||
p = pickle.dumps(tz)
|
||||
new_utcoffset = tz._utcoffset.seconds + 42
|
||||
|
||||
# Python 3 introduced a new pickle protocol where numbers are stored in
|
||||
# hexadecimal representation. Here we extract the pickle
|
||||
# representation of the number for the current Python version.
|
||||
old_pickle_pattern = pickle.dumps(tz._utcoffset.seconds)[3:-1]
|
||||
new_pickle_pattern = pickle.dumps(new_utcoffset)[3:-1]
|
||||
hacked_p = p.replace(old_pickle_pattern, new_pickle_pattern)
|
||||
|
||||
self.assertNotEqual(p, hacked_p)
|
||||
unpickled_tz = pickle.loads(hacked_p)
|
||||
self.assertEqual(unpickled_tz._utcoffset.seconds, new_utcoffset)
|
||||
self.assertTrue(tz is not unpickled_tz)
|
||||
|
||||
def testOldPickles(self):
|
||||
# Ensure that applications serializing pytz instances as pickles
|
||||
# have no troubles upgrading to a new pytz release. These pickles
|
||||
# where created with pytz2006j
|
||||
east1 = pickle.loads(_byte_string(
|
||||
"cpytz\n_p\np1\n(S'US/Eastern'\np2\nI-18000\n"
|
||||
"I0\nS'EST'\np3\ntRp4\n."
|
||||
))
|
||||
east2 = pytz.timezone('US/Eastern')
|
||||
self.assertTrue(east1 is east2)
|
||||
|
||||
# Confirm changes in name munging between 2006j and 2007c cause
|
||||
# no problems.
|
||||
pap1 = pickle.loads(_byte_string(
|
||||
"cpytz\n_p\np1\n(S'America/Port_minus_au_minus_Prince'"
|
||||
"\np2\nI-17340\nI0\nS'PPMT'\np3\ntRp4\n."))
|
||||
pap2 = pytz.timezone('America/Port-au-Prince')
|
||||
self.assertTrue(pap1 is pap2)
|
||||
|
||||
gmt1 = pickle.loads(_byte_string(
|
||||
"cpytz\n_p\np1\n(S'Etc/GMT_plus_10'\np2\ntRp3\n."))
|
||||
gmt2 = pytz.timezone('Etc/GMT+10')
|
||||
self.assertTrue(gmt1 is gmt2)
|
||||
|
||||
|
||||
class USEasternDSTStartTestCase(unittest.TestCase):
|
||||
tzinfo = pytz.timezone('US/Eastern')
|
||||
|
||||
# 24 hours before DST changeover
|
||||
transition_time = datetime(2002, 4, 7, 7, 0, 0, tzinfo=UTC)
|
||||
|
||||
# Increase for 'flexible' DST transitions due to 1 minute granularity
|
||||
# of Python's datetime library
|
||||
instant = timedelta(seconds=1)
|
||||
|
||||
# before transition
|
||||
before = {
|
||||
'tzname': 'EST',
|
||||
'utcoffset': timedelta(hours = -5),
|
||||
'dst': timedelta(hours = 0),
|
||||
}
|
||||
|
||||
# after transition
|
||||
after = {
|
||||
'tzname': 'EDT',
|
||||
'utcoffset': timedelta(hours = -4),
|
||||
'dst': timedelta(hours = 1),
|
||||
}
|
||||
|
||||
def _test_tzname(self, utc_dt, wanted):
|
||||
tzname = wanted['tzname']
|
||||
dt = utc_dt.astimezone(self.tzinfo)
|
||||
self.assertEqual(dt.tzname(), tzname,
|
||||
'Expected %s as tzname for %s. Got %s' % (
|
||||
tzname, str(utc_dt), dt.tzname()
|
||||
)
|
||||
)
|
||||
|
||||
def _test_utcoffset(self, utc_dt, wanted):
|
||||
utcoffset = wanted['utcoffset']
|
||||
dt = utc_dt.astimezone(self.tzinfo)
|
||||
self.assertEqual(
|
||||
dt.utcoffset(), wanted['utcoffset'],
|
||||
'Expected %s as utcoffset for %s. Got %s' % (
|
||||
utcoffset, utc_dt, dt.utcoffset()
|
||||
)
|
||||
)
|
||||
|
||||
def _test_dst(self, utc_dt, wanted):
|
||||
dst = wanted['dst']
|
||||
dt = utc_dt.astimezone(self.tzinfo)
|
||||
self.assertEqual(dt.dst(),dst,
|
||||
'Expected %s as dst for %s. Got %s' % (
|
||||
dst, utc_dt, dt.dst()
|
||||
)
|
||||
)
|
||||
|
||||
def test_arithmetic(self):
|
||||
utc_dt = self.transition_time
|
||||
|
||||
for days in range(-420, 720, 20):
|
||||
delta = timedelta(days=days)
|
||||
|
||||
# Make sure we can get back where we started
|
||||
dt = utc_dt.astimezone(self.tzinfo)
|
||||
dt2 = dt + delta
|
||||
dt2 = dt2 - delta
|
||||
self.assertEqual(dt, dt2)
|
||||
|
||||
# Make sure arithmetic crossing DST boundaries ends
|
||||
# up in the correct timezone after normalization
|
||||
utc_plus_delta = (utc_dt + delta).astimezone(self.tzinfo)
|
||||
local_plus_delta = self.tzinfo.normalize(dt + delta)
|
||||
self.assertEqual(
|
||||
prettydt(utc_plus_delta),
|
||||
prettydt(local_plus_delta),
|
||||
'Incorrect result for delta==%d days. Wanted %r. Got %r'%(
|
||||
days,
|
||||
prettydt(utc_plus_delta),
|
||||
prettydt(local_plus_delta),
|
||||
)
|
||||
)
|
||||
|
||||
def _test_all(self, utc_dt, wanted):
|
||||
self._test_utcoffset(utc_dt, wanted)
|
||||
self._test_tzname(utc_dt, wanted)
|
||||
self._test_dst(utc_dt, wanted)
|
||||
|
||||
def testDayBefore(self):
|
||||
self._test_all(
|
||||
self.transition_time - timedelta(days=1), self.before
|
||||
)
|
||||
|
||||
def testTwoHoursBefore(self):
|
||||
self._test_all(
|
||||
self.transition_time - timedelta(hours=2), self.before
|
||||
)
|
||||
|
||||
def testHourBefore(self):
|
||||
self._test_all(
|
||||
self.transition_time - timedelta(hours=1), self.before
|
||||
)
|
||||
|
||||
def testInstantBefore(self):
|
||||
self._test_all(
|
||||
self.transition_time - self.instant, self.before
|
||||
)
|
||||
|
||||
def testTransition(self):
|
||||
self._test_all(
|
||||
self.transition_time, self.after
|
||||
)
|
||||
|
||||
def testInstantAfter(self):
|
||||
self._test_all(
|
||||
self.transition_time + self.instant, self.after
|
||||
)
|
||||
|
||||
def testHourAfter(self):
|
||||
self._test_all(
|
||||
self.transition_time + timedelta(hours=1), self.after
|
||||
)
|
||||
|
||||
def testTwoHoursAfter(self):
|
||||
self._test_all(
|
||||
self.transition_time + timedelta(hours=1), self.after
|
||||
)
|
||||
|
||||
def testDayAfter(self):
|
||||
self._test_all(
|
||||
self.transition_time + timedelta(days=1), self.after
|
||||
)
|
||||
|
||||
|
||||
class USEasternDSTEndTestCase(USEasternDSTStartTestCase):
|
||||
tzinfo = pytz.timezone('US/Eastern')
|
||||
transition_time = datetime(2002, 10, 27, 6, 0, 0, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'EDT',
|
||||
'utcoffset': timedelta(hours = -4),
|
||||
'dst': timedelta(hours = 1),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'EST',
|
||||
'utcoffset': timedelta(hours = -5),
|
||||
'dst': timedelta(hours = 0),
|
||||
}
|
||||
|
||||
|
||||
class USEasternEPTStartTestCase(USEasternDSTStartTestCase):
|
||||
transition_time = datetime(1945, 8, 14, 23, 0, 0, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'EWT',
|
||||
'utcoffset': timedelta(hours = -4),
|
||||
'dst': timedelta(hours = 1),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'EPT',
|
||||
'utcoffset': timedelta(hours = -4),
|
||||
'dst': timedelta(hours = 1),
|
||||
}
|
||||
|
||||
|
||||
class USEasternEPTEndTestCase(USEasternDSTStartTestCase):
|
||||
transition_time = datetime(1945, 9, 30, 6, 0, 0, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'EPT',
|
||||
'utcoffset': timedelta(hours = -4),
|
||||
'dst': timedelta(hours = 1),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'EST',
|
||||
'utcoffset': timedelta(hours = -5),
|
||||
'dst': timedelta(hours = 0),
|
||||
}
|
||||
|
||||
|
||||
class WarsawWMTEndTestCase(USEasternDSTStartTestCase):
|
||||
# In 1915, Warsaw changed from Warsaw to Central European time.
|
||||
# This involved the clocks being set backwards, causing a end-of-DST
|
||||
# like situation without DST being involved.
|
||||
tzinfo = pytz.timezone('Europe/Warsaw')
|
||||
transition_time = datetime(1915, 8, 4, 22, 36, 0, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'WMT',
|
||||
'utcoffset': timedelta(hours=1, minutes=24),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'CET',
|
||||
'utcoffset': timedelta(hours=1),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
|
||||
|
||||
class VilniusWMTEndTestCase(USEasternDSTStartTestCase):
|
||||
# At the end of 1916, Vilnius changed timezones putting its clock
|
||||
# forward by 11 minutes 35 seconds. Neither timezone was in DST mode.
|
||||
tzinfo = pytz.timezone('Europe/Vilnius')
|
||||
instant = timedelta(seconds=31)
|
||||
transition_time = datetime(1916, 12, 31, 22, 36, 00, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'WMT',
|
||||
'utcoffset': timedelta(hours=1, minutes=24),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'KMT',
|
||||
'utcoffset': timedelta(hours=1, minutes=36), # Really 1:35:36
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
|
||||
|
||||
class VilniusCESTStartTestCase(USEasternDSTStartTestCase):
|
||||
# In 1941, Vilnius changed from MSG to CEST, switching to summer
|
||||
# time while simultaneously reducing its UTC offset by two hours,
|
||||
# causing the clocks to go backwards for this summer time
|
||||
# switchover.
|
||||
tzinfo = pytz.timezone('Europe/Vilnius')
|
||||
transition_time = datetime(1941, 6, 23, 21, 00, 00, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'MSK',
|
||||
'utcoffset': timedelta(hours=3),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'CEST',
|
||||
'utcoffset': timedelta(hours=2),
|
||||
'dst': timedelta(hours=1),
|
||||
}
|
||||
|
||||
|
||||
class LondonHistoryStartTestCase(USEasternDSTStartTestCase):
|
||||
# The first known timezone transition in London was in 1847 when
|
||||
# clocks where synchronized to GMT. However, we currently only
|
||||
# understand v1 format tzfile(5) files which does handle years
|
||||
# this far in the past, so our earliest known transition is in
|
||||
# 1916.
|
||||
tzinfo = pytz.timezone('Europe/London')
|
||||
# transition_time = datetime(1847, 12, 1, 1, 15, 00, tzinfo=UTC)
|
||||
# before = {
|
||||
# 'tzname': 'LMT',
|
||||
# 'utcoffset': timedelta(minutes=-75),
|
||||
# 'dst': timedelta(0),
|
||||
# }
|
||||
# after = {
|
||||
# 'tzname': 'GMT',
|
||||
# 'utcoffset': timedelta(0),
|
||||
# 'dst': timedelta(0),
|
||||
# }
|
||||
transition_time = datetime(1916, 5, 21, 2, 00, 00, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'GMT',
|
||||
'utcoffset': timedelta(0),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'BST',
|
||||
'utcoffset': timedelta(hours=1),
|
||||
'dst': timedelta(hours=1),
|
||||
}
|
||||
|
||||
|
||||
class LondonHistoryEndTestCase(USEasternDSTStartTestCase):
|
||||
# Timezone switchovers are projected into the future, even
|
||||
# though no official statements exist or could be believed even
|
||||
# if they did exist. We currently only check the last known
|
||||
# transition in 2037, as we are still using v1 format tzfile(5)
|
||||
# files.
|
||||
tzinfo = pytz.timezone('Europe/London')
|
||||
# transition_time = datetime(2499, 10, 25, 1, 0, 0, tzinfo=UTC)
|
||||
transition_time = datetime(2037, 10, 25, 1, 0, 0, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'BST',
|
||||
'utcoffset': timedelta(hours=1),
|
||||
'dst': timedelta(hours=1),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'GMT',
|
||||
'utcoffset': timedelta(0),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
|
||||
|
||||
class NoumeaHistoryStartTestCase(USEasternDSTStartTestCase):
|
||||
# Noumea adopted a whole hour offset in 1912. Previously
|
||||
# it was 11 hours, 5 minutes and 48 seconds off UTC. However,
|
||||
# due to limitations of the Python datetime library, we need
|
||||
# to round that to 11 hours 6 minutes.
|
||||
tzinfo = pytz.timezone('Pacific/Noumea')
|
||||
transition_time = datetime(1912, 1, 12, 12, 54, 12, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'LMT',
|
||||
'utcoffset': timedelta(hours=11, minutes=6),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'NCT',
|
||||
'utcoffset': timedelta(hours=11),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
|
||||
|
||||
class NoumeaDSTEndTestCase(USEasternDSTStartTestCase):
|
||||
# Noumea dropped DST in 1997.
|
||||
tzinfo = pytz.timezone('Pacific/Noumea')
|
||||
transition_time = datetime(1997, 3, 1, 15, 00, 00, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'NCST',
|
||||
'utcoffset': timedelta(hours=12),
|
||||
'dst': timedelta(hours=1),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'NCT',
|
||||
'utcoffset': timedelta(hours=11),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
|
||||
|
||||
class NoumeaNoMoreDSTTestCase(NoumeaDSTEndTestCase):
|
||||
# Noumea dropped DST in 1997. Here we test that it stops occuring.
|
||||
transition_time = (
|
||||
NoumeaDSTEndTestCase.transition_time + timedelta(days=365*10))
|
||||
before = NoumeaDSTEndTestCase.after
|
||||
after = NoumeaDSTEndTestCase.after
|
||||
|
||||
|
||||
class TahitiTestCase(USEasternDSTStartTestCase):
|
||||
# Tahiti has had a single transition in its history.
|
||||
tzinfo = pytz.timezone('Pacific/Tahiti')
|
||||
transition_time = datetime(1912, 10, 1, 9, 58, 16, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'LMT',
|
||||
'utcoffset': timedelta(hours=-9, minutes=-58),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'TAHT',
|
||||
'utcoffset': timedelta(hours=-10),
|
||||
'dst': timedelta(0),
|
||||
}
|
||||
|
||||
|
||||
class SamoaInternationalDateLineChange(USEasternDSTStartTestCase):
|
||||
# At the end of 2011, Samoa will switch from being east of the
|
||||
# international dateline to the west. There will be no Dec 30th
|
||||
# 2011 and it will switch from UTC-10 to UTC+14.
|
||||
tzinfo = pytz.timezone('Pacific/Apia')
|
||||
transition_time = datetime(2011, 12, 30, 10, 0, 0, tzinfo=UTC)
|
||||
before = {
|
||||
'tzname': 'WSDT',
|
||||
'utcoffset': timedelta(hours=-10),
|
||||
'dst': timedelta(hours=1),
|
||||
}
|
||||
after = {
|
||||
'tzname': 'WSDT',
|
||||
'utcoffset': timedelta(hours=14),
|
||||
'dst': timedelta(hours=1),
|
||||
}
|
||||
|
||||
|
||||
class ReferenceUSEasternDSTStartTestCase(USEasternDSTStartTestCase):
|
||||
tzinfo = reference.Eastern
|
||||
def test_arithmetic(self):
|
||||
# Reference implementation cannot handle this
|
||||
pass
|
||||
|
||||
|
||||
class ReferenceUSEasternDSTEndTestCase(USEasternDSTEndTestCase):
|
||||
tzinfo = reference.Eastern
|
||||
|
||||
def testHourBefore(self):
|
||||
# Python's datetime library has a bug, where the hour before
|
||||
# a daylight savings transition is one hour out. For example,
|
||||
# at the end of US/Eastern daylight savings time, 01:00 EST
|
||||
# occurs twice (once at 05:00 UTC and once at 06:00 UTC),
|
||||
# whereas the first should actually be 01:00 EDT.
|
||||
# Note that this bug is by design - by accepting this ambiguity
|
||||
# for one hour one hour per year, an is_dst flag on datetime.time
|
||||
# became unnecessary.
|
||||
self._test_all(
|
||||
self.transition_time - timedelta(hours=1), self.after
|
||||
)
|
||||
|
||||
def testInstantBefore(self):
|
||||
self._test_all(
|
||||
self.transition_time - timedelta(seconds=1), self.after
|
||||
)
|
||||
|
||||
def test_arithmetic(self):
|
||||
# Reference implementation cannot handle this
|
||||
pass
|
||||
|
||||
|
||||
class LocalTestCase(unittest.TestCase):
|
||||
def testLocalize(self):
|
||||
loc_tz = pytz.timezone('Europe/Amsterdam')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1930, 5, 10, 0, 0, 0))
|
||||
# Actually +00:19:32, but Python datetime rounds this
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'AMT+0020')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1930, 5, 20, 0, 0, 0))
|
||||
# Actually +00:19:32, but Python datetime rounds this
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'NST+0120')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1940, 5, 10, 0, 0, 0))
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'NET+0020')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1940, 5, 20, 0, 0, 0))
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'CEST+0200')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(2004, 2, 1, 0, 0, 0))
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'CET+0100')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(2004, 4, 1, 0, 0, 0))
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'CEST+0200')
|
||||
|
||||
tz = pytz.timezone('Europe/Amsterdam')
|
||||
loc_time = loc_tz.localize(datetime(1943, 3, 29, 1, 59, 59))
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'CET+0100')
|
||||
|
||||
|
||||
# Switch to US
|
||||
loc_tz = pytz.timezone('US/Eastern')
|
||||
|
||||
# End of DST ambiguity check
|
||||
loc_time = loc_tz.localize(datetime(1918, 10, 27, 1, 59, 59), is_dst=1)
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EDT-0400')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1918, 10, 27, 1, 59, 59), is_dst=0)
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EST-0500')
|
||||
|
||||
self.assertRaises(pytz.AmbiguousTimeError,
|
||||
loc_tz.localize, datetime(1918, 10, 27, 1, 59, 59), is_dst=None
|
||||
)
|
||||
|
||||
# Start of DST non-existent times
|
||||
loc_time = loc_tz.localize(datetime(1918, 3, 31, 2, 0, 0), is_dst=0)
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EST-0500')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1918, 3, 31, 2, 0, 0), is_dst=1)
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EDT-0400')
|
||||
|
||||
self.assertRaises(pytz.NonExistentTimeError,
|
||||
loc_tz.localize, datetime(1918, 3, 31, 2, 0, 0), is_dst=None
|
||||
)
|
||||
|
||||
# Weird changes - war time and peace time both is_dst==True
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1942, 2, 9, 3, 0, 0))
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EWT-0400')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1945, 8, 14, 19, 0, 0))
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EPT-0400')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1945, 9, 30, 1, 0, 0), is_dst=1)
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EPT-0400')
|
||||
|
||||
loc_time = loc_tz.localize(datetime(1945, 9, 30, 1, 0, 0), is_dst=0)
|
||||
self.assertEqual(loc_time.strftime('%Z%z'), 'EST-0500')
|
||||
|
||||
def testNormalize(self):
|
||||
tz = pytz.timezone('US/Eastern')
|
||||
dt = datetime(2004, 4, 4, 7, 0, 0, tzinfo=UTC).astimezone(tz)
|
||||
dt2 = dt - timedelta(minutes=10)
|
||||
self.assertEqual(
|
||||
dt2.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
|
||||
'2004-04-04 02:50:00 EDT-0400'
|
||||
)
|
||||
|
||||
dt2 = tz.normalize(dt2)
|
||||
self.assertEqual(
|
||||
dt2.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
|
||||
'2004-04-04 01:50:00 EST-0500'
|
||||
)
|
||||
|
||||
def testPartialMinuteOffsets(self):
|
||||
# utcoffset in Amsterdam was not a whole minute until 1937
|
||||
# However, we fudge this by rounding them, as the Python
|
||||
# datetime library
|
||||
tz = pytz.timezone('Europe/Amsterdam')
|
||||
utc_dt = datetime(1914, 1, 1, 13, 40, 28, tzinfo=UTC) # correct
|
||||
utc_dt = utc_dt.replace(second=0) # But we need to fudge it
|
||||
loc_dt = utc_dt.astimezone(tz)
|
||||
self.assertEqual(
|
||||
loc_dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
|
||||
'1914-01-01 14:00:00 AMT+0020'
|
||||
)
|
||||
|
||||
# And get back...
|
||||
utc_dt = loc_dt.astimezone(UTC)
|
||||
self.assertEqual(
|
||||
utc_dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
|
||||
'1914-01-01 13:40:00 UTC+0000'
|
||||
)
|
||||
|
||||
def no_testCreateLocaltime(self):
|
||||
# It would be nice if this worked, but it doesn't.
|
||||
tz = pytz.timezone('Europe/Amsterdam')
|
||||
dt = datetime(2004, 10, 31, 2, 0, 0, tzinfo=tz)
|
||||
self.assertEqual(
|
||||
dt.strftime(fmt),
|
||||
'2004-10-31 02:00:00 CET+0100'
|
||||
)
|
||||
|
||||
|
||||
class CommonTimezonesTestCase(unittest.TestCase):
|
||||
def test_bratislava(self):
|
||||
# Bratislava is the default timezone for Slovakia, but our
|
||||
# heuristics where not adding it to common_timezones. Ideally,
|
||||
# common_timezones should be populated from zone.tab at runtime,
|
||||
# but I'm hesitant to pay the startup cost as loading the list
|
||||
# on demand whilst remaining backwards compatible seems
|
||||
# difficult.
|
||||
self.assertTrue('Europe/Bratislava' in pytz.common_timezones)
|
||||
self.assertTrue('Europe/Bratislava' in pytz.common_timezones_set)
|
||||
|
||||
def test_us_eastern(self):
|
||||
self.assertTrue('US/Eastern' in pytz.common_timezones)
|
||||
self.assertTrue('US/Eastern' in pytz.common_timezones_set)
|
||||
|
||||
def test_belfast(self):
|
||||
# Belfast uses London time.
|
||||
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
|
||||
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
|
||||
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
|
||||
|
||||
|
||||
class BaseTzInfoTestCase:
|
||||
'''Ensure UTC, StaticTzInfo and DstTzInfo work consistently.
|
||||
|
||||
These tests are run for each type of tzinfo.
|
||||
'''
|
||||
tz = None # override
|
||||
tz_class = None # override
|
||||
|
||||
def test_expectedclass(self):
|
||||
self.assertTrue(isinstance(self.tz, self.tz_class))
|
||||
|
||||
def test_fromutc(self):
|
||||
# naive datetime.
|
||||
dt1 = datetime(2011, 10, 31)
|
||||
|
||||
# localized datetime, same timezone.
|
||||
dt2 = self.tz.localize(dt1)
|
||||
|
||||
# Both should give the same results. Note that the standard
|
||||
# Python tzinfo.fromutc() only supports the second.
|
||||
for dt in [dt1, dt2]:
|
||||
loc_dt = self.tz.fromutc(dt)
|
||||
loc_dt2 = pytz.utc.localize(dt1).astimezone(self.tz)
|
||||
self.assertEqual(loc_dt, loc_dt2)
|
||||
|
||||
# localized datetime, different timezone.
|
||||
new_tz = pytz.timezone('Europe/Paris')
|
||||
self.assertTrue(self.tz is not new_tz)
|
||||
dt3 = new_tz.localize(dt1)
|
||||
self.assertRaises(ValueError, self.tz.fromutc, dt3)
|
||||
|
||||
def test_normalize(self):
|
||||
other_tz = pytz.timezone('Europe/Paris')
|
||||
self.assertTrue(self.tz is not other_tz)
|
||||
|
||||
dt = datetime(2012, 3, 26, 12, 0)
|
||||
other_dt = other_tz.localize(dt)
|
||||
|
||||
local_dt = self.tz.normalize(other_dt)
|
||||
|
||||
self.assertTrue(local_dt.tzinfo is not other_dt.tzinfo)
|
||||
self.assertNotEqual(
|
||||
local_dt.replace(tzinfo=None), other_dt.replace(tzinfo=None))
|
||||
|
||||
def test_astimezone(self):
|
||||
other_tz = pytz.timezone('Europe/Paris')
|
||||
self.assertTrue(self.tz is not other_tz)
|
||||
|
||||
dt = datetime(2012, 3, 26, 12, 0)
|
||||
other_dt = other_tz.localize(dt)
|
||||
|
||||
local_dt = other_dt.astimezone(self.tz)
|
||||
|
||||
self.assertTrue(local_dt.tzinfo is not other_dt.tzinfo)
|
||||
self.assertNotEqual(
|
||||
local_dt.replace(tzinfo=None), other_dt.replace(tzinfo=None))
|
||||
|
||||
|
||||
class OptimizedUTCTestCase(unittest.TestCase, BaseTzInfoTestCase):
|
||||
tz = pytz.utc
|
||||
tz_class = tz.__class__
|
||||
|
||||
|
||||
class LegacyUTCTestCase(unittest.TestCase, BaseTzInfoTestCase):
|
||||
# Deprecated timezone, but useful for comparison tests.
|
||||
tz = pytz.timezone('Etc/UTC')
|
||||
tz_class = StaticTzInfo
|
||||
|
||||
|
||||
class StaticTzInfoTestCase(unittest.TestCase, BaseTzInfoTestCase):
|
||||
tz = pytz.timezone('GMT')
|
||||
tz_class = StaticTzInfo
|
||||
|
||||
|
||||
class DstTzInfoTestCase(unittest.TestCase, BaseTzInfoTestCase):
|
||||
tz = pytz.timezone('Australia/Melbourne')
|
||||
tz_class = DstTzInfo
|
||||
|
||||
|
||||
def test_suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(doctest.DocTestSuite('pytz'))
|
||||
suite.addTest(doctest.DocTestSuite('pytz.tzinfo'))
|
||||
import test_tzinfo
|
||||
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_tzinfo))
|
||||
return suite
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
warnings.simplefilter("error") # Warnings should be fatal in tests.
|
||||
unittest.main(defaultTest='test_suite')
|
||||
|
137
packages/wakatime/wakatime/packages/pytz3/tzfile.py
Normal file
137
packages/wakatime/wakatime/packages/pytz3/tzfile.py
Normal file
@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
$Id: tzfile.py,v 1.8 2004/06/03 00:15:24 zenzen Exp $
|
||||
'''
|
||||
|
||||
try:
|
||||
from io import StringIO
|
||||
except ImportError:
|
||||
from io import StringIO
|
||||
from datetime import datetime, timedelta
|
||||
from struct import unpack, calcsize
|
||||
|
||||
from pytz.tzinfo import StaticTzInfo, DstTzInfo, memorized_ttinfo
|
||||
from pytz.tzinfo import memorized_datetime, memorized_timedelta
|
||||
|
||||
def _byte_string(s):
|
||||
"""Cast a string or byte string to an ASCII byte string."""
|
||||
return s.encode('US-ASCII')
|
||||
|
||||
_NULL = _byte_string('\0')
|
||||
|
||||
def _std_string(s):
|
||||
"""Cast a string or byte string to an ASCII string."""
|
||||
return str(s.decode('US-ASCII'))
|
||||
|
||||
def build_tzinfo(zone, fp):
|
||||
head_fmt = '>4s c 15x 6l'
|
||||
head_size = calcsize(head_fmt)
|
||||
(magic, format, ttisgmtcnt, ttisstdcnt,leapcnt, timecnt,
|
||||
typecnt, charcnt) = unpack(head_fmt, fp.read(head_size))
|
||||
|
||||
# Make sure it is a tzfile(5) file
|
||||
assert magic == _byte_string('TZif'), 'Got magic %s' % repr(magic)
|
||||
|
||||
# Read out the transition times, localtime indices and ttinfo structures.
|
||||
data_fmt = '>%(timecnt)dl %(timecnt)dB %(ttinfo)s %(charcnt)ds' % dict(
|
||||
timecnt=timecnt, ttinfo='lBB'*typecnt, charcnt=charcnt)
|
||||
data_size = calcsize(data_fmt)
|
||||
data = unpack(data_fmt, fp.read(data_size))
|
||||
|
||||
# make sure we unpacked the right number of values
|
||||
assert len(data) == 2 * timecnt + 3 * typecnt + 1
|
||||
transitions = [memorized_datetime(trans)
|
||||
for trans in data[:timecnt]]
|
||||
lindexes = list(data[timecnt:2 * timecnt])
|
||||
ttinfo_raw = data[2 * timecnt:-1]
|
||||
tznames_raw = data[-1]
|
||||
del data
|
||||
|
||||
# Process ttinfo into separate structs
|
||||
ttinfo = []
|
||||
tznames = {}
|
||||
i = 0
|
||||
while i < len(ttinfo_raw):
|
||||
# have we looked up this timezone name yet?
|
||||
tzname_offset = ttinfo_raw[i+2]
|
||||
if tzname_offset not in tznames:
|
||||
nul = tznames_raw.find(_NULL, tzname_offset)
|
||||
if nul < 0:
|
||||
nul = len(tznames_raw)
|
||||
tznames[tzname_offset] = _std_string(
|
||||
tznames_raw[tzname_offset:nul])
|
||||
ttinfo.append((ttinfo_raw[i],
|
||||
bool(ttinfo_raw[i+1]),
|
||||
tznames[tzname_offset]))
|
||||
i += 3
|
||||
|
||||
# Now build the timezone object
|
||||
if len(transitions) == 0:
|
||||
ttinfo[0][0], ttinfo[0][2]
|
||||
cls = type(zone, (StaticTzInfo,), dict(
|
||||
zone=zone,
|
||||
_utcoffset=memorized_timedelta(ttinfo[0][0]),
|
||||
_tzname=ttinfo[0][2]))
|
||||
else:
|
||||
# Early dates use the first standard time ttinfo
|
||||
i = 0
|
||||
while ttinfo[i][1]:
|
||||
i += 1
|
||||
if ttinfo[i] == ttinfo[lindexes[0]]:
|
||||
transitions[0] = datetime.min
|
||||
else:
|
||||
transitions.insert(0, datetime.min)
|
||||
lindexes.insert(0, i)
|
||||
|
||||
# calculate transition info
|
||||
transition_info = []
|
||||
for i in range(len(transitions)):
|
||||
inf = ttinfo[lindexes[i]]
|
||||
utcoffset = inf[0]
|
||||
if not inf[1]:
|
||||
dst = 0
|
||||
else:
|
||||
for j in range(i-1, -1, -1):
|
||||
prev_inf = ttinfo[lindexes[j]]
|
||||
if not prev_inf[1]:
|
||||
break
|
||||
dst = inf[0] - prev_inf[0] # dst offset
|
||||
|
||||
# Bad dst? Look further. DST > 24 hours happens when
|
||||
# a timzone has moved across the international dateline.
|
||||
if dst <= 0 or dst > 3600*3:
|
||||
for j in range(i+1, len(transitions)):
|
||||
stdinf = ttinfo[lindexes[j]]
|
||||
if not stdinf[1]:
|
||||
dst = inf[0] - stdinf[0]
|
||||
if dst > 0:
|
||||
break # Found a useful std time.
|
||||
|
||||
tzname = inf[2]
|
||||
|
||||
# Round utcoffset and dst to the nearest minute or the
|
||||
# datetime library will complain. Conversions to these timezones
|
||||
# might be up to plus or minus 30 seconds out, but it is
|
||||
# the best we can do.
|
||||
utcoffset = int((utcoffset + 30) // 60) * 60
|
||||
dst = int((dst + 30) // 60) * 60
|
||||
transition_info.append(memorized_ttinfo(utcoffset, dst, tzname))
|
||||
|
||||
cls = type(zone, (DstTzInfo,), dict(
|
||||
zone=zone,
|
||||
_utc_transition_times=transitions,
|
||||
_transition_info=transition_info))
|
||||
|
||||
return cls()
|
||||
|
||||
if __name__ == '__main__':
|
||||
import os.path
|
||||
from pprint import pprint
|
||||
base = os.path.join(os.path.dirname(__file__), 'zoneinfo')
|
||||
tz = build_tzinfo('Australia/Melbourne',
|
||||
open(os.path.join(base,'Australia','Melbourne'), 'rb'))
|
||||
tz = build_tzinfo('US/Eastern',
|
||||
open(os.path.join(base,'US','Eastern'), 'rb'))
|
||||
pprint(tz._utc_transition_times)
|
||||
#print tz.asPython(4)
|
||||
#print tz.transitions_mapping
|
563
packages/wakatime/wakatime/packages/pytz3/tzinfo.py
Normal file
563
packages/wakatime/wakatime/packages/pytz3/tzinfo.py
Normal file
@ -0,0 +1,563 @@
|
||||
'''Base classes and helpers for building zone specific tzinfo classes'''
|
||||
|
||||
from datetime import datetime, timedelta, tzinfo
|
||||
from bisect import bisect_right
|
||||
try:
|
||||
set
|
||||
except NameError:
|
||||
from sets import Set as set
|
||||
|
||||
import pytz
|
||||
from pytz.exceptions import AmbiguousTimeError, NonExistentTimeError
|
||||
|
||||
__all__ = []
|
||||
|
||||
_timedelta_cache = {}
|
||||
def memorized_timedelta(seconds):
|
||||
'''Create only one instance of each distinct timedelta'''
|
||||
try:
|
||||
return _timedelta_cache[seconds]
|
||||
except KeyError:
|
||||
delta = timedelta(seconds=seconds)
|
||||
_timedelta_cache[seconds] = delta
|
||||
return delta
|
||||
|
||||
_epoch = datetime.utcfromtimestamp(0)
|
||||
_datetime_cache = {0: _epoch}
|
||||
def memorized_datetime(seconds):
|
||||
'''Create only one instance of each distinct datetime'''
|
||||
try:
|
||||
return _datetime_cache[seconds]
|
||||
except KeyError:
|
||||
# NB. We can't just do datetime.utcfromtimestamp(seconds) as this
|
||||
# fails with negative values under Windows (Bug #90096)
|
||||
dt = _epoch + timedelta(seconds=seconds)
|
||||
_datetime_cache[seconds] = dt
|
||||
return dt
|
||||
|
||||
_ttinfo_cache = {}
|
||||
def memorized_ttinfo(*args):
|
||||
'''Create only one instance of each distinct tuple'''
|
||||
try:
|
||||
return _ttinfo_cache[args]
|
||||
except KeyError:
|
||||
ttinfo = (
|
||||
memorized_timedelta(args[0]),
|
||||
memorized_timedelta(args[1]),
|
||||
args[2]
|
||||
)
|
||||
_ttinfo_cache[args] = ttinfo
|
||||
return ttinfo
|
||||
|
||||
_notime = memorized_timedelta(0)
|
||||
|
||||
def _to_seconds(td):
|
||||
'''Convert a timedelta to seconds'''
|
||||
return td.seconds + td.days * 24 * 60 * 60
|
||||
|
||||
|
||||
class BaseTzInfo(tzinfo):
|
||||
# Overridden in subclass
|
||||
_utcoffset = None
|
||||
_tzname = None
|
||||
zone = None
|
||||
|
||||
def __str__(self):
|
||||
return self.zone
|
||||
|
||||
|
||||
class StaticTzInfo(BaseTzInfo):
|
||||
'''A timezone that has a constant offset from UTC
|
||||
|
||||
These timezones are rare, as most locations have changed their
|
||||
offset at some point in their history
|
||||
'''
|
||||
def fromutc(self, dt):
|
||||
'''See datetime.tzinfo.fromutc'''
|
||||
if dt.tzinfo is not None and dt.tzinfo is not self:
|
||||
raise ValueError('fromutc: dt.tzinfo is not self')
|
||||
return (dt + self._utcoffset).replace(tzinfo=self)
|
||||
|
||||
def utcoffset(self, dt, is_dst=None):
|
||||
'''See datetime.tzinfo.utcoffset
|
||||
|
||||
is_dst is ignored for StaticTzInfo, and exists only to
|
||||
retain compatibility with DstTzInfo.
|
||||
'''
|
||||
return self._utcoffset
|
||||
|
||||
def dst(self, dt, is_dst=None):
|
||||
'''See datetime.tzinfo.dst
|
||||
|
||||
is_dst is ignored for StaticTzInfo, and exists only to
|
||||
retain compatibility with DstTzInfo.
|
||||
'''
|
||||
return _notime
|
||||
|
||||
def tzname(self, dt, is_dst=None):
|
||||
'''See datetime.tzinfo.tzname
|
||||
|
||||
is_dst is ignored for StaticTzInfo, and exists only to
|
||||
retain compatibility with DstTzInfo.
|
||||
'''
|
||||
return self._tzname
|
||||
|
||||
def localize(self, dt, is_dst=False):
|
||||
'''Convert naive time to local time'''
|
||||
if dt.tzinfo is not None:
|
||||
raise ValueError('Not naive datetime (tzinfo is already set)')
|
||||
return dt.replace(tzinfo=self)
|
||||
|
||||
def normalize(self, dt, is_dst=False):
|
||||
'''Correct the timezone information on the given datetime.
|
||||
|
||||
This is normally a no-op, as StaticTzInfo timezones never have
|
||||
ambiguous cases to correct:
|
||||
|
||||
>>> from pytz import timezone
|
||||
>>> gmt = timezone('GMT')
|
||||
>>> isinstance(gmt, StaticTzInfo)
|
||||
True
|
||||
>>> dt = datetime(2011, 5, 8, 1, 2, 3, tzinfo=gmt)
|
||||
>>> gmt.normalize(dt) is dt
|
||||
True
|
||||
|
||||
The supported method of converting between timezones is to use
|
||||
datetime.astimezone(). Currently normalize() also works:
|
||||
|
||||
>>> la = timezone('America/Los_Angeles')
|
||||
>>> dt = la.localize(datetime(2011, 5, 7, 1, 2, 3))
|
||||
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
|
||||
>>> gmt.normalize(dt).strftime(fmt)
|
||||
'2011-05-07 08:02:03 GMT (+0000)'
|
||||
'''
|
||||
if dt.tzinfo is self:
|
||||
return dt
|
||||
if dt.tzinfo is None:
|
||||
raise ValueError('Naive time - no tzinfo set')
|
||||
return dt.astimezone(self)
|
||||
|
||||
def __repr__(self):
|
||||
return '<StaticTzInfo %r>' % (self.zone,)
|
||||
|
||||
def __reduce__(self):
|
||||
# Special pickle to zone remains a singleton and to cope with
|
||||
# database changes.
|
||||
return pytz._p, (self.zone,)
|
||||
|
||||
|
||||
class DstTzInfo(BaseTzInfo):
|
||||
'''A timezone that has a variable offset from UTC
|
||||
|
||||
The offset might change if daylight savings time comes into effect,
|
||||
or at a point in history when the region decides to change their
|
||||
timezone definition.
|
||||
'''
|
||||
# Overridden in subclass
|
||||
_utc_transition_times = None # Sorted list of DST transition times in UTC
|
||||
_transition_info = None # [(utcoffset, dstoffset, tzname)] corresponding
|
||||
# to _utc_transition_times entries
|
||||
zone = None
|
||||
|
||||
# Set in __init__
|
||||
_tzinfos = None
|
||||
_dst = None # DST offset
|
||||
|
||||
def __init__(self, _inf=None, _tzinfos=None):
|
||||
if _inf:
|
||||
self._tzinfos = _tzinfos
|
||||
self._utcoffset, self._dst, self._tzname = _inf
|
||||
else:
|
||||
_tzinfos = {}
|
||||
self._tzinfos = _tzinfos
|
||||
self._utcoffset, self._dst, self._tzname = self._transition_info[0]
|
||||
_tzinfos[self._transition_info[0]] = self
|
||||
for inf in self._transition_info[1:]:
|
||||
if inf not in _tzinfos:
|
||||
_tzinfos[inf] = self.__class__(inf, _tzinfos)
|
||||
|
||||
def fromutc(self, dt):
|
||||
'''See datetime.tzinfo.fromutc'''
|
||||
if (dt.tzinfo is not None
|
||||
and getattr(dt.tzinfo, '_tzinfos', None) is not self._tzinfos):
|
||||
raise ValueError('fromutc: dt.tzinfo is not self')
|
||||
dt = dt.replace(tzinfo=None)
|
||||
idx = max(0, bisect_right(self._utc_transition_times, dt) - 1)
|
||||
inf = self._transition_info[idx]
|
||||
return (dt + inf[0]).replace(tzinfo=self._tzinfos[inf])
|
||||
|
||||
def normalize(self, dt):
|
||||
'''Correct the timezone information on the given datetime
|
||||
|
||||
If date arithmetic crosses DST boundaries, the tzinfo
|
||||
is not magically adjusted. This method normalizes the
|
||||
tzinfo to the correct one.
|
||||
|
||||
To test, first we need to do some setup
|
||||
|
||||
>>> from pytz import timezone
|
||||
>>> utc = timezone('UTC')
|
||||
>>> eastern = timezone('US/Eastern')
|
||||
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
|
||||
|
||||
We next create a datetime right on an end-of-DST transition point,
|
||||
the instant when the wallclocks are wound back one hour.
|
||||
|
||||
>>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
|
||||
>>> loc_dt = utc_dt.astimezone(eastern)
|
||||
>>> loc_dt.strftime(fmt)
|
||||
'2002-10-27 01:00:00 EST (-0500)'
|
||||
|
||||
Now, if we subtract a few minutes from it, note that the timezone
|
||||
information has not changed.
|
||||
|
||||
>>> before = loc_dt - timedelta(minutes=10)
|
||||
>>> before.strftime(fmt)
|
||||
'2002-10-27 00:50:00 EST (-0500)'
|
||||
|
||||
But we can fix that by calling the normalize method
|
||||
|
||||
>>> before = eastern.normalize(before)
|
||||
>>> before.strftime(fmt)
|
||||
'2002-10-27 01:50:00 EDT (-0400)'
|
||||
|
||||
The supported method of converting between timezones is to use
|
||||
datetime.astimezone(). Currently, normalize() also works:
|
||||
|
||||
>>> th = timezone('Asia/Bangkok')
|
||||
>>> am = timezone('Europe/Amsterdam')
|
||||
>>> dt = th.localize(datetime(2011, 5, 7, 1, 2, 3))
|
||||
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
|
||||
>>> am.normalize(dt).strftime(fmt)
|
||||
'2011-05-06 20:02:03 CEST (+0200)'
|
||||
'''
|
||||
if dt.tzinfo is None:
|
||||
raise ValueError('Naive time - no tzinfo set')
|
||||
|
||||
# Convert dt in localtime to UTC
|
||||
offset = dt.tzinfo._utcoffset
|
||||
dt = dt.replace(tzinfo=None)
|
||||
dt = dt - offset
|
||||
# convert it back, and return it
|
||||
return self.fromutc(dt)
|
||||
|
||||
def localize(self, dt, is_dst=False):
|
||||
'''Convert naive time to local time.
|
||||
|
||||
This method should be used to construct localtimes, rather
|
||||
than passing a tzinfo argument to a datetime constructor.
|
||||
|
||||
is_dst is used to determine the correct timezone in the ambigous
|
||||
period at the end of daylight savings time.
|
||||
|
||||
>>> from pytz import timezone
|
||||
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
|
||||
>>> amdam = timezone('Europe/Amsterdam')
|
||||
>>> dt = datetime(2004, 10, 31, 2, 0, 0)
|
||||
>>> loc_dt1 = amdam.localize(dt, is_dst=True)
|
||||
>>> loc_dt2 = amdam.localize(dt, is_dst=False)
|
||||
>>> loc_dt1.strftime(fmt)
|
||||
'2004-10-31 02:00:00 CEST (+0200)'
|
||||
>>> loc_dt2.strftime(fmt)
|
||||
'2004-10-31 02:00:00 CET (+0100)'
|
||||
>>> str(loc_dt2 - loc_dt1)
|
||||
'1:00:00'
|
||||
|
||||
Use is_dst=None to raise an AmbiguousTimeError for ambiguous
|
||||
times at the end of daylight savings
|
||||
|
||||
>>> try:
|
||||
... loc_dt1 = amdam.localize(dt, is_dst=None)
|
||||
... except AmbiguousTimeError:
|
||||
... print('Ambiguous')
|
||||
Ambiguous
|
||||
|
||||
is_dst defaults to False
|
||||
|
||||
>>> amdam.localize(dt) == amdam.localize(dt, False)
|
||||
True
|
||||
|
||||
is_dst is also used to determine the correct timezone in the
|
||||
wallclock times jumped over at the start of daylight savings time.
|
||||
|
||||
>>> pacific = timezone('US/Pacific')
|
||||
>>> dt = datetime(2008, 3, 9, 2, 0, 0)
|
||||
>>> ploc_dt1 = pacific.localize(dt, is_dst=True)
|
||||
>>> ploc_dt2 = pacific.localize(dt, is_dst=False)
|
||||
>>> ploc_dt1.strftime(fmt)
|
||||
'2008-03-09 02:00:00 PDT (-0700)'
|
||||
>>> ploc_dt2.strftime(fmt)
|
||||
'2008-03-09 02:00:00 PST (-0800)'
|
||||
>>> str(ploc_dt2 - ploc_dt1)
|
||||
'1:00:00'
|
||||
|
||||
Use is_dst=None to raise a NonExistentTimeError for these skipped
|
||||
times.
|
||||
|
||||
>>> try:
|
||||
... loc_dt1 = pacific.localize(dt, is_dst=None)
|
||||
... except NonExistentTimeError:
|
||||
... print('Non-existent')
|
||||
Non-existent
|
||||
'''
|
||||
if dt.tzinfo is not None:
|
||||
raise ValueError('Not naive datetime (tzinfo is already set)')
|
||||
|
||||
# Find the two best possibilities.
|
||||
possible_loc_dt = set()
|
||||
for delta in [timedelta(days=-1), timedelta(days=1)]:
|
||||
loc_dt = dt + delta
|
||||
idx = max(0, bisect_right(
|
||||
self._utc_transition_times, loc_dt) - 1)
|
||||
inf = self._transition_info[idx]
|
||||
tzinfo = self._tzinfos[inf]
|
||||
loc_dt = tzinfo.normalize(dt.replace(tzinfo=tzinfo))
|
||||
if loc_dt.replace(tzinfo=None) == dt:
|
||||
possible_loc_dt.add(loc_dt)
|
||||
|
||||
if len(possible_loc_dt) == 1:
|
||||
return possible_loc_dt.pop()
|
||||
|
||||
# If there are no possibly correct timezones, we are attempting
|
||||
# to convert a time that never happened - the time period jumped
|
||||
# during the start-of-DST transition period.
|
||||
if len(possible_loc_dt) == 0:
|
||||
# If we refuse to guess, raise an exception.
|
||||
if is_dst is None:
|
||||
raise NonExistentTimeError(dt)
|
||||
|
||||
# If we are forcing the pre-DST side of the DST transition, we
|
||||
# obtain the correct timezone by winding the clock forward a few
|
||||
# hours.
|
||||
elif is_dst:
|
||||
return self.localize(
|
||||
dt + timedelta(hours=6), is_dst=True) - timedelta(hours=6)
|
||||
|
||||
# If we are forcing the post-DST side of the DST transition, we
|
||||
# obtain the correct timezone by winding the clock back.
|
||||
else:
|
||||
return self.localize(
|
||||
dt - timedelta(hours=6), is_dst=False) + timedelta(hours=6)
|
||||
|
||||
|
||||
# If we get this far, we have multiple possible timezones - this
|
||||
# is an ambiguous case occuring during the end-of-DST transition.
|
||||
|
||||
# If told to be strict, raise an exception since we have an
|
||||
# ambiguous case
|
||||
if is_dst is None:
|
||||
raise AmbiguousTimeError(dt)
|
||||
|
||||
# Filter out the possiblilities that don't match the requested
|
||||
# is_dst
|
||||
filtered_possible_loc_dt = [
|
||||
p for p in possible_loc_dt
|
||||
if bool(p.tzinfo._dst) == is_dst
|
||||
]
|
||||
|
||||
# Hopefully we only have one possibility left. Return it.
|
||||
if len(filtered_possible_loc_dt) == 1:
|
||||
return filtered_possible_loc_dt[0]
|
||||
|
||||
if len(filtered_possible_loc_dt) == 0:
|
||||
filtered_possible_loc_dt = list(possible_loc_dt)
|
||||
|
||||
# If we get this far, we have in a wierd timezone transition
|
||||
# where the clocks have been wound back but is_dst is the same
|
||||
# in both (eg. Europe/Warsaw 1915 when they switched to CET).
|
||||
# At this point, we just have to guess unless we allow more
|
||||
# hints to be passed in (such as the UTC offset or abbreviation),
|
||||
# but that is just getting silly.
|
||||
#
|
||||
# Choose the earliest (by UTC) applicable timezone.
|
||||
sorting_keys = {}
|
||||
for local_dt in filtered_possible_loc_dt:
|
||||
key = local_dt.replace(tzinfo=None) - local_dt.tzinfo._utcoffset
|
||||
sorting_keys[key] = local_dt
|
||||
first_key = sorted(sorting_keys)[0]
|
||||
return sorting_keys[first_key]
|
||||
|
||||
def utcoffset(self, dt, is_dst=None):
|
||||
'''See datetime.tzinfo.utcoffset
|
||||
|
||||
The is_dst parameter may be used to remove ambiguity during DST
|
||||
transitions.
|
||||
|
||||
>>> from pytz import timezone
|
||||
>>> tz = timezone('America/St_Johns')
|
||||
>>> ambiguous = datetime(2009, 10, 31, 23, 30)
|
||||
|
||||
>>> tz.utcoffset(ambiguous, is_dst=False)
|
||||
datetime.timedelta(-1, 73800)
|
||||
|
||||
>>> tz.utcoffset(ambiguous, is_dst=True)
|
||||
datetime.timedelta(-1, 77400)
|
||||
|
||||
>>> try:
|
||||
... tz.utcoffset(ambiguous)
|
||||
... except AmbiguousTimeError:
|
||||
... print('Ambiguous')
|
||||
Ambiguous
|
||||
|
||||
'''
|
||||
if dt is None:
|
||||
return None
|
||||
elif dt.tzinfo is not self:
|
||||
dt = self.localize(dt, is_dst)
|
||||
return dt.tzinfo._utcoffset
|
||||
else:
|
||||
return self._utcoffset
|
||||
|
||||
def dst(self, dt, is_dst=None):
|
||||
'''See datetime.tzinfo.dst
|
||||
|
||||
The is_dst parameter may be used to remove ambiguity during DST
|
||||
transitions.
|
||||
|
||||
>>> from pytz import timezone
|
||||
>>> tz = timezone('America/St_Johns')
|
||||
|
||||
>>> normal = datetime(2009, 9, 1)
|
||||
|
||||
>>> tz.dst(normal)
|
||||
datetime.timedelta(0, 3600)
|
||||
>>> tz.dst(normal, is_dst=False)
|
||||
datetime.timedelta(0, 3600)
|
||||
>>> tz.dst(normal, is_dst=True)
|
||||
datetime.timedelta(0, 3600)
|
||||
|
||||
>>> ambiguous = datetime(2009, 10, 31, 23, 30)
|
||||
|
||||
>>> tz.dst(ambiguous, is_dst=False)
|
||||
datetime.timedelta(0)
|
||||
>>> tz.dst(ambiguous, is_dst=True)
|
||||
datetime.timedelta(0, 3600)
|
||||
>>> try:
|
||||
... tz.dst(ambiguous)
|
||||
... except AmbiguousTimeError:
|
||||
... print('Ambiguous')
|
||||
Ambiguous
|
||||
|
||||
'''
|
||||
if dt is None:
|
||||
return None
|
||||
elif dt.tzinfo is not self:
|
||||
dt = self.localize(dt, is_dst)
|
||||
return dt.tzinfo._dst
|
||||
else:
|
||||
return self._dst
|
||||
|
||||
def tzname(self, dt, is_dst=None):
|
||||
'''See datetime.tzinfo.tzname
|
||||
|
||||
The is_dst parameter may be used to remove ambiguity during DST
|
||||
transitions.
|
||||
|
||||
>>> from pytz import timezone
|
||||
>>> tz = timezone('America/St_Johns')
|
||||
|
||||
>>> normal = datetime(2009, 9, 1)
|
||||
|
||||
>>> tz.tzname(normal)
|
||||
'NDT'
|
||||
>>> tz.tzname(normal, is_dst=False)
|
||||
'NDT'
|
||||
>>> tz.tzname(normal, is_dst=True)
|
||||
'NDT'
|
||||
|
||||
>>> ambiguous = datetime(2009, 10, 31, 23, 30)
|
||||
|
||||
>>> tz.tzname(ambiguous, is_dst=False)
|
||||
'NST'
|
||||
>>> tz.tzname(ambiguous, is_dst=True)
|
||||
'NDT'
|
||||
>>> try:
|
||||
... tz.tzname(ambiguous)
|
||||
... except AmbiguousTimeError:
|
||||
... print('Ambiguous')
|
||||
Ambiguous
|
||||
'''
|
||||
if dt is None:
|
||||
return self.zone
|
||||
elif dt.tzinfo is not self:
|
||||
dt = self.localize(dt, is_dst)
|
||||
return dt.tzinfo._tzname
|
||||
else:
|
||||
return self._tzname
|
||||
|
||||
def __repr__(self):
|
||||
if self._dst:
|
||||
dst = 'DST'
|
||||
else:
|
||||
dst = 'STD'
|
||||
if self._utcoffset > _notime:
|
||||
return '<DstTzInfo %r %s+%s %s>' % (
|
||||
self.zone, self._tzname, self._utcoffset, dst
|
||||
)
|
||||
else:
|
||||
return '<DstTzInfo %r %s%s %s>' % (
|
||||
self.zone, self._tzname, self._utcoffset, dst
|
||||
)
|
||||
|
||||
def __reduce__(self):
|
||||
# Special pickle to zone remains a singleton and to cope with
|
||||
# database changes.
|
||||
return pytz._p, (
|
||||
self.zone,
|
||||
_to_seconds(self._utcoffset),
|
||||
_to_seconds(self._dst),
|
||||
self._tzname
|
||||
)
|
||||
|
||||
|
||||
|
||||
def unpickler(zone, utcoffset=None, dstoffset=None, tzname=None):
|
||||
"""Factory function for unpickling pytz tzinfo instances.
|
||||
|
||||
This is shared for both StaticTzInfo and DstTzInfo instances, because
|
||||
database changes could cause a zones implementation to switch between
|
||||
these two base classes and we can't break pickles on a pytz version
|
||||
upgrade.
|
||||
"""
|
||||
# Raises a KeyError if zone no longer exists, which should never happen
|
||||
# and would be a bug.
|
||||
tz = pytz.timezone(zone)
|
||||
|
||||
# A StaticTzInfo - just return it
|
||||
if utcoffset is None:
|
||||
return tz
|
||||
|
||||
# This pickle was created from a DstTzInfo. We need to
|
||||
# determine which of the list of tzinfo instances for this zone
|
||||
# to use in order to restore the state of any datetime instances using
|
||||
# it correctly.
|
||||
utcoffset = memorized_timedelta(utcoffset)
|
||||
dstoffset = memorized_timedelta(dstoffset)
|
||||
try:
|
||||
return tz._tzinfos[(utcoffset, dstoffset, tzname)]
|
||||
except KeyError:
|
||||
# The particular state requested in this timezone no longer exists.
|
||||
# This indicates a corrupt pickle, or the timezone database has been
|
||||
# corrected violently enough to make this particular
|
||||
# (utcoffset,dstoffset) no longer exist in the zone, or the
|
||||
# abbreviation has been changed.
|
||||
pass
|
||||
|
||||
# See if we can find an entry differing only by tzname. Abbreviations
|
||||
# get changed from the initial guess by the database maintainers to
|
||||
# match reality when this information is discovered.
|
||||
for localized_tz in list(tz._tzinfos.values()):
|
||||
if (localized_tz._utcoffset == utcoffset
|
||||
and localized_tz._dst == dstoffset):
|
||||
return localized_tz
|
||||
|
||||
# This (utcoffset, dstoffset) information has been removed from the
|
||||
# zone. Add it back. This might occur when the database maintainers have
|
||||
# corrected incorrect information. datetime instances using this
|
||||
# incorrect information will continue to do so, exactly as they were
|
||||
# before being pickled. This is purely an overly paranoid safety net - I
|
||||
# doubt this will ever been needed in real life.
|
||||
inf = (utcoffset, dstoffset, tzname)
|
||||
tz._tzinfos[inf] = tz.__class__(inf, tz._tzinfos)
|
||||
return tz._tzinfos[inf]
|
||||
|
@ -18,7 +18,7 @@ from simplejson.decoder import PosInf
|
||||
#ESCAPE = re.compile(ur'[\x00-\x1f\\"\b\f\n\r\t\u2028\u2029]')
|
||||
# This is required because u() will mangle the string and ur'' isn't valid
|
||||
# python3 syntax
|
||||
ESCAPE = re.compile(u'[\\x00-\\x1f\\\\"\\b\\f\\n\\r\\t\u2028\u2029]')
|
||||
ESCAPE = re.compile(u('[\\x00-\\x1f\\\\"\\b\\f\\n\\r\\t\u2028\u2029]'))
|
||||
ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])')
|
||||
HAS_UTF8 = re.compile(r'[\x80-\xff]')
|
||||
ESCAPE_DCT = {
|
||||
@ -265,7 +265,7 @@ class JSONEncoder(object):
|
||||
if self.ensure_ascii:
|
||||
return ''.join(chunks)
|
||||
else:
|
||||
return u''.join(chunks)
|
||||
return u('').join(chunks)
|
||||
|
||||
def iterencode(self, o, _one_shot=False):
|
||||
"""Encode the given object and yield each string
|
||||
@ -358,7 +358,7 @@ class JSONEncoderForHTML(JSONEncoder):
|
||||
if self.ensure_ascii:
|
||||
return ''.join(chunks)
|
||||
else:
|
||||
return u''.join(chunks)
|
||||
return u('').join(chunks)
|
||||
|
||||
def iterencode(self, o, _one_shot=False):
|
||||
chunks = super(JSONEncoderForHTML, self).iterencode(o, _one_shot)
|
||||
|
6
packages/wakatime/wakatime/packages/tzlocal3/__init__.py
Normal file
6
packages/wakatime/wakatime/packages/tzlocal3/__init__.py
Normal file
@ -0,0 +1,6 @@
|
||||
import sys
|
||||
if sys.platform == 'win32':
|
||||
from tzlocal.win32 import get_localzone, reload_localzone
|
||||
else:
|
||||
from tzlocal.unix import get_localzone, reload_localzone
|
||||
|
64
packages/wakatime/wakatime/packages/tzlocal3/tests.py
Normal file
64
packages/wakatime/wakatime/packages/tzlocal3/tests.py
Normal file
@ -0,0 +1,64 @@
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
import unittest
|
||||
import pytz3 as pytz
|
||||
import tzlocal.unix
|
||||
|
||||
class TzLocalTests(unittest.TestCase):
|
||||
|
||||
def test_env(self):
|
||||
tz_harare = tzlocal.unix._tz_from_env(':Africa/Harare')
|
||||
self.assertEqual(tz_harare.zone, 'Africa/Harare')
|
||||
|
||||
# Some Unices allow this as well, so we must allow it:
|
||||
tz_harare = tzlocal.unix._tz_from_env('Africa/Harare')
|
||||
self.assertEqual(tz_harare.zone, 'Africa/Harare')
|
||||
|
||||
local_path = os.path.split(__file__)[0]
|
||||
tz_local = tzlocal.unix._tz_from_env(':' + os.path.join(local_path, 'test_data', 'Harare'))
|
||||
self.assertEqual(tz_local.zone, 'local')
|
||||
# Make sure the local timezone is the same as the Harare one above.
|
||||
# We test this with a past date, so that we don't run into future changes
|
||||
# of the Harare timezone.
|
||||
dt = datetime(2012, 1, 1, 5)
|
||||
self.assertEqual(tz_harare.localize(dt), tz_local.localize(dt))
|
||||
|
||||
# Non-zoneinfo timezones are not supported in the TZ environment.
|
||||
self.assertRaises(pytz.UnknownTimeZoneError, tzlocal.unix._tz_from_env, 'GMT+03:00')
|
||||
|
||||
def test_timezone(self):
|
||||
# Most versions of Ubuntu
|
||||
local_path = os.path.split(__file__)[0]
|
||||
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone'))
|
||||
self.assertEqual(tz.zone, 'Africa/Harare')
|
||||
|
||||
def test_zone_setting(self):
|
||||
# A ZONE setting in /etc/sysconfig/clock, f ex CentOS
|
||||
local_path = os.path.split(__file__)[0]
|
||||
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'zone_setting'))
|
||||
self.assertEqual(tz.zone, 'Africa/Harare')
|
||||
|
||||
def test_timezone_setting(self):
|
||||
# A ZONE setting in /etc/conf.d/clock, f ex Gentoo
|
||||
local_path = os.path.split(__file__)[0]
|
||||
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone_setting'))
|
||||
self.assertEqual(tz.zone, 'Africa/Harare')
|
||||
|
||||
def test_only_localtime(self):
|
||||
local_path = os.path.split(__file__)[0]
|
||||
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'localtime'))
|
||||
self.assertEqual(tz.zone, 'local')
|
||||
dt = datetime(2012, 1, 1, 5)
|
||||
self.assertEqual(pytz.timezone('Africa/Harare').localize(dt), tz.localize(dt))
|
||||
|
||||
if sys.platform == 'win32':
|
||||
|
||||
import tzlocal.win32
|
||||
class TzWin32Tests(unittest.TestCase):
|
||||
|
||||
def test_win32(self):
|
||||
tz = tzlocal.win32.get_localzone()
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
113
packages/wakatime/wakatime/packages/tzlocal3/unix.py
Normal file
113
packages/wakatime/wakatime/packages/tzlocal3/unix.py
Normal file
@ -0,0 +1,113 @@
|
||||
|
||||
import os
|
||||
import re
|
||||
import pytz3 as pytz
|
||||
|
||||
_cache_tz = None
|
||||
|
||||
def _tz_from_env(tzenv):
|
||||
if tzenv[0] == ':':
|
||||
tzenv = tzenv[1:]
|
||||
|
||||
# TZ specifies a file
|
||||
if os.path.exists(tzenv):
|
||||
with open(tzenv, 'rb') as tzfile:
|
||||
return pytz.tzfile.build_tzinfo('local', tzfile)
|
||||
|
||||
# TZ specifies a zoneinfo zone.
|
||||
try:
|
||||
tz = pytz.timezone(tzenv)
|
||||
# That worked, so we return this:
|
||||
return tz
|
||||
except pytz.UnknownTimeZoneError:
|
||||
raise pytz.UnknownTimeZoneError(
|
||||
"tzlocal() does not support non-zoneinfo timezones like %s. \n"
|
||||
"Please use a timezone in the form of Continent/City")
|
||||
|
||||
def _get_localzone(_root='/'):
|
||||
"""Tries to find the local timezone configuration.
|
||||
|
||||
This method prefers finding the timezone name and passing that to pytz,
|
||||
over passing in the localtime file, as in the later case the zoneinfo
|
||||
name is unknown.
|
||||
|
||||
The parameter _root makes the function look for files like /etc/localtime
|
||||
beneath the _root directory. This is primarily used by the tests.
|
||||
In normal usage you call the function without parameters."""
|
||||
|
||||
tzenv = os.environ.get('TZ')
|
||||
if tzenv:
|
||||
return _tz_from_env(tzenv)
|
||||
|
||||
# Now look for distribution specific configuration files
|
||||
# that contain the timezone name.
|
||||
tzpath = os.path.join(_root, 'etc/timezone')
|
||||
if os.path.exists(tzpath):
|
||||
with open(tzpath, 'rb') as tzfile:
|
||||
data = tzfile.read()
|
||||
|
||||
# Issue #3 was that /etc/timezone was a zoneinfo file.
|
||||
# That's a misconfiguration, but we need to handle it gracefully:
|
||||
if data[:5] != 'TZif2':
|
||||
etctz = data.strip().decode()
|
||||
# Get rid of host definitions and comments:
|
||||
if ' ' in etctz:
|
||||
etctz, dummy = etctz.split(' ', 1)
|
||||
if '#' in etctz:
|
||||
etctz, dummy = etctz.split('#', 1)
|
||||
return pytz.timezone(etctz.replace(' ', '_'))
|
||||
|
||||
# CentOS has a ZONE setting in /etc/sysconfig/clock,
|
||||
# OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
|
||||
# Gentoo has a TIMEZONE setting in /etc/conf.d/clock
|
||||
# We look through these files for a timezone:
|
||||
|
||||
zone_re = re.compile('\s*ZONE\s*=\s*\"')
|
||||
timezone_re = re.compile('\s*TIMEZONE\s*=\s*\"')
|
||||
end_re = re.compile('\"')
|
||||
|
||||
for filename in ('etc/sysconfig/clock', 'etc/conf.d/clock'):
|
||||
tzpath = os.path.join(_root, filename)
|
||||
if not os.path.exists(tzpath):
|
||||
continue
|
||||
with open(tzpath, 'rt') as tzfile:
|
||||
data = tzfile.readlines()
|
||||
|
||||
for line in data:
|
||||
# Look for the ZONE= setting.
|
||||
match = zone_re.match(line)
|
||||
if match is None:
|
||||
# No ZONE= setting. Look for the TIMEZONE= setting.
|
||||
match = timezone_re.match(line)
|
||||
if match is not None:
|
||||
# Some setting existed
|
||||
line = line[match.end():]
|
||||
etctz = line[:end_re.search(line).start()]
|
||||
|
||||
# We found a timezone
|
||||
return pytz.timezone(etctz.replace(' ', '_'))
|
||||
|
||||
# No explicit setting existed. Use localtime
|
||||
for filename in ('etc/localtime', 'usr/local/etc/localtime'):
|
||||
tzpath = os.path.join(_root, filename)
|
||||
|
||||
if not os.path.exists(tzpath):
|
||||
continue
|
||||
with open(tzpath, 'rb') as tzfile:
|
||||
return pytz.tzfile.build_tzinfo('local', tzfile)
|
||||
|
||||
raise pytz.UnknownTimeZoneError('Can not find any timezone configuration')
|
||||
|
||||
def get_localzone():
|
||||
"""Get the computers configured local timezone, if any."""
|
||||
global _cache_tz
|
||||
if _cache_tz is None:
|
||||
_cache_tz = _get_localzone()
|
||||
return _cache_tz
|
||||
|
||||
def reload_localzone():
|
||||
"""Reload the cached localzone. You need to call this if the timezone has changed."""
|
||||
global _cache_tz
|
||||
_cache_tz = _get_localzone()
|
||||
return _cache_tz
|
||||
|
88
packages/wakatime/wakatime/packages/tzlocal3/win32.py
Normal file
88
packages/wakatime/wakatime/packages/tzlocal3/win32.py
Normal file
@ -0,0 +1,88 @@
|
||||
try:
|
||||
import winreg as winreg
|
||||
except ImportError:
|
||||
import winreg
|
||||
|
||||
from tzlocal.windows_tz import tz_names
|
||||
import pytz3 as pytz
|
||||
|
||||
_cache_tz = None
|
||||
|
||||
def valuestodict(key):
|
||||
"""Convert a registry key's values to a dictionary."""
|
||||
dict = {}
|
||||
size = winreg.QueryInfoKey(key)[1]
|
||||
for i in range(size):
|
||||
data = winreg.EnumValue(key, i)
|
||||
dict[data[0]] = data[1]
|
||||
return dict
|
||||
|
||||
def get_localzone_name():
|
||||
# Windows is special. It has unique time zone names (in several
|
||||
# meanings of the word) available, but unfortunately, they can be
|
||||
# translated to the language of the operating system, so we need to
|
||||
# do a backwards lookup, by going through all time zones and see which
|
||||
# one matches.
|
||||
handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
|
||||
|
||||
TZLOCALKEYNAME = r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation"
|
||||
localtz = winreg.OpenKey(handle, TZLOCALKEYNAME)
|
||||
keyvalues = valuestodict(localtz)
|
||||
localtz.Close()
|
||||
if 'TimeZoneKeyName' in keyvalues:
|
||||
# Windows 7 (and Vista?)
|
||||
|
||||
# For some reason this returns a string with loads of NUL bytes at
|
||||
# least on some systems. I don't know if this is a bug somewhere, I
|
||||
# just work around it.
|
||||
tzkeyname = keyvalues['TimeZoneKeyName'].split('\x00', 1)[0]
|
||||
else:
|
||||
# Windows 2000 or XP
|
||||
|
||||
# This is the localized name:
|
||||
tzwin = keyvalues['StandardName']
|
||||
|
||||
# Open the list of timezones to look up the real name:
|
||||
TZKEYNAME = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"
|
||||
tzkey = winreg.OpenKey(handle, TZKEYNAME)
|
||||
|
||||
# Now, match this value to Time Zone information
|
||||
tzkeyname = None
|
||||
for i in range(winreg.QueryInfoKey(tzkey)[0]):
|
||||
subkey = winreg.EnumKey(tzkey, i)
|
||||
sub = winreg.OpenKey(tzkey, subkey)
|
||||
data = valuestodict(sub)
|
||||
sub.Close()
|
||||
if data['Std'] == tzwin:
|
||||
tzkeyname = subkey
|
||||
break
|
||||
|
||||
tzkey.Close()
|
||||
handle.Close()
|
||||
|
||||
if tzkeyname is None:
|
||||
raise LookupError('Can not find Windows timezone configuration')
|
||||
|
||||
timezone = tz_names.get(tzkeyname)
|
||||
if timezone is None:
|
||||
# Nope, that didn't work. Try adding "Standard Time",
|
||||
# it seems to work a lot of times:
|
||||
timezone = tz_names.get(tzkeyname + " Standard Time")
|
||||
|
||||
# Return what we have.
|
||||
if timezone is None:
|
||||
raise pytz.UnknownTimeZoneError('Can not find timezone ' + tzkeyname)
|
||||
|
||||
return timezone
|
||||
|
||||
def get_localzone():
|
||||
"""Returns the zoneinfo-based tzinfo object that matches the Windows-configured timezone."""
|
||||
global _cache_tz
|
||||
if _cache_tz is None:
|
||||
_cache_tz = pytz.timezone(get_localzone_name())
|
||||
return _cache_tz
|
||||
|
||||
def reload_localzone():
|
||||
"""Reload the cached localzone. You need to call this if the timezone has changed."""
|
||||
global _cache_tz
|
||||
_cache_tz = pytz.timezone(get_localzone_name())
|
101
packages/wakatime/wakatime/packages/tzlocal3/windows_tz.py
Normal file
101
packages/wakatime/wakatime/packages/tzlocal3/windows_tz.py
Normal file
@ -0,0 +1,101 @@
|
||||
# This file is autogenerated by the get_windows_info.py script
|
||||
# Do not edit.
|
||||
tz_names = {'AUS Central Standard Time': 'Australia/Darwin',
|
||||
'AUS Eastern Standard Time': 'Australia/Sydney',
|
||||
'Afghanistan Standard Time': 'Asia/Kabul',
|
||||
'Alaskan Standard Time': 'America/Anchorage',
|
||||
'Arab Standard Time': 'Asia/Riyadh',
|
||||
'Arabian Standard Time': 'Asia/Dubai',
|
||||
'Arabic Standard Time': 'Asia/Baghdad',
|
||||
'Argentina Standard Time': 'America/Buenos_Aires',
|
||||
'Atlantic Standard Time': 'America/Halifax',
|
||||
'Azerbaijan Standard Time': 'Asia/Baku',
|
||||
'Azores Standard Time': 'Atlantic/Azores',
|
||||
'Bahia Standard Time': 'America/Bahia',
|
||||
'Bangladesh Standard Time': 'Asia/Dhaka',
|
||||
'Canada Central Standard Time': 'America/Regina',
|
||||
'Cape Verde Standard Time': 'Atlantic/Cape_Verde',
|
||||
'Caucasus Standard Time': 'Asia/Yerevan',
|
||||
'Cen. Australia Standard Time': 'Australia/Adelaide',
|
||||
'Central America Standard Time': 'America/Guatemala',
|
||||
'Central Asia Standard Time': 'Asia/Almaty',
|
||||
'Central Brazilian Standard Time': 'America/Cuiaba',
|
||||
'Central Europe Standard Time': 'Europe/Budapest',
|
||||
'Central European Standard Time': 'Europe/Warsaw',
|
||||
'Central Pacific Standard Time': 'Pacific/Guadalcanal',
|
||||
'Central Standard Time': 'America/Chicago',
|
||||
'Central Standard Time (Mexico)': 'America/Mexico_City',
|
||||
'China Standard Time': 'Asia/Shanghai',
|
||||
'Dateline Standard Time': 'Etc/GMT+12',
|
||||
'E. Africa Standard Time': 'Africa/Nairobi',
|
||||
'E. Australia Standard Time': 'Australia/Brisbane',
|
||||
'E. Europe Standard Time': 'Asia/Nicosia',
|
||||
'E. South America Standard Time': 'America/Sao_Paulo',
|
||||
'Eastern Standard Time': 'America/New_York',
|
||||
'Egypt Standard Time': 'Africa/Cairo',
|
||||
'Ekaterinburg Standard Time': 'Asia/Yekaterinburg',
|
||||
'FLE Standard Time': 'Europe/Kiev',
|
||||
'Fiji Standard Time': 'Pacific/Fiji',
|
||||
'GMT Standard Time': 'Europe/London',
|
||||
'GTB Standard Time': 'Europe/Bucharest',
|
||||
'Georgian Standard Time': 'Asia/Tbilisi',
|
||||
'Greenland Standard Time': 'America/Godthab',
|
||||
'Greenwich Standard Time': 'Atlantic/Reykjavik',
|
||||
'Hawaiian Standard Time': 'Pacific/Honolulu',
|
||||
'India Standard Time': 'Asia/Calcutta',
|
||||
'Iran Standard Time': 'Asia/Tehran',
|
||||
'Israel Standard Time': 'Asia/Jerusalem',
|
||||
'Jordan Standard Time': 'Asia/Amman',
|
||||
'Kaliningrad Standard Time': 'Europe/Kaliningrad',
|
||||
'Korea Standard Time': 'Asia/Seoul',
|
||||
'Magadan Standard Time': 'Asia/Magadan',
|
||||
'Mauritius Standard Time': 'Indian/Mauritius',
|
||||
'Middle East Standard Time': 'Asia/Beirut',
|
||||
'Montevideo Standard Time': 'America/Montevideo',
|
||||
'Morocco Standard Time': 'Africa/Casablanca',
|
||||
'Mountain Standard Time': 'America/Denver',
|
||||
'Mountain Standard Time (Mexico)': 'America/Chihuahua',
|
||||
'Myanmar Standard Time': 'Asia/Rangoon',
|
||||
'N. Central Asia Standard Time': 'Asia/Novosibirsk',
|
||||
'Namibia Standard Time': 'Africa/Windhoek',
|
||||
'Nepal Standard Time': 'Asia/Katmandu',
|
||||
'New Zealand Standard Time': 'Pacific/Auckland',
|
||||
'Newfoundland Standard Time': 'America/St_Johns',
|
||||
'North Asia East Standard Time': 'Asia/Irkutsk',
|
||||
'North Asia Standard Time': 'Asia/Krasnoyarsk',
|
||||
'Pacific SA Standard Time': 'America/Santiago',
|
||||
'Pacific Standard Time': 'America/Los_Angeles',
|
||||
'Pacific Standard Time (Mexico)': 'America/Santa_Isabel',
|
||||
'Pakistan Standard Time': 'Asia/Karachi',
|
||||
'Paraguay Standard Time': 'America/Asuncion',
|
||||
'Romance Standard Time': 'Europe/Paris',
|
||||
'Russian Standard Time': 'Europe/Moscow',
|
||||
'SA Eastern Standard Time': 'America/Cayenne',
|
||||
'SA Pacific Standard Time': 'America/Bogota',
|
||||
'SA Western Standard Time': 'America/La_Paz',
|
||||
'SE Asia Standard Time': 'Asia/Bangkok',
|
||||
'Samoa Standard Time': 'Pacific/Apia',
|
||||
'Singapore Standard Time': 'Asia/Singapore',
|
||||
'South Africa Standard Time': 'Africa/Johannesburg',
|
||||
'Sri Lanka Standard Time': 'Asia/Colombo',
|
||||
'Syria Standard Time': 'Asia/Damascus',
|
||||
'Taipei Standard Time': 'Asia/Taipei',
|
||||
'Tasmania Standard Time': 'Australia/Hobart',
|
||||
'Tokyo Standard Time': 'Asia/Tokyo',
|
||||
'Tonga Standard Time': 'Pacific/Tongatapu',
|
||||
'Turkey Standard Time': 'Europe/Istanbul',
|
||||
'US Eastern Standard Time': 'America/Indianapolis',
|
||||
'US Mountain Standard Time': 'America/Phoenix',
|
||||
'UTC': 'Etc/GMT',
|
||||
'UTC+12': 'Etc/GMT-12',
|
||||
'UTC-02': 'Etc/GMT+2',
|
||||
'UTC-11': 'Etc/GMT+11',
|
||||
'Ulaanbaatar Standard Time': 'Asia/Ulaanbaatar',
|
||||
'Venezuela Standard Time': 'America/Caracas',
|
||||
'Vladivostok Standard Time': 'Asia/Vladivostok',
|
||||
'W. Australia Standard Time': 'Australia/Perth',
|
||||
'W. Central Africa Standard Time': 'Africa/Lagos',
|
||||
'W. Europe Standard Time': 'Europe/Berlin',
|
||||
'West Asia Standard Time': 'Asia/Tashkent',
|
||||
'West Pacific Standard Time': 'Pacific/Port_Moresby',
|
||||
'Yakutsk Standard Time': 'Asia/Yakutsk'}
|
@ -14,21 +14,32 @@ import os
|
||||
|
||||
from .projects.git import Git
|
||||
from .projects.mercurial import Mercurial
|
||||
from .projects.projectmap import ProjectMap
|
||||
from .projects.subversion import Subversion
|
||||
from .projects.wakatime import WakaTime
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# List of plugin classes to find a project for the current file path.
|
||||
# Project plugins will be processed with priority in the order below.
|
||||
PLUGINS = [
|
||||
WakaTime,
|
||||
ProjectMap,
|
||||
Git,
|
||||
Mercurial,
|
||||
Subversion,
|
||||
]
|
||||
|
||||
|
||||
def find_project(path):
|
||||
def find_project(path, configs=None):
|
||||
for plugin in PLUGINS:
|
||||
project = plugin(path)
|
||||
plugin_name = plugin.__name__.lower()
|
||||
plugin_configs = None
|
||||
if configs and configs.has_section(plugin_name):
|
||||
plugin_configs = dict(configs.items(plugin_name))
|
||||
project = plugin(path, configs=plugin_configs)
|
||||
if project.process():
|
||||
return project
|
||||
return None
|
||||
|
@ -13,7 +13,7 @@ import logging
|
||||
import os
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
class BaseProject(object):
|
||||
@ -22,18 +22,19 @@ class BaseProject(object):
|
||||
be found for the current path.
|
||||
"""
|
||||
|
||||
def __init__(self, path):
|
||||
def __init__(self, path, configs=None):
|
||||
self.path = path
|
||||
self._configs = configs
|
||||
|
||||
def type(self):
|
||||
def project_type(self):
|
||||
""" Returns None if this is the base class.
|
||||
Returns the type of project if this is a
|
||||
valid project.
|
||||
"""
|
||||
type = self.__class__.__name__.lower()
|
||||
if type == 'baseproject':
|
||||
type = None
|
||||
return type
|
||||
project_type = self.__class__.__name__.lower()
|
||||
if project_type == 'baseproject':
|
||||
project_type = None
|
||||
return project_type
|
||||
|
||||
def process(self):
|
||||
""" Processes self.path into a project and
|
||||
|
@ -11,57 +11,49 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from .base import BaseProject
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError:
|
||||
from ..packages.ordereddict import OrderedDict
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class Git(BaseProject):
|
||||
|
||||
def process(self):
|
||||
self.config = self._find_config(self.path)
|
||||
if self.config:
|
||||
return True
|
||||
return False
|
||||
self.configFile = self._find_git_config_file(self.path)
|
||||
return self.configFile is not None
|
||||
|
||||
def name(self):
|
||||
base = self._project_base()
|
||||
if base:
|
||||
return os.path.basename(base)
|
||||
return unicode(os.path.basename(base))
|
||||
return None
|
||||
|
||||
def branch(self):
|
||||
stdout = None
|
||||
try:
|
||||
stdout, stderr = Popen([
|
||||
'git', 'branch', '--no-color'
|
||||
], stdout=PIPE, stderr=PIPE, cwd=self._project_base()
|
||||
).communicate()
|
||||
except OSError:
|
||||
pass
|
||||
if stdout:
|
||||
for line in stdout.splitlines():
|
||||
if isinstance(line, bytes):
|
||||
line = bytes.decode(line)
|
||||
line = line.split(' ', 1)
|
||||
if line[0] == '*':
|
||||
return line[1]
|
||||
base = self._project_base()
|
||||
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])
|
||||
except IOError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def _project_base(self):
|
||||
if self.config:
|
||||
return os.path.dirname(os.path.dirname(self.config))
|
||||
if self.configFile:
|
||||
return os.path.dirname(os.path.dirname(self.configFile))
|
||||
return None
|
||||
|
||||
def _find_config(self, path):
|
||||
def _find_git_config_file(self, path):
|
||||
path = os.path.realpath(path)
|
||||
if os.path.isfile(path):
|
||||
path = os.path.split(path)[0]
|
||||
@ -70,34 +62,4 @@ class Git(BaseProject):
|
||||
split_path = os.path.split(path)
|
||||
if split_path[1] == '':
|
||||
return None
|
||||
return self._find_config(split_path[0])
|
||||
|
||||
def _parse_config(self):
|
||||
sections = {}
|
||||
try:
|
||||
f = open(self.config, 'r')
|
||||
except IOError as e:
|
||||
log.exception("Exception:")
|
||||
else:
|
||||
with f:
|
||||
section = None
|
||||
for line in f.readlines():
|
||||
line = line.lstrip()
|
||||
if len(line) > 0 and line[0] == '[':
|
||||
section = line[1:].split(']', 1)[0]
|
||||
temp = section.split(' ', 1)
|
||||
section = temp[0].lower()
|
||||
if len(temp) > 1:
|
||||
section = ' '.join([section, temp[1]])
|
||||
sections[section] = {}
|
||||
else:
|
||||
try:
|
||||
(setting, value) = line.split('=', 1)
|
||||
except ValueError:
|
||||
setting = line.split('#', 1)[0].split(';', 1)[0]
|
||||
value = 'true'
|
||||
setting = setting.strip().lower()
|
||||
value = value.split('#', 1)[0].split(';', 1)[0].strip()
|
||||
sections[section][setting] = value
|
||||
f.close()
|
||||
return sections
|
||||
return self._find_git_config_file(split_path[0])
|
||||
|
@ -15,16 +15,44 @@ import os
|
||||
from .base import BaseProject
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class Mercurial(BaseProject):
|
||||
|
||||
def process(self):
|
||||
return False
|
||||
self.configDir = self._find_hg_config_dir(self.path)
|
||||
return self.configDir is not None
|
||||
|
||||
def name(self):
|
||||
if self.configDir:
|
||||
return unicode(os.path.basename(os.path.dirname(self.configDir)))
|
||||
return None
|
||||
|
||||
def branch(self):
|
||||
return None
|
||||
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])
|
||||
except IOError:
|
||||
pass
|
||||
return unicode('default')
|
||||
|
||||
def _find_hg_config_dir(self, path):
|
||||
path = os.path.realpath(path)
|
||||
if os.path.isfile(path):
|
||||
path = os.path.split(path)[0]
|
||||
if os.path.isdir(os.path.join(path, '.hg')):
|
||||
return os.path.join(path, '.hg')
|
||||
split_path = os.path.split(path)
|
||||
if split_path[1] == '':
|
||||
return None
|
||||
return self._find_hg_config_dir(split_path[0])
|
||||
|
72
packages/wakatime/wakatime/projects/projectmap.py
Normal file
72
packages/wakatime/wakatime/projects/projectmap.py
Normal file
@ -0,0 +1,72 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.projects.projectmap
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Use the ~/.wakatime.cfg file to set custom project names by
|
||||
recursively matching folder paths.
|
||||
Project maps go under the [projectmap] config section.
|
||||
|
||||
For example:
|
||||
|
||||
[projectmap]
|
||||
/home/user/projects/foo = new project name
|
||||
/home/user/projects/bar = project2
|
||||
|
||||
Will result in file `/home/user/projects/foo/src/main.c` to have
|
||||
project name `new project name`.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from .base import BaseProject
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class ProjectMap(BaseProject):
|
||||
|
||||
def process(self):
|
||||
if not self._configs:
|
||||
return False
|
||||
|
||||
self.project = self._find_project(self.path)
|
||||
|
||||
return self.project is not None
|
||||
|
||||
def _find_project(self, path):
|
||||
path = os.path.realpath(path)
|
||||
if os.path.isfile(path):
|
||||
path = os.path.split(path)[0]
|
||||
|
||||
if self._configs.get(path.lower()):
|
||||
return self._configs.get(path.lower())
|
||||
if self._configs.get('%s/' % path.lower()):
|
||||
return self._configs.get('%s/' % path.lower())
|
||||
if self._configs.get('%s\\' % path.lower()):
|
||||
return self._configs.get('%s\\' % path.lower())
|
||||
|
||||
split_path = os.path.split(path)
|
||||
if split_path[1] == '':
|
||||
return None
|
||||
return self._find_project(split_path[0])
|
||||
|
||||
def branch(self):
|
||||
return None
|
||||
|
||||
def name(self):
|
||||
if self.project:
|
||||
return unicode(self.project)
|
||||
return None
|
@ -11,6 +11,7 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from .base import BaseProject
|
||||
@ -20,29 +21,55 @@ except ImportError:
|
||||
from ..packages.ordereddict import OrderedDict
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class Subversion(BaseProject):
|
||||
binary_location = None
|
||||
|
||||
def process(self):
|
||||
return self._find_project_base(self.path)
|
||||
|
||||
def name(self):
|
||||
return self.info['Repository Root'].split('/')[-1]
|
||||
return unicode(self.info['Repository Root'].split('/')[-1])
|
||||
|
||||
def branch(self):
|
||||
branch = None
|
||||
if self.base:
|
||||
branch = os.path.basename(self.base)
|
||||
return branch
|
||||
unicode(os.path.basename(self.base))
|
||||
return None
|
||||
|
||||
def _find_binary(self):
|
||||
if self.binary_location:
|
||||
return self.binary_location
|
||||
locations = [
|
||||
'svn',
|
||||
'/usr/bin/svn',
|
||||
'/usr/local/bin/svn',
|
||||
]
|
||||
for location in locations:
|
||||
try:
|
||||
Popen([location, '--version'])
|
||||
self.binary_location = location
|
||||
return location
|
||||
except:
|
||||
pass
|
||||
self.binary_location = 'svn'
|
||||
return 'svn'
|
||||
|
||||
def _get_info(self, path):
|
||||
info = OrderedDict()
|
||||
stdout = None
|
||||
try:
|
||||
os.environ['LANG'] = 'en_US'
|
||||
stdout, stderr = Popen([
|
||||
'svn', 'info', os.path.realpath(path)
|
||||
self._find_binary(), 'info', os.path.realpath(path)
|
||||
], stdout=PIPE, stderr=PIPE).communicate()
|
||||
except OSError:
|
||||
pass
|
||||
@ -62,6 +89,8 @@ class Subversion(BaseProject):
|
||||
return info
|
||||
|
||||
def _find_project_base(self, path, found=False):
|
||||
if platform.system() == 'Windows':
|
||||
return False
|
||||
path = os.path.realpath(path)
|
||||
if os.path.isfile(path):
|
||||
path = os.path.split(path)[0]
|
||||
|
64
packages/wakatime/wakatime/projects/wakatime.py
Normal file
64
packages/wakatime/wakatime/projects/wakatime.py
Normal file
@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.projects.wakatime
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Information from a .wakatime-project file about the project for
|
||||
a given file. First line of .wakatime-project sets the project
|
||||
name. Second line sets the current branch name.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from .base import BaseProject
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
# str is unicode in Python3
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
class WakaTime(BaseProject):
|
||||
|
||||
def process(self):
|
||||
self.config = self._find_config(self.path)
|
||||
self._project_name = None
|
||||
self._project_branch = None
|
||||
|
||||
if self.config:
|
||||
|
||||
try:
|
||||
with open(self.config) as fh:
|
||||
self._project_name = unicode(fh.readline().strip())
|
||||
self._project_branch = unicode(fh.readline().strip())
|
||||
except IOError as e:
|
||||
log.exception("Exception:")
|
||||
|
||||
return True
|
||||
return False
|
||||
|
||||
def name(self):
|
||||
return self._project_name
|
||||
|
||||
def branch(self):
|
||||
return self._project_branch
|
||||
|
||||
def _find_config(self, path):
|
||||
path = os.path.realpath(path)
|
||||
if os.path.isfile(path):
|
||||
path = os.path.split(path)[0]
|
||||
if os.path.isfile(os.path.join(path, '.wakatime-project')):
|
||||
return os.path.join(path, '.wakatime-project')
|
||||
split_path = os.path.split(path)
|
||||
if split_path[1] == '':
|
||||
return None
|
||||
return self._find_config(split_path[0])
|
122
packages/wakatime/wakatime/queue.py
Normal file
122
packages/wakatime/wakatime/queue.py
Normal file
@ -0,0 +1,122 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.queue
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
Queue for offline time logging.
|
||||
http://wakatime.com
|
||||
|
||||
:copyright: (c) 2014 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
|
||||
import logging
|
||||
import os
|
||||
import traceback
|
||||
from time import sleep
|
||||
try:
|
||||
import sqlite3
|
||||
HAS_SQL = True
|
||||
except ImportError:
|
||||
HAS_SQL = False
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
class Queue(object):
|
||||
DB_FILE = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
||||
|
||||
def connect(self):
|
||||
conn = sqlite3.connect(self.DB_FILE)
|
||||
c = conn.cursor()
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS action (
|
||||
file text,
|
||||
time real,
|
||||
project text,
|
||||
language text,
|
||||
lines integer,
|
||||
branch text,
|
||||
is_write integer,
|
||||
plugin text)
|
||||
''')
|
||||
return (conn, c)
|
||||
|
||||
|
||||
def push(self, data, plugin):
|
||||
if not HAS_SQL:
|
||||
return
|
||||
try:
|
||||
conn, c = self.connect()
|
||||
action = {
|
||||
'file': data.get('file'),
|
||||
'time': data.get('time'),
|
||||
'project': data.get('project'),
|
||||
'language': data.get('language'),
|
||||
'lines': data.get('lines'),
|
||||
'branch': data.get('branch'),
|
||||
'is_write': 1 if data.get('is_write') else 0,
|
||||
'plugin': plugin,
|
||||
}
|
||||
c.execute('INSERT INTO action VALUES (:file,:time,:project,:language,:lines,:branch,:is_write,:plugin)', action)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
except sqlite3.Error:
|
||||
log.error(traceback.format_exc())
|
||||
|
||||
|
||||
def pop(self):
|
||||
if not HAS_SQL:
|
||||
return None
|
||||
tries = 3
|
||||
wait = 0.1
|
||||
action = None
|
||||
try:
|
||||
conn, c = self.connect()
|
||||
except sqlite3.Error:
|
||||
log.debug(traceback.format_exc())
|
||||
return None
|
||||
loop = True
|
||||
while loop and tries > -1:
|
||||
try:
|
||||
c.execute('BEGIN IMMEDIATE')
|
||||
c.execute('SELECT * FROM action LIMIT 1')
|
||||
row = c.fetchone()
|
||||
if row is not None:
|
||||
values = []
|
||||
clauses = []
|
||||
index = 0
|
||||
for row_name in ['file', 'time', 'project', 'language', 'lines', 'branch', 'is_write']:
|
||||
if row[index] is not None:
|
||||
clauses.append('{0}=?'.format(row_name))
|
||||
values.append(row[index])
|
||||
else:
|
||||
clauses.append('{0} IS NULL'.format(row_name))
|
||||
index += 1
|
||||
if len(values) > 0:
|
||||
c.execute('DELETE FROM action WHERE {0}'.format(' AND '.join(clauses)), values)
|
||||
else:
|
||||
c.execute('DELETE FROM action WHERE {0}'.format(' AND '.join(clauses)))
|
||||
conn.commit()
|
||||
if row is not None:
|
||||
action = {
|
||||
'file': row[0],
|
||||
'time': row[1],
|
||||
'project': row[2],
|
||||
'language': row[3],
|
||||
'lines': row[4],
|
||||
'branch': row[5],
|
||||
'is_write': True if row[6] is 1 else False,
|
||||
'plugin': row[7],
|
||||
}
|
||||
loop = False
|
||||
except sqlite3.Error:
|
||||
log.debug(traceback.format_exc())
|
||||
sleep(wait)
|
||||
tries -= 1
|
||||
try:
|
||||
conn.close()
|
||||
except sqlite3.Error:
|
||||
log.debug(traceback.format_exc())
|
||||
return action
|
@ -18,25 +18,67 @@ if sys.version_info[0] == 2:
|
||||
else:
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments3'))
|
||||
from pygments.lexers import guess_lexer_for_filename
|
||||
from pygments.util import ClassNotFound
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
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',
|
||||
'twig': 'Twig',
|
||||
}
|
||||
TRANSLATIONS = {
|
||||
'CSS+Genshi Text': 'CSS',
|
||||
'CSS+Lasso': 'CSS',
|
||||
'HTML+Django/Jinja': 'HTML',
|
||||
'HTML+Lasso': 'HTML',
|
||||
'JavaScript+Genshi Text': 'JavaScript',
|
||||
'JavaScript+Lasso': 'JavaScript',
|
||||
'Perl6': 'Perl',
|
||||
'RHTML': 'HTML',
|
||||
}
|
||||
|
||||
|
||||
def guess_language(file_name):
|
||||
if file_name:
|
||||
language = guess_language_from_extension(file_name.rsplit('.', 1)[-1])
|
||||
if language:
|
||||
return language
|
||||
lexer = None
|
||||
try:
|
||||
with open(file_name) as f:
|
||||
lexer = guess_lexer_for_filename(file_name, f.read(512000))
|
||||
except (ClassNotFound, IOError):
|
||||
except:
|
||||
pass
|
||||
if lexer:
|
||||
return str(lexer.name)
|
||||
return translate_language(unicode(lexer.name))
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def guess_language_from_extension(extension):
|
||||
if extension:
|
||||
if extension in EXTENSIONS:
|
||||
return EXTENSIONS[extension]
|
||||
if extension.lower() in EXTENSIONS:
|
||||
return mapping[EXTENSIONS.lower()]
|
||||
return None
|
||||
|
||||
|
||||
def translate_language(language):
|
||||
if language in TRANSLATIONS:
|
||||
language = TRANSLATIONS[language]
|
||||
return language
|
||||
|
||||
|
||||
def number_lines_in_file(file_name):
|
||||
lines = 0
|
||||
try:
|
||||
|
Reference in New Issue
Block a user