Skip to content

Commit

Permalink
Add Travis CI and unit test support
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed May 14, 2015
1 parent cccb529 commit aafa73e
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 136 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ reg_replace.pyc

.DS_Store

site/*
site/*

*.pyc
14 changes: 14 additions & 0 deletions .sublimelinterrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"@python": 3,
"linters": {
"flake8": {
"@disable": false,
"ignore": "",
"max-line-length": 120
},
"pep257": {
"@disable": false,
"ignore": "D202"
}
}
}
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: python
python:
- "3.3"
install:
- pip install flake8
- pip install nose
- pip install pep257
script:
- nosetests .
- flake8 --max-line-length=120 .
- pep257 --ignore=D202 .
68 changes: 47 additions & 21 deletions rr_extended.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Reg Replace
Reg Replace.
Licensed under MIT
Copyright (c) 2011 - 2015 Isaac Muse <isaacmuse@gmail.com>
"""
Expand All @@ -11,22 +12,26 @@


class ReplaceTemplate(object):

"""Replace template."""

def __init__(self, pattern, template):
"""Initialize."""
self.__original = template
self.__back_ref = set()
self.__add_back_references(CAP_TOKEN)
self.__template = self.__escape_template(template)
self.groups, self.literals = sre_parse.parse_template(self.__template, pattern)

def get_base_template(self):
"""
Return the unmodified template before expansion.
"""
"""Return the unmodified template before expansion."""

return self.__original

def __escape_template(self, template):
"""
Escape new backreferences.
Because the new backreferences are recognized by python
we need to escape them so they come out okay.
"""
Expand All @@ -47,8 +52,9 @@ def __escape_template(self, template):

def __add_back_references(self, args):
"""
Add new backreferences, but not if they
interfere with existing ones.
Add new backreferences.
Only add if they don't interfere with existing ones.
"""

for arg in args:
Expand All @@ -57,9 +63,7 @@ def __add_back_references(self, args):
self.__back_ref.add(arg)

def get_group_index(self, index):
"""
Find and return the appropriate group index
"""
"""Find and return the appropriate group index."""

g_index = None
for group in self.groups:
Expand All @@ -70,19 +74,27 @@ def get_group_index(self, index):


class Tokens(object):

"""Tokens."""

def __init__(self, string):
"""Initialize."""

self.string = string
self.max_index = len(string) - 1
self.index = 0
self.current = None

def __iter__(self):
"""Iterate."""

return self

def __next__(self):
"""
Iterate through characters of the string.
Count \l, \L, \c, \C and \\ as a single char.
Count escaped l, L, c, C and backslash as a single char.
"""

if self.index > self.max_index:
Expand Down Expand Up @@ -113,7 +125,12 @@ def __next__(self):


class BackReferencs(object):

"""Backrefereces."""

def __init__(self, match, template):
"""Initialize."""

self.template = template
self.upper = False
self.lower = False
Expand All @@ -122,9 +139,9 @@ def __init__(self, match, template):

def next_group_boundary(self, index):
"""
Return the next match group boundaries
in relation to the index. Return 'None'
if there are no more boundaries.
Return the next match group boundaries in relation to the index.
Return 'None' if there are no more boundaries.
"""

bound = None
Expand All @@ -136,23 +153,24 @@ def next_group_boundary(self, index):

def ignore_index(self, boundary, index):
"""
If the index falls within the current group boundary,
Ignore the index.
Ignore if the index falls within the current group boundary,
return that it should be ignored.
"""

return boundary is not None and index >= boundary[0] and index < boundary[1]

def out_of_boundary(self, boundary, index):
"""
Return if the index has exceeded the right boundary.
"""
"""Return if the index has exceeded the right boundary."""

return boundary is not None and index >= boundary[1]

def span_upper(self, i):
"""
Uppercase the next range of characters until end marker is found.
Ignore \E if found in a group bondary.
Ignore excaped E if found in a group bondary.
"""

try:
Expand All @@ -171,7 +189,8 @@ def span_upper(self, i):
def span_lower(self, i):
"""
Lowercase the next range of characters until end marker is found.
Ignore \E if found in a group bondary.
Ignore escaped E if found in a group bondary.
"""

try:
Expand All @@ -190,6 +209,7 @@ def span_lower(self, i):
def single_lower(self, i):
"""
Lowercase the next character.
If none found, allow spanning to the next group.
"""

Expand All @@ -206,6 +226,7 @@ def single_lower(self, i):
def single_upper(self, i):
"""
Uppercase the next character.
If none found, allow spanning to the next group.
"""

Expand All @@ -222,6 +243,7 @@ def single_upper(self, i):
def _expand_string(self, match):
"""
Using the template, expand the string.
Keep track of the match group bondaries for later.
"""

Expand All @@ -248,6 +270,8 @@ def _expand_string(self, match):

def expand_titles(self):
"""
Exapnd titlecases.
Walk the expanded template string and process
the new added backreferences and apply the associated
action.
Expand Down Expand Up @@ -282,8 +306,10 @@ def expand_titles(self):


def replace(m, template):
"""
Replace event. Using the template, scan for (\c | \C.*?\E | \l | \L.*?\E).
r"""
Replace event.
Using the template, scan for (\c | \C.*?\E | \l | \L.*?\E).
(c|C) are capital/upper case. (l|L) is lower case.
\c and \l are applied to the next character. While \C and \L are applied to
all characters until either the end of the string is found, or the end marker \E
Expand Down
4 changes: 4 additions & 0 deletions rr_modules/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""A simple example plugin."""


def replace(m, **kwargs):
"""Replace with groups."""
text = "Here are your groups: "
for group in m.groups():
if group is not None:
Expand Down
12 changes: 11 additions & 1 deletion rr_notify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Reg Replace
Reg Replace.
Licensed under MIT
Copyright (c) 2011 - 2015 Isaac Muse <isaacmuse@gmail.com>
"""
Expand All @@ -8,12 +9,19 @@
from SubNotify.sub_notify import SubNotifyIsReadyCommand as Notify
except:
class Notify:

"""Notify fallback."""

@classmethod
def is_ready(cls):
"""Return false to effectively disable SubNotify."""

return False


def notify(msg):
"""Notify msg."""

settings = sublime.load_settings("reg_replace.sublime-settings")
if settings.get("use_sub_notify", False) and Notify.is_ready():
sublime.run_command("sub_notify", {"title": "RegReplace", "msg": msg})
Expand All @@ -22,6 +30,8 @@ def notify(msg):


def error(msg):
"""Error msg."""

settings = sublime.load_settings("reg_replace.sublime-settings")
if settings.get("use_sub_notify", False) and Notify.is_ready():
sublime.run_command("sub_notify", {"title": "RegReplace", "msg": msg, "level": "error"})
Expand Down
16 changes: 15 additions & 1 deletion rr_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Reg Replace
Reg Replace.
Licensed under MIT
Copyright (c) 2011 - 2015 Isaac Muse <isaacmuse@gmail.com>
"""
Expand All @@ -12,21 +13,28 @@


def sublime_format_path(pth):
"""Format path for Sublime."""
m = re.match(r"^([A-Za-z]{1}):(?:/|\\)(.*)", pth)
if sublime.platform() == "windows" and m is not None:
pth = m.group(1) + "/" + m.group(2)
return pth.replace("\\", "/")


class Plugin(object):

"""Load plugins for RegReplace."""

loaded = []

@classmethod
def purge(cls):
"""Purge list of loaded plugins."""
cls.loaded = []

@classmethod
def get_module(cls, module_name, path_name):
"""Get the requested module."""

module = None
try:
module = sys.modules[module_name]
Expand All @@ -36,6 +44,8 @@ def get_module(cls, module_name, path_name):

@classmethod
def load_module(cls, module_name, path_name):
"""Load the requested module."""

module = imp.new_module(module_name)
sys.modules[module_name] = module
exec(
Expand All @@ -50,6 +60,8 @@ def load_module(cls, module_name, path_name):

@classmethod
def load(cls, module_name, loaded=None):
"""Load module."""

if module_name.startswith("rr_modules."):
path_name = join("Packages", "RegReplace", normpath(module_name.replace('.', '/')))
else:
Expand All @@ -64,4 +76,6 @@ def load(cls, module_name, loaded=None):

@classmethod
def load_from(cls, module_name, attribute):
"""Load from a module."""

return getattr(cls.load_module(module_name), attribute)
Loading

0 comments on commit aafa73e

Please sign in to comment.