def _load(filetype, location):
    sep = const['dir_sep']
    data_directory = const['cwd'] + const['data_subdir'] + sep
    attr_directory = const['cwd'] + const['attr_subdir'] + sep
    location = data_directory + location
    encodings = const['all_encodings']
    if filetype == 'sheet':
        for encoding in encodings:
            try:
                data = pd.read_csv(location, encoding=encoding)
                data.set_index(const['index_column'], inplace=True)
                data.replace(nan, const['empty_marker'], inplace=True)
            except (FileNotFoundError, PermissionError):
                err_msg('FileNotFound', data=location)
                return
            except UnicodeDecodeError:
                pass
    elif filetype == 'game':
        data = gamefiles.load(location + sep, attr_directory)
        if data.empty:
            print('ScriptLoad:', 'GameLoader returned empty DF')
            return
    else:
        err_msg('UnknownSubcall')
        return
    data.sort_values(const['auto_sort_by'], inplace=True)
    return data
Пример #2
0
def get_locl(filepath):
    try:
        with open(filepath, 'r', encoding=const['locl_enc']) as f:
            content = f.read()
    except (FileNotFoundError, PermissionError):
        err_msg('AttrFileNotFound', data=filepath)
        return
    ################
    newcontent = ''
    prev = ''
    for c in content:
        if (prev == ':') and (c in numbers):
            c = ''
        newcontent += c
        prev = c
    content = newcontent
    ################
    try:
        full_locl = yaml.load(content)
    except yaml.YAMLError as e:
        err_msg('YAMLError: ' + str(e), self_contents=True)
        return
    for lang in const['lcl_languages']:
        try:
            locl = full_locl[lang]
        except KeyError:
            pass
    if locl == {}:
        return
    return locl
Пример #3
0
def _select(_from, attribute, values):
    global selection
    if const['auto_apply']:
        _apply()
    if attribute == 'all':
        selection = alldata
        if const['autoprint_atselect']:
            _print()
        return
    if attribute not in const['column_order']:
        err_msg('UnknownColumn', data=attribute)
        return
    if len(values) == 1:
        if values[0] in const['value_empty']:
            values = [const['empty_marker']]
    try:
        if _from == 'all':
            selection = alldata.loc[alldata[attribute].isin(values)]
        if _from == 'selection':
            selection = selection.loc[selection[attribute].isin(values)]
    except (AttributeError, TypeError):
        return  # Message is displayed by _apply()
    except KeyError:
        err_msg('UnknownColumn', data=attribute)
        return
    if const['autoprint_atselect']:
        _print()
Пример #4
0
def getfile(fpath):
    ################
    def rem_cmnt(text):
        ndata = ''
        for line in text.split('\n'):
            nline = ''
            for c in line:
                if c in '#\r\n':
                    break
                if c == '\t':
                    c = ' '
                if c in '{=}':
                    c = ' ' + c + ' '
                nline += c
            ndata += nline + ' '
        return ndata

    ################
    try:
        enc = const['histload_prim_enc']
        text = open(fpath, encoding=enc).read()
    except UnicodeDecodeError:
        enc = const['histload_secn_enc']
        text = open(fpath, encoding=enc).read()
    except FileNotFoundError:
        err_msg('FileNotFound', data=fdir)
        return
    text = rem_cmnt(text)
    return text
Пример #5
0
def loop():
    global alldata
    cmnd = input(const['input_prefix']).split()
    if len(cmnd) == 0:
        return True
    funcname = cmnd[0]
    try:
        args = cmnd[1:]
    except IndexError:
        pass
    if funcname in const['legal_exit_calls']:
        return False
    if funcname not in const['legal_nonexit_calls']:
        err_msg('IllegalCall', data=funcname)
    ################################
    try:
        if funcname == 'load':
            alldata = _load(args[0], args[1])
        if funcname == 'save':
            _save(args[0], args[1])
        if funcname == 'apply':
            _apply()
        if funcname == 'operate_on':
            _operate_on()
        if funcname == 'select':
            _select('all', args[0], args[1:])
        if funcname == 'subselect':
            _select('selection', args[0], args[1:])
        if funcname == 'append':
            _append(args[0], args[1:])
        if funcname == 'sort':
            _sort(args[0], args[1:])
        if funcname == 'set':
            _set(args[0], args[1:])
        if funcname == 'inprov':
            _inprov(args[0], args[1], args[2:])
        if funcname == 'clear':
            _clear()
        if funcname == 'help':
            _help()
        if funcname == 'print':
            try:
                _print(args[0], args[1], args[2], args[3:])
            except IndexError:
                try:
                    _print(args[0])
                except IndexError:
                    _print()

    except IndexError:
        err_msg('TooLessArguments')
    return True
Пример #6
0
def _set(attribute, value):
    value = ' '.join(value)
    if attribute not in const['column_order']:
        err_msg('UnknownColumn', data=attribute)
        return
    if value.lower() in const['value_empty']:
        value = const['empty_marker']
    try:
        selection.loc[:, attribute] = value
        if const['autoprint_atchange']:
            _print()
    except AttributeError:
        err_msg('DataNotSelected')
        return
