summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2014-11-11 16:33:13 +0100
committerJan Pokorný <jpokorny@redhat.com>2014-11-14 22:11:05 +0100
commit7cb961c7c94b64664d6e453ebc71a32b92399bfd (patch)
tree7314d1d5ddd1f04849365e69a1e8b5eac3f860b9
parentede273c2e7b71c7408d2842aaa41cf0d28b4b54d (diff)
downloadclufter-7cb961c7c94b64664d6e453ebc71a32b92399bfd.tar.gz
clufter-7cb961c7c94b64664d6e453ebc71a32b92399bfd.tar.xz
clufter-7cb961c7c94b64664d6e453ebc71a32b92399bfd.zip
formats/command: parsing/linearizing a command + tests
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
-rw-r--r--.gitignore1
-rw-r--r--formats/command.py50
-rw-r--r--tests/formats/__init__.py0
-rw-r--r--tests/formats/command.py53
4 files changed, 104 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 55bd5d7..0f1d4f3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,3 +22,4 @@
!/.gitignore
!/.travis.yml
!/tests/XMLFormat-walk/
+!/tests/formats/
diff --git a/formats/command.py b/formats/command.py
new file mode 100644
index 0000000..6613dde
--- /dev/null
+++ b/formats/command.py
@@ -0,0 +1,50 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2014 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""Format representing merged/isolated (1/2 levels) of single command to exec"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+from ..format import SimpleFormat
+from ..protocol import Protocol
+from ..utils_func import apply_intercalate
+
+
+class command(SimpleFormat):
+ native_protocol = SEPARATED = Protocol('separated')
+ BYTESTRING = SimpleFormat.BYTESTRING
+ MERGED = Protocol('merged')
+
+ @SimpleFormat.producing(BYTESTRING)
+ def get_bytestring(self, protocol):
+ """Return command as canonical single string"""
+ # try to look (indirectly) if we have a file at hand first
+ ret = super(command, self).get_bytestring(self.BYTESTRING)
+ if ret is not None:
+ return ret
+
+ # fallback
+ return ' '.join(self.MERGED(protect_safe=True))
+
+ @SimpleFormat.producing(SEPARATED, protect=True)
+ def get_separated(self, protocol):
+ ret = self.MERGED(protect_safe=True)
+ newret, acc = [], []
+ for i in ret:
+ if i.startswith('-') and len(i) > 1:
+ if acc:
+ newret.append(tuple(acc))
+ acc = [i]
+ else:
+ acc.append(i)
+ # expect that, by convention, option takes at most a single argument
+ newret.extend(filter(bool, (tuple(acc[:2]), tuple(acc[2:]))))
+ return newret
+
+ @SimpleFormat.producing(MERGED, protect=True)
+ def get_merged(self, protocol):
+ # try to look (indirectly) if we have "separated" at hand first
+ if self.BYTESTRING in self._representations: # break the possible loop
+ from shlex import split
+ return split(self.BYTESTRING())
+ return apply_intercalate(self.SEPARATED(protect_safe=True))
diff --git a/tests/formats/__init__.py b/tests/formats/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/formats/__init__.py
diff --git a/tests/formats/command.py b/tests/formats/command.py
new file mode 100644
index 0000000..e820d6e
--- /dev/null
+++ b/tests/formats/command.py
@@ -0,0 +1,53 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2014 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""Testing format"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+from os.path import join, dirname as d; execfile(join(d(d((__file__))), '_go'))
+
+from unittest import TestCase
+
+from .formats.command import command
+
+
+class FormatsCommandTestCase(TestCase):
+ def testBytestringToMerged(self):
+ c = command('bytestring', 'cut -f 1 -d @ emails.txt')
+ #print c('merged')
+ self.assertEqual(c.MERGED(),
+ ['cut', '-f', '1', '-d', '@', 'emails.txt'])
+
+ def testMergedToBytestring(self):
+ c = command('merged', ['cut', '-f', '1', '-d', '@', 'emails.txt'])
+ #print c('bytestring')
+ self.assertEqual(c.BYTESTRING(), 'cut -f 1 -d @ emails.txt')
+
+ def testBytestringToSeparated(self):
+ c = command('bytestring', 'cut -f 1 -d @ emails.txt')
+ #print c('separated')
+ self.assertEqual(c.SEPARATED(),
+ [('cut',), ('-f', '1'), ('-d', '@'), ('emails.txt', )])
+
+ def testSeparatedToBytestring(self):
+ c = command('separated',
+ [('cut',), ('-f', '1'), ('-d', '@'), ('emails.txt', )])
+ #print c('bytestring')
+ self.assertEqual(c.BYTESTRING(), 'cut -f 1 -d @ emails.txt')
+
+ def testMergedToSeparated(self):
+ c = command('merged', ['cut', '-f', '1', '-d', '@', 'emails.txt'])
+ #print c('separated')
+ self.assertEqual(c.SEPARATED(),
+ [('cut',), ('-f', '1'), ('-d', '@'), ('emails.txt', )])
+
+ def testSeparatedToMerged(self):
+ c = command('separated',
+ [('cut',), ('-f', '1'), ('-d', '@'), ('emails.txt', )])
+ #print c('merged')
+ self.assertEqual(c.MERGED(),
+ ['cut', '-f', '1', '-d', '@', 'emails.txt'])
+
+
+from os.path import join, dirname as d; execfile(join(d(d(__file__)), '_gone'))