Giter Site home page Giter Site logo

Comments (1)

jdhedden avatar jdhedden commented on July 23, 2024

Offering for fix to numeric file/directory names. Also, adds file_age function.

For README.md:

filesize            :: Path   → Int
file_size           :: Path   → Int
fileage             :: Path   → Int
file_age            :: Path   → Int

For ft/ft/functions.py:

@register("basename")
@typed(None, T_PATH)
def basename(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.basename(str(inp))


@register("abspath")
@typed(None, T_PATH)
def abspath(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.abspath(str(inp))


@register("filesize", "file_size")
@typed(None, T_INT)
def filesize(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.getsize(str(inp))


@register("fileage", "file_age")
@typed(None, T_INT)
def fileage(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return int(os.path.getmtime(str(inp)))


@register("file_ext")
@typed(None, T_STRING)
def file_ext(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.splitext(str(inp))[1]


@register("dirname")
@typed(None, T_PATH)
def dirname(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.dirname(str(inp))


@register("replace_ext")
@typed(None, T_PATH)
def replace_ext(new_ext, inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    new_ext = dynamic_cast(T_STRING, new_ext).value
    (base, ext) = os.path.splitext(str(inp))
    if ext != "":
        return base + "." + new_ext
    return inp

@register("strip_ext")
@typed(None, T_STRING)
def strip_ext(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.splitext(str(inp))[0]


@register("has_ext")
@typed(None, T_BOOL)
def has_ext(ext, inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    ext = dynamic_cast(T_STRING, ext).value
    (_, file_ext) = os.path.splitext(str(inp))
    file_ext = file_ext[1:]  # strip leading dot
    return file_ext == ext


@register("split_ext")
@typed(None, T_ARRAY)
def split_ext(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    parts = os.path.splitext(str(inp))
    return [TypedValue(parts[0], T_STRING), TypedValue(parts[1][1:], T_STRING)]


@register("exists")
@typed(None, T_BOOL)
def exists(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.exists(str(inp))


@register("is_dir")
@typed(None, T_BOOL)
def is_dir(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.isdir(str(inp))


@register("is_file")
@typed(None, T_BOOL)
def is_file(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.isfile(str(inp))


@register("is_link")
@typed(None, T_BOOL)
def is_link(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.islink(str(inp))


@register("is_executable")
@typed(None, T_BOOL)
def is_executable(inp):
    if type(inp) == list:
        panic("'{}' cannot take an array as input.  Use the command's '--column' option.".format(inspect.stack()[0][3]))
    if type(inp) not in (str, int):
        panic("Incompatible input type to '{}': expected 'str', got '{}'".format(inspect.stack()[0][3], type(inp).__name__))

    return os.path.isfile(str(inp)) and os.access(str(inp), os.X_OK)

from shell-functools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.