Пример #7
0
def _sort(scope_name, sort_by):
    for c in sort_by:
        if c not in const['column_order']:
            err_msg('UnknownColumn', data=c)
            return
    if scope_name == 'all':
        try:
            alldata.sort_values(sort_by, inplace=True)
            if const['autoprint_atsort']:
                _print(dtype='full')
        except AttributeError:
            err_msg('DataNotLoaded')
            return
    elif scope_name == 'selection':
        try:
            selection.sort_values(sort_by, inplace=True)
            if const['autoprint_atsort']:
                _print()
        except AttributeError:
            err_msg('DataNotSelected', False)
            return
    else:
        err_msg('UnknownSubcall', data=scope_name)
        return
Пример #8
0
def _inprov(provid, attribute, value):
    value = ' '.join(value)
    try:
        provid = int(provid)
    except ValueError:
        err_msg('InvalidArgumentType')
        return
    if attribute not in const['column_order']:
        err_msg('UnknownColumn', data=attribute)
        return
    if value.lower() in const['value_empty']:
        value = const['empty_marker']
    try:
        selection.loc[provid, attribute] = value
        if const['autoprint_atchange']:
            _print()
    except AttributeError:
        err_msg('DataNotSelected')
        return
Пример #9
0
def _append(attribute, values):
    global selection
    if attribute not in const['column_order']:
        err_msg('UnknownColumn', data=attribute)
        return
    if len(values) == 1:
        if values[0] in const['value_empty']:
            values = [const['empty_marker']]
    try:
        new_selection = alldata.loc[alldata[attribute].isin(values)]
        selection = pd.concat([selection, new_selection])
    except (AttributeError, TypeError):
        err_msg('DataNotLoaded')
        return
    except KeyError:
        err_msg('UnknownColumn', data=attribute)
        return
    if const['autoprint_atselect']:
        _print()
Пример #10
0
def _apply():
    try:
        alldata.update(selection)
    except AttributeError:
        err_msg('DataNotLoaded')
Пример #11
0
def _print(dtype='selection', mode='full', attribute='', values=[]):
    drop_list = const['drop_from_print']
    psel = None
    ################ Drop columns listed in const['drop_from_print']
    if dtype == 'all':
        try:
            psel = alldata.drop(drop_list, axis=1)
        except AttributeError:
            err_msg('DataNotLoaded')
            return
    elif dtype == 'selection':
        try:
            psel = selection.drop(drop_list, axis=1)
        except AttributeError:
            err_msg('DataNotSelected')
            return
    elif dtype == 'info':
        try:
            c = [
                x for x in const['column_order']
                if x not in const['drop_from_print']
            ]
            h = const['drop_from_print']
            print('\nColumns:\n' + ', '.join(c))
            print('\nHidden columns:\n' + ', '.join(h))
            print('\nTotal provinces:\t', len(alldata.index))
            print('Selected provinces:\t', len(selection.index))
        except:
            pass
        print()
        return
    else:
        err_msg('UnknownSubcall', dtype)
        return
    ################ Selecting rows
    if mode == 'full':
        pass
    elif mode == 'where':
        if (attribute == '') or (values == []):
            err_msg('TooLessArguments')
            return
        try:
            psel = psel.loc[psel[attribute].isin(values)]
        except (AttributeError, TypeError):
            if dtype == 'all':
                err_msg('DataNotLoaded')
                return
            elif dtype == 'selection':
                err_msg('DataNotSelected')
                return
    else:
        err_msg('UnknownSubcall', mode)
    ################ Print
    print(psel)
Пример #12
0
def save(data, histdir):
    ################
    def _saveline(key, value, maxdepth, depth):
        i = const['indent']
        result = ''
        if depth == maxdepth:
            result += i * depth + key[0] + ' = ' + value + '\n'
        else:
            result += i * depth + key[0] + ' = {\n'
            result += _saveline(key[1:], value, maxdepth, depth + 1)
            try:
                result += i * (depth + 1) + const['additional_save_info'][
                    key[0]] + '\n'
            except KeyError:
                pass  # Optional
            result += i * depth + '}\n'
        return result

    ################
    try:
        idlist = data.index.values
    except AttributeError:
        err_msg('DataNotLoaded')
        return
    data = data.values.tolist()
    columns = const['column_order']
    columns.remove(const['index_column'])
    sep = const['dir_sep']
    prov_index = 0
    total_provs = len(data)
    ################
    for row in data:
        if const['show_save_toggle']:
            if prov_index % const['show_save_freq'] == 0:
                percentage = str(round((prov_index / total_provs) * 100))
                print(const['show_save_msg'] + percentage + '%')
        provid = idlist[prov_index]
        text = '#Province no.' + str(provid) + '\n' * 2
        filename = ''
        for i in range(len(columns)):
            cname = columns[i]
            value = row[i]
            if (type(value) == float) and not (isnull(value)):
                value = int(value)
            value = str(value)
            ################
            if cname == 'filename':
                filename = value
            if cname not in const['historyfile_keys'].keys():
                continue
            ################
            key = const['historyfile_keys'][cname]
            if (type(key) == str):
                key = [key]
            ################
            if const['multival_sep'] in value:
                value = value.split(const['multival_sep'])
            else:
                value = [value]
            ################
            for element in value:
                if (element.lower() in const['value_empty']) or (element
                                                                 == ''):
                    continue
                text += _saveline(key, element, len(key) - 1, 0)
        filedir = histdir + filename
        open(filedir, 'w', encoding=const['histsave_enc']).write(text)
        prov_index += 1
    if const['show_save_toggle']:
        print('Done.')
    return True
