Ejemplo n.º 1
0
 def initialize_keys(self, arg, split_chr='|'):
     """
     Initializes keys from string or sequence.
     From string:
          1) String is converted to list, split by 'split_chr' argument
     From list:
          2) If list item has two subitems, it will be treated as a key-value pair
             E.g: [['key', 'value'], ['key2', 'value2']] will set keys with corresponding values
          3) Otherwise, it will be treated as a list of keys
             E.g.: [['key1', 'key2', 'key3']] will instantiate keys as new Dicts
          4) If list item is not a sequence, it will be converted to a list as per Example 1
             E.g.: ['key1', 'key2', 'key3'] will act similarly to Example 3
          5) Exception: If list item is a dict, it will be handled via update.update_dict
     """
     if not is_seq_type(arg):
         arg = item_to_list(arg, split_chr=split_chr)
     for items in arg:
         if is_dict_type(items):
             self.update(items)
             continue
         if not is_seq_type(items):
             items = item_to_list(items, split_chr=split_chr)
         if len(items) is 1:
             self[items[0]]
         elif len(items) is 2:
             self[items[0]] = items[1]
         else:
             self.update_seq(items)
Ejemplo n.º 2
0
 def get_kwargs(self, *args_list, **kwargs):
     method_name='get_kwargs'
     lst = []
     for args in args_list:
         if isinstance(args, dict):
             args = item_to_list(args)
             args_dict = args
         if isinstance(args, list):
             lst += [args[i * 2:i * 2 + 2] for i in range(0, len(args) / 2)]
         else:
             lst += [[arg, None] for arg in item_to_list(args)]
     return self.process_kwargs(get_args=lst, **kwargs)
Ejemplo n.º 3
0
 def __init__(self, *args, **kwargs):
     if self._attr_order_ is None:
         self._attr_order_ = []
     if self._additional_attr_ is None:
         self._additional_attr_ = set()
     self._sql_columns_ = item_to_list(self._sql_columns_, chrs=' ,;')
     self._attr_order_ = item_to_list(self._attr_order_, chrs=' ,;')
     self._additional_attr_ = item_to_set(self._additional_attr_, chrs=' ,;')
     args = list(args)
     if args and self.setFromKeyedObject(args[0]):
         del args[0]
     self.setFromListByDefaultOrder(args)
     self.setFromKeyedObject(kwargs)
Ejemplo n.º 4
0
 def set_kwargs(self, set_list=None, set_dict=None, func_kwargs=None, name=None, delete_from_kwargs=None, *args, **kwargs):
     method_name='set_kwargs'
     new_args = []
     lst, dct = self.__get_args_and_kwargs(set_list, set_dict, allow_cls_override=False)
     if isinstance(lst, list):
         dct.update({lst[i * 2]: lst[i * 2 + 1] for i in range(0, len(lst) / 2)})
         lst = []
     for arg in args:
         new_args += item_to_list(arg, False)
     dct.update({key: None for key in item_to_list(lst, chrs=',') + new_args})
     dct.update(kwargs)
     dct_items = dct.items()
     processed_kwargs = self.process_kwargs(func_kwargs=func_kwargs, set_dict=dct, name=name, delete_from_kwargs=delete_from_kwargs).items()
     return self.process_kwargs(func_kwargs=func_kwargs, set_dict=dct, name=name, delete_from_kwargs=delete_from_kwargs)
Ejemplo n.º 5
0
 def __init__(self, *args, **kwargs):
     if self._attr_order_ is None:
         self._attr_order_ = []
     if self._additional_attr_ is None:
         self._additional_attr_ = set()
     self._sql_columns_ = item_to_list(self._sql_columns_, chrs=' ,;')
     self._attr_order_ = item_to_list(self._attr_order_, chrs=' ,;')
     self._additional_attr_ = item_to_set(self._additional_attr_,
                                          chrs=' ,;')
     args = list(args)
     if args and self.setFromKeyedObject(args[0]):
         del args[0]
     self.setFromListByDefaultOrder(args)
     self.setFromKeyedObject(kwargs)
