aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2014-08-22 20:28:43 +0200
committerLaurent Bachelier <laurent@bachelier.name>2014-08-22 20:28:43 +0200
commit93711e7ef41b9870550713df9b4e3704f57d5815 (patch)
tree738145e7b0c84872cae8736987a3e30d57faaa08
parentNot required for 2.6+ (diff)
downloadconfman-93711e7ef41b9870550713df9b4e3704f57d5815.tar.xz
Modernize the code for 2.6+
Might even work with Python 3, but not tested
-rw-r--r--confman.py50
-rwxr-xr-xexample/sync.py9
2 files changed, 32 insertions, 27 deletions
diff --git a/confman.py b/confman.py
index b409722..71c26e5 100644
--- a/confman.py
+++ b/confman.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import, print_function
+
import os
import os.path as osp
import re
@@ -99,20 +101,20 @@ class SymlinkAction(Action):
# if the old link is broken or incorrect
if not osp.exists(dest) or not osp.samefile(dest, source):
os.unlink(dest)
- print "Link target altered"
+ print("Link target altered")
else:
return
# if the destination is not a link, but has same contents as source
elif self.FORCE_SAME and self.same_contents():
os.unlink(dest)
- print "Link target was a file with same contents"
+ print("Link target was a file with same contents")
else:
self._makedirs()
relsource = osp.normpath(osp.relpath(source,
osp.join(self.config.dest, self.relpath)))
os.symlink(relsource, dest)
- print "Created new link: %s => %s" % (dest, source)
+ print("Created new link: %s => %s" % (dest, source))
class TextAction(Action):
@@ -138,12 +140,12 @@ class TextAction(Action):
raise ActionException(self, "Destination is a link")
else:
self._makedirs()
- with open(dest, "a+") as destfile:
+ with open(dest, 'a+') as destfile:
if destfile.read() != self.text:
if exists and self.ONCE:
- print "File already exists, not updated: %s" % dest
+ print("File already exists, not updated: %s" % dest)
else:
- print "Updated file contents: %s" % dest
+ print("Updated file contents: %s" % dest)
destfile.truncate(0)
destfile.write(self.text)
@@ -152,12 +154,12 @@ class TextAction(Action):
class CopyAction(TextAction):
- MATCHED = re.compile(r"\.copy$")
+ MATCHED = re.compile(r'\.copy$')
@classmethod
def matches(cls, filename):
if cls.MATCHED.search(filename):
- return cls.MATCHED.sub("", filename)
+ return cls.MATCHED.sub('', filename)
return False
def check(self):
@@ -171,12 +173,12 @@ class CopyAction(TextAction):
class CopyOnceAction(CopyAction):
ONCE = True
- MATCHED = re.compile(r"\.copyonce$")
+ MATCHED = re.compile(r'\.copyonce$')
@classmethod
def matches(cls, filename):
if cls.MATCHED.search(filename):
- return cls.MATCHED.sub("", filename)
+ return cls.MATCHED.sub('', filename)
return False
@@ -185,16 +187,16 @@ class EmptyAction(CopyOnceAction):
Ensures the destination file exists.
Creates an empty one if not.
"""
- MATCHED = re.compile(r"\.empty$")
+ MATCHED = re.compile(r'\.empty$')
@classmethod
def matches(cls, filename):
if cls.MATCHED.search(filename):
- return cls.MATCHED.sub("", filename)
+ return cls.MATCHED.sub('', filename)
return False
def check(self):
- self.text = ""
+ self.text = ''
def __repr__(self):
return "%s: EMPTY => %s" % (self.__class__.__name__, self.dest)
@@ -240,12 +242,12 @@ class IgnoreForwarder(Forwarder):
class ProgrammableAction(Action):
- MATCHED = re.compile(r"\.p\.py$")
+ MATCHED = re.compile(r'\.p\.py$')
@classmethod
def matches(cls, filename):
if cls.MATCHED.search(filename):
- return cls.MATCHED.sub("", filename)
+ return cls.MATCHED.sub('', filename)
return False
def get_env(self):
@@ -256,7 +258,7 @@ class ProgrammableAction(Action):
options = self.config.options # NOQA
def redirect(filename):
- raise SymlinkForwarder("_%s" % filename)
+ raise SymlinkForwarder('_%s' % filename)
def empty():
raise EmptyForwarder()
@@ -266,8 +268,8 @@ class ProgrammableAction(Action):
class ConfmanTemplate(Template):
def render(self, _strict=True, _warning=True, **kwargs):
- if _warning and "warning" not in kwargs:
- kwargs["warning"] = "WARNING: Do not edit this file, edit the template instead."
+ if _warning and 'warning' not in kwargs:
+ kwargs['warning'] = "WARNING: Do not edit this file, edit the template instead."
if _strict:
raise TextForwarder(self.substitute(kwargs))
raise TextForwarder(self.safe_substitute(kwargs))
@@ -286,10 +288,10 @@ class ProgrammableAction(Action):
def check(self):
source = self.source_path()
try:
- with open(source, "r") as file:
+ with open(source, 'r') as file:
exec_env = self.get_env()
- exec compile(file.read(), source, "exec") in exec_env
- except Forwarder, e:
+ exec(compile(file.read(), source, 'exec'), exec_env)
+ except Forwarder as e:
self.proxy = e.get_proxy(self)
else:
raise ActionException(self, "Unknown result")
@@ -307,7 +309,7 @@ class ProgrammableAction(Action):
class IgnoreAction(Action):
- IGNORED = re.compile(r"_|\.git$|\.gitignore$")
+ IGNORED = re.compile(r'_|\.git$|\.gitignore$')
@classmethod
def matches(cls, filename):
@@ -328,7 +330,7 @@ class ConfigSource(object):
CopyOnceAction,
SymlinkAction,
)
- ACT_AS_FILE = re.compile(r"\.F$")
+ ACT_AS_FILE = re.compile(r'\.F$')
def __init__(self, source, dest, classes=None, options=None):
# handle '~'
@@ -342,7 +344,7 @@ class ConfigSource(object):
"""
Gather files and synchronize them.
"""
- print "Synchronizing files from %s to %s..." % (self.source, self.dest)
+ print("Synchronizing files from %s to %s..." % (self.source, self.dest))
self.analyze()
self.execute()
diff --git a/example/sync.py b/example/sync.py
index 7cf0f72..b2ff7ea 100755
--- a/example/sync.py
+++ b/example/sync.py
@@ -1,11 +1,14 @@
#!/usr/bin/env python
+from __future__ import absolute_import, print_function
+
import os
import sys
+
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
-
from confman import ConfigSource
+
options = {
'tags': ['desktop'],
'hostname': 'test',
@@ -16,5 +19,5 @@ samples_path = os.path.join(os.path.dirname(__file__), 'src')
c = ConfigSource(samples_path, "/tmp/dotfiles-test", None, options)
c.sync()
-print
-print repr(c)
+print()
+print(repr(c))