Skip to content

Commit

Permalink
Add code to support single file normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmogar committed Jun 27, 2017
1 parent d2563c2 commit 67260d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
18 changes: 7 additions & 11 deletions cucco/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, config, cucco):
self._observer = None
self._watch = False

def _process_file(self, path):
def process_file(self, path):
"""Process a file applying normalizations.
Get a file as input and generate a new file with the
Expand All @@ -91,10 +91,9 @@ def _process_file(self, path):

with open(output_path, 'w') as file:
for line in lines_generator(path):
file.write('%s\n' % self._cucco.normalize(line))
file.write('%s\n' % self._cucco.normalize(line.decode('utf-8')))

if self._config.debug:
self._logger.debug('Created file "%s"', output_path)
self._logger.debug('Created file "%s"', output_path)

def process_files(self, path, recursive=False):
"""Apply normalizations over all files in the given directory.
Expand All @@ -112,7 +111,7 @@ def process_files(self, path, recursive=False):

for (path, file) in files_generator(path, recursive):
if not file.endswith(BATCH_EXTENSION):
self._process_file(os.path.join(path, file))
self.process_file(os.path.join(path, file))

def stop_watching(self):
"""Stop watching for files.
Expand Down Expand Up @@ -170,7 +169,6 @@ class FileHandler(FileSystemEventHandler):
def __init__(self, batch):
"""Inits Batch class."""
self._batch = batch
self._debug = batch._config.debug
self._logger = batch._logger

def _process_event(self, event):
Expand All @@ -186,16 +184,15 @@ def _process_event(self, event):
if (not event.is_directory and
not event.src_path.endswith(BATCH_EXTENSION)):
self._logger.info('Detected file change: %s', event.src_path)
self._batch._process_file(event.src_path)
self._batch.process_file(event.src_path)

def on_created(self, event):
"""Function called everytime a new file is created.
Args:
event: Event to process.
"""
if self._debug:
self._logger.debug('Detected create event on watched path: %s', event.src_path)
self._logger.debug('Detected create event on watched path: %s', event.src_path)

self._process_event(event)

Expand All @@ -205,7 +202,6 @@ def on_modified(self, event):
Args:
event: Event to process.
"""
if self._debug:
self._logger.debug('Detected modify event on watched path: %s', event.src_path)
self._logger.debug('Detected modify event on watched path: %s', event.src_path)

self._process_event(event)
13 changes: 10 additions & 3 deletions cucco/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import absolute_import

import click
import os
import sys

import cucco.logging as logging
Expand Down Expand Up @@ -30,10 +31,16 @@ def batch(ctx, path, recursive, watch):
"""
batch = Batch(ctx.obj['config'], ctx.obj['cucco'])

if watch:
batch.watch(path, recursive)
if os.path.exists(path):
if watch:
batch.watch(path, recursive)
elif os.path.isfile(path):
batch.process_file(path)
else:
batch.process_files(path, recursive)
else:
batch.process_files(path, recursive)
click.echo('Error: Specified path doesn\'t exists', err=True)
sys.exit(-1)

@click.command()
@click.argument('text', required=False)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_lines_generator(self, batch):
'target file should contain "Line 3"'

def test_process_file(self, batch):
batch._process_file('%s/batch_one_line' % BATCH_PATH)
batch.process_file('%s/batch_one_line' % BATCH_PATH)
assert os.path.isfile('%s/batch_one_line.cucco' % BATCH_PATH), \
'normalized file should have been created'

Expand Down

0 comments on commit 67260d8

Please sign in to comment.