Ejemplo n.º 6
0
def log_banner(title,
               filename=None,
               length=ANKNOTES.FORMATTING.BANNER_MINIMUM,
               append_newline=True,
               timestamp=False,
               chr='-',
               center=True,
               clear=True,
               crosspost=None,
               prepend_newline=False,
               *args,
               **kwargs):
    if crosspost is not None:
        for cp in item_to_list(crosspost, False):
            log_banner(
                title, cp,
                **DictCaseInsensitive(
                    kwargs,
                    locals(),
                    delete='title crosspost kwargs args filename'))
    if length is 0:
        length = ANKNOTES.FORMATTING.LINE_LENGTH + 1
    if center:
        title = title.center(length - (
            ANKNOTES.FORMATTING.TIMESTAMP_PAD_LENGTH if timestamp else 0))
    if prepend_newline:
        log_blank(filename, **kwargs)
    log(chr * length, filename, clear=clear, timestamp=False, **kwargs)
    log(title, filename, timestamp=timestamp, **kwargs)
    log(chr * length, filename, timestamp=False, **kwargs)
    if append_newline:
        log_blank(filename, **kwargs)
Ejemplo n.º 7
0
 def __args_to_list(self, func_args):
     if not is_str(func_args):
         return list(func_args)
     lst = []
     for arg in item_to_list(func_args, chrs=','):
         lst += [arg] + [None]
     return lst
Ejemplo n.º 8
0
 def process_args(self, arg_list=None, func_args=None, func_kwargs=None, update_cls_args=True):
     method_name='process_args'
     arg_list = item_to_list(arg_list) if arg_list else self.__func_args_list
     cls_args = func_args is None
     cls_kwargs = func_kwargs is None
     func_args, func_kwargs = self.__get_args_and_kwargs(func_args, func_kwargs)
     arg_error = ''
     if not func_args:
         return func_args, func_kwargs
     for i in range(0, len(arg_list)):
         if len(func_args) is 0:
             break
         arg = arg_list[i]
         if arg in func_kwargs and not self.__override_kwargs:
             formats = (caller_name(return_string=True), arg)
             raise TypeError("Anknotes.Args: %s() got multiple arguments for keyword argument '%s'" % formats)
         func_kwargs[arg] = func_args[0]
         del func_args[0]
     else:
         if self.__require_all_args:
             arg_error = 'least'
     if func_args and self.__limit_max_args:
         arg_error = 'most'
     if arg_error:
         formats = (caller_name(return_string=True), arg_error, len(arg_list), '' if arg_list is 1 else 's', len(func_args))
         raise TypeError('Anknotes.Args: %s() takes at %s %d argument%s (%d given)' % formats)
     if cls_args and update_cls_args:
         self.__func_args = func_args
     if cls_kwargs and update_cls_args:
         self.__func_kwargs = func_kwargs
     return func_args, func_kwargs
Ejemplo n.º 9
0
    def wrap_filename(self,
                      filename=None,
                      final_suffix='',
                      wrap_fn_auto_header=True,
                      crosspost=None,
                      **kwargs):
        if filename is None:
            filename = self.default_filename
        if self.base_path is not None:
            filename = os.path.join(self.base_path,
                                    filename if filename else '')
        if self.path_suffix is not None:
            i_asterisk = filename.find('*')
            if i_asterisk > -1:
                final_suffix += filename[i_asterisk + 1:]
                filename = filename[:i_asterisk]
            filename += self.path_suffix + final_suffix
        if crosspost is not None:
            crosspost = [
                self.wrap_filename(cp)[0]
                for cp in item_to_list(crosspost, False)
            ]
            kwargs['crosspost'] = crosspost

        if wrap_fn_auto_header and self.auto_header and self.default_banner and not os.path.exists(
                get_log_full_path(filename)):
            log_banner(self.default_banner, filename)
        return filename, kwargs
Ejemplo n.º 10
0
def write_file_contents(content, full_path, clear=False, try_encode=True, do_print=False, print_timestamp=True,
                        print_content=None, wfc_timestamp=True, wfc_crosspost=None, get_log_full_path=None, **kwargs):
    all_args = locals()
    if wfc_crosspost:
        del all_args['kwargs'], all_args['wfc_crosspost'], all_args['content'], all_args['full_path']
        all_args.update(kwargs)
        for cp in item_to_list(wfc_crosspost):
            write_file_contents(content, cp, **all_args)
    orig_path = full_path
    if not os.path.exists(os.path.dirname(full_path)):
        if callable(get_log_full_path):
            full_path = get_log_full_path(full_path)
            if full_path is False:
                return
        else:
            if not filter_logs(full_path):
                return
            full_path = os.path.abspath(os.path.join(FOLDERS.LOGS, full_path + '.log'))
            base_path = os.path.dirname(full_path)
            if not os.path.exists(base_path):
                os.makedirs(base_path)
            if wfc_timestamp:
                print_content = content
                content = '[%s]: ' % datetime.now().strftime(ANKNOTES.DATE_FORMAT) + content
    with open(full_path, 'w+' if clear else 'a+') as fileLog:
        try:
            print>> fileLog, content
        except UnicodeEncodeError:
            content = encode(content)
            print>> fileLog, content
    if do_print:
        print content if print_timestamp or not print_content else print_content
