upgraded wakatime.py to version 0.1.2

This commit is contained in:
Alan Hamlett 2013-07-09 20:15:01 -07:00
parent 09e67fedfe
commit eb0781688d
6 changed files with 109 additions and 35 deletions

View File

@ -30,6 +30,6 @@ PLUGINS = [
def find_project(path): def find_project(path):
for plugin in PLUGINS: for plugin in PLUGINS:
project = plugin(path) project = plugin(path)
if project.config: if project.process():
return project return project
return BaseProject(path) return BaseProject(path)

View File

@ -16,32 +16,39 @@ import os
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class BaseProject(): class BaseProject(object):
""" Parent project class only
used when no valid project can
be found for the current path.
"""
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path
self.config = self.findConfig(path)
def name(self):
base = self.base()
if base:
return os.path.basename(base)
return None
def type(self): def 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() type = self.__class__.__name__.lower()
if type == 'baseproject': if type == 'baseproject':
type = None type = None
return type return type
def base(self): def process(self):
if self.config: """ Processes self.path into a project and
return os.path.dirname(self.config) returns True if project is valid, otherwise
returns False.
"""
return False
def name(self):
""" Returns the project's name.
"""
return None return None
def tags(self): def tags(self):
tags = [] """ Returns an array of tag strings for the
return tags path and/or project.
"""
def findConfig(self, path): return []
return ''

View File

@ -11,13 +11,10 @@
import logging import logging
import os import os
from subprocess import Popen, PIPE
from .base import BaseProject from .base import BaseProject
from ..packages.ordereddict import OrderedDict
try:
from collections import OrderedDict
except ImportError:
from ..packages.ordereddict import OrderedDict
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -25,21 +22,54 @@ log = logging.getLogger(__name__)
class Git(BaseProject): class Git(BaseProject):
def base(self): def process(self):
self.config = self._find_config(self.path)
if self.config: if self.config:
return os.path.dirname(os.path.dirname(self.config)) return True
return False
def name(self):
base = self._project_base()
if base:
return os.path.basename(base)
return None return None
def tags(self): def tags(self):
tags = [] tags = []
if self.config: if self.config:
sections = self.parseConfig() base = self._project_base()
if base:
tags.append(base)
sections = self._parse_config()
for section in sections: for section in sections:
if section.split(' ', 1)[0] == 'remote' and 'url' in sections[section]: if section.split(' ', 1)[0] == 'remote' and 'url' in sections[section]:
tags.append(sections[section]['url']) tags.append(sections[section]['url'])
branch = self._current_branch()
if branch is not None:
tags.append(branch)
return tags return tags
def findConfig(self, path): def _project_base(self):
if self.config:
return os.path.dirname(os.path.dirname(self.config))
return None
def _current_branch(self):
stdout = None
try:
stdout, stderr = Popen([
'git', 'branch', '--no-color', '--list'
], stdout=PIPE, cwd=self._project_base()).communicate()
except OSError:
pass
if stdout:
for line in stdout.splitlines():
line = line.split(' ', 1)
if line[0] == '*':
return line[1]
return None
def _find_config(self, path):
path = os.path.realpath(path) path = os.path.realpath(path)
if os.path.isfile(path): if os.path.isfile(path):
path = os.path.split(path)[0] path = os.path.split(path)[0]
@ -48,9 +78,9 @@ class Git(BaseProject):
split_path = os.path.split(path) split_path = os.path.split(path)
if split_path[1] == '': if split_path[1] == '':
return None return None
return self.findConfig(split_path[0]) return self._find_config(split_path[0])
def parseConfig(self): def _parse_config(self):
sections = {} sections = {}
try: try:
f = open(self.config, 'r') f = open(self.config, 'r')

View File

@ -20,9 +20,11 @@ log = logging.getLogger(__name__)
class Mercurial(BaseProject): class Mercurial(BaseProject):
def base(self): def process(self):
return super(Mercurial, self).base() return False
def name(self):
return None
def tags(self): def tags(self):
tags = [] return []
return tags

View File

@ -11,8 +11,10 @@
import logging import logging
import os import os
from subprocess import Popen, PIPE
from .base import BaseProject from .base import BaseProject
from ..packages.ordereddict import OrderedDict
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -20,9 +22,42 @@ log = logging.getLogger(__name__)
class Subversion(BaseProject): class Subversion(BaseProject):
def base(self): def process(self):
return super(Subversion, self).base() self.info = self._get_info()
if 'Repository Root' in self.info:
return True
return False
def name(self):
return self.info['Repository Root'].split('/')[-1]
def _get_info(self):
info = OrderedDict()
stdout = None
try:
stdout, stderr = Popen([
'svn', 'info', os.path.realpath(self.path)
], stdout=PIPE).communicate()
except OSError:
pass
else:
if stdout:
interesting = [
'Repository Root',
'Repository UUID',
'URL',
]
for line in stdout.splitlines():
line = line.split(': ', 1)
if line[0] in interesting:
info[line[0]] = line[1]
return info
def tags(self): def tags(self):
tags = [] tags = []
for key in self.info:
if key == 'Repository UUID':
tags.append(self.info[key])
if key == 'URL':
tags.append(os.path.dirname(self.info[key]))
return tags return tags

View File

@ -12,7 +12,7 @@
from __future__ import print_function from __future__ import print_function
__title__ = 'wakatime' __title__ = 'wakatime'
__version__ = '0.1.1' __version__ = '0.1.2'
__author__ = 'Alan Hamlett' __author__ = 'Alan Hamlett'
__license__ = 'BSD' __license__ = 'BSD'
__copyright__ = 'Copyright 2013 Alan Hamlett' __copyright__ = 'Copyright 2013 Alan Hamlett'