Пример #13
0
def load(histdir, attrdir):
    ################
    def getvalue(keys, data):
        if len(keys) == 1:
            return data[keys[0]]
        else:
            q = []
            for sub in data[keys[0]]:
                q += getvalue(keys[1:], sub)
            if q == 'nan':
                q = ''
            return q

    ################
    def getid(filename):
        result = ''
        for c in filename:
            if c not in numbers:
                break
            result += c
        return result

    ################
    sep = const['dir_sep']
    filenames = const['fnames']
    ################
    regioning = get_regioning(attrdir, const['fnames'])
    localisation = get_locl(attrdir + const['fnames']['provloc'])
    if localisation == None:
        err_msg('LocalisationNotFound')
        return DataFrame
    ################
    provkeys = const['province_attr_keys']
    rows = []
    ################
    try:
        filelist = listdir(histdir)
    except FileNotFoundError:
        err_msg('DirectoryNotFound')
        return DataFrame
    for filename in filelist:
        filedir = histdir + filename
        data = getscope(getfile(filedir).split())
        if type(data) == int:
            err_msg('WrongHistorySyntax', data=filedir)
            return DataFrame
        ################
        province = {}
        provid = getid(filename)
        if provid == '':
            continue
        province[provkeys['id']] = int(provid)
        province[provkeys['fn']] = filename
        province[provkeys['gr']] = ''
        ################################
        try:
            province[provkeys['nm']] = localisation[const['locl_key_prefix'] +
                                                    provid]
        except KeyError:
            province[provkeys['nm']] = const['default_name']
        try:
            province[provkeys['ar']] = regioning[provid][0]
        except KeyError:
            province[provkeys['ar']] = const['default_area']
        try:
            province[provkeys['rg']] = regioning[provid][1]
        except KeyError:
            province[provkeys['rg']] = const['default_region']
        try:
            province[provkeys['sg']] = regioning[provid][2]
        except KeyError:
            province[provkeys['sg']] = const['default_segion']
        ################################
        for datakey in const['historyfile_keys']:
            provkey = const['historyfile_keys'][datakey]
            if type(provkey) == list: keys = provkey
            else: keys = [provkey]
            try:
                value = getvalue(keys, data)
            except KeyError:
                value = []
            province[datakey] = value
        ################
        row = []
        for key in const['column_order']:
            value = province[key]
            if type(value) == list:
                row.append(const['multival_sep'].join(value))
            else:
                row.append(value)
        rows.append(row)
    ################
    df = DataFrame(rows, columns=const['column_order'])
    df.set_index(const['index_column'], inplace=True)
    return df
Пример #14
0
def get_regioning(attrdir, filepaths):
    ################
    def regfile_analysis(text, mem_depth):
        par_depth = 0
        depth = 0  # Starting
        master = const['none']
        members = []
        result = {}
        for word in text.split():
            depth += word.count('{')
            depth -= word.count('}')
            if word in const['skip_at_region_load']:
                continue
            if depth == par_depth:
                if master != const['none']:
                    result[master] = members
                master = word
                members = []
            if depth == mem_depth:
                members.append(word)
        return result

    ################
    def _find(search_key, scope):
        for key in scope:
            value = scope[key]
            if search_key in value:
                return key

    ################
    def _shorten(name, rtype):
        suffixes = const['regioning_names_suffiexes']
        suffix = suffixes[rtype]
        try:
            if name.endswith(suffix):
                name = name[:-len(suffix)]
        except AttributeError:
            name = const['none']
        return name

    ################
    segions = regfile_analysis(getfile(attrdir + filepaths['segion']), 1)
    regions = regfile_analysis(getfile(attrdir + filepaths['region']), 2)
    areas = regfile_analysis(getfile(attrdir + filepaths['area']), 1)
    if (type(segions) == {}) or (type(regions) == {}) or (type(areas) == {}):
        err_msg('WrongRegionFile')
        return
    ################
    result = {}
    for area in areas:
        region = _find(area, regions)
        segion = _find(region, segions)
        areaprovs = areas[area]
        ################
        # When names are shortened the keys to search for should stay the same
        aname = area
        rname = region
        sname = segion
        ################
        if const['shorten_region_names']:
            aname = _shorten(aname, 'area')
            rname = _shorten(rname, 'region')
            sname = _shorten(sname, 'segion')
        ################
        # Assigning
        for province in areaprovs:
            result[province] = [aname, rname, sname]
    return result