Ejemplo n.º 11
0
 def _is_stmt_(sql, stmts=None):
     s = sql.strip().lower()
     stmts = ["insert", "update", "delete", "drop", "create", "replace"] + item_to_list(stmts)
     for stmt in stmts:
         if s.startswith(stmt):
             return True
     return False
Ejemplo n.º 12
0
 def BannerHeader(self, append_newline=False, filename=None, crosspost=None, bh_wrap_filename=True, **kw):
     if filename is None:
         filename = ''
     if bh_wrap_filename:
         filename = self.Label + filename
         if crosspost is not None:
             crosspost = [self.Label + cp for cp in item_to_list(crosspost, False)]
     log_banner(self.ActionNumeric.upper(), do_print=self.__do_print, **DictCaseInsensitive(kw, locals(), delete='self kw bh_wrap_filename cp'))
Ejemplo n.º 13
0
 def _get_kwarg_(self, kwargs, keys, default=None, replace_none_type=True, **kw):
     retval = replace_none_type and default or None
     for key in item_to_list(keys):
         key = self._key_transform_(key, kwargs)
         if key not in kwargs:
             continue
         val = kwargs[key]
         retval = val or retval
         del kwargs[key]
     return retval
Ejemplo n.º 14
0
def crosspost_log(content, filename=None, crosspost_to_default=False, crosspost=None, do_show_tooltip=False, **kwargs):
    if crosspost_to_default and filename:
        summary = " ** %s%s: " % ('' if filename.upper() == 'ERROR' else 'CROSS-POST TO ', filename.upper()) + content
        log(summary[:200], **kwargs)
    if do_show_tooltip:
        show_tooltip(content)
    if not crosspost:
        return
    for fn in item_to_list(crosspost):
        log(content, fn, **kwargs)
Ejemplo n.º 15
0
 def _get_kwarg_(self,
                 kwargs,
                 keys,
                 default=None,
                 replace_none_type=True,
                 **kw):
     retval = replace_none_type and default or None
     for key in item_to_list(keys):
         key = self._key_transform_(key, kwargs)
         if key not in kwargs:
             continue
         val = kwargs[key]
         retval = val or retval
         del kwargs[key]
     return retval
Ejemplo n.º 16
0
def log_banner(title, filename=None, length=ANKNOTES.FORMATTING.BANNER_MINIMUM, append_newline=True, timestamp=False,
               chr='-', center=True, clear=True, crosspost=None, prepend_newline=False, *args, **kwargs):
    if crosspost is not None:
        for cp in item_to_list(crosspost, False):
            log_banner(title, cp, **DictCaseInsensitive(kwargs, locals(), delete='title crosspost kwargs args filename'))
    if length is 0:
        length = ANKNOTES.FORMATTING.LINE_LENGTH + 1
    if center:
        title = title.center(length - (ANKNOTES.FORMATTING.TIMESTAMP_PAD_LENGTH if timestamp else 0))
    if prepend_newline:
        log_blank(filename, **kwargs)
    log(chr * length, filename, clear=clear, timestamp=False, **kwargs)
    log(title, filename, timestamp=timestamp, **kwargs)
    log(chr * length, filename, timestamp=False, **kwargs)
    if append_newline:
        log_blank(filename, **kwargs)
Ejemplo n.º 17
0
def crosspost_log(content,
                  filename=None,
                  crosspost_to_default=False,
                  crosspost=None,
                  do_show_tooltip=False,
                  **kwargs):
    if crosspost_to_default and filename:
        summary = " ** %s%s: " % ('' if filename.upper() == 'ERROR' else
                                  'CROSS-POST TO ', filename.upper()) + content
        log(summary[:200], **kwargs)
    if do_show_tooltip:
        show_tooltip(content)
    if not crosspost:
        return
    for fn in item_to_list(crosspost):
        log(content, fn, **kwargs)
Ejemplo n.º 18
0
    def wrap_filename(self, filename=None, final_suffix='', wrap_fn_auto_header=True, crosspost=None, **kwargs):
        if filename is None:
            filename = self.default_filename
        if self.base_path is not None:
            filename = os.path.join(self.base_path, filename if filename else '')
        if self.path_suffix is not None:
            i_asterisk = filename.find('*')
            if i_asterisk > -1:
                final_suffix += filename[i_asterisk + 1:]
                filename = filename[:i_asterisk]
            filename += self.path_suffix + final_suffix
        if crosspost is not None:
            crosspost = [self.wrap_filename(cp)[0] for cp in item_to_list(crosspost, False)]
            kwargs['crosspost'] = crosspost

        if wrap_fn_auto_header and self.auto_header and self.default_banner and not os.path.exists(get_log_full_path(filename)):
            log_banner(self.default_banner, filename)
        return filename, kwargs
