kneufeld/alkali

fcntl ModuleNotFoundError

hpca01 opened this issue ยท 12 comments

Machine=Windows 10

Pipenv file contents:

`[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
mypy = "*"

[packages]
alkali = "*"
fcntl = "*"
pytest = "*"

[requires]
python_version = "3.6"`

Script:

from alkali import Database, FileStorage, JSONStorage, Storage, Model, fields

class BaseErx(Model):
    id = fields.IntField(primary_key=True)
    cid = fields.StringField()
    prod_name=fields.StringField()
    pref_route=fields.ForeignKey(Route)


class Route(Model):
    route = fields.StringField()


if __name__ == "__main__":
    db = Database(models=[BaseErx, Route], storage = JSONStorage, save_on_exit=True)
    route=Route(route='PO')
    route.save()
    db.store()
    b = BaseErx(id=1, cid="C12345", prod_name="test", pref_route = route)
    b.save()
    db.store()`
```

Does it support Windows??

I'm not sure if it does or not, I've never tried. Since it's pure python I'd certainly hope so... But if anything was going to be unhappy it would be fcntl.

I'm not seeing any errors, did you have a copy paste problem?

ps. You don't have to run db.store() unless you want to flush to disk right then and there. save_on_exit makes sure state is saved to disk on program exit.

Also, you need to define a primary_key in Route.

from alkali import Database, FileStorage, JSONStorage, Storage, Model, fields

class Route(Model):
    route = fields.StringField(primary_key=True)

class BaseErx(Model):
    id         = fields.IntField(primary_key=True)
    cid        = fields.StringField()
    prod_name  = fields.StringField()
    pref_route = fields.ForeignKey(Route)


if __name__ == "__main__":
    db = Database(models=[BaseErx, Route], storage = JSONStorage, save_on_exit=True)
    route = Route(route='PO')
    route.save()
    db.store()
    b = BaseErx(id=1, cid="C12345", prod_name="test", pref_route = route)
    b.save()
    db.store()

    print(b)

works for me

Just found out that windows doesn't jive well with fcntl, or at all
https://stackoverflow.com/questions/38936259/importerror-no-module-named-fcntl-under-windows

I will try to look through your module to see which fcntl calls are being made so I can look up the win32api equivalent.

A quick "fix" would be make a storage class that doesn't call fcntl. I'll look into doing a conditional import.

That's correct.

The "point" of alkali is to be able to make your own specialized storage classes for your specialized file formats. I've just included JSONStorage as a reasonable default. Unfortunately I didn't realize as I've written it, that it doesn't work on Windows.

This works on OSX and should work on Windows. Unfortunately I'm importing fcntl at the top of file.py so you'll have to comment that out.

class MyJSONStorage(Storage):
    """
    save models in json format
    """
    extension = 'json'

    def __init__(self, filename, *args, **kw ):
        self.filename = filename

    def read(self, model_class):
        with open(self.filename, 'r') as f:
            data = json.load(f)

        if not data:
            return

        for elem in json.loads(data):
            yield elem

    def write(self, model_class, iterator):

        if iterator is None:
            return False

        with open(self.filename, 'w') as f:
            f.write('[\n')

            _peek = Peekorator(iter(iterator))
            for e in _peek:
                data = json.dumps(e.dict, indent='  ')
                f.write(data)

                if not _peek.is_last():
                    f.write(',\n')

            f.write('\n]')

            # since the file may shrink (we've deleted records) then
            # we must truncate the file at our current position to avoid
            # stale data being present on the next load
            f.truncate()
            f.flush()

        return True

awesome ill add that in on my local install! dang that was fast, here i was like okay tonight i am going to go through everything.
thanks so much man!

No worries. In the past week two other GitHub people fixed my issues in their projects inside a day so I've got internet karma to pay back. ;) I'll so try to get a patch out to pypi today with a "real" (hacky) fix.

Thanks so much again man! You rock! ๐Ÿ‘

Thanks for the kind words. In appreciation 0.7.2 is now on pypi so upgrade away. ๐Ÿ˜ƒ

Almost for surely not! Why don't you open another issue and describe what it does (how it's different than a SetField) and then we can discuss it.