<commit_before>#!/usr/bin/env python

"""JARVIS 2 - create widget

Usage:
  create_widget.py [-r] [-n] [NAME]

Options:
  -h --help         Show usage
  -n --dry-run      Show what would be done, but don't do anything
  -r --remove       Remove widget

"""
from __future__ import print_function

import os
import sys

from clint.textui import colored, puts
from docopt import docopt
from jinja2 import Environment, FileSystemLoader


class WidgetFactory(object):

    def __init__(self, name):
        self.name = name
        self.app_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                     '..', 'app'))
        self.widget_dir = os.path.join(self.app_path, 'static', 'widgets',
                                       self.name)
        self.job_file = os.path.join(self.app_path, 'jobs',
                                     '%s.py' % (self.name,))
        template_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                     'templates', 'widget'))
        self.env = Environment(loader=FileSystemLoader(template_path),
                               keep_trailing_newline=True)

    def _render_templates(self):
        js = self.env.get_template('widget.js').render(name=self.name)
        less = self.env.get_template('widget.less').render(name=self.name)
        html = self.env.get_template('widget.html').render(name=self.name)
        job = self.env.get_template('job.py').render(name=self.name)

        return {
            '%s.js' % (self.name,): js,
            '%s.less' % (self.name,): less,
            '%s.html' % (self.name,): html,
            '%s.py' % (self.name,): job
        }

    def _create_widget_dir(self, path):
        os.mkdir(self.widget_dir)
        puts('Created %s' % (colored.green(self.widget_dir),))

    def _write_file(self, file_path, contents):
        with open(file_path, 'w') as f:
            f.write(contents)
            puts('Created %s' % (colored.green(file_path),))

    def create_widget(self):
        contents = self._render_templates()

        if os.path.isdir(self.widget_dir):
            print('%s already exists' % (self.widget_dir,))
            sys.exit(1)
        if os.path.isfile(self.job_file):
            print('%s already exists' % (self.job_file,))
            sys.exit(1)

        self._create_widget_dir(self.widget_dir)
        for filename in contents:
            if filename.endswith('.py'):
                file_path = self.job_file
            else:
                file_path = os.path.join(self.widget_dir, filename)
            self._write_file(file_path, contents[filename])

    def _remove_file(self, file_path):
        if os.path.isfile(file_path):
            os.remove(file_path)
        else:
            os.rmdir(file_path)
        puts('Removed %s' % (colored.red(file_path),))

    def remove_widget(self):
        if os.path.isdir(self.widget_dir):
            filenames = map(lambda f, e: f + e, (self.name,) * 3,
                            ('.html', '.js', '.less'))
            filenames += [self.job_file, self.widget_dir]
            for filename in filenames:
                file_path = os.path.join(self.widget_dir, filename)
                if not os.path.exists(file_path):
                    continue
                self._remove_file(file_path)


class UselessFactory(WidgetFactory):

    def _create_widget_dir(self, path):
        puts('Would create %s' % (colored.green(self.widget_dir),))

    def _write_file(self, file_path, contents):
        puts('Would create %s' % (colored.green(file_path),))

    def _remove_file(self, file_path):
        puts('Would remove %s' % (colored.red(file_path),))


def get_factory(name, dry_run=False):
    return UselessFactory(name) if dry_run else WidgetFactory(name)

<commit_msg>Remove .pyc if it exists<commit_after>#!/usr/bin/env python

"""JARVIS 2 - create widget

Usage:
  create_widget.py [-r] [-n] [NAME]

Options:
  -h --help         Show usage
  -n --dry-run      Show what would be done, but don't do anything
  -r --remove       Remove widget

"""
from __future__ import print_function

import os
import sys

from clint.textui import colored, puts
from docopt import docopt
from jinja2 import Environment, FileSystemLoader


class WidgetFactory(object):

    def __init__(self, name):
        self.name = name
        self.app_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                     '..', 'app'))
        self.widget_dir = os.path.join(self.app_path, 'static', 'widgets',
                                       self.name)
        self.job_file = os.path.join(self.app_path, 'jobs',
                                     '%s.py' % (self.name,))
        template_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                     'templates', 'widget'))
        self.env = Environment(loader=FileSystemLoader(template_path),
                               keep_trailing_newline=True)

    def _render_templates(self):
        js = self.env.get_template('widget.js').render(name=self.name)
        less = self.env.get_template('widget.less').render(name=self.name)
        html = self.env.get_template('widget.html').render(name=self.name)
        job = self.env.get_template('job.py').render(name=self.name)

        return {
            '%s.js' % (self.name,): js,
            '%s.less' % (self.name,): less,
            '%s.html' % (self.name,): html,
            '%s.py' % (self.name,): job
        }

    def _create_widget_dir(self, path):
        os.mkdir(self.widget_dir)
        puts('Created %s' % (colored.green(self.widget_dir),))

    def _write_file(self, file_path, contents):
        with open(file_path, 'w') as f:
            f.write(contents)
            puts('Created %s' % (colored.green(file_path),))

    def create_widget(self):
        contents = self._render_templates()

        if os.path.isdir(self.widget_dir):
            print('%s already exists' % (self.widget_dir,))
            sys.exit(1)
        if os.path.isfile(self.job_file):
            print('%s already exists' % (self.job_file,))
            sys.exit(1)

        self._create_widget_dir(self.widget_dir)
        for filename in contents:
            if filename.endswith('.py'):
                file_path = self.job_file
            else:
                file_path = os.path.join(self.widget_dir, filename)
            self._write_file(file_path, contents[filename])

    def _remove_file(self, file_path):
        if os.path.isfile(file_path):
            os.remove(file_path)
        else:
            os.rmdir(file_path)
        puts('Removed %s' % (colored.red(file_path),))

    def remove_widget(self):
        if os.path.isdir(self.widget_dir):
            filenames = map(lambda f, e: f + e, (self.name,) * 3,
                            ('.html', '.js', '.less'))
            pyc_file = self.job_file + 'c'
            filenames += [self.job_file, pyc_file, self.widget_dir]
            for filename in filenames:
                file_path = os.path.join(self.widget_dir, filename)
                if not os.path.exists(file_path):
                    continue
                self._remove_file(file_path)


class UselessFactory(WidgetFactory):

    def _create_widget_dir(self, path):
        puts('Would create %s' % (colored.green(self.widget_dir),))

    def _write_file(self, file_path, contents):
        puts('Would create %s' % (colored.green(file_path),))

    def _remove_file(self, file_path):
        puts('Would remove %s' % (colored.red(file_path),))


def get_factory(name, dry_run=False):
    return UselessFactory(name) if dry_run else WidgetFactory(name)


{'fraction_non_alphanumeric': 0.13044038114859643, 'fraction_numerical': 0.0012876641771825909, 'mean_word_length': 2.0518664047151276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686089', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2396, 'n_words': 468}