Ejemplo n.º 19
0
def write_file_contents(content,
                        full_path,
                        clear=False,
                        try_encode=True,
                        do_print=False,
                        print_timestamp=True,
                        print_content=None,
                        wfc_timestamp=True,
                        wfc_crosspost=None,
                        get_log_full_path=None,
                        **kwargs):
    all_args = locals()
    if wfc_crosspost:
        del all_args['kwargs'], all_args['wfc_crosspost'], all_args[
            'content'], all_args['full_path']
        all_args.update(kwargs)
        for cp in item_to_list(wfc_crosspost):
            write_file_contents(content, cp, **all_args)
    orig_path = full_path
    if not os.path.exists(os.path.dirname(full_path)):
        if callable(get_log_full_path):
            full_path = get_log_full_path(full_path)
            if full_path is False:
                return
        else:
            if not filter_logs(full_path):
                return
            full_path = os.path.abspath(
                os.path.join(FOLDERS.LOGS, full_path + '.log'))
            base_path = os.path.dirname(full_path)
            if not os.path.exists(base_path):
                os.makedirs(base_path)
            if wfc_timestamp:
                print_content = content
                content = '[%s]: ' % datetime.now().strftime(
                    ANKNOTES.DATE_FORMAT) + content
    with open(full_path, 'w+' if clear else 'a+') as fileLog:
        try:
            print >> fileLog, content
        except UnicodeEncodeError:
            content = encode(content)
            print >> fileLog, content
    if do_print:
        print content if print_timestamp or not print_content else print_content
Ejemplo n.º 20
0
 def BannerHeader(self,
                  append_newline=False,
                  filename=None,
                  crosspost=None,
                  bh_wrap_filename=True,
                  **kw):
     if filename is None:
         filename = ''
     if bh_wrap_filename:
         filename = self.Label + filename
         if crosspost is not None:
             crosspost = [
                 self.Label + cp for cp in item_to_list(crosspost, False)
             ]
     log_banner(self.ActionNumeric.upper(),
                do_print=self.__do_print,
                **DictCaseInsensitive(kw,
                                      locals(),
                                      delete='self kw bh_wrap_filename cp'))
Ejemplo n.º 21
0
 def execute(self, sql, a=None, kw=None, auto=None, **kwargs):
     if is_dict_type(a):
         kw, a = a, kw
     if not is_seq_type(a):
         a = item_to_list(a)
     if is_dict_type(sql):
         auto = sql
         sql = ' AND '.join(["`{0}` = :{0}".format(key) for key in auto.keys()])
     if kw is None:
         kw = {}
     kwargs.update(kw)
     sql = self._create_query_(sql, **kwargs)
     if auto:
         kw = auto
     log_sql(sql, a, kw, self=self)
     self.ankdb_lastquery = sql
     if self._is_stmt_(sql):
         self.mod = True
     t = time.time()
     try:
         if a:
             # execute("...where id = ?", 5)
             res = self._db.execute(sql, a)
         elif kw:
             # execute("...where id = :id", id=5)
             res = self._db.execute(sql, kw)
         else:
             res = self._db.execute(sql)
     except (sqlite.OperationalError, sqlite.ProgrammingError, sqlite.Error, Exception) as e:
         log_sql(sql, a, kw, self=self, filter_disabled=False)
         import traceback
         log_error('Error with ankDB().execute(): %s\n Query: %s\n Trace: %s' %
                   (str(e), sql, traceback.format_exc()))
         raise
     if self.echo:
         # print a, ka
         print sql, "%0.3fms" % ((time.time() - t) * 1000)
         if self.echo == "2":
             print a, kw
     return res
Ejemplo n.º 22
0
def filter_logs(filename):
    def do_filter(x): return fnmatch(filename, x)
    return (filter(do_filter, item_to_list(FILES.LOGS.ENABLED)) and not
            filter(do_filter, item_to_list(FILES.LOGS.DISABLED)))
Ejemplo n.º 23
0
def filter_logs(filename):
    def do_filter(x):
        return fnmatch(filename, x)

    return (filter(do_filter, item_to_list(FILES.LOGS.ENABLED))
            and not filter(do_filter, item_to_list(FILES.LOGS.DISABLED)))