<commit_before># -*- coding: utf-8 -*-

import argparse
from colorama import init, Fore
from argparse import ArgumentParser
from .manager import OssFileManager, VERSION
from .exception import *
import yaml
import itertools


class Yui:

    ATTR_FILE = ".yui"

    def __init__(self, config_file_path):
        with open(config_file_path, 'r') as f:
            self.config = yaml.load(f)
            self.fm = OssFileManager(self.config["auth_key"],
                                     self.config["auth_key_secret"],
                                     self.config["endpoint"],
                                     self.config["bucket_name"],
                                     proxies=self.config["proxies"])

        try:
            with open(self.ATTR_FILE, 'r') as f:
                self.attrs = yaml.load(f)
                self.root = self.attrs["root"] if self.attrs and self.attrs["root"] else ""
        except FileNotFoundError:
            with open(self.ATTR_FILE, "w") as f:
                self.attrs = {"root": ""}
                yaml.dump(self.attrs, f)
                self.root = ""

        self.args = None
        self.methods = ("cd", "ls", "ul", "dl", "cp", "mv", "rm")

        self.parser = ArgumentParser(description="YuiOss console application ver " + VERSION)
        self.parser.set_defaults(verbose=True)
        self.parser.add_argument("-v", "--verbose", dest="verbose", action="store_true")
        self.parser.add_argument("-q", "--quiet", dest="verbose", action="store_false")
        self.parser.add_argument("-a", "--all", action="store_true")
        self.parser.add_argument("-r", "--recursive", action="store_true")
        self.parser.add_argument("method", choices=self.methods, nargs=1)
        self.parser.add_argument("args", nargs=argparse.ZERO_OR_MORE)

        init()

    def run(self):
        self.args = self.parser.parse_args()
        method = self.args.method[0]
        if method in self.methods:
            self.__getattribute__(method)()

    def update_attr(self):
        with open(self.ATTR_FILE, 'w+') as f:
            yaml.dump(self.attrs, f)

    def on_success(self, method, src, dest, result):
        if self.args.verbose:
            print(Fore.GREEN + str(method) + " success: " +
                  src + ("" if not dest else " --> " + dest))

    def on_error(self, method, src, dest, result):
        print(Fore.RED + str(method) + " success: " +
              src + ("" if not dest else " --> " + dest))

    def cd(self):
        """
        change oss current directory
        :param path: new current directory, will be considered as absolute path if starts with '/'
        :return:
        """
        if not len(self.args.args):
            self.root = ""
        else:
            path = self.args.args[0]
            if not self.fm.is_dir(path):
                print(Fore.RED + "cd path should be a directory")
                return
            path = self.fm.norm_path(path)
            self.root = path[1:] if path.startswith(self.fm.SEP) else self.root + path
        self.attrs["root"] = self.root
        self.update_attr()
        print(Fore.GREEN + "current directory changed to: /" + self.root)

    def ls(self):
        """
        list sub directories and files of current directory
        :return:
        """
        # TODO: needs optimization, remove common prefixes of results, don't list root itself
        files = [obj.key for obj in self.fm.list_dir(self.root, self.args.all)]
        print((Fore.GREEN + "listing {0} files in /{1}:\n".format(len(files), self.root) +
               '\t'.join(files)) if len(files)
              else (Fore.YELLOW + "current directory: /{0} is empty.".format(self.root)))

    def ul(self):
        """
        upload
        :return:<commit_msg>fix issue #2<commit_after># -*- coding: utf-8 -*-

import argparse
from colorama import init, Fore
from argparse import ArgumentParser
from .manager import OssFileManager, VERSION
from .exception import *
import yaml
import itertools


class Yui:

    ATTR_FILE = ".yui"

    def __init__(self, config_file_path):
        with open(config_file_path, 'r') as f:
            self.config = yaml.load(f)
            self.fm = OssFileManager(self.config["auth_key"],
                                     self.config["auth_key_secret"],
                                     self.config["endpoint"],
                                     self.config["bucket_name"],
                                     proxies=self.config["proxies"])

        try:
            with open(self.ATTR_FILE, 'r') as f:
                self.attrs = yaml.load(f)
                self.root = self.attrs["root"] if self.attrs and self.attrs["root"] else ""
        except FileNotFoundError:
            with open(self.ATTR_FILE, "w") as f:
                self.attrs = {"root": ""}
                yaml.dump(self.attrs, f)
                self.root = ""

        self.args = None
        self.methods = ("cd", "ls", "ul", "dl", "cp", "mv", "rm")

        self.parser = ArgumentParser(description="YuiOss console application ver " + VERSION)
        self.parser.set_defaults(verbose=True)
        self.parser.add_argument("-v", "--verbose", dest="verbose", action="store_true")
        self.parser.add_argument("-q", "--quiet", dest="verbose", action="store_false")
        self.parser.add_argument("-a", "--all", action="store_true")
        self.parser.add_argument("-r", "--recursive", action="store_true")
        self.parser.add_argument("method", choices=self.methods, nargs=1)
        self.parser.add_argument("args", nargs=argparse.ZERO_OR_MORE)

        init()

    def run(self):
        self.args = self.parser.parse_args()
        method = self.args.method[0]
        if method in self.methods:
            self.__getattribute__(method)()

    def update_attr(self):
        with open(self.ATTR_FILE, 'w+') as f:
            yaml.dump(self.attrs, f)

    def on_success(self, method, src, dest, result):
        if self.args.verbose:
            print(Fore.GREEN + str(method) + " success: " +
                  src + ("" if not dest else " --> " + dest))

    def on_error(self, method, src, dest, result):
        print(Fore.RED + str(method) + " success: " +
              src + ("" if not dest else " --> " + dest))

    def cd(self):
        """
        change oss current directory
        :param path: new current directory, will be considered as absolute path if starts with '/'
        :return:
        """
        if not len(self.args.args):
            self.root = ""
        else:
            path = self.args.args[0]
            if not self.fm.is_dir(path):
                print(Fore.RED + "cd path should be a directory")
                return
            path = self.fm.norm_path(path)
            self.root = path[1:] if path.startswith(self.fm.SEP) else self.root + path
        self.attrs["root"] = self.root
        self.update_attr()
        print(Fore.GREEN + "current directory changed to: /" + self.root)

    def ls(self):
        """
        list sub directories and files of current directory
        :return:
        """
        files = [obj.key.replace(self.root, '') for obj in self.fm.list_dir(self.root, self.args.all) if obj.key != self.root]
        print((Fore.GREEN + "listing {0} files in /{1}:\n".format(len(files), self.root) +
               '\t'.join(files)) if len(files)
              else (Fore.YELLOW + "current directory: /{0} is empty.".format(self.root)))

    def ul(self):
        """
        upload
        :return:
{'fraction_non_alphanumeric': 0.13836561422127885, 'fraction_numerical': 0.0022552401167418414, 'mean_word_length': 1.9391812865497076, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4374259', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2188, 'n_words': 532}